I am creating an Android app, and I have a button with a custom background image behind it. The background image is just a rectangle, but now the button text does not seem to be centered vertically. Here is my xml for the button:
<Button
android:id="#+id/save_to_list"
android:layout_below="#+id/info_pic"
android:text="SAVE TO LIST"
android:textColor="#android:color/white"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_width="260dp"
android:layout_height="35dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"></Button>
and I set the background here:
ImageView button_bg = (ImageView)findViewById(R.id.button_background);
savetolist_button.setBackgroundDrawable(button_bg.getDrawable());
Is there a where to change the position of the text within a button?
android:layout_gravity defines layout alignment inside it's parent. To align text within the button you need android:gravity, for example
<Button
...
android:gravity="center"
...>
</Button>
Your button is too short to fit the text properly. Increase the layout_height and the text should be centered.
Your text likely has some padding built into the font as part of each letter, and that is why your text is being pushed in one direction. Set the layout_height to "wrap_content" and your text should now be centered correctly.
I found it best to use a 9 patch image for a rectangular background rather than an xml drawable.
Related
I want to display text at the end of EditText as shown in the image above for URLName.How could I acheive this ?The text cannot be edited.I am using this editText inside TextInputLayout from the design library. thanks in advance
Create a LinearLayout. An example of a row inside this layout would be a title (i.e. URL name) or a text entry field.
Set the background of the LinearLayout for the current row to a custom drawable to create a thin red line. Follow the answers on this post for ideas.
For the URL entry field mentioned in your question, create a TextView and an EditText inside another LinearLayout inside the current row.
The TextView will contain ".sitename.com". Set it to wrap_content and give it a weight of 0. For your EditText, set its width to 0dp and its weight to 1. It should now fill all remaining space. Set the background to #null for both.
This worked for me :
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_alignParentLeft="true"
android:id="#+id/youeeditid"
android:layout_gravity="center"
/>
<TextView
android:id="#+id/text_hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="HintText"
android:textSize="18sp"
android:layout_marginRight="10dp"
android:textColor="#808080"
/>
</RelativeLayout>
It's little bit tricky but it's works for me.
BUT
If you want to set only EditText then you can do below :
1) Set Gravity of EditText to "Right" and Ellipsize to "End" .
2) Now In onCreate() method write onclicklistener of EditText and set
Gravity as "Left".
3) Or you can also set programatically on OnKeyListener , where check
if lenght of edittext is equal to Zero set EditText's gravity as
Left.
Reference : Setting cursor to the right on an EditText with HINT Gravity to center
I have a layout/alignment problem with a TextView which I haven't yet been able to find a solution for, that is, I want to align text off-center inside a TextView horizontally.
To give a little more context, I have an ImageView and a TextView side by side, the ImageView touching the left edge of the screen and the TextView filling the rest of the screen horizontally, like this:
[-img-|-----text-----]
The TextView is configured as singleLine="true" and maxLines="1", so that it will be truncated if its too long for its horizontal space. My aim is to align the text in the center of the screen, not the center of the TextView, because there are other elements on the screen that are aligned to the center and I need the text aligment to match that.
So, if I use gravity="center" on the TextView, I get the image above, but what I actually want is
[-img-|--text--------]
I tried putting image and text in a RelativeLayout, so that the TextView actually touches both edges of the screen, which does what I want with respect to the alignment, except that if the text is long enough, the first characters will be hidden by the image, since the TextView lies behind the image view.
I also experimented with margins, padding and a compound drawable to the left, but the text is always centered relative to the available space (which I would consider the expected behavior).
Does anybody have any clues on how to achieve this alignment, i.e. relative to the center of a different component than the TextView, maybe programmatically at runtime? Thanks in advance for any helpful advice.
Edit: user Budius suggested using padding to the right to achieve a centered alignment which works but leads to long texts being truncated before the right edge of the TextView is reached and I'm looking for a solution that avoids that, i.e. that uses the entire avilable space, if possible.
I believe that will center it and the move 20dp to the side.
gravity:centre padding:right=20dp
Try using
android:layout_weight="1"
if it helps you..
I know this is not the correct approach than too give it a try..
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".33"
android:src="#drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight=".33"
android:gravity="center"
android:text="text" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".33"
android:src="#drawable/ic_launcher"
android:visibility="invisible" />
My question is very simple.
How do I center the text on a button in android?
I tried to set padding to 0, gravity to center but the result when i run it still that the text is horizontal centred but not vertical. The text is a bit shifted to the bottom.
<Button
android:id="#+id/btnSame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:background="#drawable/layout_button_different"
android:gravity="center_vertical|center_horizontal"
android:height="30dp"
android:padding="0dip"
android:text="#string/equals"
android:textColor="#drawable/layout_button_different"
android:textSize="50dp"
android:width="70dp" />
My be also relevant:
In activity I do this:
btnEquals.setCompoundDrawablesWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.up2), null, null);
btnEquals.setPadding(0, 5, 0, 0);
btnEquals.setTextSize(15);
This works but after this I set this:
btnEquals.setCompoundDrawables(null, null, null, null);
btnEquals.setPadding(0, 0, 0, 0);
Result is a bad vertical alignment.
It's easy peasy, you can put this:
android:textAlignment="center"
Your existing layout cannot hope to center the text due to the sizes you have selected. You've set the button height to 30dp and your textSize is 50dp, and for whatever reason, Android is unable to deal with that and center the text. If you make your button larger or your text smaller you'll see that the centering works.
There are three systems to consider. The xml layout designer, the simulator, and real phone. The text in the button if you specify gravity="center" will look like this.
In xml layout designer it will look like the leftmost letter is in the center of the button but the rest of the letters are to the right. (looks incorrect)
In simulator, the text shows well in the center. (correct)
In phone, the text shows well in the center. (correct)
You can just add this line to your Button in layout xml code:
android:gravity="center"
You can also use negative numbers in setPadding. This may help if your font is large and your button is small. Try also setting the top or bottom to a more negative number if it's still off center.
You just have to set layout_height to wrap_content and vary the padding attribute for whatever value you want
<Button
android:id="#+id/AnswerE"
style="#style/defaultbuttonstyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:onClick="AnswerCheck"
android:text=" E "
android:textStyle="bold" />
This worked for me
In edittext the text is coming leftmost corner. but i want to display the text after one space . so it should be more readable.
Any one can help me in this problem.which tag in edit text i have to use.
Try providing margins around your EditText Background like this,
<EditText android:id="#+id/tickets_value"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dip"
/>
You can use either the padding or the margin element of the view.
While margin offsets the entire view the padding offsets the content within the view.
http://developer.android.com/reference/android/view/View.html
I have a row of buttons with custom 9 patch images, and variable length text. I would like the buttons to be the same height. When the text is long enough to wrap, it expands the button size, making the button with wrapped text bigger than the others. I'm laying these buttons out in code in linear layouts. I can fix the size of the button, but then it just cuts off the bottom. How can I make the text take up more of the padding space of the button, so that the text butts up against the top line of the button?
9patch content area is just used to set a padding. If you change the button's padding you will override the one set by the 9patch.
edit
try something like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button android:text="ButtonButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingTop="0dp"
android:paddingBottom="10dp"/>
<Button android:layout_width="wrap_content" android:text="Button"
android:layout_height="match_parent"/>
<Button android:layout_width="wrap_content" android:text="Button"
android:layout_height="match_parent"/>
</LinearLayout>
this way the button will be as high as the others. Sadly, I don't know if it depends on the original button's 9patch that might have asymmetrical paddings, but I can't make the text align with that from other buttons (I think gravity is by default set to center). Maybe with your 9patch it works though. (EDIT: oh, but if you'll have two lines of text who cares about alignment)
Ultimately I was unable to find a nice way of doing this without creating a custom view class.