Create An ImageButton In Android App Using Android Studio

Introduction

In this article you will learn how to create an image button in Android applications using Android studio.

Requirements

  • Android Studio version 2.1.3

If you want to develop the image button, it should follow the below steps.

Step 1

Now, open Android Studio and you can choose the File and New and afterwards choose NewProject.

android

Step 2

Here, we can create your application name and choose where to store your project and click on the NEXT button.

android

Now, we can choose the version -- it is Target Android Devices.Then click the NEXT button.

android

Step 3

Here,we can add the activity and click the NEXT button.

android

Now, we can write the activity name and Click the FINISH button.

android

Step 4

Now, open your project and you will go to activity_main.xml and afterwards you build the design, it should be in toolbox, and if you want some options, (Image Button), and use the drag and drop method.

android

Step 5

Now, you will go to project Drawable and click the Show Explorer button, and go to the Drawable folder and copy and paste some images and generate the images in your project.

android

Now, you can go to Drawable folder. You can choose some images and you can  copy and paste them.

android

Now some images are added to your project.

android

Here, you can see the layout of your design.

android

Step 6

Here, your build on the design and write the .XML code .

activity_mai.xml code

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="xyz.rvconstructions.www.imagebutton.MainActivity">  
  3.     <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Image Button " android:id="@+id/textView" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" />  
  4.     <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/cshabutton" android:padding="20dp" android:src="@drawable/csharp" android:layout_alignTop="@+id/textView" android:layout_toRightOf="@+id/textView" android:layout_toEndOf="@+id/textView" />  
  5.     <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/googlbutton" android:padding="20dp" android:src="@drawable/google" android:layout_below="@+id/cshabutton" android:layout_centerHorizontal="true" />  
  6. </RelativeLayout>  
Step 7

Now, you will go to MainActivity.java page and build the Java code. First, you declare the heaterfile is an  extention file.

android

Now, you will declare the full Java code in your project.

 

  1. package xyz.rvconstructions.www.imagebutton;  
  2. import android.support.v7.app.AppCompatActivity;  
  3. import android.os.Bundle;  
  4. import android.graphics.Color;  
  5. import android.view.Menu;  
  6. import android.view.MenuItem;  
  7. import android.view.View;  
  8. import android.widget.ImageButton;  
  9. import android.widget.Toast;  
  10. public class MainActivity extends AppCompatActivity {  
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.             super.onCreate(savedInstanceState);  
  14.             setContentView(R.layout.activity_main);  
  15.             ImageButton simpleImageButtonCsharp = (ImageButton) findViewById(R.id.cshabutton);  
  16.             ImageButton simpleImageButtonGoogle = (ImageButton) findViewById(R.id.googlbutton);  
  17.             simpleImageButtonCsharp.setOnClickListener(new View.OnClickListener() {  
  18.                     @Override  
  19.                     public void onClick(View view) {  
  20.                         Toast.makeText(getApplicationContext(), "your clicked c-Sharp", Toast.LENGTH_LONG).show();  
  21.                     }  
  22.                 ); simpleImageButtonGoogle.setOnClickListener(new View.OnClickListener() {  
  23.                     @Override  
  24.                     public void onClick(View view) {  
  25.                         Toast.makeText(getApplicationContext(), "your clicked Google", Toast.LENGTH_LONG).show();  
  26.                     }  
  27.                 });  
  28.             }  
  29.         } // This is just a sample script. Paste your real code (javascript or HTML) here.  
  30.   
  31.     if ('this_is' == /an_example/) {  
  32.         of_beautifier();  
  33.     } else {  
  34.         var a = b ? (c % d) : e[f];  
  35.     }  
Step 8

Here, you will go to run it, and select the RUN App option.

android

Here, you will choose the emulator or device, it is Nokia Nokia _X.

android

Step 9

Here you can see the output.

android

Now, you will click the C-sharp button

android

Now, you will click the Google button .

android

 

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now