How to Use a Compound Drawable Instead of a Linearlayout That Contains an Imageview and a Textview

How do I use a compound drawable instead of a LinearLayout that contains an ImageView and a TextView

TextView comes with 4 compound drawables, one for each of left, top, right and bottom.

In your case, you do not need the LinearLayout and ImageView at all. Just add android:drawableLeft="@drawable/up_count_big" to your TextView.

See TextView#setCompoundDrawablesWithIntrinsicBounds for more info.

Node can be replaced by a TextView with compound drawables

The warning says "can be replaced by" not "should be replaced by". So you can ignore it. Not every ImageView and TextView combo can be replaced by CompoundDrawables. Even then let me if you want to know, how to do this, I have described it below.

TextView tag comes with 4 compound drawables, one for each of left, top, right and bottom.

Just add below line to your TextView.

android:drawableLeft="@drawable/up_count_big"

Or in java code, you can do it like this:-

mTextView.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);

where left ,top ,right and bottom are all drawables.

See TextView#setCompoundDrawablesWithIntrinsicBounds for more info.

Android : Drawable next to TextView with weight to each TextView in LinearLayout

Created a custom button which doesnt require layout nestings and can align drawable images as required.

Layout file button.xml:

<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_button"
style="@style/custom_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawablePadding="5dp"
android:gravity="center" />

Custom Button Class:

public class DrawableAlignedButton extends RelativeLayout {

private View view;
private Button button;

/**
* @param context
* used to inflate the View.
* @param attrs
* XML defined attributes.
*/
public DrawableAlignedButton(final Context context, final AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}

/**
* @param context
* used to inflate the View.
*/
public DrawableAlignedButton(final Context context) {
super(context);
init(context, null);
}

/**
* @param context
* used to inflate the View.
* @param attrs
* XML defined attributes.
* @param style
* the style for the View.
*/
public DrawableAlignedButton(final Context context, final AttributeSet attrs, final int style) {
super(context, attrs, style);
init(context, attrs);
}

private void init(final Context context, final AttributeSet attributeSet) {
view = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.button, this, true);
button = (Button) view.findViewById(R.id.custom_button);

String buttonText = null;
int drawableStart = 0;
int drawableEnd = 0;

if (attributeSet != null) {
final TypedArray a = context.getTheme().obtainStyledAttributes(attributeSet, R.styleable.CustomButtonStyle, 0, 0);
buttonText = a.getString(R.styleable.CustomButtonStyle_buttonText);
drawableStart = a.getResourceId(R.styleable.CustomButtonStyle_buttonDrawableStart, 0);
drawableEnd = a.getResourceId(R.styleable.CustomButtonStyle_buttonDrawableEnd, 0);
a.recycle();
}

FontUtil.getInstance(context).useNormalRegularFont(button);

if (buttonText != null) {
button.setText(buttonText);
}

button.setCompoundDrawablesRelativeWithIntrinsicBounds(drawableStart, 0, drawableEnd, 0);
}

/**
* Sets the button text.
*
* @param text
* the text to be set.
*/
public void setButtonText(final String text) {
if (button != null) {
button.setText(text);
}
}

/**
* Sets the drawable to the button.
*
* @param drawableStart
* the drawable to set at the beginning of the text.
* @param drawableEnd
* the drawable to set at the end of the text.
*/
public void setDrawableStart(final int drawableStart, final int drawableEnd) {
if (button != null) {
button.setCompoundDrawablesRelativeWithIntrinsicBounds(drawableStart, 0, drawableEnd, 0);
}
}

}

How to use it in the XML :

<com.package.view.DrawableAlignedButton
xmlns:drawableAlignedButton="http://schemas.android.com/apk/res-auto"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/transparent_button_selector"
drawableAlignedButton:buttonDrawableStart="@drawable/small_active"
drawableAlignedButton:buttonText="Button Text" />

Android compound drawable

You can use a single TextView and a CompoundDrawable like this

Toast toast = Toast.makeText(this, "Custom Compount Drawable Toast", Toast.LENGTH_LONG);
View toastView = toast.getView(); //This'll return the default View of the Toast.

TextView toastMessage = (TextView) toastView.findViewById(android.R.id.textview);
toastMessage.setTextSize(25);
toastMessage.setCompoundDrawablesWithIntrinsicBounds(R.drawable.image_id, 0, 0, 0);
toastMessage.setGravity(Gravity.CENTER);
toastMessage.setCompoundDrawablePadding(16);
toastView.setBackgroundColor(Color.CYAN);
toast.show();

android layout: This tag and its children can be replaced by one TextView/ and a compound drawable

To expand on Romain Guy's answer, here is an example.

Before:

<LinearLayout 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:padding="5dp" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="My Compound Button" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/my_drawable" />
</LinearLayout>

After:

<TextView  
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="My Compound Button"
android:drawableRight="@drawable/my_drawable" android:padding="5dp" />

ImageView next to TextView in a LinearLayout

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<TextView
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="0.75" />
<ImageView
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="0.25"

/>
</LinearLayout>

How can I set Imageview and Textview on same line in LinearLayout

This You can use to put image in one line.. and it will work in all kind of layouts. Hope this will help you

  <LinerLayout>     
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
....... // Put how many TextViews you want

<TextView
android:id="@+id/textn"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinerLayout>

You can set visibility gone initially if you wanna show these textviews only after setting text.
Now in your activity make an array of ids of textviews like this...

public static int[] textRows = {R.id.text1, R.id.text2, ........ R.id.textn};

then use for loop for initializing them and setting text and images like this

   TextView[] allTexts = new TextView[n];
for (int i = 0; i < n; i++) {
allTexts[i] = (TextView) findViewById(textRows[i]);
allTexts[i].setText("your text");
allTexts[i].setCompoundDrawables(left_image, null, null, null);
}

It will work. Try it out



Related Topics



Leave a reply



Submit