Create Custom Background For TextView Android - android

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>

Related

Change background color on hover of a CardView

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.

How to set transparent background for RadioButton in API 16?

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.

How to create android button background svg with elevation?

I have been trying to achieve button like in attached image & for that I have created gradient SVG with shadow so that it looks like elevation but when this svg applied to button as background drawable it looks flat & ripple effect is also gone.
I want to achieve same button as below image :
And here is my code :
<Button
android:id="#+id/btn_login"
android:layout_width="match_parent"
android:layout_height="52dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="35dp"
android:enabled="false"
android:elevation="10dp"
android:translationZ="10dp"
android:stateListAnimator="#null"
android:background="#drawable/bg_gradient"
android:text="#string/btn_txt_login"
android:textColor="#android:color/white"
android:textSize="18sp" />
I'm using SVG file for creating background gradient with rounded border. Any help would be appreciated.
I have a solution for you.
Add this into your build.gradle(Module:app) dependency:
implementation 'com.android.support:cardview-v7:27.1.1'
Now use CardView layout instead of Button:
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
card_view:cardCornerRadius="10dp"
card_view:cardElevation="4dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:gravity="center_vertical|center_horizontal"
android:textSize="18sp"
android:text="LOGIN"
android:textColor="#android:color/white"
android:textStyle="bold"
android:background="#drawable/gradient"/>
</android.support.v7.widget.CardView>
Create gradient.xml file into drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#8b0ab7"
android:endColor="#0b85d0"
android:angle="0"/>
<corners android:radius="10dp"/>
</shape>
Add these attributes, if still it doesn't work use cardview
android:clipChildren="false"
android:clipToPadding="false"
and here is your code
<Button android:id="#+id/btn_login"
android:layout_width="match_parent"
android:layout_height="52dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="35dp"
android:enabled="false"
android:elevation="10dp"
android:translationZ="10dp"
android:stateListAnimator="#null"
android:clipChildren="false"
android:clipToPadding="false"
android:background="#drawable/bg_gradient"
android:text="#string/btn_txt_login"
android:textColor="#android:color/white"
android:textSize="18sp" />
Please try with using the following drawable content.
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/colorDefaultButtonRipple">
<item>
<layer-list>
<item>
<shape android:shape="rectangle">
<solid android:color="#android:color/darker_gray" />
<corners android:radius="15dp" />
</shape>
</item>
<item android:bottom="2dp" android:left="2dp">
<shape>
<gradient android:angle="0" android:endColor="#5789bc" android:startColor="#8e6fa6" />
<corners android:radius="15dp" />
<padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
</shape>
</item>
</layer-list>
</item>
</ripple>

How to draw a dot circle inside a square drawable in android?

I want a small dot inside a square button , this is what i have tried till now as answered by azizbekian:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<size android:width="100dp" android:height="100dp"/>
<solid android:color="#38b0ef"/>
</shape>
</item>
<item
android:bottom="45dp"
android:left="45dp"
android:right="45dp"
android:top="45dp">
<shape android:shape="oval">
<stroke android:width="1dp" android:color="#0c5069"/>
<size android:width="20dp" android:height="20dp"/>
<solid android:color="#0c5069"/>
</shape>
</item>
</layer-list>
The first image is what i have tried and the second image is what i want excluding the text.
and also this code is the button to which i have to apply the drawable
there are seven such buttons in an horizontal linearlayout with weightsum:100 each button has weight 14
<Button
android:id="#+id/wed"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_marginEnd="1dp"
android:background="#drawable/my_button_dot"
android:text="WED"
android:textAllCaps="true"
android:textStyle="bold"
android:textColor="#ffffff"
android:layout_weight="15"/>
the dot does not appear in the background when this drawable is applied to the button, maybe because of the size of button and drawable i tried to adjust the size of drawable, but i just do not understand it and it did not work, how can i adjust it such that the dot appears in button background.
first create color_circle in drawable
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#ffffff">
</solid>
<size
android:width="48dp"
android:height="48dp" />
</shape>
and then
<RelativeLayout
android:layout_width="64dp"
android:layout_height="148dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:textAllCaps="true"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="WED" />
</LinearLayout>
<View
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="16dp"
android:background="#drawable/color_circle" />
</RelativeLayout>
i hope this is what you want
This is
ImageView has src to dotted.xml.
<ImageView
android:layout_width="178.8dp"
android:layout_height="178.8dp"
android:src="#drawable/dotted"
android:layout_centerInParent="true"
android:layerType="software" />
add below to drawable/dotted.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<stroke
android:color="#android:color/white"
android:dashWidth="1px"
android:dashGap="10px"
android:width="6.6dp"/>
</shape>

Add custom button to MapFragment with the same style of Maps app

I need add some custom buttons on my MapFragment.
I use this code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<!--suppress AndroidDomInspection -->
<fragment
android:id="#+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true">
<ImageButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="10sp"
android:layout_marginTop="10sp"
android:src="#android:drawable/ic_menu_mylocation"/>
<ImageButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="60sp"
android:layout_marginTop="10sp"
android:src="#android:drawable/ic_menu_mylocation"/>
</RelativeLayout>
I'd like that these button have the same style of zoom buttons: white semitrasparent background, gray border and gray background on click.
How can I set the same style?
Is there a more simple way? (for example official api)
The best that I could style it is as follows:
Style your button with ImageButton, and set the android:layout_width and android:layout_height to 40dp. Set android:src to your own drawble, and android:tint to #ff595959. Set the android:background to the following drawable.
/drawable/mapbutton_state.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false">
<shape android:shape="rectangle">
<corners android:radius="1.3dp" />
<stroke android:width="1dp" android:color="#77888888" />
<solid android:color="#44FFFFFF" />
</shape>
</item>
<item
android:state_pressed="true"
android:state_enabled="true">
<shape android:shape="rectangle">
<corners android:radius="1.3dp" />
<stroke android:width="1dp" android:color="#DD888888" />
<solid android:color="#A95badce" />
</shape>
</item>
<item
android:state_focused="true"
android:state_enabled="true">
<shape android:shape="rectangle">
<corners android:radius="1.3dp" />
<stroke android:width="1dp" android:color="#DD888888" />
<solid android:color="#A9FFFFFF" />
</shape>
</item>
<item
android:state_enabled="true">
<shape android:shape="rectangle">
<corners android:radius="1.3dp" />
<stroke android:width="1dp" android:color="#DD888888" />
<solid android:color="#A9FFFFFF" />
</shape>
</item>
</selector>
I made a 9-patch image with a similar background of Google Maps button:
It's on my gist
This is my current approximation of a button for the google map api v2. It's all in the XML. I'm happy to share this technique.
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
<Button
android:id="#+id/btnFollow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="34dp"
android:background="#drawable/mapbutton"
android:padding="14dp"
android:text="#string/btnFollowText"
android:textColor="#color/black" />
/drawable/mapbutton
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="2dp" />
<stroke
android:width="1dp"
android:color="#CC444444" />
<solid android:color="#88FFFFFF" />
</shape>
/color/black
<color name="black">#000000</color>
Tailor it for your app.
You can achieve it using the 9Patch tool of the android SDK and using photoshop.
white semitrasparent background.
when you want semitransparent background you can photoshop your 9patch image using a background transparency eg. 80% transparent which will have the same background effect as the zoom button of the map.
gray background on click
You can use State List of drawable with 2 image, 1 image for light transparency and the other is the graybackground

Categories

Resources