How to display clicked button names in another screen in android? [duplicate] - android

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to store and display the button names ( in different screens)that are clicked by user in android
I have several buttons in one screen ,I want to that only clicked button names should be displayed in next screen.
How can I do this?

I assume you mean "How do you start the next screen and display the name when pressing the button?"... If so, you can use the onClickListener of the button (or the method defined in the onClick property of the xml for the button) to start an intent.

What gh is saying is you need to add
intent.putExtra("someKey", "someValue");
before the startActivity(). Then, in the next activity, you can do:
String someVariable = getIntent().getStringExtra("someKey");
and you can display someVariable in a TextView or however you want to do that. Just keep passing the values with the intent.

Declare a static String to store the name of the button which was clicked.
public static String buttonName;
Now, whenever any button is clicked, add the name of that button in the String.
myButton.setOnClickListener(new OnClickListener()
{ #Override
public void onClick(View v)
{ buttonName = "myButtonNumberX";
Intent intent = new Intent(context, PickHeatActivity.class);
startActivity(intent); }
}
);
Then, in your new PickHeatActivity, you can just access the name of the button by using MyMainActivityName.buttonName.

Related

How to change button color by clicking another button from another activity permanently

i have a problem
i have button A in first activity and button B in second activity,i want when someone click button B in second activity then color of A button is change permanently it never reverse to previous colour again when ever user not uninstall the app
This is not how you ask a question in SO, you should try something first and when you hit a problem, then you can ask your question as specific you can, along with all things you have done. you can read about how you can ask a good question in here.
Now you can try something like this:
//create a method in your first activity, (where the button color should change):
public void changeColorInFirstActivity(){
Button btnA = (Button) findViewById(R.id.myButtonA);
btnA.setBackgroundColor(getResources().getColor(R.color.red));
}
And add this in your second activity where you want to click on a button to change the first activity button color:
Button btnB = (Button) findViewById(R.id.myButtonB);
btnB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FirstActivity secondActivity = new FirstActivity();
firstActivity.changeColorInFirstActivity();
}
});
Now after setting the color, save the color int in shared preferences and set the value you get on your button color in your First activity

how to set a button to mimic another button's backgroundresource

I want to know how I could change a buttons backgroundresource that mimics another button's background resource so that whenever I change that button's backgroundresource it another button mimics the looks of the first button...
for example:
int icon = R.drawable.ic_icon; //more specifically I stored R.drawable.ic_icon in SQL and retrieve and save in int icon when retrieve from that table, so when the table is change the first button dynamically change on create;
btn_01.setBackgroundResource(icon); //when this button is pressed it inflates a layout containing btn_02
btn_02.setBackgrounResource(??????); //this button is on a different layout and is used by different activity and should take the backgroundresource of the button that have been pressed to call that layout.
I could use if else statement but I have different button to be copied by the second button and each button has different backgroundresource possibility.
I couldn't understand your question fully but anyways here is what I got
You can put the resource id in the Intent before starting the activity when Btn_01 is clicked.
Btn_01.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(this,Activity2.class);
intent.putExtra("resource_key", R.drawable.ic_heart);
startActivity(intent);
}
});
then in your Activity2 you can just get your resource data and set to whatever button you want
int DEFAULT_RESOURCE = R.drawable.ic_close;
int resourceId = getIntent().getIntExtra("resource_key",DEFAULT_RESOURCE);
Btn_02.setBackgroundResource(resourceId);

How to check if a button is clicked or not?

I have a Activity in android that has 4 buttons.
The first 3 buttons fetches a json data from a weather API for 1 day, next 5 days and next 10 days respectively.
I have a 4th button placed at the bottom of the screen, which takes user to second activity.
I want to restrict the entry of user to second Activity if no button from top 3 is clicked.
If the data is fetched, I mean any one of the top 3 buttons have been clicked, allow him to go to second activity on 4th button click else show a message.
How can i check on click of 4th button if any of the top 3 buttons have been clicked before?
Thanks
Put a boolean field in your activity, name it clicked and set it to false on the onCreate method of your first activity, then in the onClick method of your 3 buttons, set it to true,
and in the onClick method of your 4th button check it, if it's true go startActivity, else launch a Toast
You can make the 4th button look disable in "OnCreate" with the function "setEnabled"(may be wrong),
and then just set "setOnClickListener" for the 4th button when you click any of the others.
ps.
Can provide code example if needed.
Why don't you use if statement? You can keep the clicked count data under the first three buttons. Like this;
import java.util.stream.*;
int[] btnMemory = new int[4];
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
btnMemory[0] = 1;
// your code
}
});
after, you can check it with if statement under 4th button;
button4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int sum = IntStream.of(btnMemory).sum();
if(sum >= 3)
// your code
}
});

How would I create a back button for a random generator so the previous textview is displayed?

The question really is that simple. I have an activity with a button that randomly changes the text of a textview box. How do I add another button that gathers the previous number generated so the previous textview text comes back - for a quotes app.Is there a feature I require? I have searched, but I cannot find a feature that will 'go back' on the random number generator.
Thank you in advance.
You dont need to go back on the generator itself, just remember the previous values. Store it in a variable. Here we are using prev as an array, because we need it to be final so we can access it in the anonymous inner classes (click handlers). But we also need to change it. So we use the prev[0] value.
Here I am assuming you have a button called nextNum which generates the next random number. But that could be anything, it doesnt matter. When you generate a new random number simply update the 0th element of the prev array and you should be fine.
final String[] prev = new String[] { "" }
#Override
public void onCreate(Bundle bundle) {
// ....
Button BackQuote = (Button)findViewById(R.id.back);
final TextView display = (TextView) findViewById(R.id.textView1);
// number generator button
Button nextNum = (Button) findViewById(R.id.random);
nextNum.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// generate a random number
// store it in prev
prev[0] = rndNum.toString();
}
});
BackQuote.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
display.setText(prev[0]);
}
});
}
Unless there is a specific reason you want to avoid this, then this would work perfectly fine. And there is no way to go back on a random generator. By definition it IS random! (Yes, Pseudo-random. Nonetheless)
So if you want to keep an history, you are going to have to implement those yourself. I have yet to see a random generator which has a history feature. I'd be surprised if i do too.

displaying still images in drawables on text input on android/eclipse

I'm a newbie here, sorry for the dumb question. I am trying to display an image in drawables that is equal value with the text inputted in edittext.
For example I input "A" and a certain image will appear in the next activity after I click done.
My personal idea is that, this will be done in imageview, but how about if I enter "ABC" and 3 images equal to the inputted text will appear simultaneously.
I think array will be use in this problem but I have no idea how to start.
ImageView image = (ImageView) findViewById(R.id.iamgeActivity);
image.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//my problem starts here
}
});
If I got you right..you can do it like in first activity, on clicking done button take the name of image from editText to next activity through bundle.
Intent intent= new Intent(currentactivity.this, nextactivity.class);
intent.putExtra("imagename",youreditText.getText().toString());
startActivity(intent);
In next activity take this image name from bundle and use following code to set image in your imageview dynamically:
ImageView image = (ImageView)findViewById(R.id.yourimageid);
int id = getResources().getIdentifier(imagename, "drawable", getPackageName());
image.setImageResource(id);

Categories

Resources