xml - unable to set shape as background of a TextView - android

I'm trying to set a transparent circle with a stroke as the background of a TextView in android:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:background="#drawable/decibel_circle"
android:text="30 dB"
android:fontFamily="sans-serif"
android:textColor="#android:color/black"
android:textSize="70dp" />
</RelativeLayout>
And this is the shape I want to use, located in decibel-circle.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke
android:width="5dp"
android:color="#F44336">
</stroke>
<solid android:color="#android:color/transparent"/>
<size
android:width="36dp"
android:height="36dp" />
<corners android:radius="12dp" />
</shape>
</selector>
The problem is that the shape is never shown, neither in the preview nor when I run my app.
What am I doing wrong?

in your decibel-circle.xml remove selector just use shape alone
Use this only
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke
android:width="5dp"
android:color="#F44336">
</stroke>
<solid android:color="#android:color/transparent"/>
<size
android:width="36dp"
android:height="36dp" />
<corners android:radius="12dp" />
</shape>

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!

Android drawable shape storke issue

Why are there opaque areas?
I made a custom drawable,
but the same phenomenon as the image occurred.
i test phone Samsung A30, Pixel 4a
drawable/alarm_list_reward_start.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<stroke
android:width="2.0dp"
android:color="#000000" />
<solid android:color="#FFEB2A" />
<corners android:radius="10.0dp" />
</shape>
</item>
</layer-list>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginStart="12.0dp"
android:layout_marginEnd="12.0dp"
android:layout_marginBottom="50.0dp">
<TextView
android:id="#+id/rewardBtn"
android:layout_width="match_parent"
android:layout_height="44.0dp"
android:focusable="false"
android:background="#drawable/alarm_list_reward_start"
android:gravity="center"
android:textColor="#000000"
android:textSize="15.0dp"
android:textStyle="bold" />
</RelativeLayout>
This is happening, as you are using android:layout_width="match_parent". You can solve this issue by using wrap_content. Or you could set custom width and height of the layer list, such as,
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<size android:width="152dp" android:height="152dp" />
<solid android:color="#00FFFFFF" />
<stroke android:width="2.0dp" android:color="#000000" />
<solid android:color="#FFEB2A" />
<corners android:radius="10.0dp" />
</shape>
</item>
</layer-list>

How to create a EditText with the top corner rounded and the bottom rectangle

I have a simple background in my drawable folder, just a border that I use on my EditText :
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="2dp"
/>
<solid android:color="#ffffff"
/>
<stroke
android:width="1dip"
android:color="#000" />
</shape>
But I want to achieve this.
Top rounded and the bottom rectangle in the edges! I'm having trouble and do this
Create one Drawable shape xml file
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="1dip"
android:color="#color/colorAccent"/>
<corners android:radius="10dp"
android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp"/>
</shape>
Set that drawable in you EditText background
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Text"
android:padding="#dimen/dimen10"
android:background="#drawable/testing"/>
create
round_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/white"/>
<corners android:bottomRightRadius="0dp"
android:bottomLeftRadius="0dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp"/>
</shape>
Set that drawable in you Edit Text background
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/round_shape"/>
Please try this one work for me
<solid android:color="#ffffff" />
<stroke
android:width="1dp"
android:color="#000" />
<corners
android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp"
android:topLeftRadius="25dp"
android:topRightRadius="25dp" />
please check and change radius.
here create editbox_custom_top_radius.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--Here Corner use for give curve to your edit text-->
<corners android:topLeftRadius="10dp" android:topRightRadius="10dp"/>
<!--stroke is use to apply border on edit text-->
<stroke android:width="2dp"/>
<!--solid for incase you want background color to edit box-->
<solid android:color="#color/app_grey"/>
</shape>
create editbox_custom_bottom_radius.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--Here Corner use for give curve to your edit text-->
<corners android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp"/>
<!--stroke is use to apply border on edit text-->
<stroke android:width="2dp"/>
<!--solid for incase you want background color to edit box-->
<solid android:color="#color/app_grey"/>
</shape>
and apply it on your edit box
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Text"
android:padding="#dimen/dimen10"
android:background="#drawable/editbox_custom_top_radius"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Text"
android:padding="#dimen/dimen10"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Text"
android:padding="#dimen/dimen10"
android:background="#drawable/editbox_custom_bottom_radius"/>
</LinearLayout>
try it.

