Button with exactly the same code, produces two different styles - android

I have two different set of buttons, one in the activity and one inside a listview. They have very similar code, but display two different styles.
The header set of buttons code :
<Button
android:id="#+id/invoices_1year"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="0.2"
android:onClick="refresh"
android:text="#string/Invoices_1_year" />
while the Listview's buttons code is :
<Button
android:id="#+id/invoiceButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="0.25"
android:text="View Pdf" />
The Xml for Listview for #ir2pid :
<ListView
android:id="#+id/InvoiceList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/searchArea" />
The output I get on my device :
My expected results is that both the button sets look the same.

In drawable folder create round.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="3dip" />
<stroke android:width="1dip" android:color="#5e7974" />
<gradient android:angle="-90" android:startColor="#e4d46f" android:endColor="#e4d079" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#c7cf2d" />
<solid android:color="#e9da67"/>
</shape>
</item>
<item >
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#e7d851" />
<gradient android:angle="-90" android:startColor="#e4d079" android:endColor="#87cf8d" />
</shape>
</item>
</selector>
And in button add
android:background="#drawable/round"

Following Bala Raja answer , I found the generic material design button xml and modified it to only use my specified color.
This is my round.xml
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="#dimen/abc_button_inset_horizontal_material"
android:insetTop="#dimen/abc_button_inset_vertical_material"
android:insetRight="#dimen/abc_button_inset_horizontal_material"
android:insetBottom="#dimen/abc_button_inset_vertical_material">
<shape android:shape="rectangle">
<corners android:radius="#dimen/abc_control_corner_material" />
<solid android:color="#color/ButtonGray" /> // My CUSTOM COLOR
<padding android:left="#dimen/abc_button_padding_horizontal_material"
android:top="#dimen/abc_button_padding_vertical_material"
android:right="#dimen/abc_button_padding_horizontal_material"
android:bottom="#dimen/abc_button_padding_vertical_material" />
</shape>
and in the buttom xml change the backgorund toandroid:background="#drawable/round"

Related

How to make two layer circular corner in textview in android

This drawable :
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#70628b" />
<stroke android:width="12dp"
android:color="#4c3f6e"
/>
<corners android:radius="8dp"/>
</shape>
This is my textview
<TextView
android:id="#+id/tv_disabled_frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/disabled_transparent_frame"
android:fontFamily="#font/inter_medium"
android:gravity="center"
android:padding="#dimen/dp_24"
android:text="02 : 45"
android:textColor="#color/white"
android:textSize="#dimen/dp_32"
android:textStyle="bold"
android:visibility="visible" />
I am trying to apply circular drawable in inside and outside but given code its showing only outer :
Current screen using this code :
Expected screen
I am unable to do circular please help me in this .
try this code
It can show nested corner
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#4c3f6e" />
<corners android:radius="8dp" />
<padding
android:bottom="12dp"
android:left="12dp"
android:right="12dp"
android:top="12dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#70628b" />
<corners android:radius="8dp" />
</shape>
</item>
</layer-list>
note that your "expected screen" has some small shadow under inner color area, you won't achieve this with common drawable, even with layer-list
I would advice you to use CardView
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/outer_background"
android:padding="12dp">
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="4dp"
android:visibility="gone"
app:cardCornerRadius="8dp"
app:cardBackgroundColor="#70628b">
<TextView ...
adjust elevation param for more/less shadow
outer_background.xml
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#4c3f6e" />
<corners android:radius="8dp"/>
</shape>
be pixel-perfect, build beautiful apps!

After migration to Androidx SwitchCompat is white

