Native Mobile App Development With Java

Android Button

15-Sep-2024

Learn how to use buttons in Android Studio project with java


Here is the tutorial for Kotlin


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_widthmatch_parent -> Text area contains fullscreen
wrap_content-> Text area contains only text size
layout_widthmatch_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
drawableRightSet Icon in Right
drawableTopSet Icon in Top
drawableBotomSet Icon in Botom
textStyle
bold, italic, normal -> Style Change
background
Set background Color


Step 1 :

Right click on drawable -> New -> Drawable resource file and create new xml file name custom_button_bd.xml






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 :

File open res/layout/activity_main.xml file −


<?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.java file −


package com.microappvalley.newtestproject;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
Button button;
ImageButton imageButton;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button) findViewById(R.id.button);
imageButton=(ImageButton) findViewById(R.id.imageButton);

button.setText("Change Button");
button.setTextSize(20);


imageButton.setImageResource(R.drawable.youtube);

imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "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.


Output:



Comments