How to place button inside of EditText - android

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

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>

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:

In Material, there is that underline under EditText. In most applications the icons appear inside that underlining.
For that purpose, just use padding
android:paddingEnd = "#dimen/my_button_size"
Don't forget paddingRight for sub 4.2 versions.
<RelativeLayout ....>
...
<EditText
...
android:id="#+id/my_text_edit"
android:textAlignment = "viewStart"
android:paddingEnd = "#dimen/my_button_size"
android:paddingRight = "#dimen/my_button_size"
.../>
<ImageButton
....
android:layout_width="#dimen/my_button_size"
android:layout_height="#dimen/my_button_size"
android:layout_alignEnd="#id/my_text_edit"
android:layout_alignRight="#id/my_text_edit"/>
...
</RelativeLayout>
It will limit the text to the image and the image will not overlap the text, however long the text may be.

My solution will fit all screen sizes without the editText going "behind" the imageButton. This also uses android resources, so you do not need to provide your own icon or string resource. Copy and paste this snippet wherever you would like a search bar:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_margin="8dp"
android:id="#+id/etSearch"
android:hint="#android:string/search_go"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<ImageView
android:id="#+id/ivSearch"
android:background="#color/transparent_black"
android:padding="2dp"
android:layout_width="wrap_content"
android:layout_margin="8dp"
android:src="#android:drawable/ic_menu_search"
android:layout_height="wrap_content"
tools:ignore="contentDescription" />
</LinearLayout>
Restult:

you can Use the Below Code :
android:drawableRight="#android:drawable/ic_menu_search"

How about placing it in a RelativeLayout 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 for Search -->
<EditText android:id="#+id/inputSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Search whatever..."
android:textSize="14dp"
android:textStyle="italic"
android:drawableLeft="#drawable/magnifyingglass"
android:inputType="textVisiblePassword"
android:theme="#style/EditTextColorCustom"/>
<Button
android:id="#+id/btn_clear"
android:layout_width="14dp"
android:layout_height="14dp"
android:layout_marginTop="10dp"
android:layout_alignRight="#+id/inputSearch"
android:layout_marginRight="5dp"
android:background="#drawable/xbutton"
android:visibility="gone"/>
</RelativeLayout>
Also this way you´ll be able to handle the visibility of the Button (GONE / VISIBLE) in code.
Hope it helps.

Place your EditText inside a linear layout (horizontal) and add a Image Button next to it (0 padding). Setting the background color of EditText and ImageButton will blend in.

There's solution with clickable compound drawable. It's working very well - I have tested it
SOLUTION

This is pretty simple I feel. Use relative layout as your parent layout.
Place the editText to left and ImageButton to the right using either,
alignRight property for the ImageButton
by specifying the layout weight for each of the fields

In the accepted answer the ripple effect doesn't work, it stays behind the button in the back.
This example has 2 buttons in the textbox.
The ripple effect works because the parent layout has
android:background="#android:color/transparent"
To make sure the ripple works and doesn't stay on the back, set the imagebutton's parent background to transparent.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:focusableInTouchMode="true"
android:layout_margin="4dp">
<EditText
android:id="#+id/editText"
style="#android:style/TextAppearance.Medium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:layout_marginRight="2dp"
android:background="#drawable/shape_textbox"
android:hint="text"
android:imeOptions="actionDone"
android:importantForAutofill="no"
android:inputType="text" />
<ImageButton
android:id="#+id/btn1"
android:layout_width="44dp"
android:layout_height="44dp"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:layout_toStartOf="#+id/btn2"
android:background="?android:selectableItemBackgroundBorderless"
android:src="#drawable/ic_btn1" />
<ImageButton
android:id="#+id/btn2"
android:layout_width="44dp"
android:layout_height="44dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:background="?android:selectableItemBackgroundBorderless"
android:contentDescription="#string/clear"
android:src="#drawable/ic_btn2" />
</RelativeLayout>
Or you can also wrap your button in a frame layout:
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
>
<ImageButton ... />
</FrameLayout>

