How to implement onClick in custom preference that starts new activity? - android

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. :)

Related

Android Studio onClick and OnclickListener not working once changed to another Activity

I've added my acivity class in this link
Click MeI am fairly new to Android and am having difficulty trying to add a button on my second activity. I am able to place a button in my main activity and then I use it to navigate to my secondary activity (using setContentView(R.layout.)) and then I use the same 'onClick' method or even 'OnClickListener' method but the button on my second activity just wont work on another activity. Maybe i am missing something
]3
just try to do this:
public class FirstActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_activity);
findViewById(R.id.about_us).setOnClickListener(new
View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(FirstActivity.this, SecondActivity.class));
}
});
}
}
and in second activity again find your button in second activity xml by id and write onClickListener for it
You need to implement two separate methods for two different buttons. I would suggest do these things in the Java code instead of XML.
You can do some thing like this:
Button button = findViewById(R.something.something);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//perform your operation(s) here.
}
});
As I understand you try to use one layout.xml for both activities.
You need to declare method click1 in both activities, not only in first.
It means that your first activity has to have method public void click1() and the second activity has to duplicate method public void click1()
I know it's old however,
#Meikiem idea is great. When you use setContentView(View View) you are just setting the activity's
content to another view (xml), and thus not really using the other .java file which has another
onClick method defined for the second button.
Activity's setContentView(view)
You need to create an Intent and pass it along the startActivity method.
Intent Definition

How do I call a method of a running child activity from a parent activity?

I am developing an android app which has 3 tabs, created in MainActivity.java. Every tab has its own activity. In those activities I have a method called "Refresh()" to update the listview in that tab.
When the user clicks on a button the method "refreshTab(View v)" is called.
// Tab refreshen
public void refreshTab (View v) {
Activity MyActivity = this.getCurrentActivity();
MyActivity.Refresh();
}
This is throwing "The Method Refresh() is undefined for the type Activity. However, "MyActivity" is filled with the tab activity.
How would I go about getting this to work?
You need to cast the activity to your type of activity. Right now you are trying to call the Android class activity, which does not have a "Refresh" function.
Your button handler is a little over-complicated (even though it's only two lines)...
Just do something like:
// Tab refreshen
public void refreshTab (View v) {
Refresh();
}
If the way you've defined your OnClickListener is directly inline (but still within your activity's class), you may need to add a little direction, where MyClassType is the name of your class that extends Activity:
// Tab refreshen
public void refreshTab (View v) {
MyClassType.this.Refresh();
}

Subactivity onClick Firing in Parent Class

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.

Several activities using same listener

I got 4 activites that all include a xml-footer which contains 4 buttons (one for each activity).
I would now like to setup onclicklisteners to these buttons (it's a self made menu in the footer).
The question is, how do I use listeners so that I can reuse code?
I have two ideas:
Create a class that implements onclicklistener and in every activity i would get the buttons and then create a new instance of the listener class and do button.setOnClickListener(onClickListener)
The problem is that in the listener class, how would i check which button called the event?
And how would I create an intent to start an activity, usually i would do:
Intent intent = new Intent(FromActivity.this, ToAcitivty.class)
But i don't have the reference to FromActivity.
Create a base class that extends from activity and then the 4 activies will extend from the base class. I would then like to setup the listeners in the base class. The problem here is that i can't get the references to the buttons by doing
Button button1 = (Button)findViewById(R.id.menu_button1);
button1 will be null. I haven't even called setEventView because this should be done in the activity not in the base class.
Any ideas?
Thank you
Same code is here:
public class MyClass extends Activity implements View.OnClickListener{
btnA=(Button)findViewById(R.id.btnA);
btnA.setOnClickListener(this);
btnB=(Button)findViewById(R.id.btnB);
btnB.setOnClickListener(this);
}
#Override
public void onClick(View v)
{
Button clickedButton = (Button) v;
switch (clickedButton.getId())
{
case R.id.btnA:
Intent regIntent = new Intent(Home.this,Registration.class);
startActivityIfNeeded(regIntent, 1);
break;
case R.id.btnB:
//Some code
break;
}
}
(edited as the original first line is broken on code format.

how to call google map class from another activity class in android?

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.)

Categories

Resources