Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to place button inside of EditText

I want to place an image button inside of EditText, but I don't have Idea please tell me how to do so as shown in the figure . Thanks

enter image description here

like image 432
Arshad Ali Avatar asked Oct 31 '12 04:10

Arshad Ali


People also ask

How do I add an icon to EditText?

2- First thing we need to do is to look for icons that we can use them for the android edittext. Right click on res folder → New → Vector Asset . Adjust the size from (24dp x 24dp) to (18dp x 18dp), choose the icon that you want by clicking on the android icon, click “Next” button and then click “Finish”.

Can you set text in EditText?

In android, we can set the text of EditText control either while declaring it in Layout file or by using setText() method in Activity file.

How do I restrict inputs in EditText?

Use TextWatcher to check each thing as it is entered and determine whether it should be allowed into the EditText or ignored. Make yourself one and override its methods to implement whatever logic you want.

How do you center text in EditText?

Simpler Solution to align text in EditText is to add android:gravity="center" in layout xml. There is no need to add Java code.


2 Answers

If You dont want click on that Image, Then you can use drawableRight property for EditText..

android:drawableRight="@drawable/icon" 

If you want click then use below code.

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="wrap_content" >      <EditText         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:hint="Enter search key" />      <ImageButton         android:id="@+id/button1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignParentRight="true"         android:src="@drawable/search"         android:layout_centerVertical="true"         android:layout_margin="5dp"         android:text="Button"/>  </RelativeLayout> 
like image 159
Niranj Patel Avatar answered Oct 21 '22 08:10

Niranj Patel


If you want such layout and also image as clickable then you can do something like this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="wrap_content"     android:layout_height="wrap_content" >      <EditText         android:id="@+id/editText1"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:layout_centerVertical="true" >      </EditText>      <ImageView         android:id="@+id/imageView1"         android:padding="5dp"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignTop="@+id/editText1"         android:layout_alignBottom="@+id/editText1"         android:layout_alignRight="@+id/editText1"         android:src="@drawable/ic_launcher" />  </RelativeLayout> 

Output:

enter image description here

like image 23
Mohammed Azharuddin Shaikh Avatar answered Oct 21 '22 08:10

Mohammed Azharuddin Shaikh