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);
Related
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
Can someone help me with the code for
-if i click on button it should display a full screen image then again if i click on button on 2nd page it should do the same.
i want to know how to connect different pages/activities,when you click a button it should display some image and then again clickin on that page will display another image and so on,
images should be full screen
In your activity after setting content view use this code to start your image activity.
Button btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getActivity(),
ImageActivity.class);
startActivity(intent);
}
});
Now put a ImageSwitcher or Image view and set a touch or click listener to change Images as per your logic.
Let's say you have two activities : MyActivity1 and My Activity2
In MyActivity1 class,create a button and on Button's click listener call MyActivity2.class using startActivity(new Intent(MyActivity1.this,MyActivity.class))
MyActivity2 class will contain one ImageView that will have its width and height as "fillparent" value in xml.
You can continue this process for as many activities you want.
As the title states, I am looking to find out how to create a button dynamically when another button in another activity is pressed. This is being done with the Android SDK.
Basically, I have two activities, MainActivity and SecondaryActivity, within the SecondaryActivity you enter some information, title, text, id, so on and so forth. When you click the "Save" button it sends some, information to MainActivity(not the issue). As well as sending the information, I need to create an entirely new button within the MainActivity.
Any suggestions on how this should be accomplished?
Thanks.
Edit 1
public void CreateNewButton(View view)
{
LinearLayout lineLayout = (LinearLayout)findViewById(R.id.linear_layout);
TextView newTextView = new TextView(this);
int id = rand.nextInt(100);
int newId;
newTextView.setVisibility(View.VISIBLE);
newTextView.setId( R.id.new_button + id );
newTextView.setText("New Item");
newTextView.setTextSize(35);
newTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
intent = new Intent(getBaseContext(), SecondActivity.class);
startActivity(intent);
}
});
lineLayout.addView(newTextView);
}
This code generates the new TextView( Decided to change it up ) but now the issue I have, is newTextView.setText(); needs to get the text from the other activity
newTextView.setText(i.getData().toString());
putting this in the CreateNewButton(View view) methods causes an error since technically there is no data in the field that it is trying to grab from.
The problem at hand is I need to create the new TextView field WITH the name of the new account that has yet to be created. If that makes any sense.
I'm going to assume you want to add this button to a LinearLayout:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout1);
Button button = new Button(this);
button.setText("I'm a button!");
// add whatever other attributes you want to the button
linearLayout.addView(button);
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.
Can any one tell me how to pass the value from one screen to its previous screen. Consider the case.i m having two screen first screen with one Textview and button and the second activity have one edittext and button.
If i click the first button then it has to move to second activity and here user has to type something in the textbox. If he press the button from the second screen then the values from the textbox should move to the first activity and that should be displayed in the first activity textview.
This is passing Values from one activity to another
i want to pass the text with same FontStyle, Color and Size of the font.
Use the same style,color and size for first activity TextView and second Activity's EditTextView.
If you are done it with programming, then just pass the other parameters also with the intents, and set it in the first activity's TextView.
Like,
intent.putExtra("fontStyle","American Dream");
intent.putExtra("color","value");
intent.putExtra("size","value");
Use this in first activity to set TextView's properties.
String fontStyle = getIntent().getExtras().getString("fontStyle");
String fontColor = getIntent().getExtras().getString("color");
String fontSize = getIntent().getExtras().getString("size");
Typeface font2 = Typeface.createFromAsset(getApplicationContext().getAssets(),"fonts/"+fontStyle+".ttf");
// here fontStyle is string which you passed from the second Activity
textView.setTypeface(font2);
Also, you can do this onClick
SecondButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
TextView text= (TextView) findViewById(R.id.textview);
text.settext(Edittext.getText());
Intent intent= new Intent(form2.this, form1.class);
startActivity(intent);
}
});