Button Onclick automatically called when press back button from next activity - android

my question is i have set button color as white as default button and in onclick event i have set yellow as background of this button in onCreate method of First Activity class and after clicking this button i go to next activity and from next activity,i press android back button, Button color is shown yellow to me not white one as default. How to resolve this.
final Button myButton= (Button) findViewById(R.id.myButton);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myButton.setBackgroundColor(Color.parseColor("#eee83e"));
Intent i = new Intent(FirstActivity.this,NextActivity.class);
startActivity(i);
}
});

do this onResume() to set again your button background color white like..
myButton.setBackgroundColor(Color.parseColor("Whitecolor code..."));

Related

changing the button text from the edittext state

I have one button and 1 edittext. The button is set to "change", and EditText is enable-false. When you click on a button, its text is changed to "save", and edit text is available for input. After a second press, the text changes to "change" again, and the button becomes Enable-false.
how to implement it?
Use this onClickListener on your button -
Button yourButton = (Button) view.findViewById(R.id.button)
EditText yourEditText = (EditText) view.findViewById(R.id.edittext)
yourButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (!yourEdittext.isEnabled()){
yourButton.setText("Save");
yourEditText.setEnabled(true);
counter++;
}else if (yourEdittext.isEnabled()){
yourButton.setText("Change again");
yourButton.setEnabled(false);
}
}
});
Make sure to set the initial button's text and the edit text disabled for the starting point

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

setting edit text visible each time button is pressed

Actually I want that each time I press the button 2 edit text is set visible,and same thing should happen each time the button is pressed.
Basically whenever user presses the button 2 edittext should appear(any loop concept?)
Please suggest.ThankYou :)
Just add a click listener to the button, and change the visibility of editText into it :
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editTextName.setVisible(true);
}
});
Not sure if i got your question right but you can use android:setvisibility=gone in xml editext fields and then in your button onclick use
edittext.setVisibility(View.VISIBLE);
to make the edittext fields visible.
Just set the initial visibility in the code or in the xml to GONE
and then add onClickListener
android:visibility="gone"
//or
btn2.setVisibility(View.GONE)
btn2.setOnClickListener(new View.OnClickListener(){
e#Override
public void onClick(View view) {
editText.setVisible(true);
}
});
You want to populate an edittext each time a button is pressed ?
Create a layout for your edittext:
public EditText createEditText() {
final LayoutParams lparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
final EditText edittext = new EditText(this); Editext.setLayoutParams(lparams);
return edittext; }
And then add edittexts to your layout:
rl.addView(createEditText());
If I understand correctly, you want to set edit text to visible on the press of a button. This can be done by following steps:
In your Main Class:
Create 2 new EditText variable:
EditText myEditText1;
EditText myEditText2;
Create a new method to be called on button click:
void buttonClick(View view){
//Get References
myEditText1 = (EditText) findViewById(R.id.first_edit_text);
myEditText2 = (EditText) findViewById(R.id.second_edit_text);
//Set visible
myEditText1.setVisibility(View.VISIBLE);
myEditText2.setVisibility(View.VISIBLE);
//Set edit texts to empty string to reset ("Recreation")
myEditText1.setText("");
myEditText2.setText("");
}
In your xml:
Add the onClick attribute to your Button:
android:onClick="buttonClick";
Add the id to your EditTexts:
android:id="#+id/first_edit_text"
android:id="#+id/second_edit_text"
Now, Whenever the button is pressed, the Edit Text becomes visible, no loop is required. And if you also want to be hidden before pressing button, add:
android:visibility="invisible"
Sources: setVisibility, onClick

How to enable a button after a certain button is clicked?

Here is the scenario : I have a gui which contains two buttons.Now is there any way by which second button is clickable only after first button is clicked ?
say you have activity with two buttons defined in the xml layout : button1 and button2
in activity onCreate method write:
button2.setEnabled(false);
In the in click listener of first button write
button2.setEnabled(true);
so finally
in onCreate method of the activity we have
button2.setEnabled(false);
private OnClickListener l = new OnClickListener() {
public void onClick(View v) {
button2.setEnabled(true);
}
};
button1.setOnClickListener(l);

Android: How to only hear when my custom class is clicked

when I make a custom view class and add a clickListener it fires anywhere on the screen I click, even where the custom view is not. If I use the same code with a button from a layout it only fires when I click the button not anywhere on screen. Any ideas how to just only listen for when my custom class is directly clicked?
button only fire when pressed
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d(DEBUG_TAG, "button clicked");
}
});
stroke object fires when you press anywhere on screen, even outside of stroke's bounding box
Stroke stroke = new Stroke(this);
mainLayout.addView(stroke);
stroke.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// fires on every screen click :>(
Log.d(Main.DEBUG_TAG, this.toString()+"shape clicked");
}
});
I think your custom view just fills the whole screen. That's why it reacts on every click on the screen. You need to make it smaller and everything will work fine.

Categories

Resources