Reputation: 1759
I just wanted to set icon to the top and left corner of my TextView
.
This is my code and the output respectively:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Content"
android:drawableLeft="@drawable/icon" />
Output:
but I want to set my icon to the top and left like this one :
Upvotes: 3
Views: 5681
Reputation: 635
If you want to resolve it using compound drawable, you should create a custom textview and use it.
import android.content.Context
import android.graphics.Canvas
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
class TopAlignCompoundDrawableTextView: AppCompatTextView {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(
context,
attrs,
defStyle
)
override fun dispatchDraw(canvas: Canvas?) {
super.dispatchDraw(canvas)
val centerTextView = height / 2
val drawableLeft = compoundDrawables[0]
drawableLeft?.let {
val topDrawable = it.intrinsicHeight/2 - centerTextView
it.setBounds(
0,
topDrawable,
it.intrinsicWidth,
it.intrinsicHeight + topDrawable
)
}
val drawableRight = compoundDrawables[2]
drawableRight?.let {
val topDrawable = it.intrinsicHeight/2 - centerTextView
it.setBounds(
0,
topDrawable,
it.intrinsicWidth,
it.intrinsicHeight + topDrawable
)
}
}
}
Upvotes: 0
Reputation: 3645
None of the solutions from SO worked form me. In the end I did it like this:
class MyTextView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null
) : AppCompatTextView(context, style) {
private val leftDrawable = ContextCompat.getDrawable(context, R.drawable.checkmark)
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
setBulletPoint(compoundDrawables[0], canvas)
}
private fun setBulletPoint(drawableLeft: Drawable?, canvas: Canvas?) {
if (!TextUtils.isEmpty(text)) {
leftDrawable?.let { drlft ->
if (lineCount == 1) {
setCompoundDrawablesWithIntrinsicBounds(drlft, null, null, null)
} else {
val buttonWidth = drlft.intrinsicWidth
val buttonHeight = drlft.intrinsicHeight
val topSpace = abs(buttonHeight - lineHeight) / 2
drlft.setBounds(0, topSpace, buttonWidth, topSpace + buttonHeight)
canvas?.apply {
save()
drlft.draw(canvas)
restore()
}
}
}
}
}
}
Upvotes: 0
Reputation: 69744
try 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/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hello world"
android:layout_toEndOf="@+id/imageView" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_error" />
</RelativeLayout>
Upvotes: 0
Reputation: 94
This code will help you and see the attached image.
<?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:orientation="vertical">
<LinearLayout
android:id="@+id/llMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.10"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.90"
android:text="It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using" />
</LinearLayout>
</LinearLayout>
https://i.sstatic.net/hLUGT.png
Upvotes: 0
Reputation: 549
For this purpose you have to take the seperate imageView like this and in textview field you have to add one line code that android:layout_toRightOf="@+id/imageView": For better understanding see following code:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:background="@mipmap/ic_launcher"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/imageView"
android:text="this si fodfjkhsdhaffjsdfasdfjhsdfhjsdhfjhsdfhsdhfhdsjfhsdjhfjhdsfhsdhfjhsdjhfjsdhfjhsdjfhjsdhfjhsdjfhjdshfsdjhfjsdhfsdkjhfjsdhfjhsdjfhjsdhjfhsdjhfjsdhfjhjsdhfjsdhjfhsdjf"
/>
</RelativeLayout>
Upvotes: 5
Reputation: 782
You can use setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom)
(For better Understand refer this link:)
https://developer.android.com/reference/android/widget/TextView.html#attr_android:drawableTop
Upvotes: 0
Reputation: 6899
You can align a compound-Drawable to the top (or bottom) by creating a custom Drawable that wraps your Drawable.
Usage
GravityCompoundDrawable gravityDrawable = new GravityCompoundDrawable(innerDrawable);
// NOTE: next 2 lines are important!
innerDrawable.setBounds(0, 0, innerDrawable.getIntrinsicWidth(), innerDrawable.getIntrinsicHeight());
gravityDrawable.setBounds(0, 0, innerDrawable.getIntrinsicWidth(), innerDrawable.getIntrinsicHeight());
textView.setCompoundDrawables(gravityDrawable, null, null, null);
Custom GravityCompoundDrawable class:
public class GravityCompoundDrawable extends Drawable {
// inner Drawable
private final Drawable mDrawable;
public GravityCompoundDrawable(Drawable drawable) {
mDrawable = drawable;
}
@Override
public int getIntrinsicWidth() {
return mDrawable.getIntrinsicWidth();
}
@Override
public int getIntrinsicHeight() {
return mDrawable.getIntrinsicHeight();
}
@Override
public void draw(Canvas canvas) {
int halfCanvas= canvas.getHeight() / 2;
int halfDrawable = mDrawable.getIntrinsicHeight() / 2;
// align to top
canvas.save();
canvas.translate(0, -halfCanvas + halfDrawable);
mDrawable.draw(canvas);
canvas.restore();
}
}
Upvotes: 0
Reputation: 3422
Use below code;
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Content"
android:drawableTop="@drawable/def"
android:drawableLeft="@drawable/icon" />
Upvotes: 0