I've migrated my project to AndroidX. My MainActivity extend FragmentActivity my first SwitchCompat looks all white, it doesn't have any color at all when I first time come to that screen. SwitchCompat is white. All other SwitchCompact under it are with correct color. If I press back and come again to that screen, my first SwitchCompact receive correct color and looks fine.
If I change that my MainActivty extend AppCompactActivity, then everything is ok when I first time reach that screen. Does anyone know where is problem here because before migration my MainActivity also extend FragmentActivity and everything was fine.
My xml code is the same in the both case:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".BlankFragment">
<androidx.appcompat.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<androidx.appcompat.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
I didn`t go deep why this problem occurs when you extend FragmentActivity. but to show appropriate colors on the action event of "SwitchCompat" can achievable by using custom track and thumb.
your switch button
<androidx.appcompat.widget.SwitchCompat
android:id="#+id/sw_autoplay"
android:layout_width="44dp"
android:layout_height="25dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:paddingStart="4dp"
android:paddingEnd="4dp"
android:textOff=""
android:textOn=""
android:theme="#style/SwitchButtonTheme"
android:thumb="#drawable/thumb"
app:track="#drawable/track" />
thumb
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false">
<shape android:shape="oval">
<solid android:color="#ffffff" />
<size android:width="20dp" android:height="20dp" />
<stroke android:width=".5dp" android:color="#A4A0A0" />
</shape>
</item>
<item android:state_checked="true">
<shape android:shape="oval">
<solid android:color="#ffffff" />
<size android:width="20dp" android:height="20dp" />
<stroke android:width=".5dp" android:color="#57F47F" />
</shape>
</item>
track
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false">
<shape android:shape="rectangle">
<corners android:radius="20dp" />
<solid android:color="#ffffff" />
<stroke android:width=".3dp" android:color="#A4A0A0" />
<size android:height="20dp" />
</shape>
</item>
<item android:state_checked="true">
<shape android:shape="rectangle">
<corners android:radius="20dp" />
<solid android:color="#57F47F" />
<stroke android:width=".3dp" android:color="#A4A0A0" />
<size android:height="20dp" />
</shape>
</item>
It`s just for when you want you to use "FragmentActivity".

composite EditText using drawable as side icon

My problem is there isn't any rounded corner at the left bottom region of the rectangular, even though I defined it as 10dp.
Is there a better way to put a Drawable next to the EditText, rather than using my silly approach ?
Or how can my approach be redefined ?
Excerpt from the layout:
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#drawable/email_bordered"
android:ems="10"
android:inputType="textEmailAddress"
android:paddingLeft="34dp"
android:textSize="30sp"
android:layout_marginBottom="20dp"
/><EditText
The email_bordered.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- This controls the white background-->
<item
android:width="32dp"
android:height="90dp"
android:left="1.9dp"
android:bottom="1.9dp"
>
<shape android:shape="rectangle" >
<corners
android:topLeftRadius="10dp"
android:bottomLeftRadius="10dp"
android:topRightRadius="0dp"
android:bottomRightRadius="0dp"
/>
<solid android:color="#fff" />
</shape>
</item>
<!-- This adds the vector image -->
<item
android:bottom="50dp"
android:top="10dp"
android:width="24dp"
android:height="24dp"
android:left="5dp"
android:drawable="#drawable/ic_if_mail_461377"
android:gravity="left">
</item>
<!-- This draws the border around everything -->
<item>
<shape android:shape="rectangle">
<stroke
android:width="2dp"
android:color="#ffffff"/>
<corners
android:radius="10dp"
/>
</shape>
</item>
</layer-list>
And this is the preview:
You can use the drawableLeft tag within the EditText to get the desired design.
<EditText
...
android:drawableLeft="#drawable/ic_if_mail_461377"
android:drawablePadding="10dp"/>
EDIT
With a few changes in the layer-list, the final outcome is something like this.
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="45dp"
android:drawableLeft="#drawable/pic"
android:drawablePadding="10dp"
android:background="#drawable/email_bordered"
android:ems="10"
android:inputType="textEmailAddress"
android:paddingLeft="4dp"
android:textSize="30sp"
/>
email_border.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- This controls the white background-->
<item
android:width="62dp"
android:height="45dp"
android:left="1.9dp"
android:bottom="1.9dp"
>
<shape android:shape="rectangle" >
<corners
android:topLeftRadius="10dp"
android:bottomLeftRadius="10dp"
android:topRightRadius="0dp"
android:bottomRightRadius="0dp"
/>
<solid android:color="#fff" />
</shape>
</item>
<!-- This adds the vector image -->
<!-- This draws the border around everything -->
<item>
<shape android:shape="rectangle">
<stroke
android:width="1.9dp"
android:color="#ffffff"/>
<corners
android:radius="10dp"
/>
</shape>
</item>
</layer-list>

