Struggling to get the text centered - android

Can someone help me get this text centered please,
I have tried changing the layout width, height and padding but only managed to make it worse,
<com.github.mmin18.widget.FlexLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/login_bg"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/logo_main"
app:layout_centerX="50%"
app:layout_centerY="30%" />
<EditText
android:textSize="18.0sp"
android:textColorHint="#ff4d4d4d"
android:gravity="center"
android:id="#id/edit_code"
android:focusable="true"
android:visibility="gone"
android:layout_width="341.32812dip"
android:layout_height="46.0dip"
android:hint="#string/active_code"
android:layout_centerHorizontal="true"
android:inputType="text"
app:layout_centerX="50%"
app:layout_top="logo.bottom + parent.height/20" />
<Button
android:textSize="18.0sp"
android:id="#id/btn_reset"
android:background="#drawable/btn_bg"
android:focusable="true"
android:visibility="gone"
android:layout_width="160.0dip"
android:layout_height="36.0dip"
android:text="#string/reset"
android:textAllCaps="false"
app:layout_left="edit_code.left + 2dp"
app:layout_top="edit_code.bottom + 8dp" />
<Button
android:textSize="18.0sp"
android:id="#id/btn_login"
android:background="#drawable/btn_bg"
android:focusable="true"
android:visibility="gone"
android:layout_width="160.0dip"
android:layout_height="36.0dip"
android:text="#string/login"
android:textAllCaps="false"
app:layout_right="edit_code.right"
app:layout_top="edit_code.bottom + 8dp" />
<ProgressBar
android:id="#id/pb"
android:visibility="gone"
android:layout_width="22.0dip"
android:layout_height="22.0dip"
android:indeterminateDrawable="#drawable/login_progress"
android:layout_alignEnd="#id/btn_login"
app:layout_centerY="btn_login.centerY"
app:layout_right="btn_login.right-0.0556*screen.height" />
<TextView
android:textSize="22.0sp"
android:textColor="#color/white"
android:id="#id/change_login"
android:focusable="true"
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please click here to login"
android:paddingStart="3.0dip"
android:paddingEnd="3.0dip"
app:layout_right="btn_login.right"
app:layout_top="btn_login.bottom + 8dp" />
<android.support.v7.widget.AppCompatCheckBox
android:theme="#style/MyCheckBox" android:gravity="center"
android:id="#id/statement_check"
android:layout_width="#dimen/_36dp"
android:layout_height="#dimen/_36dp"
app:layout_height="btn_reset.height"
app:layout_left="btn_reset.left"
app:layout_top="change_login.bottom + 8dp"
app:layout_width="btn_reset.height" />
<TextView
android:textSize="#dimen/text_12"
android:textColor="#color/white"
android:id="#id/statement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/state_all"
app:layout_left="statement_check.right + 8dp"
app:layout_right="btn_login.right"
app:layout_top="statement_check.top" />
</com.github.mmin18.widget.FlexLayout>
I thought removing the padding would help or changing it to fill content would help but the text just disappeared

Maybe you got a better answer if you post the XML code of your application.
Usually, to get the text of an item centered you just need to use the "gravity" property.
In your XML file, search for the component with the text you want to center and add the following to it's properties:
android:gravity="center"

There are two properties of TextViews that might help you.
First is the gravity attribute. This attribute would center your text within the boundary of your TextView. Therefore, no matter how large or how small your actual TextView is, the text would always be in the center of it. Here's an implementation:
android:gravity="center"
There's also the layout_gravity attribute, this attribute positions your TextView relative to the nearest parent view. It is important to note that this attribute does not center the text, rather, it centers the whole TextView. Here's an implementation:
android:layout_gravity="center"
Refer to the documentation to learn more.
I hope this helps. Merry coding!

Related

layout_alignBottom aligns center instead of bottom

