Try to search your question here, if you can't find : Ask Any Question Now ?

Adding Google Maps Activity to button using Android Studio

HomeCategory: stackoverflowAdding Google Maps Activity to button using Android Studio
Avatarbhawya asked 5 months ago

I am trying to add new Google Maps Activity to a button in my main activity here is the code.
Code for activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="20dp"
    android:id="@+id/layout_1"
    android:orientation="vertical"
    android:nestedScrollingEnabled="false"
    android:weightSum="1">
    <ImageView
        android:id="@+id/test_image"
        android:src="@drawable/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SITbus"
        android:id="@+id/textView"
        android:layout_gravity="center_horizontal"
        android:textColor="#ffff1b37"
        android:textColorHighlight="#ff3a57ff"
        android:textSize="@android:dimen/app_icon_size"
        android:theme="@android:style/Animation"
        android:typeface="monospace"
        android:textIsSelectable="false" />
    <Button
        android:layout_width="120dp"
        android:layout_height="35dp"
        android:layout_marginTop="10dp"
        android:text="@string/button_send1"
        android:onClick="sendMessageR"
        android:layout_gravity="center_horizontal"
        android:background="#ffff2605"
        android:visibility="visible" />  
</LinearLayout>

MainActivity.java

package com.example.harish.mysampleapplication;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
    public final static String EXTRA_MESSAGE = "com.example.harish.mysampleapplication.MESSAGE";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void sendMessageR(View view) {
        Intent intent = new Intent(this, MapsRedActivity.class);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
            if (id == R.id.action_settings) {
            return true;
        }
            return super.onOptionsItemSelected(item);
    }
}

MapsRedActivity.java

package com.example.harish.mysampleapplication;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsRedActivity extends FragmentActivity {
    private GoogleMap mMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps_red);
        setUpMapIfNeeded();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }
    private void setUpMapIfNeeded() {
        if (mMap == null) {
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            if (mMap != null) {
                setUpMap();
            }
        }
    }
  private void setUpMap() {
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    }
}

I am able to open my main activity but not able to open the Google Map Activity when I click the button. The program is not showing any errors.
Can anyone tell me where I have done wrong? and can anyone suggest me solution for this problem

1 Answers
Best Answer
AvatarFernando answered 5 months ago
Your Answer

17 + 4 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com