I don't know why you want to place ImageButton to achieve, which can be done with simple property of EditText
android:drawableRight="your drawable"
Can't it be work for you?

Use the android:drawableLeft property on the EditText.
<EditText
...
android:drawableLeft="#drawable/my_icon" />

Related

Align the drawableEnd image to the end of the text on the button

How to align the drawableEnd image to the end of the text on the button? Button width set to match_parent.
I want to get the following button:
[]
I'm sorry, but I couldn't find a solution and can't give any code examples.
for that you have to make custom and give click event to parent layout button(try this code it will work for you)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="#dimen/value_40"
android:background="#color/text1">
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:textColor="#color/white"
android:layout_centerInParent="true"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/cross"
android:layout_toRightOf="#+id/tv1"
android:layout_centerVertical="true"
android:layout_marginStart="5dp"/>
</RelativeLayout>
</LinearLayout>

Android Button/TextView styling

I want to have a button like this.
It's basically a Text: NEXT with a Chevron on the right side.
I already tried a button with transparent background and android:drawableRight, but the padding of the drawable was looking weird.
How can I achieve the wanted look?
What I have so far:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:textColor="#color/colorAccent"
android:drawableRight="#drawable/chevron_right"
android:background="#00000000"/>
/Sorry for posting a link and not the photo directly, but my reputations aren't high enough./
You can do it using a FrameLayout/RelativeLayout containing the ImageButton and an horizontal LinearLayout/RelativeLayout which contains the text and the chevron image aligned to the right. Something like this:
<FrameLayout
android:id="#+id/fl_btn_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton
android:id="#+id/imbtn_btn_bg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/white_drawable" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical">
<TextView
android:id="#+id/tv_btn_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginRight="10dp"
android:gravity="center_horizontal"
android:text="#string/next" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/tv_btn_text"
android:src="#drawable/chevron" />
</RelativeLayout>
</FrameLayout>
This example is from my app, probably you'll need to adapt it.
Have you tried setting the spaces between the views of chevron and borders with negative values of dp .
I had a similar problem last year and a proper combination of positive and negative dp fixed it
You can use android:drawablePadding="8dp" to set the padding between the text and the drawable.
I'm not sure what you want but that's how I will do the button you attached
<RelativeLayout
android:layout_width="80dp"
android:background="#color/colorAccent"
android:layout_height="40dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NEXT"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/arrow_up_float"/>
</LinearLayout>
</RelativeLayout>

I want use edit text with cross button,but when I'm trying to enter the text my text is going on the top of the button

I want use edit text with cross button,but when I'm trying to enter the text my text is going on the top of the button
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="9dp"
android:padding="5dp">
<EditText
android:id="#+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
[![enter image description here][1]][1]android:layout_marginTop="20dp"
android:textSize="25dp"
android:textStyle="bold"
android:hint="hi"
android:singleLine="true" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_gravity="right|center_vertical"
android:background="#drawable/ic_action" />
</FrameLayout>
put the button before the editText, in a framelayout whichever is the first child will be at the top of all the other children so what you need to do is this,
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="9dp"
android:padding="5dp">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_gravity="right|center_vertical"
android:background="#drawable/ic_action" />
<EditText
android:id="#+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
[![enter image description here][1]][1]android:layout_marginTop="20dp"
android:textSize="25dp"
android:textStyle="bold"
android:hint="hi"
android:singleLine="true" />
</FrameLayout>
Apart from the suggestions provided:
You can also try to use android:layout_below
Multiple children inside a FrameLayout might overlap across different screen sizes (see FrameLayout), try using another parent layout like RelativeLayout, Or LinearLayout.

Android Relative layout: ToLeftOf gone view

