Develop an application that uses the image button.

Design

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <FrameLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/frame1"
        android:background="#9D8FF0"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="#9D8FF0"
            android:scaleType="fitCenter"
            android:src="@drawable/music_img"
            android:layout_marginBottom="10dp"
            android:layout_gravity="center"
            app:tint="@color/black" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Music"
            android:layout_gravity="center"
            android:textColor="@color/black"
            android:layout_marginTop="30dp"
            android:textSize="20dp"/>
    </FrameLayout>




</androidx.constraintlayout.widget.ConstraintLayout>

Logic

MainActivity.java

package com.example.imagebtn;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    ImageButton imgButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imgButton = (ImageButton) findViewById(R.id.imageButton);
        imgButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "You Click Music Button", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Last updated