Android Text is wrapped in button - why? - android

I have the following XML:
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/verylongtext" android:layout_weight="1000" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sometext" android:onClick="onClickButton" android:layout_weight="1" />
</LinearLayout>
Though I have set a layout_weight there is still a linebreak in the text on the button, the button should show
sometext
but it shows
somet
ext
Why is this and how can I fix it?
Thanks!

Include android:singleLine="true" to Button xml.

You don't specify the orientation of your LinearLayout, so it is defaulting to horizontal. The TextView is squeezing the Button. Also, when you use android:layout_weight, then you often don't want to constrain the objects in the layout in the relevant dimension, so you can put "0px" instead of "wrap_content" or "match_parent" and let the weight determine the respective proportional sizes.

Related

How to align two TextView in a LinearLayout in the same line

I have two TextView in a LinearLayout, I want to align them one to the left (or center) and one to right in the same line. How to do this? I try to use gravity but they ignore it.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="TextView" />
</LinearLayout>
The easiest way is to change your LinearLayout to a RelativeLayout.
You can use android:layout_alignParentRight="true" and android:layout_alignParentLeft="true". Or to center it use android:layout_centerInParent="true"
See here why gravity won't work
You are using gravity instead of layout_gravity which is what you would want. This post should help clarify the difference
The docs show you available properties.
android:gravity is used to set the gravity of content inside the view. However, in your case the width is wrap_content, hence the content has nowhere to go in the text views.
Use a RelativeLayout with layout_width as match_parent. Then use the android:layout_alignParentLeft="true" and android:layout_alignParentRight="true"with the textViews.
Use it with or without the android:gravity in the second textview and try .
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1" />
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:layout_weight="1" />
</LinearLayout>
If LinearLayout is Vertical, you can put only an object per line.
You can use RelativeLayout, or else put in a line a LinearLayout Horizontal, that contains textviews
ex.
<LinearLayout vertical>
<LinearLayout horizontal>
<textview 1></>
<textview 2></>
</LinearLayout>
</LinearLayout>
I fixed all my issues with GridLayout...is the best thing bcos u don't need to align anithing to nothing...just put what u want into the matrix (row,column)...and this will allow you to visualize all the field in exactly wrap content of your datas also in landscape is perfect!

Why view elements are going out of the page

I have a keyword search option with EditView and a button in my android app, which in ADT looks perfect.
But when i run the program, the EditView expand beyond the viewing area and search button disappears because it goes out of page.
Xml for this is:
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/detail_ll"
style="#style/search_bg_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/keyword_detail"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:ems="10"
android:hint="#string/keyword" />
<Button
android:id="#+id/button_search"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="#string/search" />
</LinearLayout>
</TableRow>
Please help me in getting this in viewing area..
can you post the entire xml?
i mean with the
<TableLayout />
main tag
try setting android
android:layout_weight="1"
for both
or try android:layout_weight="1.5"
for edit text and
android:layout_weight="0.5"
for the button
I think you should make the android:layout_width="wrap_content" to android:layout_width="match_parent" or any fixed width (in dps) in the TableRow element.
layout_weight attribute divides a pre-known space according to its weight which is not present in this case.
Here you have to TableRow and LinearLayout height MatchParent and
give Weightsum="1" to LinearLayout it will fixed your problem and also remove android:ems property from Edittext
Hope this Help you.

Button layout_height=wrap_content inside linear layout has height much bigger then wrap_content

I have LinearLayout with TextView and Button. I set layout_height attribute to wrap_content but layout_height has much bigger size. If I remove this button then all is alright. I try to decrease button text size but frame of the button still the same. I don't want hardcode button size. What can be the reason this strange result?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="#dimen/left_margin"
android:layout_marginTop="#dimen/top_margin"
android:layout_weight=".3"
android:text="#string/contact" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:layout_marginLeft="#dimen/left_margin"
android:layout_marginTop="#dimen/top_margin"
android:orientation="horizontal"
android:weightSum="1" >
<TextView
android:id="#+id/contact"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/contact" android:textSize="#dimen/button_font_size"/>
</LinearLayout>
Button has background and other layout properties and does not seem possible to reduce its height. An option would be to use a TextView with custom style and handle click event so that it behaves like a Button.
Example:
<TextView
android:layout_height="wrap_content"
android:background="#ddd"
android:padding="6dp"
android:layout_width="wrap_content"
android:text="Contact" />
It requires some work and time spent to style the TextView properly, but if you have no other choice, it may do the trick for you.
I had this issue as well when using a button, you can manually set the height and width, but I also have not found a set way to reduce the wrap_content size.
android:layout_width="40dp"
android:layout_height="30dp"
This is because of minimum height of a button. Set android:minHeight="1dp" or any other size to make it happen.

