How to make drawable circle shape with count - android

I really appreciate if someone can help me with using how to drawable circle view with count as like my below image.
I wrote some code for my requirement but it's showing rectangular shape but i want circular shape..
linear_layout_border(drawable file)
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1dp"
android:color="#color/colorPrimary" />
<solid android:color="#color/colorLightSky" />
<padding
android:bottom="2dp"
android:left="10dp"
android:right="10dp"
android:top="2dp" />
</shape>
xml:
<FrameLayout
android:id="#+id/group_attachment_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="3dp"
android:background="#drawable/linear_layout_border">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:src="#drawable/ic_group"
android:tint="#color/rosecolor" />
</RelativeLayout>
<TextView
android:id="#+id/members_count_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:layout_marginBottom="3dp"
android:background="#drawable/test_circle"
android:gravity="center"
android:minWidth="15dp"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:text="0"
android:textColor="#fff"
android:textStyle="bold"
android:visibility="visible" />
</FrameLayout>
image:-

Try this it looks as you want
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<corners
android:radius="10dip"/>
<solid
android:color="#2196F3" />
<stroke
android:width="2dip"
android:color="#FFF" />
<padding
android:left="2dip"
android:right="2dip"
android:top="2dip"
android:bottom="2dip" />
</shape>

<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#666666"/>
<size
android:width="120dp"
android:height="120dp"/>
</shape>
Use this code to draw a circle and put it as background of your TextView and make its android:gravity="center"
Change height and width according to your need.

Try following drawable
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item xmlns:android="http://schemas.android.com/apk/res/android">
<shape>
<solid android:color="#58050505" />
<corners
android:topLeftRadius="3dp"
android:topRightRadius="3dp"
android:bottomLeftRadius="3dp"
android:bottomRightRadius="3dp"
/>
</shape>
</item>
</selector>

Use this for the TextView background
inside drawable folder
badge_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:endColor="#color/purple"
android:startColor="#color/purple"></gradient>
<corners android:radius="100dp"></corners>
<stroke
android:width="0.5dp"
android:color="#color/white" />
</shape>
The TextView
<TextView
android:id="#+id/serial_no"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_gravity="top|start"
android:layout_marginTop="5dp"
android:background="#drawable/badge_background"
android:gravity="center|center_vertical"
android:maxLines="1"
android:text="1"
android:textColor="#android:color/white"
android:textSize="13sp"
android:textStyle="bold" />

Related

Is there a way to create this on XML ? 2 colors box

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.

How to create square button in android for background?

img
I want to create square shape in background. Android studio shape doesn,t support square.How to create manually in border.xml?
Here is my code
border.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#9294a3" />
<stroke
android:width="2dp"
android:color="#c2bbbb" />
<corners android:radius="2dp" />
</shape>
my.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#9294a3">
<TextView
android:id="#+id/tv_2"
android:layout_width="wrap_content"
android:text="2"
android:textColor="#c2bbbb"
android:textSize="30sp"
android:background="#drawable/border"
android:layout_height="wrap_content" />
</LinearLayout
Button like this
xml
<TextView
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#drawable/border"
android:text="2"
android:textSize="50sp"
android:gravity="center"
android:textColor="#ffffff"
android:padding="5dp"/>
Style (drawable/border.xml)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#color/colorGray"/>
<stroke android:color="#color/colorBlack"
android:width="4dp" />
<!--corners allow us to make the rounded corners button-->
<corners android:radius="15dp" />
</shape>
</item>
</selector>
<TextView
android:id="#+id/input_name"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_gravity="right"
android:direction="center"
android:gravity="center"/>
you should specify shape='Rectangle' for square it will be better.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle" >
<solid android:color="#9294a3" />
<stroke
android:width="2dp"
android:color="#c2bbbb" />
<corners android:radius="2dp" />

Style a EditText android

How to style a EditText like below image.
I tried different option but cant achieve that.
Image
<EditText
android:layout_width="300dp"
android:layout_height="40dp"
android:hint=" Name*"
android:background="#drawable/border"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
create a xml in drawble folder add this in it
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff" />
<stroke android:width="1dip" android:color="#363534" />
<corners android:radius="5dp" />
</shape>
<LinearLayout
android:layout_width="match_parent"
android:layout_margin="10dp"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/round_corner"
android:padding="10dp"
android:hint="First Name *"
android:textColorHint="#a9a9b3"
android:textSize="18dp"
/>
</LinearLayout>
then create a drawable file for the round corner background
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<stroke android:width="2dip" android:color="#b0b6bc" />
<corners android:radius="3dip"/>
<padding android:left="0dip" android:top="0dip" android:right="0dip"
android:bottom="0dip" />
</shape>
output image

how to set border linear layout on top and bottom

