Left align text inside a button in Android - java

I want to align text of a button to the left, I don't know how to do this, please help me how to do this in the xml file. I didnĀ“t find the properties for this.

Maybe this will help you:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left|center_vertical"
android:text="Button" />
</LinearLayout>

You probably want both
android:gravity="left|center_vertical"
and then a little bit of space
android:paddingLeft="5dp"
to keep the text from running up against the left edge of the button.
(Adjust the amount of padding_left as appropriate for your button art.)

android:gravity="left"
Hope This will help

android:gravity="left|center_vertical"
It will work but now you may need to add some padding left according to your requirement.
android:paddingLeft="10dp"

This is combination from above that worked for me, icon on left and text left aligned with some padding
<Button
android:id="#+id/butt_labs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/dt_labjob"
android:paddingLeft="10dip"
android:gravity="left|center_vertical"
android:text="#string/l_labs" />

You probably need BOTH the android:gravity AND the android:layout_gravity to align text to "left".

As everyone else has mentioned, "left|center_vertical" works, but I have found that if left or right is not explicitly stated, most properties default to left.
So putting the following should be enough to get your text left-justified and vertically centered:
android:gravity="center_vertical"

If you have to support many different languages it is better to use start or end to position text. That way for right to left (RTL) languages it will still work.
Example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="start|center_vertical"
android:text="Button" />
</LinearLayout>

Related

Right to left in android studio

I'm using Android Studio for application development and I have a problem with placing elements in the screen.
For example: I'm trying to place a button to the right of the screen and in the studio its shown to the right of the screen but when I install the app in my smartphone i see it in the left of the screen (my smartphone is right to left configured).
How can i resolve it? (I use RelativeLayout if that matters).
Thanks!
illustration image
It would be easier to answer your question if you included some code. However, assuming your button is inside a relative parent layout which fills the screen, adding this to your button's xml code will make it align to the right:
android:layout_alignParentRight="true"
ie.
<Button
android:id="#+id/btn"
android:layout_width="145dp"
android:layout_height="75dp"
android:layout_alignParentRight="true"
android:onClick="btnclick"
android:text="Click!"
android:textSize="30sp" />
Or to make it really easy, replace your entire XML with:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent"
android:weightSum="1">
<Button
android:id="#+id/btn"
android:layout_width="145dp"
android:layout_height="75dp"
android:onClick="btnclick"
android:text="Click!"
android:textSize="30sp"
android:layout_alignParentRight="true"/>
</RelativeLayout>
The code for those who requested:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent"
android:weightSum="1"><![CDATA[
android:splitMotionEvents="true"
tools:context="tomer.newapp.MainActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
]]>
<Button
android:id="#+id/btn"
android:layout_width="145dp"
android:layout_height="75dp"
android:layout_column="2"
android:layout_row="2"
android:onClick="btnclick"
android:text="Click!"
android:textSize="30sp"
tools:layout_editor_absoluteX="223dp"
tools:layout_editor_absoluteY="140dp"
android:layout_marginLeft="11dp"
android:layout_marginStart="11dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
Note that the absolute values that you are using are in the tools namespace - this means they are not compiled into your app.
tools:layout_editor_absoluteX="223dp"
tools:layout_editor_absoluteY="140dp"
That is probably why you are getting this kind of behavior.
Remove them and then see if the button moves to the left of the screen.
More about tools namespace - here
You mentioned "(my smartphone is right to left configured)".
This could be a dud answer, but...
With the layout_editor, for either the "gravity" or "layout_gravity" properties, there is a "right", "left", or "start" and "end". In English, one starts writing at the left, and ends at the right. In Arabic, for example, one starts at the right and ends left. Any chance your property says "end", instead of "right"?

how to differentiate two "views" from overlapping each other

