I have one application. I need to display map view when we click on map button. Actually this map button is in one class and google map is in another class. Now How can we call one map activity class from another activity class.
Please help me!
For the button, in the xml layout, set android:clickable="true" and android:onClick="showMap", then in your first activity/class, it's like this:
public void showMap(View v) {
Intent intent = new Intent(this, Map.class);
startActivity(intent);
}
(Map.class being the class/name of your activity with the map. You need to adjust the name to your case.)
Related
How to make onClick in custom preference start new acitivity (startActivityForResult)?
I would like my custom preference to start new activity and when it returns result it should save it in the preferences. I know how to implement saving but I am not sure how to implement onClick.
This is what I did in custom Preference class:
#Override
protected void onClick() {
Intent pickLocationIntent = new Intent(SettingsActivity.this, MapsActivity.class);
pickLocationIntent.putExtra("latitude", 0.0);
pickLocationIntent.putExtra("longitude", 0.0);
startActivityForResult(pickLocationIntent, REQUEST_LOCATION);
}
It does not allow me to use startActivityForResult like this.
Edit
To summarise: how would one implement custom preference which when clicked opens new activity?
Edit2
Here's the solution if anyone needs it:
https://stackoverflow.com/a/24885463/9801221
Put this android:onClick="anyname" inside your layout item for which you want a click listener.
public void anyname(View v) {
Intent pickLocationIntent = new Intent(SettingsActivity.this, MapsActivity.class);
pickLocationIntent.putExtra("latitude", 0.0);
pickLocationIntent.putExtra("longitude", 0.0);
startActivityForResult(pickLocationIntent, REQUEST_LOCATION);
}
To implement a onClick event your class e.g. your MainActivity has to implement the OnClickListener interface from the package View, e.g. public class SettingsActivity extends AppCompatActivity implements View.OnClickListener { . Then you need something to click on thats a view and you implement the OnClickListener by writing:View.setOnClickListener(this) in your MainActivity. Whenever the user touches this view the onClick method of the MainActivity will be called.
To get a result from the MapsActivity you just implement onActivityResult() method in your Activity.
I hope i didn't understood you wrong. :)
I have a tab view with two fragments. Those two fragments contain a recycler view with cards.
Each card in both fragments had a button.
Clicking on fragment 1's button should open the fragment 2 as a separate page and vice-versa.
I am struggling to find a method to implement this without making every too complex and tightly coupled.
This is fragment one with its own Adapter.
And this is fragment two:
Clicking on that SELECT DONOR button in Donees page should open donor fragment in a new page where the user will be able to assign a donor for the selected donee.
So I have two needs here
1) To start a fragment from a fragment
2) To Keep track from which Donee the new donor page was opened so that I can assign a donor for that specific donee.
I hope this is understandable.
so far I have tried LocalBroadcast and FragmentManager but its hard to keep track of what I'm doing with the code.
Can you guys suggest a better technique to achieve this ?
the easiest solution would probably be, starting a new activity, passing something like an ID, name or something to the intent on an Button click.
Context.startActivity(new Intent(Context, YourAssigneeActivity.class)
.putExtra("ID",id));
So I assume that you do not switch to the other tab when you click a button on one tab. Therefore the fragment should fill the whole screen.
With this assumption in mind you most likely have to switch the Activity. This can be dones easily with an Intent:
Intent intent = new Intent(getActivity(), ActivityB.class)
intent.putExtra("KEY", <your required data to transfer>);
getActivity().startActivityForResult(intent);
Note that when you use putExtra() don't forget that you need to implement Parcelable in those objects (explained here)
To get to know which item was clicked you can use the following pattern (pseudocode - I personally think it's really clean):
FragmentA implements YourAdapter.callback {
onItemClicked(<YourObject> item) {
<starting new activity as described above>
}
}
class YourAdapter extends RecyclerView.Adapter {
Callback mCallback;
YourAdapter(Context context, otherStuff) {
mCallback = (Callback) context;
}
interface Callback {
onItemClicked(<YourObject> item)
}
YourViewHolder implements View.OnClickListener {
onClick(View v) {
mCallback.onItemClicked(<YourObject> item)
}
}
}
Once you are in your Activity, you can set the Fragment in onCreate() of your Activity. In the Activity retrieve the data with getIntent() in the onCreate before creating the Fragment. Then you can put your data in the Fragment with setArguments(<Bundle>). In the Fragment in the onCreateview() retrieve it with getArguments().
I know this is kind of conmplicated. A better solution would be to just switch to an Activity and forget about the Fragment. This would remove one layer of complexity.
If you directly go from Fragment to Fragment you can ignore the Activity part but the rest should stay the same.
Hope this was the answer you were looking for!
Edit: Note that mCallback = (Callback) context is null if the Activity is not implementing Callback
When i click on a button in recycler view adapter then one popup is appeared from service then it will go to next fragment.i write code like this it shows error how to solve this?
dialog_confirm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new ConfirmAsyncTask().execute();
Intent intent = new Intent(context,OngoingFragment.class);
v.getContext().startActivity(intent);
}
});
If your are using Activity beside Fragment then add your activity in Manifest file. and if your are using Fragment then use FragmentManager to switch your fragment instead of Intent.
You can not start a Fragment like starting an Activity.
See this.
I'm obviously new to app development but basically what I want to do is make a simple app that has a button called 'View Map' that, when pressed, will open up Google maps.
So far, I've coded an app with the 'View Map' button that simply returns some text below the button when pressed, which looks like this.
I have also made another app which uses the Google Developers' tutorial to display a Google map when the app is opened.
My problem arises when I try to combine the two apps together. Instead of having it display just the button alone when opened, it displays both the button and the map in the home screen which looks like so:
and does nothing when the button is pressed.
What I've tried to do is use an OnClickListener that loads the map when the button is pressed, but that clearly ain't working. I assume it has something really basic to do with my activity_main class but I need help sorting it out so that the map isn't created immediately when the app is loaded.
My activity_main class looks like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.google.android.maps.MapView
android:id="#+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="**My Key**"
android:clickable="true"
android:enabled="true" />
<Button
android:id="#+id/buttonMap"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="View Map" />
</RelativeLayout>
And my MainActivity class looks like this:
public class MainActivity extends MapActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonMap = (Button) findViewById(R.id.buttonMap);
buttonMap.setOnClickListener(mapListener);
}
private OnClickListener mapListener = new OnClickListener() {
public void onClick(View v) {
MapView mapView = (MapView) findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
}
};
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
Any help is appreciated, cheers.
Ok, I think you need to use your MainActivity for the button, then set the button's OnClickListener to trigger another Activity which will extend MapActivity. Your MainActivity will then extend Activity only. Here's what your "mapListener" should consist of:
Intent i = new Intent(ctx, MyMapView.class);
// i.putExtra("key", "value"); // use this to give extra data to the new Activity
startActivity(i);
Define ctx as an instance variable (i.e. outside of any functions) in MainActivity and set it to "this":
Context ctx = this;
From within mapListener, "this" is the OnClickListener object itself, not the Activity, so using "this" in "new Intent()" will cause an error. Since mapListener exists inside the Activity though, "ctx" can be seen just fine and its meaning won't change.
Everything inside "mapListener.onClick" should go into your new Map Activity's onCreate() method.
I am having some trouble with the onClick handler in a sub activity of an ActivityGroup.
I am launching a StoreActivity using:
Intent storeIntent = new Intent(this, StoreActivity.class);
storeIntent.putExtra(StoreActivity.INTENT_STORE_ID, storeId);
View newVeiw = getLocalActivityManager().startActivity("StoreActivity", storeIntent).getDecorView();
setContentView(newVeiw);
Log.e("DEBUG", "current activity: " + getLocalActivityManager().getCurrentActivity().toString());
In the StoreActivity layout I have a button which defines an onClick method. For some reason however, it is trying to call this in the parent class that launched StoreActivity. Am I doing something wrong when launching the activity? The output of Log.e above says that StoreActivity is the current activity so I am a bit lost as to why this is happening. I can get around this by defining an onClickListener for the button in code in StoreActvity but I would like to avoid that if possible.
I think this is because you are calling setContentView from the parent activity instead of the subactivity. Why don't you just start the activity in the intent instead and set the content view in the new activity? It would be much simpler.
Try this:
Intent storeIntent = new Intent(this, StoreActivity.class);
storeIntent.putExtra(StoreActivity.INTENT_STORE_ID, storeId);
startActivity(storeIntent);
and then in StoreActivity.java do:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View newVeiw = getLocalActivityManager().startActivity("StoreActivity", storeIntent).getDecorView();
setContentView(newView); //not sure if this would work, would probably be easier to put your xml layout file in here.
}
Ok I have solved this. The problem was unrelated to any of this code. I had a common base class to my activities and in that I had accidentally made the inflater a singleton. This meant that all inflated layouts belonged to the first class that created that singleton instance which happened to be the class that was incorrectly receiving the onClick event. Removing that singleton resolved this.