Align Textview drawable and text to baseline bottom? [duplicate] - android

This question already has answers here:
How to add drawable image inside textview
(2 answers)
Closed 4 years ago.
I have a textView with an image and text, I want to align text with the baseline of the image. I am trying but unable to do this. How can I do this? Thanks in advance.
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:drawablePadding="4dp"
android:text="Hello"
android:drawableStart="#drawable/date"
android:gravity="bottom"
android:textColor="#color/black"
android:textSize="10sp" />
I want to text and image like this, they share the same baseline:

Try Below:
Use android:drawableTop to set Image at top.
<TextView
android:id="#+id/news_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="3"
android:drawableTop="#drawable/progress_image"
android:textColor="#color/black"
android:text="Below Text"
android:textSize="10sp" />

Related

long text doesn't fit in a textview [duplicate]

This question already has answers here:
How can I show ellipses on my TextView if it is greater than the 1 line?
(7 answers)
Closed 1 year ago.
i have a simple question but i didn't how to solve i tried some stuff but didn't helpso i wanna know if i display a long text into a textview and it doesn't fit they just cut the last word i want to put "..." in the end if the text if it is too big for the textview.
this is the textview code
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="60dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="65dp"
android:layout_weight="0.5"
android:autoSizeTextType="uniform"
android:clickable="true"
android:gravity="left"
android:text="TextView"
android:textColor="#000"
android:textSize="13sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
enter image description here
You can add below code in your TextView
android:ellipsize="end"
android:maxLines="1"

How to create material design button with image and text vertically aligned? [duplicate]

This question already has answers here:
Android Material Button with Icon on top of Text
(2 answers)
Closed 2 years ago.
Using Material design, I need to create a button with text and Image aligned vertically.
<com.google.android.material.button.MaterialButton
android:id="#+id/btn"
style="#style/Widget.MaterialComponents.Button.UnelevatedButton"
android:layout_width="160dp"
android:layout_height="160dp"
android:clickable="true"
android:elevation="4dp"
android:fontFamily="#font/roboto_bold"
android:onClick="myfunction()"
android:text="BUTTON"
android:textColor="#android:color/black"
android:textSize="20dp" />
Use android:drawableBottom="..." or android:drawableTop="..." to align text and an image vertically.
dependencies {
implementation "com.google.android.material:material:1.2.0-alpha01"
}
<com.google.android.material.button.MaterialButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawableTop="#drawable/ic_android_black_24dp"
android:drawableTint="#color/white"
app:backgroundTint="#429835"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp"
android:text="Hello world" />

How to set ImageView in edittext [duplicate]

This question already has answers here:
Inserting imageview inside edittext android
(4 answers)
How can I add an image on EditText
(10 answers)
Closed 4 years ago.
I am using the EditText with drawableLeft property and set ImageView in Left side for displaying mobile icon. My Question is how to put Imageview in EditText?
Please help me.
Actually i want like this.
I have tried using linear layout with horizontal. below is xml code.
<LinearLayout
android:weightSum="2"
android:layout_below="#id/welcome"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_weight="1"
android:layout_gravity="center"
android:id="#+id/image_mobile"
android:layout_width="#dimen/_50sdp"
android:layout_height="#dimen/_50sdp"
android:src="#mipmap/ic_launcher" />
<EditText
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_30sdp"
android:layout_toRightOf="#+id/image_mobile"
android:hint="Mobile number"
android:inputType="text" />
</LinearLayout>
But am not able to adjust the image view and edittext.
You should use android:drawableStart
The drawable to be drawn to the start of the text.
DEMO
<EditText
......
android:drawableStart="#drawable/your_icon"
android:drawablePadding="5dp"
/>

how to align the drawable to left of text in textview?