Android - Button text center gravity not working

I have this XML layout in my App (example of one button):
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/scrollViewMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff0e8" >
///some views
<Button
android:id="#+id/btnVysledkySportka"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/chckSprotka"
android:layout_alignRight="#+id/chckSprotka"
android:layout_alignTop="#+id/chckSprotka"
android:layout_margin="3dp"
android:layout_toRightOf="#+id/textView3"
android:background="#ffe118"
android:gravity="center"
android:onClick="vysledkySportka"
android:text="Archiv výsledků"
android:textStyle="bold" />
///some views
</RelativeLayout>
</ScrollView>
But when I start my app at Android 4.3, the text of buttons isn´t in center of button. Look at the screenshot (in red rectangle):
Where can be problem?
EDIT:
Whole layout
When you specify:
android:layout_alignBottom="#+id/chckSprotka"
android:layout_alignRight="#+id/chckSprotka"
android:layout_alignTop="#+id/chckSprotka"
It does make the bottom/right/top edge of your button match the bottom edge of the given anchor view ID & accommodates bottom/right/top margin, but while doing that, the android_gravity does not take the resultant height/width into consideration.
So the gravity of the text is center according to wrap_content for layout_height and layout_width.
You can verify that by setting values for layout_height and layout_width (Eg. 200dp and 100dp to try with) and you will get the text with gravity center but for that height and width.
To confirm the same, what you can do is use a container LinearLayout for your Button like:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/chckStastnych"
android:layout_alignRight="#+id/chckStastnych"
android:layout_alignTop="#+id/chckStastnych"
android:layout_margin="3dp"
android:layout_toRightOf="#+id/textView3"
android:background="#ffe118" >
<Button
android:id="#+id/btnVysledkyStastnych"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffe118"
android:gravity="center"
android:onClick="vysledkyStastnych"
android:text="Archiv výsledků"
android:textStyle="bold" />
</LinearLayout>
Set the gravity of LinearLayout as center and then center the Button within or as shown above, use layout_gravity for the button to center it in parent LinearLayout.
This will work as a solution when you do that for all 4 buttons, however there might be better options if you restructure your xml and avoid this kind of nesting.
Try This:
<Button
android:id="#+id/btnVysledkySportka"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/chckSprotka"
android:layout_alignRight="#+id/chckSprotka"
android:layout_alignTop="#+id/chckSprotka"
android:layout_margin="3dp"
android:layout_toRightOf="#+id/textView3"
android:background="#ffe118"
android:gravity="center_vertical"
android:onClick="vysledkySportka"
android:text="Archiv výsledků"
android:textStyle="bold" />

How to right-align text in an Android TextView or EditText?

I have an EditText in my application. I want to align the text in it to the right instead of the default left. I tried adding
android:layout_gravity="right"
but this doesn't seem to work. any other suggestions please?
You should use android:gravity="right". layout_gravity is for the view (EditText) alignment against the container.
You may use android:layout_alignParentRight="true" to align the EditText's right edge to its parent's right edge. Also, if you really want to use the layout_gravity or gravity attributes, check out this article that discusses the proper use of them.
You can jst set property for edit text in property window i.e. Gravity to right or by adding code of lin e in xml file of UI : android:gravity="right"
layout_gravity means that you are specifying the layouts attribute, not the element inside. In some conditions, such as layout_width and layout_height is wrap_content, layout specifications can give what you want about the element inisde your layout, since the boundries of the layout and the element (not elements) are the same..
use layout width and height as wrap content inside relative layout :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/icon"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_alignParentStart="true"
android:paddingEnd="16dp"
android:paddingStart="8dp"
tools:src="#mipmap/ic_launcher"
android:layout_alignParentLeft="true"
android:paddingRight="16dp"
android:paddingLeft="8dp"
android:contentDescription="#string/todo" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/icon"
android:maxLines="1"
android:lines="1"
android:paddingTop="8dp"
tools:text="Google"
android:layout_toRightOf="#id/icon" />
<TextView
android:id="#+id/last_used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:paddingTop="8dp"
tools:text="Last used: yesterday at 18.54" />
</RelativeLayout>

Categories

Resources