How to create a custom radio button Android - java

I'm trying to make a custom radio button...
I'm trying to guide myself in some tutorials here in the community but I'm not successful...
Below I'll leave an image of how I want my radio button to be checked/unchecked.
and then I'll leave my code as it is so far:
<RadioButton
android:id="#+id/btn_language_english_us"
android:layout_width="match_parent"
android:layout_height="34dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:button="#drawable/radio_bg"
android:fontFamily="#font/sansation"
android:text="#string/us_english"
android:textColor="#color/white"
android:textSize="16sp" />
radio_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#drawable/circle_indicator_blue" />
<item android:state_checked="false" android:drawable="#drawable/circle_indicator_white" />
</selector>
#drawable/circle_indicator_blue and #drawable/circle_indicator_white
are Vector assets
and here is the uncked/checked result
could someone help me where I'm doing wrong?
my intention is to leave as the first image mentioned above.

Instead using vector asset you may use selector item . Chane your circle_indicator_blue.xml code as below .
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<stroke android:width="5dp" android:color="#android:color/white"/>
<size android:width="24dp" android:height="24dp" />
<solid android:color="#android:color/holo_blue_light" />
</shape>
</item>
</selector>
And circle_indicator_white.xml as below -
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval" android:tint="#android:color/white">
<size android:width="24dp" android:height="24dp"/>
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="20dp"/>
</shape>
</item>
</selector>
Output-

Related

adding corners to my tabs background in android coding

I was implementing a drawable file to add a background to my tabs but i want it to have corners . I've used a shape and corners tags all embedded into a selector tag but it has no effects to my background corners . May I please get your help ? here's the code i've implemented :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/bleue_sky" android:state_selected="true">
<shape android:shape="rectangle">
<corners
android:bottomLeftRadius="15dp"
android:bottomRightRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp"
/>
</shape>
</item>
<item android:drawable="#color/blackTransparent"/>
</selector>
Try This
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="#drawable/main_tab_bg"
android:paddingHorizontal="10dp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:tabBackground="#drawable/tab_layout_background"
app:tabIndicator="#drawable/tab_layout_indicator"
app:tabIndicatorColor="#null"
app:tabIndicatorFullWidth="true"
app:tabIndicatorGravity="center"
app:tabMode="auto"
app:tabRippleColor="#null"
app:tabSelectedTextColor="#FFFFFF"
app:tabTextColor="#000000" />
res/drawable/main_tab_bg.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="#DDDDDD"/>
</shape>
</item>
<item android:top="1dp" android:bottom="1dp">
<shape android:shape="rectangle">
<solid android:color="#FFFFFF"/>
</shape>
</item>
</layer-list>
res/drawable/tab_layout_background.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="9dp"
android:bottom="9dp">
<shape android:shape="rectangle">
<solid android:color="#color/transparent" />
<corners android:radius="15dp" />
<size android:height="30dp"/>
</shape>
</item>
</layer-list>
res/drawable/tab_layout_indicator.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="9dp"
android:bottom="9dp">
<shape android:shape="rectangle">
<corners android:radius="15dp" />
<solid android:color="#000000" />
<size android:height="30dp"/>
</shape>
</item>
</layer-list>
output like this

How to give gradient on view in at top , bottom and transparent center?

