i'm trying to extend an HorizontalScrollView with a RadioGroup inside, when i populate the radiogroup all the button become selected on click, instead only the one i clicked
this is my class:
package widgets;
import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.widget.CompoundButton;
import android.widget.HorizontalScrollView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class HorizontalListView extends HorizontalScrollView {
ArrayList<String> lista = new ArrayList<String>();
ArrayList<RadioButton> listaButton = new ArrayList<RadioButton>();
int selected = -1;
Drawable itemSelector;
Context context;
android.widget.CompoundButton.OnCheckedChangeListener listener;
RadioGroup group;
public HorizontalListView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
initView();
}
public HorizontalListView(Context context, ArrayList<String> stringhe, Drawable itemBackground) {
super(context);
this.context = context;
this.lista = stringhe;
this.itemSelector = itemBackground;
initView();
}
#SuppressLint("NewApi")
private synchronized void initView() {
this.removeAllViews();
group = new RadioGroup(getContext());
group.setOrientation(RadioGroup.HORIZONTAL);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
this.addView(group, params);
int i = 0;
for (String string : lista) {
RadioButton button = new RadioButton(getContext());
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
button.setBackgroundDrawable(itemSelector);
} else {
button.setBackground(itemSelector);
}
button.setButtonDrawable(new StateListDrawable());
button.setText(string);
button.setTag(i);
LayoutParams paramsButton = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
button.setPadding(20, 0, 20, 0);
listaButton.add(button);
group.addView(button, paramsButton);
i++;
}
}
and this is my selector, i made all the state i need to select the correct one, but as i said the radiogroup select all the items (they also become selected if i scroll the horizontalscrollview)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_pressed="false"><shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="1dp" android:color="#color/sinottica_button_border" />
<solid android:color="#color/sinottica_button_bg_selected" />
<padding android:bottom="2dp" android:left="5dp" android:right="5dp" android:top="2dp" />
</shape></item>
<item android:state_checked="false" android:state_pressed="false"><shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="1dp" android:color="#color/sinottica_button_border" />
<solid android:color="#color/sinottica_button_bg" />
<padding android:bottom="2dp" android:left="5dp" android:right="5dp" android:top="2dp" />
</shape></item>
<item android:state_checked="true" android:state_pressed="true"><shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="1dp" android:color="#color/sinottica_button_border" />
<solid android:color="#color/sinottica_button_bg_selected" />
<padding android:bottom="2dp" android:left="5dp" android:right="5dp" android:top="2dp" />
</shape></item>
<item android:state_checked="false" android:state_pressed="true"><shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="1dp" android:color="#color/sinottica_button_border" />
<solid android:color="#color/sinottica_button_bg" />
<padding android:bottom="2dp" android:left="5dp" android:right="5dp" android:top="2dp" />
</shape></item>
</selector>
UPDATE:
i tried to not remove the StateListDrawable, so i commented this row
button.setButtonDrawable(new StateListDrawable());
now i have the checkbox over my textview, but while the background is out of sync with the selection, (change when i scroll, the other radiobutton in the group change their selection state when i touch one button) the checkmark is correct... there is a way to put the statelist drawable as background for the Button instead of the default (on the left of the button)
Related
MOCK UP
Requirement
I want to put custom button with selector.
Mock up is given above.
If anyone knows solution then share it.
Thank you.
basically you will need to create some new XML files and apply them to your Button element. As i can see from the mockup you will need a stroke and the background color with some shading effect applied, you can research more into the shading thing but the background color and the stroke is pretty straight forward.
Here is an example, done_rounded_btn.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#drawable/zzzzzzzzz_btn_orange" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="#drawable/zzzzzzzzz_btn_orange" />
<item
android:state_focused="false"
android:state_enabled="false"
android:drawable="#drawable/zzzzzzzzz_btn_inactiv" />
<item android:drawable="#drawable/zzzzzzzzz_btn_black"/>
</selector>
for the selection part and then you create the custom drawables corresponding to the mockup.
An example, zzzzzzzzzz_btn_orange:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid
android:color="#color/done_color">
</solid>
<corners
android:bottomLeftRadius="3dp"
android:bottomRightRadius="3dp"
android:topLeftRadius="3dp"
android:topRightRadius="3dp" />
</shape>
And then add it to your button as background, main.xml:
<Button
android:id="#+id/registers_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginBottom="10dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:background="#drawable/done_rounded_btn"
android:text="#string/done_txt"
android:textColor="#color/white"
android:textSize="15sp" />
Hope this helps!
You can use this instead of standard Button and set selector as background in xml:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Button;
/**
* Custom Shape Button which ignores touches on transparent background.
*/
public class ButtonWithUntouchableTransparentBg extends Button {
public ButtonWithUntouchableTransparentBg(Context context) {
this(context, null);
}
public ButtonWithUntouchableTransparentBg(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ButtonWithUntouchableTransparentBg(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setDrawingCacheEnabled(true);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
// ignores touches on transparent background
if (isPixelTransparent(x, y))
return true;
else
return super.onTouchEvent(event);
}
/**
* #return true if pixel from (x,y) is transparent
*/
private boolean isPixelTransparent(int x, int y) {
Bitmap bmp = Bitmap.createBitmap(getDrawingCache());
int color = Color.TRANSPARENT;
try {
color = bmp.getPixel(x, y);
} catch (IllegalArgumentException e) {
// x or y exceed the bitmap's bounds.
// Reverts the View's internal state from a previously set "pressed" state.
setPressed(false);
}
// Ignores touches on transparent background.
if (color == Color.TRANSPARENT)
return true;
else
return false;
}
}
You can also create a shape that is using a selector inside. If your shape is just changing its color in different states, this is a lot cleaner.
color/color_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/blue_dark" android:state_pressed="true" />
<item android:color="#color/blue_light" />
</selector>
drawable/shape.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/color_selector" />
<corners android:bottomLeftRadius="6dip" android:bottomRightRadius="6dp" />
<padding android:bottom="0dip" android:left="0dip" android:right="0dip" android:top="0dip" />
</shape>
Button with rounded corners with two states (enabled/disabled):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true">
<shape android:shape="rectangle">
<corners android:radius="28dp" />
<solid android:color="#color/white" />
<stroke android:width="1dp" android:color="#color/orange" />
</shape>
</item>
<item android:state_enabled="false">
<shape android:shape="rectangle">
<corners android:radius="28dp" />
<solid android:color="#color/grey_card_background" />
<stroke android:width="1dp" android:color="#color/grey" />
</shape>
</item>
</selector>
inside your item put the shape in the selector XML
EX FROM MY CODE :
<!-- if pressed -->
<item android:state_pressed="true"><shape android:padding="10dp" android:shape="rectangle">
<solid android:color="#color/blue" />
<corners android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp" android:topLeftRadius="10dp" android:topRightRadius="10dp" />
</shape></item>
<!-- if not pressed -->
<item><shape android:padding="10dp" android:shape="rectangle">
<solid android:color="#color/Purbble" />
<corners android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp" android:topLeftRadius="10dp" android:topRightRadius="10dp" />
</shape></item>
Following android code creates a TextView with a filled-circle background. When it is clicked a circle is created around it. If it is clicked again then the circle goes away. This works fine in API 19 but not in API 8 or 10. In those older APIs no circular boundary is drawn upon clicking. What is matter here ?
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:orientation="vertical"
android:background="#ffffff" >
<TextView
android:id="#+id/col1"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/circle_notapped"
android:layout_gravity="center"
android:layout_marginTop="50dp"/>
</LinearLayout>
MainActivity.java
package com.kitr.sview;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
private int select1;
private TextView col1;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
col1 = (TextView) findViewById(R.id.col1);
col1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(select1==0)
{
select1=1;
selectIt(v);
}
else
{
select1=0;
deselectIt(v);
}
}
});
initializeActivity();
}
private void initializeActivity()
{
LayerDrawable bgDrawable = (LayerDrawable)col1.getBackground();
GradientDrawable shape = (GradientDrawable) bgDrawable.findDrawableByLayerId(R.id.pat3);
shape.setColor(0xFFF04646);
select1=0;
}
private void selectIt(View v)
{
if(v.getId()==col1.getId())
{
LayerDrawable bgDrawable = (LayerDrawable)col1.getBackground();
GradientDrawable shape = (GradientDrawable) bgDrawable.findDrawableByLayerId(R.id.pat1);
shape.setColor(0xFFF04646);
}
}
private void deselectIt(View v)
{
if(v.getId()==col1.getId())
{
LayerDrawable bgDrawable = (LayerDrawable)col1.getBackground();
GradientDrawable shape = (GradientDrawable) bgDrawable.findDrawableByLayerId(R.id.pat1);
shape.setColor(Color.WHITE);
}
}
}
circle_notapped.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/pat1" android:top="0dp" android:left="0dp" android:right="0dp" android:bottom="0dp">
<shape android:shape="oval">
<solid android:color="#FFFFFFFF" />
</shape>
</item>
<item android:id="#+id/pat2" android:top="3dp" android:left="3dp" android:right="3dp" android:bottom="3dp">
<shape android:shape="oval">
<solid android:color="#FFFFFFFF" />
</shape>
</item>
<item android:id="#+id/pat3" android:top="6dp" android:left="6dp" android:right="6dp" android:bottom="6dp">
<shape android:shape="oval">
<solid android:color="#FF27AE60" />
</shape>
</item>
</layer-list>
one has to use View.Invalidate() in older APIs. This might be a hack but this works fine.
MOCK UP
Requirement
I want to put custom button with selector.
Mock up is given above.
If anyone knows solution then share it.
Thank you.
basically you will need to create some new XML files and apply them to your Button element. As i can see from the mockup you will need a stroke and the background color with some shading effect applied, you can research more into the shading thing but the background color and the stroke is pretty straight forward.
Here is an example, done_rounded_btn.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#drawable/zzzzzzzzz_btn_orange" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="#drawable/zzzzzzzzz_btn_orange" />
<item
android:state_focused="false"
android:state_enabled="false"
android:drawable="#drawable/zzzzzzzzz_btn_inactiv" />
<item android:drawable="#drawable/zzzzzzzzz_btn_black"/>
</selector>
for the selection part and then you create the custom drawables corresponding to the mockup.
An example, zzzzzzzzzz_btn_orange:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid
android:color="#color/done_color">
</solid>
<corners
android:bottomLeftRadius="3dp"
android:bottomRightRadius="3dp"
android:topLeftRadius="3dp"
android:topRightRadius="3dp" />
</shape>
And then add it to your button as background, main.xml:
<Button
android:id="#+id/registers_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginBottom="10dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:background="#drawable/done_rounded_btn"
android:text="#string/done_txt"
android:textColor="#color/white"
android:textSize="15sp" />
Hope this helps!
You can use this instead of standard Button and set selector as background in xml:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Button;
/**
* Custom Shape Button which ignores touches on transparent background.
*/
public class ButtonWithUntouchableTransparentBg extends Button {
public ButtonWithUntouchableTransparentBg(Context context) {
this(context, null);
}
public ButtonWithUntouchableTransparentBg(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ButtonWithUntouchableTransparentBg(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setDrawingCacheEnabled(true);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
// ignores touches on transparent background
if (isPixelTransparent(x, y))
return true;
else
return super.onTouchEvent(event);
}
/**
* #return true if pixel from (x,y) is transparent
*/
private boolean isPixelTransparent(int x, int y) {
Bitmap bmp = Bitmap.createBitmap(getDrawingCache());
int color = Color.TRANSPARENT;
try {
color = bmp.getPixel(x, y);
} catch (IllegalArgumentException e) {
// x or y exceed the bitmap's bounds.
// Reverts the View's internal state from a previously set "pressed" state.
setPressed(false);
}
// Ignores touches on transparent background.
if (color == Color.TRANSPARENT)
return true;
else
return false;
}
}
You can also create a shape that is using a selector inside. If your shape is just changing its color in different states, this is a lot cleaner.
color/color_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/blue_dark" android:state_pressed="true" />
<item android:color="#color/blue_light" />
</selector>
drawable/shape.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/color_selector" />
<corners android:bottomLeftRadius="6dip" android:bottomRightRadius="6dp" />
<padding android:bottom="0dip" android:left="0dip" android:right="0dip" android:top="0dip" />
</shape>
Button with rounded corners with two states (enabled/disabled):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true">
<shape android:shape="rectangle">
<corners android:radius="28dp" />
<solid android:color="#color/white" />
<stroke android:width="1dp" android:color="#color/orange" />
</shape>
</item>
<item android:state_enabled="false">
<shape android:shape="rectangle">
<corners android:radius="28dp" />
<solid android:color="#color/grey_card_background" />
<stroke android:width="1dp" android:color="#color/grey" />
</shape>
</item>
</selector>
inside your item put the shape in the selector XML
EX FROM MY CODE :
<!-- if pressed -->
<item android:state_pressed="true"><shape android:padding="10dp" android:shape="rectangle">
<solid android:color="#color/blue" />
<corners android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp" android:topLeftRadius="10dp" android:topRightRadius="10dp" />
</shape></item>
<!-- if not pressed -->
<item><shape android:padding="10dp" android:shape="rectangle">
<solid android:color="#color/Purbble" />
<corners android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp" android:topLeftRadius="10dp" android:topRightRadius="10dp" />
</shape></item>
I have a custom ImageButton class mimicing the ToggleButton's checked state based on this tutorial How to add a custom button state .
Everything works fine, when I have a state list drawable as the android:src attribute, but the custom state doesn't work with the ImageButton's android:background attribute.
Here is my code:
import android.content.Context;
import android.util.AttributeSet;
import android.widget.Checkable;
import android.widget.ImageButton;
public class CheckableImageButton extends ImageButton implements Checkable {
private static final int[] STATE_CHECKED = {R.attr.state_checked};
private boolean mChecked = false;
public CheckableImageButton(Context context) {
super(context);
}
public CheckableImageButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if(mChecked){
mergeDrawableStates(drawableState, STATE_CHECKED);
}
return drawableState;
}
#Override
public boolean isChecked() {
return mChecked;
}
#Override
public void setChecked(boolean checked) {
mChecked = checked;
refreshDrawableState();
}
#Override
public void toggle() {
setChecked(!mChecked);
}
}
And the relevant snippet from the layout XML:
<com.my.package.view.CheckableImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="#drawable/header_button_bg"
android:padding="5dp"
android:src="#drawable/menu_button"
tools:ignore="ContentDescription" />
And the state list drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/my.package" >
<item android:state_pressed="true">
<shape android:shape="rectangle">
<stroke android:width="1dp" android:color="#ff000000" />
<gradient android:angle="-90" android:endColor="#d2914e" android:startColor="#906434" />
<corners android:radius="5dp" />
</shape>
</item>
<item app:state_checked="true">
<shape android:shape="rectangle">
<stroke android:width="1dp" android:color="#ff000000" />
<gradient android:angle="-90" android:endColor="#d2914e" android:startColor="#906434" />
<corners android:radius="5dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<stroke android:width="1dp" android:color="#ff000000" />
<gradient android:angle="-90" android:endColor="#4f5b6c" android:startColor="#345b75" />
<corners android:radius="5dp" />
</shape>
</item>
</selector>
Another wonderful trait of eclipse probably..
When I tried to revert my code to the last working version by hand (the state list drawable in the android:src tag), it produced the same error. I reverted from the SVN repo, it worked. Then I made the exact same changes as before, character by character, no difference, and voilá, it works now!
So that's it, the code in the question is fully functional.
In this simple game I want to change the background color of the button that I press. But I get the following result, the buttons appearance becomes not good (the shape becomes different):
pressedButton.setBackgroundColor(Color.RED);
Is there a nicer way to do that? Thanks.
[Edit: my full code]
package com.example.xo_game;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button[] btns;
char[][] gameState = new char[3][3];
char turn = 'X';
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button[] btns = new Button[9];
btns[0] = (Button) findViewById(R.id.btn1);
btns[1] = (Button) findViewById(R.id.btn2);
btns[2] = (Button) findViewById(R.id.btn3);
btns[3] = (Button) findViewById(R.id.btn4);
btns[4] = (Button) findViewById(R.id.btn5);
btns[5] = (Button) findViewById(R.id.btn6);
btns[6] = (Button) findViewById(R.id.btn7);
btns[7] = (Button) findViewById(R.id.btn8);
btns[8] = (Button) findViewById(R.id.btn9);
for (int i = 0; i < 9; i++) {
btns[i].setTag(i);
btns[i].setOnClickListener(clickListener);
gameState[i / 3][i % 3] = 'E';
}
}
View.OnClickListener clickListener = new View.OnClickListener() {
public void onClick(View v) {
Button pressedButton = (Button) v;
int indexOfPressedButton = Integer.parseInt(pressedButton.getTag()
.toString());
int row = indexOfPressedButton / 3;
int col = indexOfPressedButton % 3;
if (gameState[row][col] != 'E')
return;
gameState[row][col] = turn;
String turnAsString = String.valueOf(turn);
pressedButton.setText(turnAsString);
if (turn == 'X') {
pressedButton.setBackgroundColor(Color.RED);
turn = 'O';
} else {
pressedButton.setBackgroundColor(Color.GREEN);
turn = 'X';
}
}
};
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
pressedButton.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);
create selector file any name like button_selector.xml in drawable folder
Edited with Gradient
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true">
<shape android:shape="rectangle">
<corners android:radius="5dp"/>
<gradient android:startColor="#ad1c1c" android:endColor="#cc3737" android:angle="90"/>
<padding android:left="10.0dip" android:top="10.0dip"
android:right="10.0dip" android:bottom="10.0dip"/>
<stroke android:width="1.0dip" android:color="#7d0000"/>
</shape>
</item>
<item android:state_pressed="false">
<shape android:shape="rectangle">
<corners android:radius="5dp"/>
<gradient android:startColor="#cfcfcf" android:endColor="#ebebeb" android:angle="90"/>
<padding android:left="10.0dip" android:top="10.0dip"
android:right="10.0dip" android:bottom="10.0dip"/>
<stroke android:width="1.0dip" android:color="#8f8f8f"/>
</shape>
</item>
</selector>
then set in button background
<Button
android:background="#drawable/button_selector"
/>
As #pratik said save this button.xml in drawable folder
button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true">
<shape android:shape="rectangle">
<corners android:radius="5dp"/>
<solid android:color="#f00"/>
<padding android:left="10.0dip" android:top="10.0dip"
android:right="10.0dip" android:bottom="10.0dip"/>
<stroke android:width="1.0dip" android:color="#222"/>
</shape>
</item>
<item android:state_pressed="false">
<shape android:shape="rectangle">
<corners android:radius="5dp"/>
<solid android:color="#f1f1f1"/>
<padding android:left="10.0dip" android:top="10.0dip"
android:right="10.0dip" android:bottom="10.0dip"/>
<stroke android:width="1.0dip" android:color="#222"/>
</shape>
</item>
</selector>
Apply this button as background
<Button
android:background="#drawable/button"/>
and in your class file do like this
public void onClick(View v) {
pressedButton.setPressed(true);
}
so that the red color will be stable
try this :
you have create like this in the ImageButton xml
create xml file using the button image like this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="#drawable/backbutton" />
<item
android:drawable="#drawable/closebutton" />
</selector>
add that xml file as background for IMageButton
<ImageButton
android:layout_height="50px"
android:layout_width="50px"
android:id="#+id/settings"
android:background="#drawable/settings_button" //setting_button in
the xml file
android:text="Settings"/>