I'm wrinting an application for andorid and I have difficulties with change buttons background. I have a simple button and when the users click on the button change the background. I'd like to make the button to turn back into the original form when the user click on it again.
I have no idea how to do that, if anyone has one please response!
use the android.R.drawable.btn_default in order to change the button to default color
#Howlett Logan : You can try this way,
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextButton"
/>
Then
public boolean flag=true; // Global
Button buttonTest;
#Override
protected void onCreate(Bundle savedInstanceState)
{
buttonTest=(Button)findViewById (R.id.button);
buttonTest.setBackgroundResource(R.drawable.your_drawble);
buttonTest.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
if (flag)
{
buttonTest.setBackgroundResource(R.drawable.your_image_1);
}else
{
buttonTest.setBackgroundResource(R.drawable.your_image_2);
}
flag=!flag;
}
});
}
Related
I'm making an app that has buttons and when a button is clicked, it will play a sound. My problem is that I couldn't find a way to change mSoundButton's background when pressed and released. I'm using 'background' instead of 'src' so I can shrink the button without cutting from edges. I haven't added sounds yet because I want to solve this issue before starting to add sounds.
Here is my codes for the button in MainActivity.java (It works okay, but not in the way I wanted. It changes background when pressed but not released.)
final ImageButton mSoundBtn;
final boolean[] soundBtnClicked = {false};
mSoundBtn = findViewById(R.id.soundButton);
mSoundBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(soundBtnClicked[0])
mSoundBtn.setBackgroundResource(R.drawable.button_clicked);
else
mSoundBtn.setBackgroundResource(R.drawable.button_not_clicked);
soundBtnClicked[0] = !soundBtnClicked[0];
}
});
and here is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:id="#+id/soundButton"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#drawable/button_not_clicked"
android:contentDescription="TODO" />
</LinearLayout>
UPDATE (PROBLEM SOLVED):
I wanted the mSoundBtn to change image when pressed, then change image again when it is released. I used the Handler class and the postDelayed() method to create a delay between two image changes. It doesn't actually wait for user to release the button but still solved my issue. Following is the solution I found:
public ImageButton mSoundBtn;
Handler h = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSoundBtn = findViewById(R.id.soundButton);
mSoundBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mSoundBtn.setBackgroundResource(R.drawable.button_clicked);
h.postDelayed(new Runnable() {
public void run() {
mSoundBtn.setBackgroundResource(R.drawable.button_not_clicked);
}
}, 1000); // 1 Second
}
});
What happens is when the mSoundBtn is clicked, It changes image and waits for 1000 milliseconds (1 second), then changes back to previous image which creates some kind of animation when it is pressed so the user can understand when the button is pressed.
Thanks for anyone who have tried to help, thanks for reading. :)
You shouldn't define it as 'final'
Sample:
public class SampleActivity extends AppCompatActivity {
//Variables
public ImageButton mSoundBtn;
public boolean isSoundBtnSelected = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
mSoundBtn = findViewById(R.id.soundButton);
mSoundBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(!isSoundBtnSelected)
mSoundBtn.setBackgroundResource(R.drawable.button_clicked);
else
mSoundBtn.setBackgroundResource(R.drawable.button_not_clicked);
isSoundBtnSelected = !isSoundBtnSelected;
}
});
}
}
If I understand you correctly, you want an action to be done after the user removed the finger from the button (meaning the press is done).
Try using OnTouchListener instead of OnClickListener:
imageButton.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_UP)
{
// change background and other stuff...
return true;
}
return false;
}
});
If you only wanted to change the button's background when it pressed, you can use a selector drawable for that, like so:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#color/black" /> <!-- pressed -->
<item android:state_activated="true"
android:drawable="#color/colorAccent" /> <!-- focused -->
<item android:drawable="#color/colorPrimary" /> <!-- default -->
and just place it as the button's background.
Another option is to use a toggle button and OnChecked listener and change the background when the user checks (press) the button. If you want the background to be permanently changed after the first toggle you can implement this by changing the background only after the first toggle.
I am trying to toggle my button's background drawables, so that when the user clicks the button its background is changed and when the user clicks the button again its background returns to defaul. Here is my code:
public void Favorites(View V) {
Button star = (Button) findViewById(R.id.buttonStar);
if(star.getBackground().equals(R.drawable.btn_star_off)) {
star.setBackgroundResource(R.drawable.btn_star_on);
} else {
star.setBackgroundResource(R.drawable.btn_star_off);
}
}
I am pretty sure this is not how you use drawables with if statements. Can someone suggest a way to do it?
private boolean isButtonClicked = false; // You should add a boolean flag to record the button on/off state
protected void onCreate(Bundle savedInstanceState) {
......
Button star = (Button) findViewById(R.id.buttonStar);
star.setOnClickListener(new OnClickListener() { // Then you should add add click listener for your button.
#Override
public void onClick(View v) {
if (v.getId() == R.id.buttonStar) {
isButtonClicked = !isButtonClicked; // toggle the boolean flag
v.setBackgroundResource(isButtonClicked ? R.drawable.btn_star_on : R.drawable.btn_star_off);
}
}
});
}
You can create an xml in the drawable folder.
This xml (you choose the name...let's call it "bg_button_star.xml") could look just like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#drawable/btn_star_on" />
<item android:drawable="#drawable/btn_star_off" />
Then you have to assign this drawable file to the Button background property in the layout file.
android:background="#drawable/bg_button_star"
If you want to do this programmatically then you just have to do:
button.setBackgroundResource(R.drawable.bg_button_star);
When the user click the first time on the button, you set the Selected state to 'true'. The background changes accordingly. (viceversa for the 'false' Selected state).
You can do in your onClick() something like:
if(star.getTag()==R.drawable.btn_star_on){
star.setTag(R.drawable.btn_star_off);
star.setBackgroundResource(R.drawable.btn_star_off);
} else {
star.setTag(R.drawable.btn_star_on);
star.setBackgroundResource(R.drawable.btn_star_on);
}
Obviously it's better to the the tag before the if and else statement based on your informations. I don't know the rest of your code and how you check if this button has to be iniziatilized with the drawable resource btn_star_off or btn_star_on
You can try this.
public void Favorites(View V) {
Button star = (Button) findViewById(R.id.buttonStar);
if(star.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.btn_star_off).getConstantState()))
{
star.setBackground(R.drawable.btn_star_on);
} else {
star.setBackground(R.drawable.btn_star_off);
}
}
But make sure you are calling this method onClick() of the start button.
Other wise you have to do something like this.
Button star = (Button) findViewById(R.id.buttonStar);
star.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(v.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.btn_star_off).getConstantState()))
{
v.setBackground(R.drawable.btn_star_on);
} else {
v.setBackground(R.drawable.btn_star_off);
}
}
});
In this case, instead of using Button you should use ToggleButton.
There is a API Guide for it:
http://developer.android.com/guide/topics/ui/controls/togglebutton.html
Dont do it like that. Use a selector resource instead http://www.compiletimeerror.com/2014/03/android-button-selector-tutorial-with.html
I have a button which is called Check, I want it to be invisible and visible as I click each time on it, as If its visible and I clicked it will become invisible and verse vies !
But my code doesn't work ! any ideas ?
Button Check ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
Check = (Button)findViewById(R.id.checkButton);
Check.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View View) {
if (View.getVisibility() == android.view.View.VISIBLE)
View.setVisibility(android.view.View.INVISIBLE);
else if (View.getVisibility() == android.view.View.INVISIBLE)
View.setVisibility(android.view.View.VISIBLE);
}
});
In my activity its visible at the beginning and when I click on it, it become invisible, BUT when I click again it stays invisible !
Change your code to this,
Check.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (v.isShown())
v.setVisibility(View.INVISIBLE);
else
v.setVisibility(View.VISIBLE);
}
But i think problem is, when button goes invisible, you are not getting any click event on it. First make sure that onClick method get call when button is invisible.
An invisible button will not dispatch any interaction event. So instead of setting button's visibility to the invisible, you can set a transparent or blank background or something like that.
But i personally believe, you should change your use-case because why one will click on the invisible button.
Try This:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="abcd" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:onClick="abc"
android:text="Button" />
</LinearLayout>
public void abc(View v) {
v.setVisibility(View.INVISIBLE);
}
public void abcd(View v) {
v.findViewById(R.id.button1).setVisibility(View.VISIBLE);
}
Invisible Items don't receive on-click event. So the only way you can receive a click on invisible is by receiving on some other view in place of the invisible view. The above solution wraps the button in a layout, so when button is invisible the on-click is passed on to layout, which handles the event and do accordingly. If you have a high usage of such layout you can also create a custom button with above mechanism.
I am newbie to the programming world and my knowledge is limited. Please excuse me if i ask any blunder.
My question is that.
I am creating an Activity which has START & STOP button. when user clicks on START button a service must start; and on STOP service must stop.
Now I want to disable my START button when i Click start button(service starts on click START button) and when clicks STOP button i want to see the START button as normal clickable button.
I have used .setEnabled(false) by creating the button object.
i need help...Thanks in advance
int count = 0;
if (count == 0) {
stop.setEnabled(false);
PlayButton.setEnabled(true);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.play:
count++;
play.setEnabled(false);
Toast.makeText(this, "Button Disabled", Toast.LENGTH_LONG).show();
Stopbutton.setEnabled(true);
break;
case R.id.stop:
Toast.makeText(this, "Button Disabled", Toast.LENGTH_LONG).show();
count--;
PlayButton.setEnabled(true);
stop.setEnabled(false);
break;
}
}
& check this link
How to disable an Android button?
You can also try:-
for button enable-
button.setClickable(true);
for button disable-
button.setClickable(false);
in the body of onclick
disable button1 as it get clicked
public void onClick(View v) {
if(v.getId() == R.id.button1)
{
Button btn = (Button)findViewById(R.id.buton1);
btn.setEnabled(false);
}
}
If you want to disable it from another class you can use this,
Button btn = ((MainActivity)context).findViewById(R.id.myButton);
btn.setEnabled(false); //or (true) to enable it
You must also declare 'context' at the beginning of your class
public class MyClass extends AppCompatActivity {
Context context;
I usually use it in my onPreExecute and onPostExecute when I need to perform an action and don't want a user to keep clicking the button.
#Override
protected void onPreExecute() {
//some actions to be performed or set before executing task
Button btn = ((MainActivity)context).findViewById(R.id.myButton);
btn.setEnabled(false);
}
#Override
protected void onPostExecute() {
//some actions to be performed or set after executing task
Button btn = ((MainActivity)context).findViewById(R.id.myButton);
btn.setEnabled(true);
}
With kotlin you disable a button on click with,
myButton.setOnClickListener {
it.isClickable = false // to disable clicking on button
it.isEnabled = false // to disable button
}
Don't forget that the view here is it
Try this:
MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.view.View;
public class MainActivity extends Activity {
private Button start, stop;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
start = (Button)findViewById(R.id.start);
stop = (Button)findViewById(R.id.stop);
start.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
start.setVisibility(View.GONE);
/* do something else */
}
});
stop.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
start.setVisibility(View.VISIBLE);
/* do something else */
}
});
}
}
And your layout main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="#+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start"
android:visibility="visible"
/>
<Button
android:id="#+id/stop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Stop"
android:visibility="visible"
/>
If you wanna make the button invisible after button cleck then 1st disable it as vipin said and also add this .setVisibility(View.INVISIBLE); this will hide the button after the button click and when you want to again make it visible use this .setVisibility(View.VISIBLE);
NOTE: if you want the button to be invisible and not don't want it to consume the layout space it requires then you can use View.GONE instead of View.INVISIBLE
I hope I am clear.
more preferred solution is,
onclick(){
btn.setEnabled(false);
btn.setClickable(false);
//yourwork
myWork();
}
myWork(){
//your tasks.
btn.setEnabled(true);
btn.setClickable(true);
}
initialise onClickListener for the button.inside the fist button simply do setEnable() to false ..and from the second button click listener set setEnable to true
enjoy
You can call button.setOnClickListener(null); to cancel the event listner. Additionally you can change the background drawable to give it a disabled effect.
PS: Only try this solution when nothing else works.
myButton.setEnabled(false);
Timer buttonTimer = new Timer();
buttonTimer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
myButton.setEnabled(true);
}
});
}
}, 5000);
try this it,s work perfectly
in my app i have three buttons namely A,B and C. I want the buttons B and C to be disabled until button A is clicked. they should be ready to perform this function until button A is clicked how to do this.....
protected void onCreate(Bundle savedInstanceState)
{
buttonB.setEnabled(false);
buttonC.setEnabled(false);
}
public void onClick(View v)
{
if (v == buttonA)
{
buttonB.setEnabled(true);
buttonC.setEnabled(true);
}
}
you should write this will creating your app
myButton.setEnabled(false);
and in the button click function you should enable it by doing this.
myButton.setEnabled(true);
// assuming valid references to buttons
buttonB.setEnabled(false);
buttonC.setEnabled(false);
buttonA.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
buttonB.setEnabled(true);
buttonC.setEnabled(true);
}
});
Disable the button
myButton.setEnabled(false);
Enable the button
myButton.setEnabled(true);