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);
Related
I'm working on an application that contains an activity "MainActivity" this activity contains 9 edittext and this activity connect to the custom keyboard that contains number buttons from 0 to 9 and three button (delete, replay and help) all works perfectly but el rest two buttons (replay and help) I want to click on the button play again (new activity) activity MainActivity restart ... help me please
Inside your code, first findViewById your replay button, then add a OnClickListener like below.
Button replay = findViewById(...);
replay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent restartIntent = new Intent(this,MainActivity.class);
startActivity(restartIntent)
}
});
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..."));
I have 2 buttons to switch between 2 layouts in the same activity: clicking button1 on layout1, it goes to layout 2 (using setContentView). On layout2, clicking button2, it goes back to layout1. Then button1 is no longer responding OnClickListener. I looked into "Input Events" but still couldn't figure it out. What happened and how to fix it?
Thanks in advance!
Button submitBtn;
Button backBtn;
submitBtn = (Button)findViewById(R.id.button1); //on layout1
backButn = (Button)findViewById(R.id.button2); //on layout2
submitBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.layout2);
}
});
backBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.layout1);
}
});
You should re-assign listeners when you switch layout, cause when you're calling setContentView old view is destroyed, and new components are created.
You MUST set the contentview when it is time to change the layout, otherwise the views will be null.
I have three buttons in one activity.when each of the button is clicked ,different layout should be shown with in the same activity.for example if first button is clicked,edit boxes and button should be shown.if second buttojn is clicked listview should be shown etc..
Define different layout files for each layout.
Then after each click event have the intent call this particular activity recalled.
Have setContentView() called conditionally ie determining the particular clickevent and vice versa.
This you can do if you want complete activity to be layuot in diffrent manner. Otherwise if you want some widgets to be displayed on button click then it is pretty easy to show them on click event.
You might wanna consider a "TabWidget" for this. It actually does what you need.
A sample tutorial here.
Why don't you just include the all the layout elements in your single layout, then use the setVisibility attribute to turn them on and off, depending on which button is pressed.
Something like this pseudo code:
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
view1.setVisibility(View.GONE);
view2.setVisibility(View.GONE);
view2.setVisibility(View.VISIBLE);
}
});
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
view1.setVisibility(View.VISIBLE);
view2.setVisibility(View.GONE);
view2.setVisibility(View.GONE);
}
});
I'm creating buttons dynamically ...
for(int i=0; i<colSize;i++){
final Button btn = new Button(this);
btn.setText(SectionName[i]);
btn.setTextSize(10);
btn.setPadding(8, 3,8, 3);
btn.setTextColor(Color.WHITE);
btn.setTypeface(Typeface.SERIF, Typeface.BOLD);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//***Every time that I click my button is selected !:)
btn.setSelected(true);
}
});
}
But how could I deselect the other buttons that were selected, I just want one Button selected! :)
The brutal way (works if you have few buttons) - save your button references and create private method which loops through your buttons and deselects once you don't need
Extend your button class and make it listen for custom event which is generated when one of the buttons is clicked
Look at the RadioGroup implementation
Variation of #1. Instead of creating separate listeners for your buttons create just one and reuse it for all buttons. Extend that listener from OnClickListener and add List field. Each time you assign listener to the button add button reference to that list. Now, when onClick is triggered simply loop through the list and disable "other" buttons
Declare a variable to store the Id of the Clicked Button ::
private int EnabledButton;
set an ID on every button when are created ::
btn.setId(i);
or a tag ::
btn.setTag(i);
then in that Listener get the "EnabledButton", and call a function to deselect the other buttons::
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
EnabledButton=btn.getId();
DeselectButtons();
btn.setSelected(true);
}
});
The Function to deselect the other Buttons ::
public void DeselectButtons() {
for(int i=0; i<NumberofButtons;i++){
if (EnabledButton!= i)
this.findViewById(i).setSelected(false);
}
}