I have a relative layout in which I have a TextView on the Left and a Spinner on the right. Also I need an error image to show if user selected incorrect variant.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:text="#string/some_text"/>
<Spinner
android:id="#+id/spinner"
android:layout_width="74dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/err"/>
<ImageView
android:id="#+id/err"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/ic_error"/>
</RelativeLayout>
I want my spinner to be on the aligned to the right when error is not shown (visibility = GONE) and move it to be to the left of error image when the error is visible. How do I do that? Now it just ignores this:
android:layout_toLeftOf="#+id/err"
EDIT: thank you, I corrected the typo, but it's not the cause of the problem
Perhaps you want something like this:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|right"
android:layout_alignParentRight="true"
android:orientation="horizontal" >
<Spinner
android:id="#+id/spinner"
android:layout_width="74dp"
android:layout_height="40dp" />
<ImageView
android:id="#+id/err"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_error"/>
</LinearLayout>
EDIT: Just put your Spinner and ImageView inside a LinearLayout like this one.
EDIT 2: I just received a +10 and as I am a bit ashamed about this solution, follow this:
So, for future questions related to this one, please ignore my answer and create your own custom view extending ConstraintLayout and include a Spinner and an ImageView on the custom layout, preferably using Kotlin. Thanks!
Adding an anchor view on the right side of the relative layout can solve this issue. Add the anchor view to the RelativeLayout:
<View
android:id="#+id/anchor_top_right"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
/>
Replace the error ImageView relative position from android:layout_alignParentRight="true" to android:layout_toLeftOf="#id/anchor_top_right". Also remove android:layout_alignParentRight="true" from the Spinner, it's aligned to the left of the err.
Here's the full layout with the anchor:
<?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"
>
<View
android:id="#+id/anchor_top_right"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
/>
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:text="#string/some_text"
/>
<Spinner
android:id="#+id/spinner"
android:layout_width="74dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/err"
/>
<ImageView
android:id="#+id/err"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/anchor_top_right"
android:src="#drawable/ic_error"
/>
</RelativeLayout>
I think you have a typo in your code.
It has to be android:layout_toLeftOf="#+id/err" instead of android:layout_toLeftOf="#id/err"
Make 2 Spinners, and every time make one of them invisible

How to show the text on a ImageButton?

