Is there an easy way to place a TextView on top of ImageView?
My code:
<ImageView
android:layout_width="120dp"
android:layout_height="120dp"
android:src="#drawable/finaloval"
android:elevation="10dp"
android:layout_centerHorizontal="true"
android:id="#+id/greencircleId"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="23dp"
android:id="#+id/textView"
android:textSize="40sp"
android:textColor="#color/colorAccent" />
Example Image
The green circle would be my #drawable/finaloval. And the %100 organic would be the TextView.
<TextView android:id="#+id/tv_versionstatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="5dp"
android:drawableBottom="#drawable/icon_new"
android:text="text"
android:textColor="#363636"
android:textSize="20sp" />
drawableBottom is also a choise for you
Please try to set the background for the layout and place your textviews inside that , i have coded it as per the image you shared in it example
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:background="#drawable/finaloval">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="100% natural"
android:layout_marginTop="23dp"
android:id="#+id/natural"
android:textSize="20sp"
android:textColor="#color/colorAccent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="organic"
android:layout_marginTop="23dp"
android:layout_below="#id/"
android:id="#+id/natural"
android:textSize="40sp"
android:textColor="#color/colorAccent" />
</RelativeLayout>
try this xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="120dp"
android:layout_height="120dp"
android:src="#drawable/finaloval"
android:layout_gravity="center"
android:elevation="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
android:textSize="40sp"
android:layout_gravity="center"
android:textColor="#color/text_black" />
</FrameLayout>
Just use a LinearLayout as root and re-sequence your views from top to bottom, meaning in your case should be TextView then ImageView.
Edited based on comment
To do overlap of views, you can use RelativeLayout or FrameLayout as suggested by others.
Try this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
android:layout_marginTop="23dp"
android:id="#+id/textView"
android:textSize="40sp"
android:textColor="#color/colorAccent" />
<ImageView
android:layout_width="120dp"
android:layout_height="120dp"
android:src="#drawable/finaloval"
android:elevation="10dp"
android:id="#+id/greencircleId"/>
</RelativeLayout>
Think this is the simplest way of doing. Hope this helps
Try to set the imageview as the background of the textview.
Do you want to put some words upon a image or a sharp drawable?
I'll use a shape drawable to make an example.
Now I have a shape like this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
>
<size
android:height="100dp"
android:width="100dp"
/>
<solid
android:color="#66ccff"
/>
</shape>
Then set it as the background of a textview in a layout xml.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/circle"
android:text="Hello World!"
/>
That looks like it. It's a 100dp*100dp one.
100*100
And try to modify the width and height of the textview.
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#drawable/circle"
android:text="Hello World!"
/>
Now it looks like this. A Lot Larger.
200*200
So the conclusion is:
Change the size of drawable and use wrap_content in the layout xml.
Change "layout_width" and "layout_height" in the layout xml.
But if you want to use the smaller one or the lager one, maybe you should add some control codes in the control java file.
(Sorry I'm not in a English-spoken country so maybe my words is not clear enough)
I'm sorry that I didn't obverse the rules before answer (cuz it's my first answer).
Related
I need relative layout background with some transparent for that i used gradient option, But it is affecting text view color also , I gave color of text view is white color but it looks like gray color. Please help me to get out of this issue.
<RelativeLayout
android:id="#+id/topLayout1"
android:layout_width="match_parent"
android:layout_height="210dp"
android:layout_alignParentTop="true"
android:foreground="#drawable/image_overlay"
>
<RelativeLayout
android:id="#+id/topLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/preloader_image"
>
<TextView
android:id="#+id/resName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:text="Green Way"
android:textColor="#color/White"
android:textSize="25sp"
android:backgroundTint="#color/White"
/>
<TextView
android:id="#+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/resName"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="#string/items"
android:textColor="#color/White"
android:textSize="16sp"
/>
</RelativeLayout>
</RelativeLayout>
and image overlay code,
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#B0000000"
android:centerColor="#A0000000"
android:endColor="#00FFFFFF"
android:angle="270"
/>
</shape>
<color name="White">#FFFFFF</color>
You can use #83000000 color code for setting transparency and that too in android:background instead of android:background property. Alternatively, you can also use "android:aplha="0.x" where 1 < x < 9, for transparency. Your final code will look like this.
<RelativeLayout android:id="#+id/topLayout1"
android:layout_width="match_parent"
android:layout_height="210dp"
android:layout_alignParentTop="true"
android:background="#83000000"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:id="#+id/topLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/resName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:text="Green Way"
android:textColor="#FFFFFF"
android:textSize="25sp"
android:backgroundTint="#FFFFFF" />
<TextView
android:id="#+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/resName"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="Items"
android:textColor="#FFFFFF"
android:textSize="16sp" />
</RelativeLayout>
You should see this ANSWER to know more about transparency. You can use various hex codes to achieve different level of transparency. Like for 50% transparency 80 code is used and color code would be like #80000000. Similary, you can use various codes in the scheme #xx000000.
I want to make a shopping cart image, with a little indicator at the top-right corner to show how many products were selected/ordered.
The thing is, I can't place it in the top right corner, only the left one (default) or center.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/shoppingCartIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon_cart" />
<ImageView
android:id="#+id/indicatorIcon"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_gravity="top|right"
android:src="#0066ff" />
</RelativeLayout>
By using FrameLayout you can give it with some padding & margin
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/shoppingCartIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon_cart" />
<ImageView
android:id="#+id/indicatorIcon"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_gravity="top|right"
android:src="#0066ff" />
</FrameLayout>
UPDATE
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/shoppingCartIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon_cart" />
<TextView
android:id="#+id/indicatorIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:layout_marginLeft="25dp"
android:background="#drawable/circle_shape"
android:text="5"
android:textSize="24sp" />
</FrameLayout>
You can create shape as per your wish
res/drawable/circle_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#0066ff"></solid>
</shape>
Apart from this i would suggest you other third library which is very nice for displaying this type of badge or highlighted view
https://github.com/jgilfelt/android-viewbadger
https://github.com/mikepenz/Android-ActionItemBadge
you can add this code to your ImageView tag.
android:layout_marginLeft="90dp"
It will make the ImageView go right marginLeft gives a blank depends on the size you want. You can change the size however you want.
The parent is a RelativeLayout so try to use the properties :
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
on your "#+id/indicatorIcon".
Hope it helps.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/shoppingCartIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon_cart" />
<ImageView
android:id="#+id/indicatorIcon"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:src="#0066ff" />
</RelativeLayout>
I have an imageview and textview in a LinearLayout with horizontal orientation. Do not know why the textview has a space on the left that I can not remove ...
I tested with padding, gravities, etc. ...
This is my xml and result in an image
Any idea?
Thank you!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left|center_vertical"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/dot" />
<TextView
android:id="#+id/text_resultado"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:gravity="center"
android:padding="3dp"
android:text="TextView"
android:textColor="#color/negroLetras"
android:textSize="#dimen/titles" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:paddingBottom="3dp"
android:paddingRight="3dp"
android:paddingTop="3dp"
android:text="%"
android:textColor="#color/negroLetras"
android:textSize="#dimen/titles" />
</LinearLayout>
Propably you have something wrong with you drawable "dot" or other values like dimen etc.
I think that because I used your code without this values with other sample values and layout was ok.
There is no problem in your code. Your code is perfect. May your problem be in image. Check it or replace it with other and then check it.
May i suggest you to change a little bit your layout?
Delete second LinearLayout and use a TextView like this:
<TextView
android:id="#+id/text_resultado"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/dot"
android:fontFamily="sans-serif-light"
android:gravity="center"
android:padding="3dp"
android:text="TextView"
android:textColor="#color/negroLetras"
android:textSize="#dimen/titles" />
Is it possible to align a view in XML in a RelativeLayout centered horizontal or vertical according another already existing view.
For example: lets say there is something like this:
The second text view should be displayed centered below the first text view:
<?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="match_parent" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="72dp"
android:text="dynamic text" />
<TextView
android:id="#+id/second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textView"
android:layout_marginLeft="43dp" <!-- Is there a rule to center it? -->
android:text="centered below text 1" />
</RelativeLayout>
Is is possible to implement something like that in XML? Is there a rule that i have missed yet? I do not want to calculate the position programmatically
I have a much better solution than the accepted one. NO EXTRA NESTING! You can do this by combining two attributes on the smaller view. if you are centering horizontally you can use both align_start & align_end to the bigger view. Make sure the text gravity is centered btw "android:gravity="center". For Vertical alignment use both align_top & align_bottom. below is the modified implementation of your layout.
<?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="match_parent"
android:paddingLeft="43dp" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/second"
android:layout_alignEnd="#+id/second"
android:gravity="center"
android:text="dynamic text" />
<TextView
android:id="#+id/second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textView"
android:gravity="center"
android:text="centered below text 1" />
</RelativeLayout>
No need for unnecessary nesting like the accepted answer.
Use a separate parent layout for those views and add it in your main layout(can contain other things if you have)
<?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="match_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:layout_marginLeft = "30dp">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dynamic text" />
<TextView
android:id="#+id/second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="centered below text 1" />
</LinearLayout>
...
...
other veiws go here
...
</RelativeLayout>
Use the followings which suits you
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
Correct solution is to use ConstraintLayout
I tried to align a textview horizontally below a button in a RelativeLayout but it was not possible as align_bottom and layout_below didn't play well together. Finally i picked up constraintLayout and tried the same logic and it worked like a charm. here is the code below.
<android.support.constraint.ConstraintLayout
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"
android:clipChildren="false"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="#+id/episode_recording_header_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#mipmap/ic_launcher"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.399" />
<TextView
android:id="#+id/button_selected_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="12sp"
android:text="text which is exactly center aligned w.r.t the Button above"
app:layout_constraintEnd_toEndOf="#+id/episode_recording_header_stop"
app:layout_constraintStart_toStartOf="#+id/episode_recording_header_stop"
app:layout_constraintTop_toBottomOf="#+id/episode_recording_header_stop"
/>
</android.support.constraint.ConstraintLayout>
The final output is attached below
Do this->
<?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="match_parent" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="dynamic text" />
<TextView
android:id="#+id/second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true" <!-- Is there a rule to center it? -->
android:text="centered below text 1" />
</RelativeLayout>
I found the solution:
Wrap the content into another RelativeLayout and then you can place this LayoutWrapper wherever you want:
<?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="match_parent" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="58dp"
android:layout_marginTop="58dp" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:text="dynamic text" />
<TextView
android:id="#+id/second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_centerInParent="true"
android:text="centered below text 1" />
</RelativeLayout>
</RelativeLayout>
If you need to use RelativeLayout please don't use the nested layouts as far as possible. Today we have many other efficient cases to solve your task. But with use of the old faithful RelativeLayout the better way is to use attributes android:layout_alignTop, android:layout_alignBottom and android:gravity. For example, you would like to align TextView according to EditText vertically.
<RelativeLayout ...>
...
<TextView
android:id="#+id/tv1"
...
android:gravity="center"
android:layout_alignTop="#+id/edt1"
android:layout_alignBottom="#id/edt1"/>
<EditText
android:id="#+id/edt1"
...
android:layout_toRightOf="#id/tv1"/>
</RelativeLayout>
In the same way you can use android:layout_alignLeft, android:layout_alignRight and android:gravity or android:layout_alignStart, android:layout_alignEnd and android:gravity horizontally.
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)).