- Android Image Button with Text Example | Java & Kotlin
- Understanding Image Button Layout
- Creating a custom background for Button
- Android Image Button (XML Code)
- Custom Image Button Java Code
- Custom Image Button Kotlin Code
- Android ImageButton with Examples
- Create ImageButton in XML Layout File
- Create ImageButton Control in Activity File
- Anndroid Handle ImageButton Click Events
- Define ImageButton Click Event in XML Layout File
- Define ImageButton Click Event in Activity File
- Android ImageButton Control Attributes
- Android ImageButton Control Example
- activity_main.xml
- MainActivity.java
- Output of Android ImageButton Example
- Android Image Buttons
Android Image Button with Text Example | Java & Kotlin
Up next
Activity life cycle in Android | Illustrated using FlowChart
Share article
What’s in this blog? Hide
Android Image Button with Text Example in both Java and Kotlin. In this blog learn how to create an Image Button with text in Android Studio using XML code.
Though we have a widget in Android Studio known as Android ImageButton, but an ImageButton can’t have an image and a text together.
So if you’re building an app where you want to show the user the image and text both on a button, there is a smarter way to achieve the result as shown below using a RelativeLayout in Android.
Understanding Image Button Layout
To create a custom image button with a text we will create a relative layout with a custom background. Inside the relative layout, there will be an ImageView and a textView widget.
Creating a custom background for Button
We will first start with crating a custom background for our image button. Here is the XML code to create custom background.
Android Image Button (XML Code)
Now lets code the android image button with textView inside it.
As you can interpret from the code above, we have used an android relative layout inside which we have an ImageView and a TextView both together formaing an Image Button for our project.
Custom Image Button Java Code
Here is an OnClickListener Java Code for our custom image button
Custom Image Button Kotlin Code
Here is an OnClickListener Kotlin Code for our custom image button
TaDa! That’s all. You’re done with creating your own Custom Image Button for your Android Project.
However, if you want to add a custom more styling like a gradient effect to your image button, view here.
Is Relative Layout something new to you or you want to refresh your knowledge about it? You can View some of our more detailed tutorial with illustrations about Relative Layout here.
Источник
Android ImageButton with Examples
In android, Image Button is a user interface control that is used to display a button with an image and to perform an action when a user clicks or taps on it.
By default, the ImageButton looks same as normal button and it performs an action when a user clicks or touches it, but the only difference is we will add a custom image to the button instead of text.
Following is the pictorial representation of using Image Buttons in android applications.
In android, we have different types of buttons available to use based on our requirements, those are Button, ImageButton, ToggleButton, and RadioButton.
In android, we can add an image to the button by using attribute android:src in XML layout file or by using the setImageResource() method.
In android, we can create ImageButton control in two ways either in the XML layout file or create it in the Activity file programmatically.
Create ImageButton in XML Layout File
Following is the sample way to define ImageButton control in XML layout file in android application.
xml version= «1.0» encoding= «utf-8» ?>
LinearLayout xmlns: android = «http://schemas.android.com/apk/res/android»
android :orientation= «vertical» android :layout_width= «match_parent»
android :layout_height= «match_parent» >
ImageButton
android :id= «@+id/addBtn»
android :layout_width= «wrap_content»
android :layout_height= «wrap_content»
android :src= «@drawable/add_icon»/>
LinearLayout >
If you observe above code snippet, here we defined ImageButton control and we are showing the image from drawable folder using android:src attribute in xml layout file.
Create ImageButton Control in Activity File
In android, we can create ImageButton control programmatically in activity file based on our requirements.
Following is the example of creating ImageButton control dynamically in an activity file.
LinearLayout layout = (LinearLayout)findViewById(R.id. l_layout );
ImageButton btn = new ImageButton( this );
btn.setImageResource(R.drawable. add_icon );
layout.addView(btn);
Anndroid Handle ImageButton Click Events
Generally, whenever the user clicks on ImageButton, the ImageButton object will receives an on-click event.
In android, we can define button click event in two ways either in XML layout file or create it in Activity file programmatically.
Define ImageButton Click Event in XML Layout File
We can define click event handler for button by adding android:onClick attribute to the element in our XML layout file.
The value of android:onClick attribute must be the name of method which we need to call in response to a click event and the Activity file which hosting XML layout must implement the corresponding method.
Following is the example of defining a button click event using android:onClick attribute in XML layout file.
android :onClick= «addOperation»/>
LinearLayout >
In Activity that hosts our XML layout file, we need to implement click event method like as shown below
/** Called when the user touches the button */
public void addOperation(View view) <
// Do something in response to button click
>
Define ImageButton Click Event in Activity File
In android, we can define ImageButton click event programmatically in Activity file rather than XML layout file.
To define button click programmatically, create View.OnClickListener object and assign it to the button by calling setOnClickListener(View.OnClickListener) like as shown below.
ImageButton btnAdd = (ImageButton)findViewById(R.id. addBtn );
btnAdd.setOnClickListener( new View.OnClickListener() <
public void onClick(View v) <
// Do something in response to button click
>
>);
>
This is how we can handle ImageButton click events in android applications based on our requirements.
Android ImageButton Control Attributes
Following are some of the commonly used attributes related to ImageButton control in android applications.
Attribute | Description |
---|---|
android:id | It is used to uniquely identify the control |
android:src | It is used to specify the source file of an image |
android:background | It is used to set the background color for an image button control. |
android:padding | It is used to set the padding from left, right, top and bottom of the image button. |
android:baseline | It is used to set the offset of the baseline within the view. |
Android ImageButton Control Example
Following is the example of defining a one ImageButton and two EditText controls in LinearLayout to get the data of EditText controls when click on ImageButton in android application.
Create a new android application using android studio and give names as ButtonExample. In case if you are not aware of creating an app in android studio check this article Android Hello World App.
Now open an activity_main.xml file from \res\layout path and write the code like as shown below
activity_main.xml
xml version= «1.0» encoding= «utf-8» ?>
LinearLayout xmlns: android = «http://schemas.android.com/apk/res/android»
android :orientation= «vertical» android :layout_width= «match_parent»
android :layout_height= «match_parent» android :id= «@+id/l_layout» >
TextView
android :id= «@+id/fstTxt»
android :layout_width= «wrap_content»
android :layout_height= «wrap_content»
android :layout_marginLeft= «100dp»
android :layout_marginTop= «150dp»
android :text= «First Number»/>
EditText
android :id= «@+id/firstNum»
android :layout_width= «wrap_content»
android :layout_height= «wrap_content»
android :layout_marginLeft= «100dp»
android :ems= «10»/>
TextView
android :id= «@+id/secTxt»
android :layout_width= «wrap_content»
android :layout_height= «wrap_content»
android :text= «Second Number»
android :layout_marginLeft= «100dp»/>
EditText
android :id= «@+id/secondNum»
android :layout_width= «wrap_content»
android :layout_height= «wrap_content»
android :layout_marginLeft= «100dp»
android :ems= «10»/>
ImageButton
android :id= «@+id/addBtn»
android :layout_width= «wrap_content»
android :layout_height= «wrap_content»
android :layout_marginLeft= «100dp»
android :src= «@drawable/add_icon»/>
LinearLayout >
If you observe above code we created one ImageButton, two TextView controls and two EditText controls in XML Layout file.
Once we are done with the creation of layout with required control, we need to load the XML layout resource from our activity onCreate() callback method, for that open main activity file MainActivity.java from \java\com.tutlane.buttonexample path and write the code like as shown below.
MainActivity.java
package com.tutlane.buttonexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity <
@Override
protected void onCreate(Bundle savedInstanceState) <
super .onCreate(savedInstanceState);
setContentView(R.layout. activity_main );
final EditText firstNum = (EditText)findViewById(R.id. firstNum );
final EditText secNum = (EditText)findViewById(R.id. secondNum );
ImageButton btnAdd = (ImageButton)findViewById(R.id. addBtn );
btnAdd.setOnClickListener( new View.OnClickListener() <
@Override
public void onClick(View v) <
if ( firstNum .getText().toString().isEmpty() || secNum .getText().toString().isEmpty())
<
Toast.makeText(getApplicationContext(), «Please fill all the fields» , Toast. LENGTH_SHORT ).show();
>
else <
int num1 = Integer.parseInt( firstNum .getText().toString());
int num2 = Integer.parseInt( secNum .getText().toString());
Toast.makeText(getApplicationContext(), «SUM = » + (num1 + num2), Toast. LENGTH_SHORT ).show();
>
>
>);
>
>
If you observe above code we are calling our layout using setContentView method in the form of R.layout.layout_file_name in our activity file. Here our xml file name is activity_main.xml so we used file name activity_main and we are getting the values from two EditText controls on ImageButton click and performing an addition operation.
Generally, during the launch of our activity, the onCreate() callback method will be called by the android framework to get the required layout for an activity.
Output of Android ImageButton Example
When we run the above example using an android virtual device (AVD) we will get a result like as shown below.
This is how we can use ImageButton control in android applications to perform required operations on ImageButton tap or click based on our requirements.
Источник
Android Image Buttons
You have used a normal button in a previous project. You simply dragged a button widget on a layout and then added some text. However, you can have images on your buttons. In the image below, we have a red X image on the left with the text WARNING! to the right of it:
You can also have an Image Button without any text. In the screenshot below, we have placed a green tick image on the standard grey button background. This replaces the text that normally appears on a button.
Another option is to have a different image on the button, depending on whether the button is pressed/clicked, focused, or just in a normal state. In this section, you’ll learn how to do all three of these Android button styles. First up, let’s see how to have an image and text on the same button.
Start a new project for this. Give it the Application name of Button Widgets. When your project loads in Android Studio, click on the activity_main.xml tab. Make sure you are on the Design tab and not the Text tab. Click on the Hello World TextView to select it. Now hit the delete key on your keyboard to get rid of it. (You can also select a control in the Component Tree and hit the delete key there.)
You’ll now need some images to go on your buttons. Right click the red X image below and save it to your own computer. (Don’t forget where you saved it to.)
In Android Studio, select the res > drawable folder from the left. Now open up the folder where you saved your image to. Copy this image. Go back to Android Studio and your drawable folder. Paste your image into this folder. (You can use the standard CTRL + C and CTRL + V to copy and paste in Windows and Linux, or Command + C and Command + V on a Mac.)
When you paste the image into the drawable folder, you may see this dialog box first:
Click OK on this to see the the following message box:
Click OK on this dialogue box. Your res > drawable folder will then look like this (there may be some default XML files as well):
Go over to the Palette and drag a Button Widget on to your design surface. Change the Text property of your button to Warning! (We won’t worry about adding a string resource for this tutorial.) It should look like this:
Notice that we’ve added constraints to the top and bottom of the button to the top and bottom of the screen (If you’re not sure how to do this, see the section on Adding Layout Widgets.)
To get an image to the left of button text, the property you need is called drawableLeft.
Scroll down to the bottom of the properties area in earlier versions of Android Studio and click the link that says View all properties:
In later versions of Android Studio, expand the All Attributes item instead.
Locate the drawableLeft property from the new list:
Click the grey Pick a Resource button:
You will then see the following dialogue box appear:
Select your image from the left and click OK.
If you’re using an earlier vesion of Android Studi, to get back to the first properties page, click the link at the bottom that says View fewer properties:
Now have a look at your button:
You should find that it has an image to the left of your text.
In the next lesson, we’ll have a look at the ImageButton Widget.
Источник