banner



How To Change Font In Android Without Root

ColorStateList is an object which tin ascertain in an XML file that can be used to use different colors on widgets (such as Buttons , etc) depending on the state of Widgets to which it is being applied. For Example, There are many states of Buttons similar (pressed, focussed, or none of them ) and other widgets states similar enable, checkable, checked, etc,  Using Color State List is a nice way to alter the color of the push button without using shape drawables or custom images. 1 should remember the color state list tin can be used anywhere, where color is used. The color state list is divers in XML and saved under the res/colour folder. The root chemical element of the colour land list is a selector and the detail element is defined for each country that you desire to define the color by using color and alpha attributes. The default color should be the last element which is used when colour for a specific country is not defined. A sample GIF is given beneath to get an thought almost what we are going to do in this commodity. Note that we are going to implement this projection using theKotlin language.

Background Color of Button

Approach

Step 1: Create a New Project

To create a new project in Android Studio delight refer to How to Create/Start a New Project in Android Studio. Notation that select Kotlin every bit the programming linguistic communication.

Step 2: Working with the activity_main.xml file

Go to the layout binder and in the activity_main.xml file change the ConstraintLayout to LinearLayout and give its orientation vertical. Add the Button and Switch to the layout. Below is the code for the activity_main.xml file.

XML

<? xml version = "1.0" encoding = "utf-viii" ?>

< LinearLayout

android:layout_width = "match_parent"

android:layout_height = "match_parent"

android:gravity = "center"

android:orientation = "vertical"

tools:context = ".MainActivity" >

< Push button

android:id = "@+id/push"

android:layout_width = "160dp"

android:layout_height = "110dp"

android:backgroundTint = "@color/button_background_color"

android:text = "Click Me"

android:textColor = "@color/button_text_color"

android:textSize = "24sp"

android:textStyle = "bold" />

< Switch

android:id = "@+id/buttonSwitch"

android:layout_width = "160dp"

android:layout_height = "80dp"

android:checked = "truthful"

android:text = "Enabled"

android:textAlignment = "centre"

android:textSize = "24sp"

android:textStyle = "bold" />

</ LinearLayout >

Footstep 3: Add a resource directory named as the color

Add together a resources directory named every bit color to the res folder and keep the root element as a selector, since we want to select the color on the basis of the state. Add ii resource files named equally button_text_color.xml and button_background_color.xml to the color resources directory. Go on the selector as the root element for the aforementioned reason every bit mentioned above. For performing the above things refer to the beneath images and codes.

In order to create the colour resources file, exercise right click on the res binder, click on New and select Android Resource Directory.

Image representation of how to create the resource directory

At present create both the resources file (button_text_color.xml and button_background_color.xml) within the color resource directory by doing correct-click on the colour directory and keeping the selector equally the root element.

Click on color resource file and enter the respective file names

Beneath is the code for the button_background_color.xml file.

XML

<? xml version = "1.0" encoding = "utf-8" ?>

< particular android:state_enabled = "false" android:color = "#b6b7b5" />

< item android:state_pressed = "truthful" android:color = "#22a540" />

< detail android:state_selected = "true" android:color = "#fabcff" />

< item android:color = "@android:color/white" />

</ selector >

Below is the lawmaking for the button_text_color.xml file.

XML

<? xml version = "1.0" encoding = "utf-8" ?>

< item android:state_enabled = "false" android:colour = "@android:color/white" />

< item android:state_selected = "truthful" android:color = "@android:color/white" />

< detail android:color = "#db402c" />

</ selector >

Step 4: Working with the MainActivity.kt file

Get to the MainActivity.kt file, and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to sympathise the code in more detail.

Kotlin

import android.bone.Bundle

import androidx.appcompat.app.AppCompatActivity

import kotlinx.android.synthetic.chief.activity_main.*

grade MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Parcel?) {

super .onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

button.setOnClickListener {

button.isSelected != button.isSelected

}

buttonSwitch.setOnCheckedChangeListener { buttonView, isChecked ->

button.isEnabled = isChecked

}

}

}

Output: Run on Emulator



Source: https://www.geeksforgeeks.org/how-to-change-the-background-color-of-button-in-android-using-colorstatelist/

Posted by: martinpervou.blogspot.com

0 Response to "How To Change Font In Android Without Root"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel