How to Align Textview Around an Imageview

Android: How to align an ImageView to the right of a TextView and make sure the ImageView is always visible

The below xml is enough to achieve what you want. Just set singleLine to true and use drawableEnd to set your image. Also, replace the text in my code with yours. That's all.

    <TextView
android:singleLine="true"
android:text=" HehhsasasashasgghgahgshagshgahsghagshaghsgahsghaHehhsasasashasgghgahgshagshgahsghagshaghsgahsgha shgasagsasghag shahsghag"
android:layout_width="wrap_content"
android:drawableEnd="@drawable/myImageDrawable"
android:layout_height="wrap_content"/>

How to align TextView to center of the screen, and ImageView to stick next to it?

Replace your Linear layout with Relative layout as shown below, using torightof attribute we can make sure the image always stays to the right.

Layout_centerHorizontal makes it always align to the center of the screen.

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>

<TextView
android:textColor="@android:color/background_dark"
android:id="@+id/textViewCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:text="logng_name_here"
**android:layout_centerHorizontal="true"**
android:textSize="24sp"
/>

<ImageView
android:id="@+id/imageViewPen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@android:drawable/ic_menu_edit"
**android:layout_toRightOf="@+id/textViewCity"**
/>

</RelativeLayout>

How to align TextView around an ImageView?

You can achieve this by using the android.text.style.LeadingMarginSpan.LeadingMarginSpan2 interface which is available in API 8. Here is the article, not in English though, translate it using your browser. Besides you can download the source code of the example directly from here.

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="wrap_content"
android:padding="5dp">
<TextView
android:textSize="18.0sp"
android:id="@+id/message_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/text" />
<ImageView
android:src="@drawable/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/icon" />
</RelativeLayout>

Helper class implements LeadingMarginSpan.LeadingMarginSpan2

class MyLeadingMarginSpan2 implements LeadingMarginSpan.LeadingMarginSpan2 {
private int margin;
private int lines;

MyLeadingMarginSpan2(int lines, int margin) {
this.margin = margin;
this.lines = lines;
}

/* Возвращает значение, на которе должен быть добавлен отступ */
@Override
public int getLeadingMargin(boolean first) {
if (first) {
/*
* Данный отступ будет применен к количеству строк
* возвращаемых getLeadingMarginLineCount()
*/
return margin;
} else {
// Отступ для всех остальных строк
return 0;
}
}

@Override
public void drawLeadingMargin(Canvas c, Paint p, int x, int dir,
int top, int baseline, int bottom, CharSequence text,
int start, int end, boolean first, Layout layout) {}

/*
* Возвращает количество строк, к которым должен быть
* применен отступ возвращаемый методом getLeadingMargin(true)
* Замечание:
* Отступ применяется только к N строкам первого параграфа.
*/
@Override
public int getLeadingMarginLineCount() {
return lines;
}
};

Your activity code:

 @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

String text = getString(R.string.text);

// Получаем иконку и ее ширину
Drawable dIcon = getResources().getDrawable(R.drawable.icon);
int leftMargin = dIcon.getIntrinsicWidth() + 10;

// Устанавливаем иконку в R.id.icon
ImageView icon = (ImageView) findViewById(R.id.icon);
icon.setBackgroundDrawable(dIcon);

SpannableString ss = new SpannableString(text);
// Выставляем отступ для первых трех строк абазца
ss.setSpan(new MyLeadingMarginSpan2(3, leftMargin), 0, ss.length(), 0);

TextView messageView = (TextView) findViewById(R.id.message_view);
messageView.setText(ss);
}

And finally here a demo result:

Sample Image

How to align (an ImageView and a TextView) to the left and (an ImageView and a TextView) to the right

You probably don't need an ImageView at all.

TextView has two properties for left and right images:

android:drawableLeft

android:drawableRight


For example,

<TextView
android:id="@+id/img_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_marginRight="10dp"
android:text="MUSIC - "
android:drawableRight="@drawable/play" /> <!-- See here -->

Align ImageView to end to text in textView

Add app:layout_constraintStart_toEndOf="@+id/tvMerchantTitle" to your ImageView

Example Code:

<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">

<TextView
android:id="@+id/tvMerchantTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="16dp"
android:drawablePadding="4dp"
android:ellipsize="middle"
android:maxWidth="200dp"
android:singleLine="true"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/ivMerchantLogo"
app:layout_constraintBottom_toBottomOf="@+id/ivMerchantLogo"
android:text="@string/app_name"/>
<ImageView
android:id="@+id/ivMerchantLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toEndOf="@+id/tvMerchantTitle"
app:layout_constraintTop_toTopOf="parent"/>
<androidx.appcompat.widget.AppCompatCheckBox
android:id="@+id/cbMerchantSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:clickable="false"
android:gravity="end"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/ivMerchantLogo"
app:layout_constraintBottom_toBottomOf="@+id/ivMerchantLogo" />

</androidx.constraintlayout.widget.ConstraintLayout>

Output for above code is like:

Sample Image

How can I align my ImageView with my TextView in a LinearLayout?

Try this:

<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/headerLinearLay">
<ImageView
android:id="@+id/avatarImageView"
android:src="@drawable/icon"
android:layout_height="wrap_content"
android:layout_width="wrap_content"></ImageView>
<TextView
android:id="@+id/usernameTextView"
android:text="TextView"
android:paddingLeft="4dp"
android:layout_toRightOf="@id/avatarImageView"
android:layout_centerVertical="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"></TextView>
</RelativeLayout>


Related Topics



Leave a reply



Submit