I have a problem, i can't change the background of my button
This is my button :
<Button
android:id="#+id/btnLogin"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Login"
android:background="#drawable/btn_bg"
app:layout_constraintEnd_toStartOf="#id/gLineRight"
app:layout_constraintStart_toStartOf="#id/gLineLeft"
app:layout_constraintTop_toBottomOf="#id/etPassword"/>
And this is my background in drawable/btn_bg :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<stroke android:color="#color/login_form_details" android:width="2dp"/>
<corners android:radius="2dp"/>
</shape>
</item>
<item>
<shape>
<stroke android:color="#color/login_form_details_light" android:width="2dp"/>
<corners android:radius="2dp"/>
</shape>
</item>
</selector>
This is the result :
Button
The color is always in "colorPrimary"
Did I make a mistake ? Thanks for your help !
Related
I am trying to create Monzo (Android) style switch in my android app. I managed to create it very close by custom drawable for Thumb and Track, but still not same.
Things still missing:
I gave top and bottom padding, but still not padding showing. There is no option of Margin in Thumb and Track drawable.
In Monzo app, both names are showing, but in my case name only showing based on switch on/off
I am attaching both screenshots - from Monzo App and from mine test app. I am also attaching my Thumb & Track drawables code.
Could someone please advise, how can I achieve same design. If Switch is not right component then which other component I can use.
Monzo:
My App:
Thumb Drawable code:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<shape android:shape="rectangle">
<solid android:color="#android:color/white"/>
<size android:width="160dp"/>
<corners android:radius="20dp"/>
</shape>
</item>
<item android:state_checked="false">
<shape android:shape="rectangle">
<solid android:color="#android:color/white"/>
<size android:width="160dp"/>
<corners android:radius="20dp"/>
</shape>
</item>
</selector>
Track Drawable code:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<shape android:shape="rectangle">
<solid android:color="#android:color/darker_gray"/>
<size android:height="40dp"/>
<corners android:radius="20dp"/>
<padding android:top="10dp"
android:right="10dp"
android:bottom="10dp"
android:left="10dp"/>
</shape>
</item>
<item android:state_checked="false">
<shape android:shape="rectangle">
<solid android:color="#android:color/darker_gray"/>
<size android:height="40dp"/>
<corners android:radius="20dp"/>
<padding android:top="10dp"
android:right="10dp"
android:bottom="10dp"
android:left="10dp"/>
</shape>
</item>
</selector>
Try below code:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:orientation="horizontal"
android:background="#drawable/bg_track">
<LinearLayout
android:id="#+id/llFeed"
android:layout_weight=".5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#drawable/bg_thumb"
android:orientation="horizontal"
android:gravity="center"
android:paddingTop="10dp"
android:paddingBottom="10dp">
<TextView
android:id="#+id/tvFeed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Feed"
android:textSize="14sp"/>
</LinearLayout>
<LinearLayout
android:id="#+id/llManage"
android:layout_weight=".5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:paddingTop="10dp"
android:paddingBottom="10dp">
<TextView
android:id="#+id/tvManage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Manage"
android:textSize="14sp"
android:textColor="#color/white"/>
</LinearLayout>
</LinearLayout>
Output for above :
Actually, I was able to solve this via setting Stroke for "Thumb" of the same color as "Track". Next issue was that it not displaying text on the track side...means when switch is off there was not text on other side.
The way I resolve that was by create 2 lables and constraint them to be on top of Switch, and i kept Switch On Off text as empty "".
Thumb Code:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<shape android:shape="rectangle">
<solid android:color="#android:color/white"/>
<size android:width="160dp"/>
<corners android:radius="20dp"/>
<stroke android:color="#color/lightGray"
android:width="5dp"/>
</shape>
</item>
<item android:state_checked="false">
<shape android:shape="rectangle">
<solid android:color="#android:color/white"/>
<size android:width="160dp"/>
<corners android:radius="20dp"/>
<stroke android:color="#color/lightGray"
android:width="5dp"/>
</shape>
</item>
</selector>
Don't show background color in button. Create for background one xml file and set color and radius in that xml file , why don't show color?
my layout file and xml file`
<Button
android:id="#+id/signButton"
android:layout_width="257dp"
android:layout_height="53dp"
android:text="#string/sign_in"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="15sp"
android:background="#drawable/sign_in_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.564"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.921" />
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">
<solid android:color="#color/white" />
<corners android:radius="100dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<gradient android:endColor="#f92c8b" android:startColor="#f6bbd6" />
<corners android:radius="100dp" />
</shape>
</item>
</selector>
I would say you need to add android:state_pressed="false" to your second item.
Otherwise you are not providing a background for both states, pressed and not pressed.
Hope it helps.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#color/white" />
<corners android:radius="100dp" />
</shape>
</item>
<item android:state_pressed="false">
<shape android:shape="rectangle">
<gradient android:endColor="#f92c8b" android:startColor="#f6bbd6" />
<corners android:radius="100dp" />
</shape>
</item>
I want the color to be a specific color such as #ff6f00 and simply black or white.
<ImageView
android:id="#+id/addnote"
android:layout_width="65dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_margin="15dp"
android:layout_height="65dp"
android:background="#drawable/ic_add_circle_black_24dp" />
You should use a Button element and set the background to a selectable to achieve the best results.
<Button
android:id="#+id/addnote"
android:layout_width="65dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_margin="15dp"
android:layout_height="65dp"
android:background="#drawable/custom_selector" />
Add a the file "custom_selector.xml" in your drawable folder.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- disable state. -->
<item android:state_enabled="false">
<shape android:shape="rectangle">
<corners android:radius="16dp"/>
<solid android:color="#ebe5df"/>
<stroke android:color="#e4d6d0" android:width="2dp"/>
</shape>
</item>
<!--released state. -->
<item android:state_pressed="false">
<shape android:shape="rectangle">
<corners android:radius="16dp"/>
<solid android:color="#63b8ff"/>
<stroke android:color="#4580b2" android:width="2dp"/>
</shape>
</item>
<!--pressed state. -->
<item android:state_pressed="true">
<shape android:shape="rectangle">
<corners android:radius="16dp"/>
<solid android:color="#63b8ff"/>
<stroke android:color="#c0e2ff" android:width="2dp"/>
</shape>
</item>
Just set the colors of each of the states as you like.
I want to change Button background color programmatically the button
shape(oval). I have changed in xml.
This is the code:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="20dp"/>
<solid android:color="#f9f9f9"/>
<stroke
android:width="2dp" android:color="#FFFF4917" />
</shape>
I am using Button.backgroundColor(COLOR.RED) to change this Button but problem is that it's changing button color, but it is making my button rectangular shape(default shape of a button)
You could modify it using this simple method down below
GradientDrawable BackgroundShape = (GradientDrawable)btn.getBackground();
BackgroundShape.setColor(Color.BLACK);
Use it. It will make your rectangular shap to oval shap when click on button.
style_login_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="oval">
<solid android:color="#color/colorDeepOrange"/>
<size android:width="120dp" android:height="120dp"/>
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="oval">
<solid android:color="#color/colorOrange"/>
<size android:width="120dp" android:height="120dp"/>
</shape>
</item>
<item >
<shape android:shape="oval">
<solid android:color="#color/colorOrange"/>
<size android:width="120dp" android:height="120dp"/>
</shape>
</item>
</selector>
And apply to Button . Like -
<Button
android:id="#+id/btnSignin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text_btn_login"
android:textColor="#color/colorWhite"
android:textSize="25dp"
android:padding="30dp"
android:textAllCaps="false"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:background="#drawable/style_login_button"/>
I am trying to style a button with different colors for different states. The coloring (and border radius) works for normal state, but not for state_pressed="true". What can be wrong with my code?
This is the button element:
<Button
android:text="#string/login_button_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="220dp"
android:textColor="#FFF"
android:background="#drawable/loginbtn"
android:clickable="true"/>
And this one is loginbtn.xml in drawable folder:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item >
<shape android:shape="rectangle" >
<corners android:radius="4dp" />
<solid android:color="#27ae60"/>
</shape>
</item>
<item android:state_pressed="true">
<shape android:shape="rectangle" >
<corners android:radius="4dp" />
<solid android:color="#2ecc71"/>
</shape>
</item>
</selector>
Thank you.
You need to swap the order of your definition as Android picks up the first one that matches, in this case the one without a state defined. Move it to be last:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle" >
<corners android:radius="4dp" />
<solid android:color="#2ecc71"/>
</shape>
</item>
<item >
<shape android:shape="rectangle" >
<corners android:radius="4dp" />
<solid android:color="#27ae60"/>
</shape>
</item>
</selector>