I want to give gradient to this view like around 10% of imageview at top , transparent center with 70% and 30% gradient at the bottom.
How i can achieve this
Instead of just using a shape with a gradient use a layer-list with 2 gradients with specified height for each
API 21
The android:bottom , android:top attributes represent the padding for the layer list, change these to change the percentage of the gradient to show
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:bottom="690dp"
android:gravity="top">
<shape android:shape="rectangle" >
<gradient
android:angle="270"
android:type="linear"
android:endColor="#android:color/transparent"
android:startColor="#000000" />
</shape>
</item>
<item
android:top="610dp"
android:gravity="bottom">
<shape android:shape="rectangle" >
<gradient
android:angle="270"
android:type="linear"
android:endColor="#000000"
android:startColor="#android:color/transparent" />
</shape>
</item>
If your minSDK is 23+ then it's more easier to specify the height for each layer type
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:height="40dp"
android:gravity="top">
<shape android:shape="rectangle">
<gradient
android:angle="270"
android:endColor="#android:color/transparent"
android:startColor="#000000"
android:type="linear" />
</shape>
</item>
<item
android:height="70dp"
android:gravity="bottom">
<shape android:shape="rectangle">
<gradient
android:angle="270"
android:endColor="#000000"
android:startColor="#android:color/transparent"
android:type="linear" />
</shape>
</item>
Then just apply this as the background of your ViewPager
Try this out, hope this solves your issue
top_gradient:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="270"
android:centerColor="#android:color/transparent"
android:centerY="0.1"
android:startColor="#000000" />
</shape>
bottom_gradient:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="90"
android:centerColor="#android:color/transparent"
android:centerY="0.3"
android:startColor="#000000" />
</shape>
background:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/bg" />
<item android:drawable="#drawable/bg2" />
</layer-list>
gradient.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#00000000"
android:centerColor="#00000000"
android:endColor="#color/primaryTextColor"
android:angle="270"
android:centerY="0.7"
android:dither="true"
android:type="linear"
/>
</shape>
layout.xml:
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/bottomConstraint"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="bottom"
android:background="#drawable/gradient">
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/tvTitle"
style="#style/TextStyleNormal.Large.Bold.White"
android:paddingHorizontal="#dimen/padding_large"
android:paddingTop="#dimen/padding_medium"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="#+id/bottomConstraint"
app:layout_constraintTop_toTopOf="#+id/bottomConstraint"
tools:text="System Solution to Online Education:
Look To Northeast communal." />
</androidx.constraintlayout.widget.ConstraintLayout>
This will have transparent view but in bottom the gradient would be dark .
You can achieve it like this
gradient_top.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#2B3135"
android:centerColor="#2B3135"
android:endColor="#android:color/transparent"
android:angle="270"
android:centerY="0.1"
/>
</shape>
gradient_bottom.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#2B3135"
android:centerColor="#2B3135"
android:endColor="#android:color/transparent"
android:angle="90"
android:centerY="0.3"
/>
</shape>
layout.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/ib"/>
<View
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentTop="true"
android:background="#drawable/gradient_top"/>
<View
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:background="#drawable/gradient_bottom"/>
</RelativeLayout>

How to remove the delay from an android button?

I created a specially effect, if a button pressed. So the the xml file is:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/clicked"
/>
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/clicked"
/>
<item android:drawable="#drawable/shape"
/>
</selector>
shape.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<padding android:bottom="0dp" />
<solid android:color="#ffffff" />
<stroke android:width="1dp" android:color="#000000" />
</shape>
clicked.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<padding android:bottom="0dp" />
<solid android:color="#2B4881" />
<stroke android:width="1dp" android:color="#000000" />
</shape>
In activity.xml I set the file as background for the button:
<Button android:background="#drawable/background"/>
But if i press the button, it tooks a while, until the button changes it's color. Can i remove this delay?
Use below code inside selectors
<item android:drawable="#drawable/clicked" android:state_pressed="true"></item>
<item android:drawable="#drawable/shape"></item>"

How to have a button with backgroundTint and corner radius