I want to display a drawable and text in a textview. When I use below code it is displaying both but drawable is in left most side of textview.
<app.com.myapp.views.TextViewMedium
android:id="#+id/approve_btn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:background="#color/color_5eba6f"
android:gravity="center"
android:drawableLeft="#drawable/approve"
android:text="#string/notification_approve"
android:textColor="#color/color_FFFFFF"
android:textSize="#dimen/dp_14" />
My text is aligned in center. I want to align drawable to left of that text.Gap between drawable and text should be just 6 dp. Is it possible to do? Or do I need to create a layout with imageview and textview to achieve the same?
Use these tags to set the image in your textView.
android:drawableLeft="#drawable/your_image"
android:drawablePadding="6dp"
Hope this helps!
This can be possible way :
<TextView
android:id="#+id/etName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_weight="1"
android:background="#drawable/edittext"
android:drawableLeft="#mipmap/icon_username"
android:drawablePadding="6dp"
android:gravity="fill_horizontal|fill|left"
android:padding="5dp"
android:singleLine="true"
android:textSize="15dp"
android:windowSoftInputMode="stateAlwaysHidden" />
Try this
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sample Text"
android:id="#+id/handling"
android:textSize="20dp"
android:drawableLeft="#drawable/emoticons_bear"
android:drawablePadding="6dp"/>
You need to use drawableLeft attribute as below:
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="#+id/userFname"
android:drawableLeft="#drawable/user_name"
android:drawablePadding="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:textSize="16sp"
android:textColor="#android:color/white"
/>
to do that you must use relative layout like this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!--Your text view-->
<TextView
android:id="#+id/myText"/>
<!--Your image view-->
<ImageView
android:layout_toLeftOf="#+id/myText"
android:padding_right="6dp"/>
</RlativeLayout>
with that code you ImageView is forced to be at the left of your TextView and by 6dp spacing
Don't forget you must use relativeLayout as a parent layout so you can use toLeftOf
This is possible way to do it
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Text"
android:id="#+id/yourTextView"
android:textSize="12sp"
android:drawableLeft="#drawable/yourDrawable"
android:drawablePadding="6dp"/>
If not satisfies your demands you must use separate ImageView and TextView i think.. Hope to help you!

Double center text issue in TextView and RelativeLayout

I've a problem centering a text in an already centered TextView:
That's the layout:
<RelativeLayout>...
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="#drawable/w0"
android:layout_margin="10dip"
android:background="#color/green1"
/>
<TextView
android:id="#+id/temperature"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignBottom="#id/image"
android:layout_alignTop="#id/image"
android:layout_toRightOf="#id/image"
android:text="15º"
android:textSize="60dip"
style="bold"
android:textColor="#color/blue"
android:gravity="center"
android:layout_marginLeft="7dip"
android:background="#color/green1"
/>
<TextView
android:id="#+id/min_temperature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/temperature"
android:layout_toRightOf="#id/temperature"
android:text="M 21º"
android:textSize="20dip"
style="bold"
android:textColor="#color/blue"
android:layout_marginLeft="5dip"
android:background="#color/orange1"
/>
<TextView
android:id="#+id/max_temperature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/temperature"
android:layout_toRightOf="#id/temperature"
android:text="m 10º"
android:textSize="20dip"
style="bold"
android:textColor="#color/blue"
android:layout_marginLeft="5dip"
android:background="#color/orange2"
/>
And that's the displayed layout image:
What I don't understand is why the 15º isn't centered in the TextView.
The problem is due the TextView's forced center using alignTop and alignBottom but I can't see why it doesn't works.
I know I can solve this using some nesting layouts but I prefer to get a solution using only the RelativeLayout or if it isn't possible understand the reason.
Also is there a solution to align the maximum and minimum TextViews with the top and bottom of the 15º text (not the TextView) as there is now.
Also is there a solution to align the maximum and minimum TextViews with the top and bottom of the 15º text (not the TextView) as there is now.
I don't think so. You can only make things relative to the actual box that the View represents. And in the case of TextView the box is larger than the Text. Any relations that you do make are going to use the position of the Box no matter where the text is at or what it looks like.
Try using
android:layout_centerVertical="true"
on your temperature TextView in addition to the gravity you have now.

Categories

Resources