This question already has answers here:
android change image button background
(4 answers)
Closed 6 years ago.
how to change image button with another image button for example :
Play and Pause
You can just change the icon on the button, instead of changing the entire button.
To change the icon programmatically you can just do this:
ImageButton btn = (ImageButton)findViewById(R.id.button);
btn.setImageResource(R.drawable.your_image);
If however, you wish to replace the button with a different button, you can create two buttons in the same location using a RelativeLayout, and then hide or show the buttons, based on what you want.
To do this:
ImageButton playBtn = (ImageButton)findViewById(R.id.play);
ImageButton pauseBtn = (ImageButton)findViewById(R.id.pause);
playBtn.setVisibility(View.GONE);
pauseBtn.setVisibility(View.VISIBLE);
You don't need to change the button, just the text / icon on the button.
Button button = (Button) findViewById(R.id.button_play_pause);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (playing) {
playing = false;
button.setText("PLAY");
} else {
playing = true;
button.setText("PAUSE");
}
}
});
Further reading: https://developer.android.com/reference/android/widget/Button.html
Related
I am trying to change visibility of two buttons when pressing one of them.
So say I have an "on" and an "off" button. When I press the on button, I want the on button to hide itself and the off button to show, and vice versa.
How could one do this?
Basically, you have to inform on to point.
How can I handle the visibility ?
How can I listen to a click event ?
How can I handle the visibility ?
You can handle the visibility of a view by using the enter link description here property.
This property controls the visibility of the view.
Must be one of the following constant values.
GONE Completely hidden, as if the view had not been added.
INVISIBLE Not displayed, but taken into account during layout (space is left for it).
VISIBLE Visible on screen (the default value)
Now you know how you could display or not and element on your view.
How can I listen to a click event ?
Simply add on onClickListner on your button
Sample Code
btnOff, btnOn // your buttons
btnOff.setOnclickListner {
// handle visibility
btnOn.visibility = View.INVISIBLE
}
BTW. If you want an On/Off button, you should use Toggle Buttons there are like the switch buttons you used to have on settings screens
See this example in Kotlin, adapt it for your own needs:
val onButton = Button(this)
val offButton = Button(this)
onButton.setOnClickListener {
onButton.visibility = INVISIBLE
offButton.visibility = VISIBLE
}
offButton.setOnClickListener {
onButton.visibility = VISIBLE
offButton.visibility = INVISIBLE
}
Or in Java:
Button onButton = (Button) findViewById(R.id.onButton);
Button offButton = (Button) findViewById(R.id.offButton);
onButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
onButton.visibility = INVISIBLE;
offButton.visibility = VISIBLE;
}
});
offButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
onButton.visibility = VISIBLE;
offButton.visibility = INVISIBLE;
}
});
I recently started using android studio. So I come across a problem, which is like this: I have 2 buttons and 5 editTexts. Using onClickListener, I am trying to input values to the editTexts. But the problem is I can make only one editText to listen what button I am pressing. If I use different onClickListener for 5 editTexts, only the last one is responding to the buttons (which is obvious). I want to happen like this: Whatever etitText I select, when I press a button, the value will reflect on only that one. But it is not working. I was hoping something like a if statement, where if I select a specific editText the onClickListener will respond to that.
I created this following method.
private void selectBox(EditText e, Button b1, Button b2) {
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
Button b = (Button) v;
e.append(b.getText().toString());
}
};
b1.setOnClickListener(listener);
b2.setOnClickListener(listener);
}
then I want something like this in onCreate method:
if(editText1 is selecte`enter code here`d in the emulator) {
selectBox(editText1, button1, button2);
} else if(editText2 is selected in the emulator) {
selectBox(editText2, button1, button2);
} ......
How would I proceed? Thanks.
Here You Go !
fun handleText() {
var flag: Int
txtBox1.setOnClickListener {
flag = 1
if (flag == 1) {
button.setOnClickListener {
txtBox1.text = "add text to field 1"
}
}
}
txtBox2.setOnClickListener {
flag = 2
if (flag == 2) {
button.setOnClickListener {
txtBox2.text = "add text to field 2"
}
}
}
}
Post: I don't like Java as much as I like Kotlin. That's why your question is Java and my answer is Kotlin.
I mean is like this
button = (Button)findViewById(R.id.button1);
button = (Button)findViewById(R.id.button2);
button = (Button)findViewById(R.id.button3);
button = (Button)findViewById(R.id.button4);
button = (Button)findViewById(R.id.button5);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
//Do same logic
}
});
}
it is possible to define like that? or it can causes force close?
and one question again, it is okay to use copying ID from current xml to another xml? i mean the xml was different but the Widget and ID is same, and define the ID in diferrent class. Because it is more simple to copying than make the Same widget with new Id, it is okay?
example :
so in activityone.xml
i had this
so i was copy the widget to activitytwo.xml so they have a same Widget and ID
it is okay if i do like that?
That will only set the listener for button5. Every time you assign button you lose the previous assignment, so when you set the listener the variable button doesn't know that it used to be pointing to button1 to 4.
So no, it will not work.
I put a comment about your second question.
For that you should have single listener but five different buttons (Objects). Currently you are assigning every button one by one to same button reference. In that way your button will have R.id.button5 and in further code you won't have reference to your previous buttons butons1-4. So, while you add listener to the button you are actually adding listener to only button 5 and any other buttons will not have that listener.
First of all you need five different buttons and you should create class which implement the listener and add instance of that listener to your setOnClicklistener method or in a better way you can just specify android:onClick="onClick" in your xml and add onClick method in your activity.
FOR EXAMPLE :
//.. on create
Button button1 = (Button)findViewById(R.id.button1);
Button button2 = (Button)findViewById(R.id.button2);
Button button3 = (Button)findViewById(R.id.button3);
Button button4 = (Button)findViewById(R.id.button4);
Button button5 = (Button)findViewById(R.id.button5);
ListenerClass listener = new ListenerClass();
button1.setOnClickListener(listener);
button2.setOnClickListener(listener);
//..and so on
//Create class
public class ListenerClass implements View.OnClickListener {
//...
#Override
public void onClick(View view) {
//You can determine which button is clicked by view.getId()
//Add logic here
}
}
OR
public class YourActivity implements View.OnClickListener {
//...on create
button1.setOnClickListener(this);
button2.setOnClickListener(this);
//..and so on
//...
#Override
public void onClick(View view) {
//You can determine which button is clicked by view.getId()
//Add logic here
}
}
it is okay to use copying ID from current xml to another xml?
Yes it is fine but you should avoid using that as it may create confusion in a long run. In a wide project working with a team can create problem in understanding that. Other than that you can have same id because when you call findViewById it will refer to the current view and does not get influenced by ids of other views.
This question already has answers here:
How to set a Button gray and unclickable? [duplicate]
(3 answers)
Closed 6 years ago.
So right now i am having trouble making the next button unclickable when it is on the last page of the activity. As of right now it goes back to the first screen. How do i make it so that it know when to grey out the button or make it unclickable when the user gets to the last screen.
Here is my code:
public class ReadingActivity extends Activity implements OnClickListener {
private ViewFlipper viewFlipper;
Button btnNext, btnPrev;
private float lastX;
/** Called when the activity is first created */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reading);
viewFlipper=(ViewFlipper)findViewById(R.id.view_flipper);
btnNext=(Button)findViewById(R.id.btnNext);
btnPrev=(Button)findViewById(R.id.btnPre);
btnNext.setOnClickListener(this);
btnPrev.setOnClickListener(this);
btnNext.setEnabled(true);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId()){
case R.id.btnNext:
viewFlipper.setInAnimation(this, R.anim.in_from_right);
viewFlipper.setOutAnimation(this, R.anim.out_to_left);
viewFlipper.showNext();
break;
case R.id.btnPre:
viewFlipper.setInAnimation(this, R.anim.in_from_left);
viewFlipper.setOutAnimation(this, R.anim.out_to_right);
viewFlipper.showPrevious();
break;
}
}
}
you can set the OnClickListener to null like so
btnNext.setOnClickListener(null);
I think you just want this method
button.setClickable(false);
To make a button grey and UnClickable
put android:enabled="false" in button tag
And using code button.setEnabled(false);
hi i am a new developer. i am trying to design an app. In my app i want to calculate the no of touches in a particular button. Is this can be calculated by onTouch process if yes can anyone give me an example or idea.
Try below code
First Create an Global variable
int numberOfClick = 0;
Now for your button try following code
clickButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
numberOfClick++;
}
}
now you can get the number of clicks by this variable
A click on a button is sent to the app via the onClick event. So if you have a Button:
Button myButton = (Button) findViewById(R.id.myButton);
myButton.setOnClickListener(myClickListener);
You can set up your onClickListener to do whatever you want when the button is clicked.
// Create an anonymous implementation of OnClickListener
private OnClickListener myClickListener = new OnClickListener() {
public void onClick(View v) {
// increment the counter on click
numberOfClicks++;
}
};