Button color change when button pressed - java

I have been trying over an hour to make a button's color change when I click the button.
Here is my "mybutton.xml"
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#c4161616"
android:endColor="#c4161616"
android:angle="270" />
<corners
android:radius="10dp" />
<stroke
android:width="5px"
android:color="#00FF00" />
</shape>
Now the Button looks like this:
And when the button gets pressed it should look like this:
<gradient
android:startColor="#c4161616"
android:endColor="#c4565656"
android:angle="270" />
I'm a beginner in Android and XML. Can you please show me how to do it?

Bro try this:-
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_pressed_yellow"
android:state_pressed="true" />
<item android:drawable="#drawable/button_focused_orange"
android:state_focused="true" />
<item android:drawable="#drawable/button_normal_green" />
</selector>

Related

How to create a custom radio button Android

I'm trying to make a custom radio button...
I'm trying to guide myself in some tutorials here in the community but I'm not successful...
Below I'll leave an image of how I want my radio button to be checked/unchecked.
and then I'll leave my code as it is so far:
<RadioButton
android:id="#+id/btn_language_english_us"
android:layout_width="match_parent"
android:layout_height="34dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:button="#drawable/radio_bg"
android:fontFamily="#font/sansation"
android:text="#string/us_english"
android:textColor="#color/white"
android:textSize="16sp" />
radio_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#drawable/circle_indicator_blue" />
<item android:state_checked="false" android:drawable="#drawable/circle_indicator_white" />
</selector>
#drawable/circle_indicator_blue and #drawable/circle_indicator_white
are Vector assets
and here is the uncked/checked result
could someone help me where I'm doing wrong?
my intention is to leave as the first image mentioned above.
Instead using vector asset you may use selector item . Chane your circle_indicator_blue.xml code as below .
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<stroke android:width="5dp" android:color="#android:color/white"/>
<size android:width="24dp" android:height="24dp" />
<solid android:color="#android:color/holo_blue_light" />
</shape>
</item>
</selector>
And circle_indicator_white.xml as below -
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval" android:tint="#android:color/white">
<size android:width="24dp" android:height="24dp"/>
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="20dp"/>
</shape>
</item>
</selector>
Output-

Android seekbar customize

I am trying to customize and android seekbar (using API 17) so that the whole progress bar line is blue.
I have created the following XML in res/drawable:
draw_seekbar_settings.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<clip>
<shape
android:shape="line">
<stroke
android:color="#color/progressFxBar"
android:width="2dp"
/>
</shape>
</clip>
</item>
</selector>
and
<SeekBar
android:id="#+id/t_seekbar"
android:layout_width="220dp"
android:layout_height="35dp"
android:layout_centerHorizontal="true"
android:max="100"
android:progress="50"
android:progressDrawable="#drawable/draw_seekbar_settings"
The problem is only half the progress bar is blue, the other half doesn't have any colour whatsoever.
I would like to get this image
Instead I am getting this
change the draw_seekbar_settings.xml as follows:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Define the background properties like color etc -->
<item android:id="#android:id/background">
<shape
android:shape="line">
<stroke
android:color="#color/progressFxBar"
android:width="2dp"
/>
</shape>
</item>
<!-- Define the progress properties like start color, end color etc -->
<!-- if you want to change the progress edit following -->
<item android:id="#android:id/progress">
<clip>
<shape>
<gradient
android:startColor="#007A00"
android:centerColor="#007A00"
android:centerY="1.0"
android:endColor="#06101d"
android:angle="270"
/>
</shape>
</clip>
</item>
</layer-list>

How Can I add line(view) below each of the item name in bottom navigation view?

I want to add a line below each name of the items. How can I do this with bottom navigation view? The image has an orange line below the item name. So It should be visible for the selected item only. I have done with the Linear layout. I need to do with the bottom navigation view.
Please check with this image. I did with selector functions for this
attribute app:itemBackground="#drawable/selector"
For do this:
Create 2 drawables, 1 whit and 1 without the under line
Whit line:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#FFFFFF" />
</shape>
</item>
<item
android:right="20dp"
android:left="20dp">
<shape>
<stroke
android:width="4dp"
android:color="#color/bottom_sheet_divider" />
<solid android:color="#color/transparent" />
</shape>
</item>
<item
android:bottom="4dp">
<shape>
<solid android:color="#android:color/white" />
</shape>
</item>
</layer-list>
without line:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle" >
<solid android:color="#FFFFFF" />
</shape>
</item>
<item android:top="-41dp" android:right="-41dp" android:left="-41dp">
<shape>
<solid android:color="#android:color/transparent" />
<stroke
android:width="4dp"
android:color="#FFFFFF" />
</shape>
</item>
</layer-list>
Create a drawable whit drawable checked state
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/line_selected" android:state_checked="true"/>
<item android:drawable="#drawable/line_unselected" android:state_checked="false"/>
</selector>
Add this drawable on the BottomNavigationView
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
---> app:itemBackground="#drawable/item_line"
app:menu="#menu/bottom_navigation_menu" />

How to remove the delay from an android button?

I created a specially effect, if a button pressed. So the the xml file is:
<?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/clicked"
/>
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/clicked"
/>
<item android:drawable="#drawable/shape"
/>
</selector>
shape.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<padding android:bottom="0dp" />
<solid android:color="#ffffff" />
<stroke android:width="1dp" android:color="#000000" />
</shape>
clicked.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<padding android:bottom="0dp" />
<solid android:color="#2B4881" />
<stroke android:width="1dp" android:color="#000000" />
</shape>
In activity.xml I set the file as background for the button:
<Button android:background="#drawable/background"/>
But if i press the button, it tooks a while, until the button changes it's color. Can i remove this delay?
Use below code inside selectors
<item android:drawable="#drawable/clicked" android:state_pressed="true"></item>
<item android:drawable="#drawable/shape"></item>"

How to change style attribute value in programmatically?

I have button and I set style from xml now on click of button I want to change style like
style="#style/ButtonNotSelected"
Place to
style="#style/ButtonSelected"
programmatically..
Please help..!!!
It will better if you use xml and a selector , to get your button change color when it is pressed, you could define an XML file called res/drawable/my_button.xml.
Set my_button.xml as background to your button.
<?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/button_pressed" />
<item
android:state_pressed="false"
android:drawable="#drawable/button_normal" />
</selector>
#drawable/button_pressed something like this :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<solid android:color="#449def"/>
<stroke android:width="1dp" android:color="#2f6699"/>
<corners android:radius="3dp"/>
<padding android:left="10dp" android:top="10dp" android:right="10dp"
android:bottom="10dp"/>
</shape>
</item>
</selector>
#drawable/button_normal something like this :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient android:startColor="#449def" android:endColor="#2f6699" android:angle="270"/>
<stroke android:width="1dp" android:color="#2f6699"/>
<corners android:radius="4dp"/>
<padding android:left="10dp" android:top="10dp" android:right="10dp"
android:bottom="10dp"/>
</shape>
</item>
</selector>
try like this:
ContextThemeWrapper newContext = new ContextThemeWrapper(baseContext, R.style.MyStyle);
Button button = new Button(newContext);
OR
btn.setBackgroundResource(R.drawable.back_button_answer);
\res\drawable\back_button_answer.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="10dip" />
<!-- background -->
<gradient
android:startColor="#D6D7D6"
android:centerColor="#E2E2E2"
android:centerY="0.75"
android:endColor="#D6D7D6"
android:angle="270"
/>
<stroke android:width="2dip" android:color="#fff"/>
</shape>

Categories

Resources