When I click on button then I have invoked the buttonView.setSelected(true)
method to set button in selected state, but first time button not selected and second time click button get selected.
Code:
main.xml
<Button
android:id="#+id/pauseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/pause"
android:background="#drawable/item_selected" />
item_selected.xml
<?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_pause">
</item>
</selector>
Initialize Button
Button pauseBtn = (Button) findViewById(R.id.pauseButton);
Click code:
#Override
public void onClick(View v)
{
int viewID = v.getId();
if(viewID == R.id.pauseButton)
{
pauseBtn.setSelected(true);
}
}
I don't understand why button not selected on first click.
Use
pauseBtn.setPressed(true);
Read this blog
Related
I want to know if there is a way of changing a button image when it is clicked.
Initially the button has a pause icon. When it is clicked I want the play icon to be displayed. Each time the button is clicked the icon should vary between play and pause.
Is there any way of doing this?
XML Layout file:
<ImageButton
android:id="#+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="109dp"
android:src="#drawable/play_btn"
android:background="#null" />
play_btn.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/pause" android:state_selected="true" />
<item android:drawable="#drawable/play" />
</selector>
Android's Toggle Button with a custom selector sounds like what you want.
You might use something like this for your button's selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/play_icon" android:state_checked="true" />
<item android:drawable="#drawable/pause_icon" android:state_checked="false" />
</selector>
Button mPlayButton;
boolean isPlay = false;
#Override
public void onCreate(){
mPlayButton = (Button) findViewById(R.id.play);
// Default button, if need set it in xml via background="#drawable/default"
mPlayButton.setBackgroundResource(R.drawable.default);
mPlayButton.setOnClickListener(mTogglePlayButton);
}
View.OnClickListener mTogglePlayButton = new View.OnClickListener(){
#Override
public void onClick(View v){
// change your button background
if(isPlay){
v.setBackgroundResource(R.drawable.default);
}else{
v.setBackgroundResource(R.drawable.playing);
}
isPlay = !isPlay; // reverse
}
};
Let's add an onClick field to your button's xml in your layout (requires API level 4 and onwards)
<ImageButton
android:id="#+id/play"
...
android:onClick="buttonPressed" />
Your icons are drawables pause and play.
Keep track of which icon your button has.
private boolean paused = true;
public void buttonPressed(View view) {
ImageButton button = (ImageButton) view;
int icon;
if (paused) {
paused = false;
icon = R.drawable.pause;
else {
paused = true;
icon = R.drawable.play;
}
button.setImageDrawable(
ContextCompat.getDrawable(getApplicationContext(), icon));
}
Checkout this documentation http://developer.android.com/reference/android/widget/ImageButton.html specifically the inherited methods from android.widget.imageview. You'll want to use either setImageBitmap or setImageDrawable. Update it as part of your on click code.
You can use MaterialButton instead of Button and then just use setIcon method for your button:
import com.google.android.material.button.MaterialButton;
MaterialButton mPlayButton;
boolean isPlay = false;
#Override
public void onCreate(){
mPlayButton = (MaterialButton) findViewById(R.id.play);
}
View.OnClickListener mTogglePlayButton = new View.OnClickListener(){
#Override
public void onClick(View v){
// change your button background
if(isPlay){
v.setIcon(ContextCompat.getDrawable(this, R.drawable.your_drawable_resource));
}else{
v.setIcon(ContextCompat.getDrawable(this, R.drawable.your_drawable_resource2));
}
isPlay = !isPlay; // reverse
}
};
I have a listview in my app and want to set a background color for each item that is selected and at same time i want to move to another activity. Then if I select another item in the list the one that was selected before should be deselected. I'm not getting the deselection when another item is clicked.
<ImageView
android:id="#+id/imgfavoriteactive"
android:layout_width="35dip"
android:layout_height="35dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dip"
android:src="#drawable/favorite" />
favorite.xml set in drawable folder
<?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/favoriteactive"/>
<item android:state_selected="false"
android:drawable="#drawable/favoriteinactive"/>
</selector>
img_nfl_favoriteactive.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < listview.getChildCount(); i++) {
listview.getChildAt(i)
.findViewById(R.id.imgfavoriteactive)
.setSelected(false);
}
img_nfl_favoriteactive.setSelected(true);
}
});
I want to custom button ,
If user pressed it will show red color and still show red until user pressed other button
how to do this? thanks.
Try this code:
final Button b1 = (Button)findViewById(R.id.btn_1);
final Button b2 = (Button)findViewById(R.id.btn_2);
b1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
b1.setBackgroundColor(Color.RED);
b2.setBackgroundColor(Color.WHITE);
}
});
b2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
b2.setBackgroundColor(Color.RED);
b1.setBackgroundColor(Color.WHITE);
}
});
Use OnClickListeners to change the background of the Button using setBackgroundColor() or setBackgroundDrawable()
You can use a CheckBox for your button and set the background to a state list drawable that tests for the android:state_checked attribute.
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListeners(new OnClickListener() {
public void onClick(View v) {
button1.setBackgroundColor(Color.RED);
}
});
button2.setOnClickListeners(new OnClickListener() {
public void onClick(View v) {
button1.setBackgroundColor(Color.WHITE);
}
});
What you want is a state list. A quick Google found this article: http://blog.androgames.net/40/custom-button-style-and-theme/ that explains them step by step. That way you don't need any code :)
Here's a code that you need to save as an .xml file and place into your drawable folder.
The android:drawable tags point to the drawable resources for each button state. You can shorten this list if you want to.
Then you can use it as a drawable when creating your layout.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/menu_button_normal" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/menu_button_normal" />
<item android:state_pressed="true" android:drawable="#drawable/menu_button_pressed" />
<item android:state_enabled="true" android:drawable="#drawable/menu_button_normal" />
<item android:state_enabled="false" android:drawable="#drawable/menu_button_normal"/>
</selector>
How to put dynamic button using xml.
i want to fetch the position of button from the xml and place them in screen.
What do you mean by dynamic?
create an xml file called filename.xml, put this in. Change the drawables where necessary. That is bt_return and bt_returnpress
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/bt_return" android:state_pressed="false" />
<item android:drawable="#drawable/bt_returnpress" android:state_pressed="true" />
<item android:drawable="#drawable/bt_return" android:state_focused="false" />
<item android:drawable="#drawable/bt_returnpress" android:state_focused="true" />
</selector>
Then assign the filename.xml to the background of the dynamic button u want.
If I'm not wrong you want to add a button dynamically in your view pragmatically?
If you have to get the parent view in which you want to add button, then create a button and add.
For example you have a LinearLayout already defined.
LinearLayout controls =
(android.widget.LinearLayout)
findViewById(R.id.id_of_your_layout);
Button button = new Button(context);
controls.add(button);
that's it.
In your xml layout file:
<LinearLayout android:id="#+id/layoutbase"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
Then in your code:
LinearLayout layoutbase =
(LinearLayout) findViewById(R.id.layoutbase);
LinearLayout.LayoutParams mylayout = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button mybutton = new Button(this);
mybutton.setText("Button!");
mybutton.setTag("mybutton");
mybutton.setId(999);
mybutton.setLayoutParams(mylayout);
mybutton.setOnClickListener(this);
You also need to implement the OnClickListener:
#Override
public void onClick(View v) {
String tag = (String) v.getTag();
if (tag == "mybutton") {
// do some stuff
}
}
OR
#Override
public void onClick(View v) {
switch (v.getId()) {
case 999:
// do some stuff
break;
default:
}
}
I'm not sure this is even possible. I have this button:
<Button
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="45px"
android:drawableTop="#drawable/buttontv"
android:layout_weight="1"
android:background="#null"
android:textColor="#ffffff"
android:textSize="9px"
android:text="TV"/>
And this button has this item xml:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/tv" />
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/tv_pressed" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/tv_pressed" />
<item
android:drawable="#drawable/tv" />
</selector>
And in my application I use this code for when clicking my button:
OnClickListener b1Listener = new OnClickListener(){
#Override
public void onClick(View v) { loadUrl("http://example.org/"); v.setPressed(true); }
};
Button b1 = (Button) findViewById(R.id.b1);
b1.setOnClickListener(b1Listener);
What I would like that when I have pressed the button, the drawableTop sets to one of the items with the #drawable/tv_pressed attribute value - and stays there (as in 'this is the currently active button').
I tried adding v.setPressed(true) in the onClick function (as this was all I could find with Google) but that didn't work.
Can this be done? Or is there a better alternative to what I'm trying to accomplish?
If you need a button that gets pressed and stays active, use a ToggleButton
https://stackoverflow.com/a/9750125/1257369
button.setFocusable(true);
button.setFocusableInTouchMode(true);///add this line
or with styles.xml
<item name="android:focusable">true</item>
<item name="android:focusableInTouchMode">true</item>
as mentioned above by the answer, it is ideal to use a togglebutton but if you are using a listview in this scenario, then the togglebutton will block out your listview's setOnItemClickListener. You can fix that issue as well by adding the descendants as blocked in the listview layout. However some people have reported that even then, their listview items won't click after using toggle buttons.
Are you working with dialogs/dialogfragments?
This code might help you if you are, because i had a simmilar problem and this worked for me. :)
1- extend your class with DialogFragment
,Override Onstart() method.
#Override
public void onStart() {
super.onStart();
final AlertDialog D = (AlertDialog) getDialog();
if (D != null) {
Button positive = (Button) D.getButton(Dialog.BUTTON_POSITIVE);
positive.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (edittext.equals("")) {
Toast.makeText(getActivity(), "EditText empty don't close",Toast.LENGTH_SHORT).show();
} else {
D.dismiss(); //dissmiss dialog
}
}
});
}}