<?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:background="#ffffff"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/home"
android:layout_width="match_parent"
android:layout_height="60dip"
android:layout_marginRight="5dip"
android:src="#anim/linearsavelocationbackground"
android:weightSum="1" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_marginTop="15dip"
android:layout_weight=".2"
android:src="#drawable/homestatus" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="17dp"
android:layout_weight=".6"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="Home"
android:textColor="#3a3838"
android:textSize="15dp"
android:textStyle="bold" />
<ImageView
android:id="#+id/homeimage"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_marginRight="5dip"
android:layout_marginTop="15dp"
android:layout_weight="0.20"
android:src="#drawable/deletingright" />
</LinearLayout>
<LinearLayout
android:id="#+id/office"
android:layout_width="match_parent"
android:layout_height="60dip"
android:layout_marginRight="5dip"
android:background="#ffffff"
android:src="#anim/linearsavelocationbackground"
android:weightSum="1" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_marginTop="15dip"
android:layout_weight=".2"
android:src="#drawable/work" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="17dp"
android:layout_weight=".6"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="Home"
android:textColor="#3a3838"
android:textSize="15dp"
android:textStyle="bold" />
<ImageView
android:id="#+id/homeimage"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_marginRight="5dip"
android:layout_marginTop="15dp"
android:layout_weight="0.20"
android:src="#drawable/deletingright" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
bacground for border
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="20dp"/>
<padding android:left="10dp" android:right="10dp" android:top="10dp" android:bottom="10dp"/>
<solid android:color="#CCCCCC"/>
</shape>
This is my code i am trying to display border top and bottom on Linarlayout but i am unable to show effect i have to set border top and bottom of home Linear Layout and office linear layout i am tried to set that but effect is not coming please check where am doing mistake.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:bottom="1dp"
android:left="-2dp"
android:right="-2dp"
android:top="1dp">
<shape android:shape="rectangle" >
<stroke
android:width="1dp"
android:color="#android:color/darker_gray" />
<solid android:color="#android:color/transparent" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
</item>
Use the above layout as backgroud border. It will give you a very good effect. change the color and padding if you wish. Add this as xml in your drawable folder and make this drawable as background for the layout.
Hope this helps.
This can be done using an LayerList
Create a drawable name border_top_bottom.xml
<item>
<shape android:shape="rectangle" >
<solid android:color="#color/your_border_color" />
</shape>
</item>
<item
android:top="1dp"
android:bottom="1dp">
<shape android:shape="rectangle" >
<solid android:color="#color/your_fill_color" />
</shape>
</item>
On LinearLayout background property use
andorid:background:"#drawable/border_top_bottom.xml"
Add a xml in your drawable with the following code & then assign this xml as your linear layout background
<?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="1dp" android:color="#FF000000" />
<solid android:color="#FFDDDDDD" />
</shape>
</item>
<item android:top="1dp" android:bottom="1dp">
<shape
android:shape="rectangle">
<stroke android:width="1dp" android:color="#000" />
<solid android:color="#FFFFFF" />
</shape>
</item>
</layer-list>
I think Your problem is not Your shape, it is how You referred it inside the layout:
<LinearLayout
android:id="#+id/home"
android:layout_width="match_parent"
android:layout_height="60dip"
android:layout_marginRight="5dip"
android:src="#anim/linearsavelocationbackground"
android:weightSum="1" >
You had set the reference to the anim folder, but it has to be in the drawable folder. If Your shape is inside the anim folder, put it into drawable folder and correct Your LinearLayouts src attribute:
android:src="#drawable/linearsavelocationbackground"
And then if this is not what you want, create a shape with the examples from krunal and EagleEye.
#Edge Please use this code
Let's say name of the xml file linearbg.xml and put it in drawable folder under resources.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape>
<solid android:color="#EEEEEE" />
</shape>
</item>
<!-- This is the line -->
<item
android:top="1dp"
android:bottom="1dp">
<shape>
<solid android:color="#FFFFFF" />
</shape>
</item>
</layer-list>
Now use this linearbg.xml as background in your linear layout as
android:background="#drawable/linearbg".

Pass color attribute from custom view to drawable shape in Android

I have created a custom view that uses two <shape>'s as background in the view xml. Right now I have hardcoded the <solid color=""/> of the shape, but I would like to pass the color as an argument from code to the custom view and forward to the shape.
How do I achieve this? I'd like to pass a color reference to both #+id/rectangle and #+id/triangle from code.
Shape:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="1dp"/>
<solid android:color="#color/giddyologyOrange"/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<size android:height="60dp"
/>
<stroke
android:width="1dp"
android:color="#C95800"
/>
</shape>
View:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/rectangle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/rectangle"
android:orientation="horizontal"
android:padding="5dp">
<ImageView
android:id="#+id/iconImageView"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="center_vertical|left"
android:contentDescription="Icon"
android:src="#drawable/iconplaceholder"
android:scaleType="fitCenter"/>
<View
android:id="#+id/spaceView"
android:layout_height="match_parent"
android:layout_width="10dip"/>
<TextView
android:id="#+id/labelTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|left"
android:textColor="#color/white"
android:gravity="center|left"
android:textSize="16dip"
android:maxWidth="150dip"
android:text="This is a really long text that should wrap"/>
</LinearLayout>
<View
android:id="#+id/triangle"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="-1dp"
android:background="#drawable/triangle"
android:rotation="180"/>
</LinearLayout>
did you try this?
LinearLayout myView;
myView = (LinearLayout)findViewById(R.id.rectangle);
myView.setBackgroundColor(current_color);
I have a layout that has an ImageButton
<ImageButton
android:id="#+id/color_swatch"
android:layout_width="#dimen/standard_btn_width"
android:layout_height="#dimen/standard_btn_width"
android:background="#drawable/checker_tile"
android:contentDescription="#string/paint"
android:src="#drawable/paint" />
my src is a layer-list drawable--
layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<stroke
android:width="2dp"
android:color="#FF999999" />
<solid android:color="#00000000" />
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp" />
</shape>
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="4dp"
android:color="#FF999999" />
<solid android:color="#00000000" />
<corners android:radius="0dp" />
</shape>
</item>
</layer-list>
the background is a layer-list drawable-
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<gradient
android:type="radial" android:gradientRadius="500"
android:startColor="#17568A"
android:endColor="#494C4F" />
</shape>
</item>
<item>
<bitmap
android:src="#drawable/checkerpattern"
android:tileMode="repeat" />
</item>
</layer-list>
I am able to setBackgroundColor to any color with 50% opacity and see the checkerpattern below
I ended up using a color filter to colorize the background image.
rectangle.getBackground().setColorFilter(color, PorterDuff.Mode.SRC);
Since the color comes from my model object this seemed like a pretty easy and maintainable solution.

Categories

Resources