Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaHow to Combine Text and Image on a Button or ImageButton in...

How to Combine Text and Image on a Button or ImageButton in Android?

Image Buttons are used in many android applications for performing some actions. Some of the users of mobile applications sometimes are not aware of the images which are present on that specific buttons. So for informing the user about that button we also add a simple text to our button so that the user will get to know what is the use of the button. In this article, we will take a look at How to Combine Text and Image on a Button or Image Button in Android. A sample video is given below to get an idea about what we are going to do in this article.

Note: This Android article covered in both Java and Kotlin languages. 

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.

Step 2: Working with the activity_main.xml file

Navigate to app > res > layout > activity_main.xml and add the code below. Comments are added in the code to get to know in detail. 

XML




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/idRLContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
  
    <!--on below line we are creating
        a text for our app-->
    <TextView
        android:id="@+id/idTVHeading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:gravity="center"
        android:padding="10dp"
        android:text="Combine Text and Image on a Button or ImageButton"
        android:textAlignment="center"
        android:textColor="@color/black"
        android:textSize="20sp"
        android:textStyle="bold" />
  
    <!--on below line we are creating
       a text for our app-->
    <TextView
        android:id="@+id/idTVHead1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:padding="4dp"
        android:text="Simple Button"
        android:textAlignment="center"
        android:textSize="20sp"
        android:textStyle="bold" />
  
    <!--on below line we are creating a simple button-->
    <Button
        android:id="@+id/idSimpleBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:drawableTop="@drawable/ic_android"
        android:drawablePadding="20dp"
        android:text="Android"
        android:textAllCaps="false"
        android:textStyle="bold" />
  
    <!--on below line we are creating a text for image button-->
    <TextView
        android:id="@+id/idTVHead2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:padding="4dp"
        android:text="Image Button"
        android:textAlignment="center"
        android:textSize="20sp"
        android:textStyle="bold" />
  
    <!--on below line we are creating a frame
         layout for overlapping views-->
    <FrameLayout
        android:id="@+id/idFrame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center">
  
        <!--on below line we are creating an image button-->
        <ImageButton
            android:id="@+id/idImgBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/purple_200"
            android:padding="10dp"
            android:src="@drawable/ic_android" />
  
        <!--on below line we are creating a
             text for our image button-->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="45dp"
            android:clickable="false"
            android:text="Android"
            android:textColor="@color/white"
            android:textStyle="bold" />
    </FrameLayout>
  
</LinearLayout>


Step 3: Working with the MainActivity file 

Navigate to app > java > your app’s package name > MainActivity file and add the below code to it. Comments are added in the code to get to know in detail. 

Kotlin




package com.gtappdevelopers.kotlingfgproject
  
import android.os.Bundle
import android.widget.Button
import android.widget.ImageButton
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
  
class MainActivity : AppCompatActivity() {
  
    // on below line creating a variable.
    lateinit var simpleBtn: Button
    lateinit var imgBtn: ImageButton
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // on below line we are initializing our variables.
        simpleBtn = findViewById(R.id.idSimpleBtn)
        imgBtn = findViewById(R.id.idImgBtn)
  
        // on below line we are adding click listener
        // for our simple button
        simpleBtn.setOnClickListener {
            // on below line we are displaying a toast message.
            Toast.makeText(this, "This is a simple button", Toast.LENGTH_SHORT).show()
        }
  
        // on below line we are adding click listener
        // for our image button
        imgBtn.setOnClickListener {
            // on below line we are displaying a toast message.
            Toast.makeText(this, "This is a image button", Toast.LENGTH_SHORT).show()
        }
    }
}


Java




package com.gtappdevelopers.kotlingfgproject;
  
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    // on below line we are creating variables.
    private Button simpleBtn;
    private ImageButton imgBtn;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // on below line we are initializing our variables.
        simpleBtn = findViewById(R.id.idSimpleBtn);
        imgBtn = findViewById(R.id.idImgBtn);
  
        // on below line we are adding click listener 
        // for our simple button
        simpleBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // on below line we are displaying a toast message.
                Toast.makeText(MainActivity.this, "This is a simple button", Toast.LENGTH_SHORT).show();
            }
        });
  
        // on below line we are adding click listener for our image button
        imgBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // on below line we are displaying a toast message.
                Toast.makeText(MainActivity.this, "This is a image button", Toast.LENGTH_SHORT).show();
            }
        });
    }
}


Now run your application to see the output of it. 

Output:

RELATED ARTICLES

Most Popular

Recent Comments