How to set height and width of compound drawable textview in xml? [closed]
Asked Answered
S

2

19

I can't see any tag to set width

Here I set textview height, if I set width it will not work properly.

<TextView
   android:id="@+id/nome"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textColor="#FFFFFF"
   android:drawableLeft="@drawable/ic_launcher"
   android:height="50dp" />

How can I set this in xml? Is it possible?

Saiff answered 8/8, 2012 at 23:9 Comment(6)
What exactly is your question? What is not working? What errors are you getting? ectFrederick
I want set the image size to 50dp/50dpSaiff
you have two viable answers. hence -1Deforest
You can achieve this by creating the right size images for each dimension. For mdpi you should use 50px by 50px, for hdpi you should use 75px by 75px, or xhdpi you should use 100px by 100px. The TextView will use the intrinsic size of these images.Kimbrakimbrell
This question should not be closed , the question is VERY clear and one of the top search results on the topic. Stack really needs some better adminsOrfinger
@Orfinger It is possible to do it with custom view. It is additional code, but only once. See my comment - https://mcmap.net/q/120656/-compounddrawable-sizeSchaumberger
F
2

You can try something like:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


<TextView
    android:id="@+id/Home"
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:layout_centerHorizontal="true"
    android:gravity="center"
    android:text="Something"
    android:textColor="#FFFFFF" />

<ImageView 
    android:src="@drawable/icon" 
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_toLeftOf="@id/Home"/>

</RelativeLayout>

This way I think you would have more control of your image. Hope that helps.

Frederick answered 8/8, 2012 at 23:31 Comment(3)
YES, i was doing like this, but eclipse said not to do this. thank you I will forget eclipse warningsSaiff
That is weird. Yea no problem.Frederick
Well the whole point of compound drawables is to avoid using an entire layout like this.Timotheus
D
-2

try setting to android:width="50dip" and see what you get. dip is device independent pixels. You should be able to see in eclipse in design view. You cannot just set to "300"

Deforest answered 8/8, 2012 at 23:23 Comment(9)
If i set width the text will not appear.. because it set the width of the textviewSaiff
I see so you want it to wrap?Deforest
Actually what you need to do is just create an image button and place it to the left.Deforest
So just create another LinearLayout set to horizontal orientation and place the ImageView to the left.Deforest
Or actually I think you might be able to pass the ImageView as the drawable left. Note sure about that however.Deforest
I was using an imageview and a textview, and it works perfectly. But eclipse said to use the drawable in the textview.Saiff
Here is a link about how to use it. izvornikod.com/Blog/tabid/82/EntryId/8/…Deforest
I thought it will work better, maybe android team forgot to introduce the tags for the drawableSaiff
Never mind eclipse its loco. Just get rid of the drawableLeft.Deforest

© 2022 - 2024 — McMap. All rights reserved.