i am beginner to android programming and trying to build a simple app program.
whenever i try to make more than one views they stack on each other.
I am trying to make a text field and a button, but when i run it, the text field and button overlap each other however, i want them to be separated by some distance.
i am using the following code.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="com.example.new1.MainActivity" >
<EditText android:id="#+id/edit_message"
android:layout_weight= "1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send" />
</RelativeLayout>
i am checking this code on Samsung Galaxy S2.
does anybody knows the solution to this problem and can guide me where i am doing it wrong.
Either use LinearLayout so that elements stack horizontally or vertically, or use attributes such as android:layout_toRightOf="#id/edit_message" to control placement within a RelativeLayout.
Find out more about LinearLayout here: http://developer.android.com/guide/topics/ui/layout/linear.html and RelativeLayout here: http://developer.android.com/guide/topics/ui/layout/relative.html
RelativeLayout positions views relative to each other. So if you do not specify the relationship between view, all the views will be put one above the other. You can either use LinearLayout with orientation attribute or define relationship between views. Following can be your working code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<EditText
android:id="#+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:hint="#string/edit_message"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/edit_message"
android:text="#string/button_send" />
</RelativeLayout>
Here android:layout_toRightOf="#id/edit_message" lets your button to be positioned to right of your edittext
Depending on what you want, a horizontal layout or vertical, you need a fitting layout xml.
You use relativeLayout, where you have to specify the parents to layout like:
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
If your beginner better take a look at a linear layout in vertical or horizontal mode.
You dont have to specify this then.
Like this:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/bResults"
android:text="Try Command"
android:layout_weight="20" />
<ToggleButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/tbPassword"
android:layout_weight="80"
android:checked="true"
/>
</LinearLayout>
This will put your two buttons nexto each other

Scaling images in Android not working like supposed to

I have a long vertical image (224x1600). I want it to fit the image width, and scroll vertically. It needs to be scroll-able. I have tried everything and can't get it to work :\
Here is what I have
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android1:id="#+id/tab3"
android1:layout_width="fill_parent"
android1:layout_height="fill_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/tiles" />
</LinearLayout>
</ScrollView>
Second question is: Is there a way to PREVENT image scaling? The image is 224x1632 and shows that way on my galaxy nexus. On the Nexus 7 (much larger screen but lower DP) it goes to 199x1440. A weird scaling of 88%. Can I prevent this? I need it to display 224x1632 on all machines and scale the width accordingly.
Many thanks!
If you need to avoid scaling entirely, you should place your drawables in the res/drawable-nodpi folder. Keep in mind that the physical size will be much different on devices of different densities, but as long as you understand the risks, that is the solution you should pursue.
On your first question, I'm inclined to think you'll need to do some manual work in code, but although I can't test it right now, this also might work:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/tiles"
/>
</ScrollView>
Give it a shot and let me know how it goes.
It should work... I think the problem is that you have android1: and it is suppose to be android:
Something like:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tab3"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/tiles" />
</LinearLayout>
</ScrollView>
As for the second question, you can try and use android:minWidth="" and android:minHeight="" to see if that solves your problem.
EDIT: try android:scaleType="fitCenter" it will display the image as is.
Hope that helps.

Multiple Screen Support