I have an ImageButton and I want to show a text and an image on it. But when I try on emulator:
<ImageButton
android:text="OK"
android:id="#+id/buttonok"
android:src="#drawable/buttonok"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
I get the image but without the text. How can I show the text? Please help me!
As you can't use android:text I recommend you to use a normal button and use one of the compound drawables. For instance:
<Button
android:id="#+id/buttonok"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/buttonok"
android:text="OK"/>
You can put the drawable wherever you want by using: drawableTop, drawableBottom, drawableLeft or drawableRight.
UPDATE
For a button this too works pretty fine. Putting android:background is fine!
<Button
android:id="#+id/fragment_left_menu_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_bg"
android:text="#string/login_string" />
I just had this issue and is working perfectly.
It is technically possible to put a caption on an ImageButton if you really want to do it. Just put a TextView over the ImageButton using FrameLayout. Just remember to not make the Textview clickable.
Example:
<FrameLayout>
<ImageButton
android:id="#+id/button_x"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#null"
android:scaleType="fitXY"
android:src="#drawable/button_graphic" >
</ImageButton>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:clickable="false"
android:text="TEST TEST" >
</TextView>
</FrameLayout>
Guys I need to develop the setting and logout button, I used the below code.
<Button
android:id="#+id/imageViewLogout"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/size_30dp"
android:layout_alignParentLeft="true"
android:text="Settings"
android:drawablePadding="10dp"
android:background="#android:color/transparent"
android:layout_alignParentBottom="true"
android:drawableTop="#drawable/logout" />
Actually, android:text is not an argument accepted by ImageButton
but, If you're trying to get a button with a specified background (not android default) use the android:background xml attribute, or declare it from the class with .setBackground();
I solved this by putting the ImageButton and TextView inside a LinearLayout with vertical orientation. Works great!
<LinearLayout
android:id="#+id/linLayout"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageButton
android:id="#+id/camera_ibtn"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:background="#drawable/camera" />
<TextView
android:id="#+id/textView2"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/take_pic"
android:textColor="#FFFFFF"
android:textStyle="bold" />
</LinearLayout>
You can use a LinearLayout instead of using Button it's an arrangement i used in my app
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="#color/mainColor"
android:orientation="horizontal"
android:padding="10dp">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/ic_cv"
android:textColor="#color/offBack"
android:textSize="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="#string/cartyCv"
android:textColor="#color/offBack"
android:textSize="25dp" />
</LinearLayout>
you can use a regular Button and the android:drawableTop attribute (or left, right, bottom) instead.
Best way:
<Button
android:text="OK"
android:id="#+id/buttonok"
android:background="#drawable/buttonok"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Heres a nice circle example:
drawable/circle.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#ff87cefa"/>
<size
android:width="60dp"
android:height="60dp"/>
</shape>
And then the button in your xml file:
<Button
android:id="#+id/btn_send"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="#drawable/circle"
android:text="OK"/>
Here is the solution
<LinearLayout
android:id="#+id/buttons_line1"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:id="#+id/btn_mute"
android:src="#drawable/btn_mute"
android:background="#drawable/circle_gray"
android:layout_width="60dp"
android:layout_height="60dp"/>
<ImageButton
android:id="#+id/btn_keypad"
android:layout_marginLeft="50dp"
android:src="#drawable/btn_dialpad"
android:background="#drawable/circle_gray"
android:layout_width="60dp"
android:layout_height="60dp"/>
<ImageButton
android:id="#+id/btn_speaker"
android:layout_marginLeft="50dp"
android:src="#drawable/btn_speaker"
android:background="#drawable/circle_gray"
android:layout_width="60dp"
android:layout_height="60dp"/>
</LinearLayout>
<LinearLayout
android:layout_below="#+id/buttons_line1"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="mute"
android:clickable="false"
android:textAlignment="center"
android:textColor="#color/Grey"
android:layout_width="60dp"
android:layout_height="wrap_content"/>
<TextView
android:text="keypad"
android:clickable="false"
android:layout_marginLeft="50dp"
android:textAlignment="center"
android:textColor="#color/Grey"
android:layout_width="60dp"
android:layout_height="wrap_content"/>
<TextView
android:text="speaker"
android:clickable="false"
android:layout_marginLeft="50dp"
android:textAlignment="center"
android:textColor="#color/Grey"
android:layout_width="60dp"
android:layout_height="wrap_content"/>
</LinearLayout>
Best way to show Text on button(with image)
Your Question: How to show text on imagebutton?
Answer: You can not display text with imageButton. Method that tell in Accepted answer also not work.
because
If you use android:drawableLeft="#drawable/buttonok" then you can not set drawable in center of button.
If you use android:background="#drawable/button_bg" then color of your drawable will be changed.
In android world there are thousands of option to do this. But here i provide best alternate according to my point of view. (see below)
Solution: Use cardView with LinearLayout
Your drawable/image use in LinearLayout because it shows in center. And with help of textView you can set text on this. We makes cardView background to transparent.
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="99dp"
android:layout_margin="16dp"
app:cardBackgroundColor="#android:color/transparent"
app:cardElevation="0dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/your_selected_image"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Happy Coding"
android:textSize="33sp"
android:gravity="center"
>
</TextView>
</LinearLayout>
</androidx.cardview.widget.CardView>
here i explain some terms:
app:cardBackgroundColor="#android:color/transparent" for make transparent background of cardView
app:cardElevation="0dp" for hide evelation lines around cardView
app:cardUseCompatPadding="true" its provide actual size of cardView. Always use this when you use cardView
set your image/drawable in LinearLayout as a background.
Sorry, for my Bad English.
Happy Coding:)
ImageButton can't have text (or, at least, android:text isn't listed in its attributes).
The Trick is:
It looks like you need to use Button (and look at drawableTop or setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)).

Categories

Resources