Button on Android shows text to the user and gives them the option to modify it programmatically. Although the basic class is set up to prevent modification, we are still able to alter it, TextView is a fully functional text editor.
There Are Some Common Attributes for Button :
id | This is unique identity. |
layout_width | match_parent -> Text area contains fullscreen wrap_content-> Text area contains only text size |
layout_width | match_parent -> Text area contains fullscreen wrap_content-> Text area contains only text size |
textSize | Change The Text Size |
textColor | Change Text Color |
drawableLeft | Set Icon in Left |
drawableRight | Set Icon in Right |
drawableTop | Set Icon in Top |
drawableBotom | Set Icon in Botom |
textStyle | bold, italic, normal -> Style Change |
background | Set background Color |
Step 1 :
Write this code in custom_button_bd.xml file-
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#6E0404" /><!-- background color-->
<corners android:radius="10dp" /><!-- round corner-->
<stroke android:width="1dp"
android:color="#D3CFCF" /><!-- border color-->
<padding android:bottom="5dp"
android:left="10dp"
android:right="10dp"
android:top="5dp"/>
</shape>
Step 2 :
File open res/values/themes/theme.xml file −
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.NewTestProject" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your light theme here. -->
<!-- <item name="colorPrimary">@color/my_light_primary</item> -->
</style>
<style name="Theme.NewTestProject" parent="Base.Theme.NewTestProject" />
</resources>
Step 3 :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_margin="20dp"
android:orientation="vertical">
<!--Simple Button-->
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<!-- Button textColor,textSize,textStyle,backgroundTint,drawableRight-->
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="20dp"
android:layout_margin="10dp"
android:textStyle="italic"
android:backgroundTint="@color/black"
android:drawableRight="@drawable/tap"
android:text="Button" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#F17070"
android:textSize="20dp"
android:layout_margin="10dp"
android:textStyle="italic"
android:backgroundTint="#000000"
android:drawableLeft="@drawable/tap"
android:text="Button" />
<!--Custum Background-->
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="20dp"
android:layout_margin="10dp"
android:textStyle="italic"
android:background="@drawable/custom_button_bg"
android:drawableLeft="@drawable/tap"
android:text="Button" />
<!-- Image Button-->
<ImageButton
android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#F40909"
android:src="@drawable/youtube" />
</LinearLayout>
</RelativeLayout>
Step 4 :
File open com.microappvalley.newtestproject/MainActivity.kt file −
package com.microappvalley.newtestproject
import android.os.Bundle
import android.widget.Button
import android.widget.ImageButton
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button: Button =findViewById(R.id.button)
val imageButton: ImageButton =findViewById(R.id.imageButton)
imageButton.setOnClickListener {
Toast.makeText(this@MainActivity,"Click me",Toast.LENGTH_SHORT).show()
}
}
}
Open Device Manager, run the emulator, and then run the application. Next, check the working output and check the output you declared in your code.