Asked  7 Months ago    Answers:  5   Viewed   84 times

This is a button selector such that when normal it appears red, when pressed it appears grey.

I would like to ask how could the code be further directly modified such that when PRESSED the text size and color could also change? Many thanks!

<item android:state_pressed="true" >         
    <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
        <stroke android:width="2dp" android:color="@color/black" />
        <solid android:color="@color/grey"/>
        <padding android:left="5dp" android:top="2dp" 
            android:right="5dp" android:bottom="2dp" /> 
        <corners android:radius="5dp" /> 
    </shape>    
</item>

<item>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
        <stroke android:width="2dp" android:color="@color/black" />
        <solid android:color="#FF6699"/>
        <padding android:left="5dp" android:top="2dp" 
            android:right="5dp" android:bottom="2dp" /> 
        <corners android:radius="5dp" /> 
    </shape>
</item>

 Answers

25

You just need to set selector of button in your layout file.

<Button
     android:id="@+id/button1"
     android:background="@drawable/selector_xml_name"
     android:layout_width="200dp"
     android:layout_height="126dp"
     android:text="Hello" />

and done.

Edit

Following is button_effect.xml file in drawable directory

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/numpad_button_bg_selected" android:state_selected="true"></item>
    <item android:drawable="@drawable/numpad_button_bg_pressed" android:state_pressed="true"></item>
    <item android:drawable="@drawable/numpad_button_bg_normal"></item>

</selector>

In this, you can see that there are 3 drawables, you just need to place this button_effect style to your button, as i wrote above. You just need to replace selector_xml_name with button_effect.

Tuesday, June 1, 2021
 
pwaring
answered 7 Months ago
97

You can add this in Android Studio, use Right click on project structure -> New -> Drawable resource file. It should look like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" 
        android:drawable="@drawable/cell_top_selected" />
    <item android:drawable="@drawable/cell_top" />
</selector>
Friday, June 25, 2021
 
christina
answered 6 Months ago
55

You can surely try custom radio buttons or you could simply use or inflate views to achieve this kind of color picker.

with xml: you will need to create two drawable resource files in the drawable folder. First goes like this,

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">

<solid android:color="#e91e63" />
<size
    android:width="48dp"
    android:height="48dp" />

This is applicable when you haven't received any click on the view (clickable). The second file applies when we detect a click.

    xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">

<solid android:color="#e91e63" />
<size
    android:width="53dp"
    android:height="53dp" />
<stroke
    android:width="5dp"
    android:color="#d2d1d2" />

Now, in the activity one needs to set the background drawable to the view (be it image button or imageview). This goes like this (just an example):

public class MainActivity extends AppCompatActivity {
private ImageButton img;
private boolean isSelected = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    img = (ImageButton) findViewById(R.id.img);
    img.setClickable(true);
    img.setBackground(getDrawable(R.drawable.unselected_circle));
    img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            img.startAnimation(AnimationUtils.loadAnimation(getBaseContext(), android.R.anim.fade_in));
            if (isSelected) {
                isSelected = false;
                img.setBackground(getDrawable(R.drawable.unselected_circle));
            } else {
                isSelected = true;
                img.setBackground(getDrawable(R.drawable.selected_circle));
            }
        }
    });
}

}

and the activity_main layout looks something like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewGroup"
tools:context="com.android.empty.MainActivity">

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:layout_margin="20dp"
    android:clickable="true"
    android:id="@+id/img"/>

However, with this method one will end up creating multiple drawables for different colors. To avoid that, we can create the drawables programmatically, writing code once and using the same for different colors using setColor(int color) method:

public class MainActivity extends AppCompatActivity {
private ImageButton img;
private boolean isSelected = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final GradientDrawable unselected = new GradientDrawable();
    unselected.setShape(GradientDrawable.OVAL);
    unselected.setColor(Color.parseColor("#e91e63"));
    unselected.setSize(144, 144);

    final GradientDrawable selected = new GradientDrawable();
    selected.setShape(GradientDrawable.OVAL);
    selected.setColor(Color.parseColor("#E91E63"));
    selected.setSize(159, 159);
    selected.setStroke(15, Color.parseColor("#D2D1D2"));

    img = (ImageButton) findViewById(R.id.img);
    img.setBackground(unselected);
    img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            img.startAnimation(AnimationUtils.loadAnimation(getBaseContext(), android.R.anim.fade_in));
            if (isSelected) {
                isSelected = false;
                img.setBackground(unselected);
            } else {
                isSelected = true;
                img.setBackground(selected);
            }
        }
    });
}

}

The result looks something like this

Note: This example tells only the way to achieve one selector like the one mentioned in the question. To create multiple selectors, one needs to inflate the view (Image Button) using LayoutInflater class.

Friday, August 13, 2021
 
VinoCoder
answered 4 Months ago
73

put this at the end

item android:drawable="@color/red"

i mean as the third option, it will work. android checks the xml conditions from the start, the first tag doesn't have any condition, so it will always pick red, so you have put conditions first and then the default one.

Monday, August 23, 2021
 
Mihail Feraru
answered 4 Months ago
85

The issue you are facing has been reported here https://github.com/47deg/android-swipelistview/issues/41.

As per the cooment by @strgev in that thread, the fix is to -

edit class SwipeListViewTouchListener by adding the line below to onTouch method:

....
case MotionEvent.ACTION_UP: {
view.onTouchEvent(motionEvent);
...

can you try that?

Friday, October 22, 2021
 
Mawg says reinstate Monica
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share