I'm trying to get the bottom of my TextView and EditText to line up (perhaps not to the pixel, but close) inside of a RelativeLayout. But layout_alignBottom seems to line up their center line instead of their bottom:
I tried layout_alignBaseline also, but the result was that the EditText does not display on the screen. I also made various efforts at adjusting padding, but it didn't change anything. This seems like it should be easy and a common issue, but I couldn't find anything Googling. Any help would be greatly appreciated.
Thanks.
<TextView
android:id="#+id/firstNameLabel"
android:layout_marginTop="20dp"
android:layout_marginRight="24dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Name" />
<EditText
android:layout_toRightOf="#+id/firstNameLabel"
android:layout_alignBottom="#+id/firstNameLabel"
android:id="#+id/firstName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
Try this.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/firstNameLabel"
android:layout_marginRight="24dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="First Name"
android:textSize="20sp"/>
<EditText
android:layout_toRightOf="#+id/firstNameLabel"
android:layout_alignParentBottom="true"
android:id="#+id/firstName"
android:hint="YO BABY"
android:layout_centerVertical="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
Hope this helps.
Remove margin top from code and check
What's causing this is the EditText being bigger than the textview. Check your layout that contains both and set the gravity to "center" or "center_vertical".
Edit: If you're using RelativeLayout, you should probably use "layout_centerVertical" or "layout_centerInParent" instead

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!

Android: Need Ellipsis for Multiple TextViews in Relative Layout

I am new to Android, i have below task.
I want to align three TextViews adjacent to each other with ellipsis if the text is too long . Currently i am using below code which aligns my TextView adjacent to each other, but my problem is if the text is too long, the below code not putting the ellipsis. i got some trick to use both toleftof and torightof from here, but in my case it going to circular dependency as mentioned here.
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="#+id/reminderText">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/noteTagOne"
android:ellipsize="end"
android:singleLine="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/noteTagTwo"
android:ellipsize="end"
android:singleLine="true"
android:layout_toRightOf="#+id/noteTagOne"
android:layout_marginLeft="5sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/noteTagThree"
android:layout_toRightOf="#+id/noteTagTwo"
android:ellipsize="end"
android:singleLine="true"
android:layout_marginLeft="5sp"/>
</RelativeLayout>
I want output as,
Tag1... Tag2... Tag3...
Basically, i want some ideas, whether in xml i can achieve this or need to do it programmatically, since TextView text's are dynamic i cannot fix maxLength, sometimes only single tag may exist, at that time it should take full text.
Note: I am developing Android project using C# in Xamarin.
Added code is for the above highlighted part. I fixed overlapping of tagsicon & text. Now only two issues.
1) Last tag is overlapping with right aligned image.
2) If TextView texts are short, gap is shown in-between the tags
How to fix it?
try this solution
<LinearLayout 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"
android:orientation="horizontal"
tools:context=".MainActivity" >
<TextView
android:id="#+id/noteTagOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxWidth="100dp"
android:singleLine="true"
android:text="Tag111111111111111111"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/noteTagTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5sp"
android:layout_toRightOf="#+id/noteTagOne"
android:ellipsize="end"
android:maxWidth="100dp"
android:singleLine="true"
android:text="Tag2222222222222222"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/noteTagThree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5sp"
android:layout_toRightOf="#+id/noteTagTwo"
android:ellipsize="end"
android:maxWidth="100dp"
android:singleLine="true"
android:text="Tag3333333333333333"
android:textAppearance="?android:attr/textAppearanceLarge" />
Use this android:ellipsize="marquee" it will work
android:ellipsize="marquee"
or
android:ellipsize="end"
android:maxLines="1"
android:scrollHorizontally="true"
android:singleLine="true"

Image + Text to be centre alligned in button

