I've a radio button with a custom background for it's different states. To use this background I set the android:button attribute to transparent. It seems this was not (yet) supported back in API 16 and the background appears only black.
How can I achieve the same effect as in API 17 and above?
radio_button_background.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="3dp"/>
<stroke android:width="1dp" android:color="#color/grey"/>
<solid android:color="#color/grey"/>
</shape>
</item>
<item android:state_checked="true">
<shape android:shape="rectangle">
<corners android:radius="3dp"/>
<stroke android:width="1dp" android:color="#color/grey"/>
<solid android:color="#color/grey"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<corners android:radius="3dp"/>
<stroke android:width="1dp" android:color="#color/grey"/>
</shape>
</item>
</selector>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="8dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your choice"
android:textSize="12sp"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatRadioButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="8dp"
android:padding="8dp"
android:button="#android:color/transparent"
android:background="#drawable/radio_button_background"
android:gravity="center"
android:checked="true"
android:text="Easy"/>
<androidx.appcompat.widget.AppCompatRadioButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="8dp"
android:padding="8dp"
android:button="#android:color/transparent"
android:background="#drawable/radio_button_background"
android:gravity="center"
android:text="Middle"/>
<androidx.appcompat.widget.AppCompatRadioButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="8dp"
android:padding="8dp"
android:button="#android:color/transparent"
android:background="#drawable/radio_button_background"
android:gravity="center"
android:text="Hard"/>
</RadioGroup>
</LinearLayout>
Screenshot API 16
Screenshot API 17
Set <solid android:color="#00000000"/> to the shape of your third <item> in the drawable background and remove android:button attribute from AppCompatRadioButtons. I suggest using your custom colours as practice shows android colours can be different from device to device.
Related
I want to change background color to my CardView in Android Studio, I want to use selector, but I dont know what I do
This in my code:
<androidx.cardview.widget.CardView
android:id="#+id/card_ordina"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
app:cardBackgroundColor="#color/orange"
app:cardCornerRadius="20dp">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:layout_marginRight="30dp"
android:layout_marginBottom="40dp"
android:background="#drawable/options"
android:backgroundTint="#F44336"
android:padding="10dp"
android:src="#drawable/carrello"
android:tint="#color/background" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:layout_marginBottom="5dp"
android:fontFamily="#font/title"
android:text="ORDINA"
android:textColor="#color/background"
android:textSize="18sp" />
</androidx.cardview.widget.CardView>
You need to make a selector and set the following attributes to your CardView.
android:foreground="#drawable/card_foreground"
android:clickable="true"
android:focusable="true"
Here is the code for card_foreground.xml, add it in your project's drawable folder.
<?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="#e0F5AD6E"/>
<corners android:radius="2dp" />
</shape>
</item>
<item android:state_focused="true" android:state_enabled="true">
<shape android:shape="rectangle">
<solid android:color="#0f000000"/>
<corners android:radius="2dp" />
</shape>
</item>
</selector>
You can change the state colors and radius according to your requirements. Now, when you click on the CardView the color will get changed.
I want to develop a text view background as shown below image:
How should I achieve this? and the yellow dot will change based on data.
I have just spend sometime to achieve same. This is not perfect, but you can refer this at-least . :)
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingLeft="#dimen/d10dp"
android:paddingTop="#dimen/d10dp"
android:paddingRight="#dimen/d10dp"
android:paddingBottom="#dimen/d10dp">
<item
android:id="#+id/item1">
<shape android:shape="rectangle">
<solid android:color="#3CF8C2"/>
<corners android:radius="12dp"/>
<size
android:width="40dp"
android:height="40dp"/>
</shape>
</item>
<item
android:id="#+id/item2"
android:width="16dp"
android:height="16dp"
android:end="-2dp"
android:gravity="end"
android:top="-2dp">
<shape
android:gravity="right"
android:shape="oval">
<stroke
android:width="2dp"
android:color="#fff"/>
<solid android:color="#FFC730"/>
<size
android:width="16dp"
android:height="16dp"/>
</shape>
</item>
</layer-list>
To dynamically change color of drawable, refer below code.
Find drawable by id and change its color:
LayerDrawable shape = (LayerDrawable) ContextCompat.getDrawable(YourActivity.this,R.drawable.here_drawable_name)
GradientDrawable gradientDrawable = (GradientDrawable) shape.findDrawableByLayerId(R.id.item2);
gradientDrawable.setColor(Color.Green); // changing color to Green
I have tried to do like this before. Maybe it helps.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="16dp"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_margin="4dp"
android:layout_alignParentEnd="true"
android:src="#drawable/ic_launcher_background"
android:layout_width="20dp"
android:layout_height="20dp" />
<TextView
android:layout_margin="4dp"
android:gravity="center"
android:layout_centerInParent="true"
android:textSize="25sp"
android:background="#color/blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="13" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Is there a way to create these two boxes with XML only (beside the icon of course)
The tob box has rounded bordered around it and shadow but inside it divided to two parts which one is pink background and one is white background (without stroke between them)
Create 2 XML drawables
rect_colored.xml
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/colorAccent"/>
<corners android:bottomRightRadius="5dp"
android:topRightRadius="5dp"/>
</shape>
rect_white.xml
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke android:color="#color/colorPrimaryDark"
android:width="2dp"/>
<corners android:radius="5dp"/>
</shape>
Position the drawables appropriately
This will work differently with different root views. I'm using ConstraintLayout for simplicity
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
android:background="#drawable/rect_white"
android:id="#+id/frameLayout"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp">
<!--Content here-->
</FrameLayout>
<FrameLayout
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginBottom="0dp"
android:layout_marginRight="2dp"
android:layout_marginTop="0dp"
android:background="#drawable/rect_colored"
app:layout_constraintBottom_toBottomOf="#+id/frameLayout"
app:layout_constraintRight_toRightOf="#+id/frameLayout"
app:layout_constraintTop_toTopOf="#+id/frameLayout">
<!--Content here-->
</FrameLayout>
</android.support.constraint.ConstraintLayout>
The output
Just use a LayerList with the shapes you need nested. May take a little tweaking, but it is simple enough. You can even add in your bitmaps for the left icon if you like. Just make your layers, and adjust from the sides and handle your corners.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:left="50dp">
<shape
android:shape="rectangle" >
<solid android:color="#0000FF" />
<corners android:bottomRightRadius="20dp" android:topRightRadius="20dp" />
</shape>
</item>
<item android:right="50dp">
<shape
android:shape="rectangle" >
<solid android:color="#ffffff" />
<corners android:bottomLeftRadius="20dp" android:topLeftRadius="20dp" />
</shape>
</item>
</layer-list>
Here simply you create a one xml file & drawable file to create shape & border.
MyActivity.Xml
<LinearLayout
android:id="#+id/ll_mobile_img"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:paddingLeft="15dp"
android:paddingRight="15dp">
<RelativeLayout
android:id="#+id/rr_verification_code"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_weight="0.3">
<EditText
android:id="#+id/et_verification_code"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/ractangle_mobile_white_border"
android:ems="10"
android:hint="Enter Code"
android:drawableLeft="#mipmap/ic_launcher"
android:imeOptions="actionGo"
android:inputType="number|textAutoComplete"
android:maxLength="6"
android:paddingBottom="15dp"
android:paddingLeft="10dp"
android:paddingTop="16dp"
android:singleLine="true"
android:textColor="#android:color/white"
android:textColorHint="#android:color/white"
android:textSize="18sp" />
</RelativeLayout>
<LinearLayout
android:id="#+id/ll_country_img"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#android:color/white"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="#+id/tv_verification_code_img"
android:layout_width="38dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/globe"
android:textColor="#color/blue_font_color"
android:textSize="20dp" />
</LinearLayout>
</LinearLayout>
Drawable:- ractangle_mobile_white_border.xml
[![<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<layer-list>
<item>
<shape>
<gradient android:angle="360" android:endColor="#android:color/transparent" android:startColor="#android:color/transparent" />
<stroke android:width="1dp" android:color="#android:color/white" />
<corners android:bottomRightRadius="1dp" android:topRightRadius="1dp"/>
<padding android:bottom="3dp" android:left="0dp" android:right="3dp" android:top="3dp" />
</shape>
</item>
</layer-list>
</item>
</selector>
Hope this helps you.
I need to create a drawable with a 3dp height line/rectangle on top, a black rectangle with 70% opacity and 2dp height line/rectangle on the bottom. How can I do this?
Here is the code I'm using:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="0dp" android:left="0dp" android:right="0dp">
<shape android:shape="rectangle" >
<size android:height="3dp"></size>
<solid android:color="#C81F25"></solid>
</shape>
</item>
<item android:top="2dp">
<shape android:shape="rectangle">
<solid android:color="#99000000"></solid>
</shape>
</item>
</layer-list>
PS: Ignore the gray gradient around the image, it's from Android Studio preview.
Case 1: Rectangle inside another rectangle.
Case 2: 3 Rectangles of which 2 are transparent which have negative paddings and strokes. On eclipse previews the case 2 shows wrong but it is correct and works when deployed.
case 1:
actionbarbg.xml in your drawable folder:
<?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="#F00" />
</shape>
</item>
<item android:top="3dp" android:right="0dp" android:bottom="2dp"
android:left="0dp">
<shape android:shape="rectangle">
<solid android:color="#9000" />
</shape>
</item>
</layer-list>
case 2: (sort of a hack)
<?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="#9000" />
</shape>
</item>
<item
android:top="-2dp"
android:left="-2dp"
android:right="-2dp">
<shape>
<solid android:color="#android:color/transparent" />
<stroke
android:width="2dp"
android:color="#C81F25" />
</shape>
</item>
<item
android:left="-3dp"
android:right="-3dp"
android:bottom="-3dp">
<shape>
<solid android:color="#android:color/transparent" />
<stroke
android:width="3dp"
android:color="#C81F25" />
</shape>
</item>
</layer-list>
Is it possible to build the layout without drawables using linearlayouts. I dont know which one of the two cases below you ment:
On the top layout there is a black alphamask with top and bottom margins of 2dp/3dp nested inside a red layout.
On the bottom layout there is a rectangle of 3dp height on top of a black alphamask and a rectangle of 2dp height below the alpha mask.
`
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginTop="3dp"
android:background="#9000" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Content"
android:textColor="#android:color/white" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Content"
android:textColor="#android:color/white" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Content"
android:textColor="#android:color/white" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Content"
android:textColor="#android:color/white" />
</LinearLayout>
</LinearLayout>`
OR
`
<LinearLayout
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#C81F25" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#9000" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Content"
android:textColor="#android:color/white" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Content"
android:textColor="#android:color/white" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Content"
android:textColor="#android:color/white" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#C81F25" >
</LinearLayout>
</LinearLayout>`
Here is an example of an app that used XML to design button.
How can i have the same design with XML?
How make my buttons look like it is floating just like in the image below?
I think you will need to use a shape drawable with a layered list, here is an example of a button that has a different color on the top and bottom (the drop shadow effect). You will set this as the background attribute of the Button.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<corners android:radius="2dp" />
<solid android:color="#color/button_border_dark" />
</shape>
</item>
<item android:top="1dp">
<shape android:shape="rectangle" >
<corners android:radius="2dp" />
<solid android:color="#color/button_border_light" />
</shape>
</item>
<item
android:bottom="1dp"
android:top="1dp">
<shape android:shape="rectangle" >
<corners android:radius="2dp" />
<solid android:color="#color/button_general_color" />
</shape>
</item>
</layer-list>
Here is the XML of the Button. You can also use custom typeface as well as shadows to make it the way you want.
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#android:color/holo_blue_dark"
android:textColor="#android:color/white"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_gravity="center"
android:shadowColor="#android:color/holo_blue_light"
android:id="#+id/btnClickMe"
android:text="Click Me!"
/>
Use this one....
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:background="#drawable/selected" // selected is the name of your custom file
android:text="Register"
android:textColor="#fff" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:background="#37a8f7"
android:text="Login"
android:layout_marginTop="15dp"
android:textColor="#fff" />
</LinearLayout>
you can make custom file selected.xml in drawable folder for red button.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ff0000"/>
<corners android:radius="1dp"/>
<padding android:left="3dp" android:top="2dp"
android:right="3dp" android:bottom="2dp" />
</shape>
and set it to your red button.
And you can make same as for your blue button.