Android - can't set margins for custom button

I am using Holo.Light theme and I am trying to make buttons not be transparent. I accomplished that, but now I want to add margin to the buttons and I have not been able to do that. Here is the button xml I have:
<?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="#ffffff" />
<stroke
android:width="1dp"
android:color="#E3E3E3" />
<corners android:radius="4dip" />
<margin
android:left="15dp"
android:right="15dp" />
</shape>
</item>
<item>
<shape>
<solid android:color="#edeff1" />
<stroke
android:width="1dp"
android:color="#E3E3E3" />
<corners android:radius="4dip" />
<margin
android:left="15dp"
android:right="15dp" />
</shape>
</item>
</selector>
How can I add margin? My current setting doesn't render.
Thanks!
Why not add it to your button in XML?
<Button android:id="#+id/mybutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="xyz"
android:background="#drawable/custom_button"
android:layout_marginleft="15dp"
android:layout_marginleft="15dp" />

How to add a gradient to buttons in android through xml?

I cannot figure out why this code will not work. Can anyone help? I am trying to get the button to use a custom color titled 'greenstart' and 'greenend'. The colors have been created in the res/value/string.xml file. I have looked at similar questions but either they were left unanswered, unclear, or the solution did not work. Thanks in advance.
XML FILE SAMPLE:
<Button
android:id="#+id/mycollection"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<Gradient
android:angle="270"
android:endColor="#color/greenstart"
android:startColor="#color/greenend" >
</Gradient>
</Button>
Create a new xml file and put it in drawable and then add it to button as background
gradient.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- Gradient Bg for listrow -->
<gradient
android:startColor="#f1f1f2"
android:centerColor="#e7e7e8"
android:endColor="#cfcfcf"
android:angle="270" />
</shape>
layout.xml
<Button
android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#drawable/gradient"
android:text="Übernehmen" >
Try 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="#70c656" />
<stroke
android:width="1dp"
android:color="#53933f" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#70c656"
android:endColor="#53933f"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#53933f" />
<corners
android:radius="4dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
So here we go with the gradient. As above #Dusean Singh said. If you will use an angle 270 then your gradient will start from top to down : TOP -> Center -> bottom
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#FF0000"
android:centerColor="#00FF00"
android:endColor="#0000FF"
android:angle="270" />
</shape>
If you will use an angle 360 then your gradient will start from left to right : Left -> Center -> Right
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- Gradient Bg for listrow -->
<gradient
android:startColor="#FF0000"
android:centerColor="#00FF00"
android:endColor="#0000FF"
android:angle="360" />
</shape>
Here we go with the effect. and how to apply the same on button
<LinearLayout
android:id="#+id/design_bottom_sheet"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:drawableLeft="#drawable/ic_grid"
android:layout_width="match_parent"
android:text="Find"
android:background="#drawable/gradient_button"
android:textColor="#color/white"
android:textAllCaps="false"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:drawableLeft="#drawable/ic_card"
android:layout_width="match_parent"
android:textColor="#color/white"
android:text="Match"
android:background="#drawable/gradient_button"
android:textAllCaps="false"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
Create gradient.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="270"
android:endColor="#color/greenstart"
android:startColor="#color/greenend" />
</shape>

Categories

Resources