i want to display text on image view.
Not sure how much this would apply but if you're trying to do this (pseudo code):
<LinearLayout>
<TextView />
<ImageView />
</LinearLayout>
As you're discovering, you'll have issues doing that, if you change it to be this though:
<LinearLayout Background="YourImagePath">
<TextView />
</LinearLayout>
You'll probably find that easier.
Assuming that you're not after creating a derived type and painting yourself as others are suggesting.
This worked for me inside a RelativeLayout
<ImageView
android:id="#+id/image_ct"
android:src="#drawable/categories_bill"
android:scaleType="center"
android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/catdescription_ct"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Sample Description"
android:layout_alignTop="#id/image_ct"
android:textColor="#000000" />
you'll have to subclass it and overload the onDraw method, then draw on the graphic context using the paintString method.
or just overlay a label over the ImageView
You can create a new class derived from ImageView. Override the onDraw method, drawing the parent class first and then your own text.
Couldn't he just put an ImageView and a TextView in a RelativeLayout and set the layout_align properties of one of them to reference the other view so they will simply overlap each other or am I missing something? Seems more simple to me.
Best Option to use text and image in a single view try this:
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
**android:drawableBottom="#drawable/ic_launcher"**
android:text="TextView" />
That's how I did it and it worked exactly as you asked for:
<ImageView
android:id="#+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/myImageSouce" />
<TextView
android:id="#+id/myImageViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/myImageView"
android:layout_alignTop="#+id/myImageView"
android:layout_alignRight="#+id/myImageView"
android:layout_alignBottom="#+id/myImageView"
android:layout_margin="1dp"
android:gravity="center"
android:text="Hello"
android:textColor="#000000" />
PS: Only works inside a RelativeLayout
You can also try just TextView with image
<TextView
app:layout_gravity="center_horizontal"
android:drawableTop="#drawable/icon"
android:gravity="center_horizontal"
android:text="Icon"
android:textSize="20sp"
android:textStyle="bold" >
</TextView>
I stuck together several ways, and that's work for me, with the text centered in the image inside RelativeLayout.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/grid_item_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:contentDescription="line"
android:src="#drawable/tile1" />
<TextView
android:id="#+id/grid_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:text="Line 1"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
Related
I've created a text line using some TextViews one behind another, like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:textSize="14sp"
android:textColor="#9b9b9b" >
<TextView
android:id="#+id/messageStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="3sp"
android:textColor="#color/$selectAll" />
<TextView
android:id="#+id/messageTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="3sp"
android:textColor="#color/$addRed" />
<TextView
android:id="#+id/messageMiddle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="3sp"
android:textColor="#color/$selectAll" />
<TextView
android:id="#+id/messageEnd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/$selectAll" />
</LinearLayout>
I programatically set the different texts, all was working nicely. But, when I added styling attributes, sometimes, the length of the TextView is set to nothing even if it contains some text.
I don't call any setLayoutParams() in my code, so I guess the wrap_content mechanics are the key of this, but I can't figure out what's happening…
Thanks for your help! :)
Somebody can please explain this?
Why the third TextView is weird?
The definitions for the 2 lines before are the same, and I haven't touched the style with java...
Edit: I've added the XML Layout file.
The XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top" >
<TextView
android:id="#+id/quadEqu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="#string/quadequ"
android:textSize="27sp" />
<Button
android:id="#+id/closeBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="30dp"
android:text="#string/close"
android:onClick="close" />
<TextView
android:id="#+id/stepOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/quadEqu"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="#string/quadEquForm"
android:textSize="18sp" />
<TextView
android:id="#+id/stepTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/stepOne"
android:layout_below="#+id/stepOne"
android:text="#string/quadEquForm"
android:textSize="18sp" />
<TextView
android:id="#+id/stepThree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/stepOne"
android:layout_below="#+id/stepTwo"
android:text="#string/quadEquForm"
android:textSize="18sp" />
</RelativeLayout>
It's a strange behaviour.
Try to set the content of the 'stepTwo' to the 'stepThree'.
If the same issue appeared, so the problem from the TextView.
You should check if you modify the padding of 'stepThree' textView programmatically.
In android Text View, when you try to render the subscript, the text gets clipped at the top and bottom. To avoid this, you can try to set the text from HTML.
Example.
stepThree.setText(Html.fromHtml("Some text<sup><small>1</small></sup>"));
stepThree.setText(Html.fromHtml(
"HCO<sub><small><small>3</small></small></sub>));
Refer this link for more details.
Android TextView's subscript being clipped off
Subscript and Superscript a String in Android
You need to change android:layout_height for the TextView (for all 3 is better).
<TextView
android:id="#+id/stepOne"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_below="#+id/quadEqu"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="#string/quadEquForm"
android:textSize="18sp" />
<TextView
android:id="#+id/stepTwo"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignLeft="#+id/stepOne"
android:layout_below="#+id/stepOne"
android:text="#string/quadEquForm"
android:textSize="18sp" />
<TextView
android:id="#+id/stepThree"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignLeft="#+id/stepOne"
android:layout_below="#+id/stepTwo"
android:text="#string/quadEquForm"
android:textSize="18sp" />
I'm trying to develop an android application, but I'm having some problems with the GUI design. The following part of screenshot is my problem (red and blue lines were generated by Android debug options).
This is the code:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/caption"
android:layout_below="#+id/caption" >
<ImageButton
android:id="#+id/button_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:src="#drawable/content_edit"
style="?android:attr/borderlessButtonStyle" />
<TextView
android:id="#+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="#string/num_placeholder"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
As you can see, the TextView myText overlaps the ImageButton button_edit.
A simple solution would be to use a LinearLayout instead of a RelativeLayout. The problem is that:
I need the edit button on the right
I need the text to fill the rest of the layout (at the left of edit button obviously)
I also tried to set myText's layout_width to 'fill_parent', but it fill the entire rest of the screen at the left of the edit button.
The expected behavior would be myText to increase its height (becoming a two-lines TextView) instead of overlapping 'button_edit'
Can anyone help me? Thanks
use layout_toLeftOf in TextView layout
like this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/caption"
android:layout_below="#+id/caption" >
<ImageButton
android:id="#+id/button_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:src="#drawable/content_edit"
style="?android:attr/borderlessButtonStyle" />
<TextView
android:id="#+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="#string/num_placeholder"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_toLeftOf="#+id/button_edit" />
Another way of doing is to use android:layout_toEndOf attribute
<ImageButton
android:id="#+id/button_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:src="#drawable/content_edit"
style="?android:attr/borderlessButtonStyle"
android:layout_toEndOf="#+id/myText" />
<TextView
android:id="#+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="#string/num_placeholder"
android:textAppearance="?android:attr/textAppearanceLarge" />
So My text View has to be drawn over the image view, so its defined in xml like this:
<ImageView
android:id="#+id/chatBalloon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="-5dp"
android:layout_marginRight="-5dp"
android:layout_marginTop="2dp"
android:layout_toRightOf="#+id/chatItemProfPic"
android:scaleType="fitXY"
android:src="#drawable/chat_bar_user" />
<TextView
android:id="#+id/userText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="7dp"
android:layout_marginTop="3dp"
android:layout_toRightOf="#+id/chatItemProfPic"
android:text="username"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="15sp" />
but i because the textView can contain multiline text, i need to have the imageView increase its height accordingly. It would be accomplished by adding this rule:
android:layout_alignBottom="idOfText"
but because the textView hasnt been defined at that part, the app crashes. I get the same when trying to do it from code by addRule in the LayoutParams because i call it in onCreate, before the view has been drawn.
Any ideas how to bypass this?
SOLVED:
Final xml:
<ImageView
android:id="#+id/chatBalloon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="-5dp"
android:layout_marginRight="-5dp"
android:layout_marginTop="2dp"
android:layout_toRightOf="#+id/chatItemProfPic"
android:scaleType="fitXY"
android:layout_alignBottom="#+id/userText"
android:src="#drawable/chat_bar_user" />
<TextView
android:id="#id/userText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="7dp"
android:layout_marginTop="3dp"
android:layout_toRightOf="#+id/chatItemProfPic"
android:text="username"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="15sp" />
You use
android:layout_alignBottom="#+id/idOfText"
when you first make a reference to a particular id. Note the "+" which is adding the id to the list of resource identifiers.
Then, you can assign an existing id to a widget without using the "+", as follows:
android:id="#id/idOfText"
after. It's typical to create the id when we're assigning it, which is why you only really need to care about the presence of the "+" in relative layouts.
In your specific case:
<ImageView
android:id="#+id/chatBalloon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="-5dp"
android:layout_marginRight="-5dp"
android:layout_marginTop="2dp"
android:layout_toRightOf="#id/chatItemProfPic"
android:layout_alignBottom="#+id/userText"
android:scaleType="fitXY"
android:src="#drawable/chat_bar_user" />
<TextView
android:id="#id/userText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="7dp"
android:layout_marginTop="3dp"
android:layout_toRightOf="#id/chatItemProfPic"
android:text="username"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="15sp" />
You should have set the ID of the profile pic widget using: android:id = "#+id/chatItemProfPic" assuming that you are declaring the widget before any references to it. Otherwise, similarly, use "+" for the first reference, and then assign the ID when you declare the widget without the "+".
why dont put that image like textview background.?
or something like:
<RelativeLayout
android:id="#+id/Rlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="35dp" >
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/img" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="lorem ipsum"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="#dimen/dimension" />
</RelativeLayout>
in relative layout you could easily put text right over imageview
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)).