I have a Button with its width as match_parent. And I want a image/icon with a text in its center. I have triedandroid:drawableStart attribute but its of no help. I don't know what I am missing. Is there any one who is also facing the same problem.
Note:This link is not solving my problem. I am attaching my button portion of xml
<Button
android:background="#drawable/blue_button"
android:id="#+id/btn_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:drawableStart="#drawable/ic_list"
android:text="#string/btn_schedule"
android:textColor="#android:color/white" />
After adding these attribute my text is coming on center but image/icon is still on left side of button. Can any body help!!!
You might want to check my answer to this question:
Android Button with text and image
Its an ugly solution but it you want the image and text to be centered as a group in the button rather than text center and drawable padded from the side then its the only way I have found.
100% working
<FrameLayout
style="?android:attr/buttonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
style="?android:attr/buttonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#null"
android:clickable="false"
android:drawableLeft="#android:drawable/ic_delete"
android:focusable="false"
android:gravity="center"
android:minHeight="0dp"
android:minWidth="0dp"
android:text="Button Challenge" />
from here
This works for me. Basically play with the paddingLeft and PaddingRight of the button to get the text and image close together to the center.
Note: Might be an issue with rather long texts.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="search button"
android:drawableStart="#drawable/ic_action_search"
android:paddingLeft="60dp"
android:paddingRight="60dp"/>
</LinearLayout>
I had the same problem and I hadn't found anywhere clear solution. BUT I got some information which also help me to provide following solutions (I have chosen the second one):
Create image which will contain your image and text
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableTop="#drawable/YOUR_IMAGE"
android:gravity="center_horizontal|center_vertical" />
It is not so clear and you have to add more complicated changes to your layout but it is easier to modify the text later
<FrameLayout
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:layout_height="50dp"
android:layout_width="match_parent" />
<TextView
android:text="sample"
android:layout_height="50dp"
android:layout_width="wrap_content"
android:drawableLeft="#drawable/YOUR_IMAGE"
android:gravity="center_horizontal|center_vertical" />
</FrameLayout>
I tried android:drawableTop
The image shown in the center of the button

Relative layout - Right justify the data in textview

I am using the following xml to generate a row in a list view. Though i have set the gravity as center_Vertical and right. Only center_Vertical is applied but not the "right" gravity. I have even tried inside the code. the "right" gravity is not working. Kindly help me to correct the code.
Thanks in Advance
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tvTicketID"
android:layout_width="wrap_content"
android:layout_height="52dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:gravity="center_vertical|left"
android:text="Ticket ID"
android:textSize="15dp" />
<TextView
android:id="#+id/tvCreationHour"
android:gravity="center_vertical|right"
android:layout_width="wrap_content"
android:layout_height="52dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="40dp"
android:layout_toRightOf="#+id/tvTicketID"
android:text="Hour" />
<TextView
android:id="#+id/tvTableNumber"
android:gravity="center_vertical|right"
android:layout_width="wrap_content"
android:layout_height="52dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="24dp"
android:layout_toRightOf="#+id/tvCreationHour"
android:text="Table #" />
<TextView
android:id="#+id/tvAmount"
android:gravity="center_vertical|right"
android:layout_width="wrap_content"
android:layout_height="52dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_toRightOf="#+id/tvTableNumber"
android:text="Amount" />
</RelativeLayout>
The code I use to create a row using the above xml
holder.tvTicketID.setText(String.valueOf(ticket.getId())) ;
holder.tvCreationHour.setText(String.valueOf(ticket.getCreationHour())) ;
holder.tvTableNumber.setText(String.valueOf(ticket.getTableNumber())) ;
holder.tvAmount.setText(String.valueOf(ticket.getTotalAmount())) ;
Try setting android:layout_gravity="right"
In general, android:gravity="right" is different from android:layout_gravity="right".
The first one affects the position of the text itself within the View, so if you want it to be right-aligned, then layout_width= should be either "fill_parent" or "match_parent".
The second one affects the View's position inside its parent, in other words - aligning the object itself (edit box or text view) inside the parent view.
Finally, I fonund the answer. I have changed the android:layout_width="wrap_content" in the TEXTVIEW to android:layout_width="30dp". Now it works..

Categories

Resources