Sunday, September 8, 2019

Android Styling Radio Button

Android Koltin RadioButton allows user select one option from a set , RadioButton Generally used along with RadioGroup .


RadioGroup group contains multiple RadioButton , when user click one radio button that will it marked as checked and rest others are unchecked

colorAccent


To Change Radio Button Mark Color everywhere in your entire application , then change the colorAccent color of BaseTheme in style.xml

 <!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>


Particular Radio Button


 When you want to particular Radio Button then create  your own style and extend it to Widget.AppCompat.CompoundButton.RadioButton Add items to style .
  1. colorControlNormal controls the 'normal' state of components such as an unselected EditText, and unselected Checkboxes and RadioButtons
  2. colorControlActivated overrides colorAccent as the activated or checked state for Checkboxes and RadioButtons
  3. colorControlHighlight controls the ripple coloring .
<style name="CustomRadioTheme" parent="Widget.AppCompat.CompoundButton.RadioButton">
<item name="android:colorControlNormal">#7a7a7a</item>
<item name="android:colorControlActivated">#FF9800</item>
<item name="android:colorControlHighlight">#FF9800</item>
</style>


<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/CustomRadioTheme"
android:id="@+id/radio_grp">

<RadioButton;
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="option 1"
android:textSize="16sp"/>

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="option 2"
android:textSize="16sp"/>

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="option 2"
android:textSize="16sp"/>

</RadioGroup>

If you are searching life partner. your searching end with kpmarriage.com. now kpmarriage.com offer free matrimonial website which offer free message, free chat, free view contact information. so register here : kpmarriage.com- Free matrimonial website

Android Kotlin SwitchCompat (on/off) Button

A Switch is a two-state toggle switch widget that can select between two options. The user may drag the "thumb" back and forth to choose the selected option, or simply tap to toggle


SwitchCompat is a version of the Switch widget which runs on devices back to API 7.

Android Switch and ToggleButton both are the subclasses of CompoundButton class.

Lets see a sample example .

 XML Layout

  1. Create XML Layout activity_main.xml , this will be set as content view for launcher Activity (MainActivity.kt)
  2. Added Seekbar To the xml layout .
file : activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity"
android:padding="30dp"
android:orientation="vertical"
android:id="@+id/root">

<androidx.appcompat.widget.SwitchCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Swtich Button"
android:id="@+id/switchCompat01"/>

</LinearLayout>

setOnCheckedChangeListener For Switch Button


setOnCheckedChangeListener is a callback that notifies swtich button is on /off

switchCompat01.setOnCheckedChangeListener { buttonView, isChecked ->
val msg: String = if (isChecked)
"Switch Button is Checked"
else
"Switch Button is UnChecked"
showToast(msg)
}

Activity


  1. Create a koltin class MainActivity.kt and extend it to AppCompatActivity class .
  2. Override onCreate function and set the content of this MainActivity with above defined xml layout (activity_main.xml).
  3. Set the setOnCheckedChangeListener for swtich and show the status  .

file : MainActivity.kt
package com.tutorialsbuzz.androidswtich

import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

switchCompat01.setOnCheckedChangeListener { buttonView, isChecked ->
val msg: String = if (isChecked)
"Switch Button is Checked"
else
"Switch Button is UnChecked"
showToast(msg)
}
}

private fun showToast(msg: String) {
Toast.makeText(this@MainActivity, msg, Toast.LENGTH_SHORT).show()
}
}






If you are searching life partner. your searching end with kpmarriage.com. now kpmarriage.com offer free matrimonial website which offer free message, free chat, free view contact information. so register here : kpmarriage.com- Free matrimonial website