How to make custom spinner with image?

I'm trying to make a custom design for spinner but no luck, i only made it through edittext.
I want something like this.
in edittext i use drawableleft, can someone help me how can i customize my spinner?
in xml:
<EditText
android:id="#+id/etBusinessKeywor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_spinner"
android:cursorVisible="false"
android:drawableRight="#drawable/ic_keyboard_arrow_down_white_24dp"
android:hint="SERVICE"
android:padding="#dimen/_12sdp"
android:textColorHint="#color/pbr_lightgray"
android:textSize="#dimen/fontsize_title"
android:textStyle="bold" />
bg_spinner:
<item>
<shape>
<corners android:radius="3dp" />
<solid android:color="#android:color/white" />
</shape>
</item>
<item>
<shape>
<corners android:radius="3dp" />
<solid android:color="#color/colorPrimary" />
</shape>
</item>
<item android:right="50dp">
<shape>
<solid android:color="#android:color/white" />
<corners
android:bottomLeftRadius="3dp"
android:topLeftRadius="3dp" />
</shape>
</item>
<item>
<shape>
<solid android:color="#android:color/transparent" />
</shape>
</item>
Try This,
<Spinner
android:id="#+id/spn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/selector_spinner"
android:popupBackground="#fff"
android:spinnerMode="dropdown"
/>
selector_spinner.xml
<?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="#000000" />
<corners
android:radius="2dp" />
</shape>
</item>
<item
android:bottom="1dip"
android:left="1dip"
android:right="1dip"
android:top="1dip">
<shape android:shape="rectangle" >
<solid android:color="#FFFFFF" >
</solid>
<corners
android:radius="2dp" />
<padding
android:right="5dp"
android:top="2dp" />
</shape>
</item>
<item>
<bitmap
android:gravity="right|center_vertical"
android:src="#drawable/ic_down_arrow" />
</item>
</layer-list>
You can find tons of answer for customizing the spinner in How to customize a Spinner in Android .Also you shouldn't use edittext as spinner.Spinners are made for choosing an option from many chooses.
try this: wrap spinner with your imageview in RelativeLayout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#drawable/spinner_border"
android:orientation="horizontal">
<Spinner
android:id="#+id/spinner2"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_centerVertical="true"
android:background="#android:color/transparent"
android:gravity="center"
android:spinnerMode="dropdown" />
<FrameLayout
android:layout_width="50dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#color/colorPrimary"
android:padding="3dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/dropdown_bar" />
</FrameLayout>
</RelativeLayout>
in res/drawable/spinner_border use:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#android:color/transparent" />
<corners android:radius="5dp" />
<stroke
android:width="1dp"
android:color="#color/colorPrimary" />
</shape>

Textview with rounded left and right

I'm wondering how to create something like the image below using drawables so I can apply it as background?
This isn't a simple rectangle with rounded corners but a kind of a rectangular oval,
Attempts:
Using a rectangle:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FFFFFF"/>
<size android:width="40dp"
android:height="10dp" />
<corners
android:radius="100dp" />
Using an oval:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#FFFFFF"/>
<size android:width="40dp"
android:height="10dp" />
I think for this you have to change the size of text view, By using the below code I got the result.
Code used in Drawable...
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ff2233"/>
<corners
android:radius="100dp" />
</shape>
Code used in button...
<Button
android:id="#+id/btn_click"
android:layout_width="90dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="22dp"
android:layout_marginTop="16dp"
android:background="#drawable/sh"
android:text="Click" />
Result: ..........

Categories

Resources