I wanna have a button with backgroundTint and corner radius :D
Here is the style I defined:
<style xmlns:tools="http://schemas.android.com/tools" name="myButton" parent="Widget.AppCompat.Button.Colored">
<item name="android:background">#drawable/button_light</item>
<item name="android:backgroundTint" tools:targetApi="lollipop">#e0e0e0</item>
</style>
And the #drawable/button_light code:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="15dp" />
<solid android:color="#FAFAFA" />
</shape>
And I apply #style/myButton to the button I want and the result is I have round corners but no ripple effect (backgroundTint doesn't work indeed).
How can I solve this?
You can use layer-list to achieve this.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="rectangle">
<corners android:radius="15dp" />
<solid android:color="#FAFAFA" />
</shape>
</item>
<!-- Put any image or anything here as the drawable -->
<item android:drawable="?attr/selectableItemBackground"/>
</layer-list>
Create a drawable/butt.xml file containing:
?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<shape android:shape="rectangle">
<corners android:radius="1000dp" />
<solid android:color="#41ba7a" />
<stroke
android:width="2dip"
android:color="#03ae3c" />
<padding
android:bottom="4dp"
android:left="4dp"
android:right="4dp"
android:top="4dp" />
</shape>
</item>
<item android:state_pressed="true">
<shape android:shape="rectangle">
<corners android:radius="1000dp" />
<solid android:color="#3AA76D" />
<stroke
android:width="2dip"
android:color="#03ae3c" />
<padding
android:bottom="4dp"
android:left="4dp"
android:right="4dp"
android:top="4dp" />
</shape>
</item>
</selector>
2: Use it in button tag in any layout file in your code
<Button
android:layout_width="220dp"
android:layout_height="220dp"
android:background="#drawable/butt"
android:text="#string/btn_scan_qr"
android:id="#+id/btn_scan_qr"
android:textSize="15dp"
/>
you can use from this library :
material ripple effect
to add ripple effect to your button and adding your shape to button
like this :
<com.balysv.materialripple.MaterialRippleLayout
style="#style/RippleStyleBlack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
>
<Button
android:id="#+id/call_btn"
android:layout_width="60dp"
android:layout_height="25dp"
style="#style/Base.Widget.AppCompat.Button.Borderless"
android:text="call"
android:textSize="12sp"
android:background="#drawable/call_btn_bg"
android:textColor="#color/deep_orange_600"
/>
</com.balysv.materialripple.MaterialRippleLayout>

How to change height of "progress line" in Progress Bar?

Can I change the height of "progress line" in ProgressBar in Android, so it's larger than the "background line" to achieve this effect?
I have tried setting size on background and progress items on my custom progressBarDrawable, but doesn't seem to work.
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="false"
android:max="100"
android:progress="50"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:progressDrawable="#drawable/xml_progress_bar_line"
style="#style/Widget.AppCompat.ProgressBar.Horizontal"/>
Here is the xml_progress_bar_line.xml file.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background">
<shape>
<size
android:height="5dp"/>
<solid
android:color="#color/grey" />
</shape>
</item>
<item
android:id="#android:id/progress">
<clip>
<shape>
<size
android:height="10dp"/>
<solid
android:color="#color/green" />
<corners
android:radius="10dp"/>
</shape>
</clip>
</item>
</layer-list>
Yes you can do it with add android:top & android:bottom like this:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background" android:top=2dp android:bottom=2dp>
<shape>
<size
android:height="5dp"/>
<solid
android:color="#color/grey" />
</shape>
</item>
<item
android:id="#android:id/progress">
<clip>
<shape>
<size
android:height="10dp"/>
<solid
android:color="#color/green" />
<corners
android:radius="10dp"/>
</shape>
</clip>
</item>
Change the maxHeight and minHeight to equal each other, then set the layout_heightexplicitly.
Also try using #android:style/Widget.ProgressBar.Horizontal
You can also set vertical scale - mProgressBar.setScaleY(3f);
Here's an example I took from: https://xinyustudio.wordpress.com/2014/08/19/android-change-progressbar-height/
I ended up not using the progress bar at all!
I just used 2 lines in relative layout. Here is the corresponding code!
Here is the progress bar background line xml (xml_custom_progress_bar_bg.xml):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners
android:radius="4dp"/>
<solid
android:color="#color/light_grey"/>
</shape>
Here is the progress bar "progress line" xml (xml_custom_progress_bar_progress.xml):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners
android:radius="4dp"/>
<gradient
android:angle="270"
android:startColor="#color/green"
android:endColor="#color/green"/>
</shape>
And here is the progress bar container:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:layout_width="match_parent"
android:layout_height="4dp"
android:layout_centerVertical="true"
android:background="#drawable/xml_custom_progress_bar_bg"/>
<View
android:layout_width="0dp"
android:layout_height="8dp"
android:layout_centerVertical="true"
android:background="#drawable/xml_custom_progress_bar_progress"/>
</RelativeLayout>
Then, I calculate the progress manually and set the width of "progress line" programmatically.

Categories

Resources