Android Button Selector

This is a button selector, so that in normal display a red color appears, when pressed it looks gray.

I would like to ask how can the code be directly changed so that when PRESSED the text size and color can also change? Thank you very much!

<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> 

+71
android selector
Dec 24
source share
5 answers

You just need to install the selector from 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

Below is the button_effect.xml file in the button_effect.xml 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 see that there are 3 drawings, you just need to put this button_effect style in your button , as I wrote above. You just need to replace selector_xml_name with button_effect .

+164
Dec 24 '12 at 17:11
source share
— -

You cannot achieve text resizing with the state list>. To change the text color and text size, follow these steps:

Text color

To change the color of text, you can create a color list of color states . This will be a separate resource located in the res/color/ directory. In the xml layout, you should set it as the value for the android:textColor attribute. Then the color selector will contain something like the following:

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:color="@color/text_pressed" /> <item android:color="@color/text_normal" /> </selector> 

Text size

You cannot resize text simply using resources. There is no "size selector". You must do this in code. And there is no direct solution.

Perhaps the easiest solution would be to use View.onTouchListener() and handle up and down events accordingly. Use something like this:

 view.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // change text size to the "pressed value" return true; case MotionEvent.ACTION_UP: // change text size to the "normal value" return true; default: return false; } } }); 

Another solution might be to expand the view and override the setPressed(Boolean) method. The method is internally called when a pressed state changes. Then resize the text accordingly in the method call (don't forget to call super).

+18
Dec 24
source share

Create custom_selector.xml in the drawing folder

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/unselected" android:state_pressed="true" /> <item android:drawable="@drawable/selected" /> </selector> 

Create a selected.xml form in a folder with the ability to move

 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="90dp"> <solid android:color="@color/selected"/> <padding /> <stroke android:color="#000" android:width="1dp"/> <corners android:bottomRightRadius="15dp" android:bottomLeftRadius="15dp" android:topLeftRadius="15dp" android:topRightRadius="15dp"/> </shape> 

Create an unselected.xml form in a folder with the ability to move

 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="90dp"> <solid android:color="@color/unselected"/> <padding /> <stroke android:color="#000" android:width="1dp"/> <corners android:bottomRightRadius="15dp" android:bottomLeftRadius="15dp" android:topLeftRadius="15dp" android:topRightRadius="15dp"/> </shape> 

Add the following colors for the selected / unselected state in the color.xml values ​​folder

 <color name="selected">#a8cf45</color> <color name="unselected">#ff8cae3b</color> 

you can check the complete solution from here

+9
Oct. 14 '15 at 5:46
source share

The best way to implement the selector is to use xml instead of using the software method, since it is more easily implemented using xml.

  <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/button_bg_selected" android:state_selected="true"></item> <item android:drawable="@drawable/button_bg_pressed" android:state_pressed="true"></item> <item android:drawable="@drawable/button_bg_normal"></item> </selector> 

For more information, I used this link http://www.blazin.in/2016/03/how-to-use-selectors-for-botton.html

+1
Mar 14 '16 at 12:54 on
source share

You can use this code:

 <Button android:id="@+id/img_sublist_carat" android:layout_width="70dp" android:layout_height="68dp" android:layout_centerVertical="true" android:layout_marginLeft="625dp" android:contentDescription="" android:background="@drawable/img_sublist_carat_selector" android:visibility="visible" /> 

(selection file) img_sublist_carat_selector.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="true" android:drawable="@drawable/img_sublist_carat_highlight" /> <item android:state_pressed="true" android:drawable="@drawable/img_sublist_carat_highlight" /> <item android:drawable="@drawable/img_sublist_carat_normal" /> </selector> 
0
Jul 09 '15 at 10:12
source share



All Articles