I have some problem with multiple screen support, I work with dp(dpi) for specify the layout_heigth and layout_width and I hope that is the better way to support multiple screen, but when I tried with two smartphone I meet two different result.
I give an example, this is a layout I use:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/cities_main_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#+id/citieslist"
android:layout_width="wrap_content"
android:layout_height="320dip"
android:layout_gravity="center_vertical"
android:layout_below="#id/cities_main_layout"
/>
<LinearLayout
android:id="#+id/cities_button_layout"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/citieslist"
android:layout_gravity="center_vertical">
<Button
android:id="#+id/bycountry"
android:layout_height="50dip"
android:layout_width="105dip"
android:background="#drawable/buttonmarket"
android:text="#string/button_bycountry"
/>
<Button
android:id="#+id/top10"
android:layout_height="50dip"
android:layout_width="105dip"
android:background="#drawable/buttonmarket"
android:text="#string/button_top10"
/>
<Button
android:id="#+id/recommended"
android:layout_height="50dip"
android:layout_width="105dip"
android:background="#drawable/buttonmarket"
android:text="#string/button_recommended"
/>
</LinearLayout>
</RelativeLayout>
The button are at the bottom of the layout, and I see two different result:
http://img600.imageshack.us/img600/5513/htcmagicg2.png http://img600.imageshack.us/img600/5513/htcmagicg2.png
http://img441.imageshack.us/img441/6440/samsunggalaxys.png http://img441.imageshack.us/img441/6440/samsunggalaxys.png
In the last smartphone I can see the buttons, instead in the first I cannot...what's wrong?
I have to write a layout for any set of screen??!!!
Your ListView has
android:layout_height="320dip"
Now if the phone screen is smaller, it will not fit.
Try doing this instead: (Edited due to comments. This is displayed correcty in eclipse)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relative"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF0000"
android:layout_above="#+id/linlay">
</ListView>
<LinearLayout
android:id="#+id/linlay"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="30dip"
android:background="#00FF00"
android:layout_alignParentBottom="true">
</LinearLayout>
</RelativeLayout>
Thant should fix it I think.
Cheers
As others have indicated, your problem is that you hardwired in a size for the ListView. If you want a business rule of "have the buttons at the bottom and have the ListView fill up the rest", you actually need to write code that implements "have the buttons at the bottom and have the ListView fill up the rest".
There are two main approaches here:
Use a LinearLayout parent for the buttons and the ListView. Use
android:layout_height="0px" and android:layout_weight="1" for the
ListView. Use a regular android:layout_height for the buttons (presumably in their own LinearLayout) and no
android:layout_weight for for them
Use a RelativeLayout parent for the buttons and the ListView.
Define the buttons as having android:layout_alignParentBottom="true".
Define the ListView as having android:layout_alignParentTop="true"
and android:layout_above="...", where the ... is the ID of the buttons' LinearLayout.
I would say it's because you are specifically declaring a height for your ListView and then laying the LinearLayout that holds your buttons at the bottom. Try changing it instead of being at the bottom of the ListView to something like
<LinearLayout
android:id="#+id/cities_button_layout"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alignparentbottom="true"
android:layout_gravity="center_vertical">
I'm not entirely sure if align_parent_bottom is the 100% correct spelling of that.
Well, others have beaten me to it while I was typing, haha, but yeah, you're hardwiring a lot of things that shouldn't be, both the ListView and the Buttons. Take a look at this:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/cities_main_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:id="#+id/cities_button_layout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
>
<Button
android:id="#+id/bycountry"
android:layout_height="50dip"
android:layout_width="fill_parent"
android:layout_weight="1"
android:background="#drawable/buttonmarket"
android:text="#string/button_bycountry"
/>
<Button
android:id="#+id/top10"
android:layout_height="50dip"
android:layout_width="fill_parent"
android:layout_weight="1"
android:background="#drawable/buttonmarket"
android:text="#string/button_top10"
/>
<Button
android:id="#+id/recommended"
android:layout_height="50dip"
android:layout_width="fill_parent"
android:layout_weight="1"
android:background="#drawable/buttonmarket"
android:text="#string/button_recommended"
/>
</LinearLayout>
<ListView
android:id="#+id/citieslist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/cities_button_layout"
/>
</RelativeLayout>
You have android:orientation on your RelativeLayout, which isn't actually an attribute that RelativeLayout contains.
You should use the layout_weight attribute rather than hardwiring sizes for the Buttons. In my example, all buttons have a width of fill_parent, and a weight of 1. This makes them distribute the space evenly.
List the fixed button layout first, setting it to alignParentBottom="true". Then set the ListView to fill_parent, and layout_above your button layout. This keeps the button layout at the bottom, and makes the ListView take all the space above your buttons.
Tada!

How to place a text between two images in Android

I want to create layout where I have two images at left and at right and text in center.
I have tried to do it with relative layout but unfortunetly it was unsuccessfully. Could anybody provide me an example?
Have you tried something like?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/img1"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_alignParentLeft="true"
android:src="#drawable/image1"/>
<ImageView
android:id="#+id/img2"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_alignParentRight="true"
android:src="#drawable/image2"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/img1"
android:layout_toLeftOf="#id/img2"
android:layout_alignTop="#id/img1"
android:text="I'm between!"/>
</RelativeLayout>
If you don't need more things on your view, you could want to use LinearLayout instead, since it's more easier to implement. In that case, you just have to play with the layout_weight attribute.

Categories