Position a LinearLayout (horizontal center) - android

I have a problem positioning a LinearLayout. See image for the problem. If I use wrap_content in the field specified, it is not centered. If I use match_parent, it is centered, but the components are spaced too far apart. How do I 'wrap' the contents, but center the layout horizontally? (vertical is not a consideration here)
<LinearLayout
android:layout_width="wrap_content" !!! (or match_parent)
android:layout_height="wrap_content"
android:layout_margin="25dip"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/game1CheckButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="4dp"
android:background="#null"
android:contentDescription="#string/checkbutton"
android:gravity="center"
android:onClick="clickCheck"
android:src="#drawable/checkbutton" />
</LinearLayout>
<TextView
android:id="#+id/game1Step"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:contentDescription="#string/game1step1"
android:gravity="center"
android:textColor="#FFFFFF"
android:textSize="50sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/game1NextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="4dp"
android:background="#null"
android:clickable="false"
android:contentDescription="#string/nextbutton"
android:enabled="false"
android:gravity="center"
android:onClick="clickNext"
android:src="#drawable/nextbutton" />
</LinearLayout>

Try using android:layout_width="wrap_content" and android:layout_gravity="center_horizontal"

As far as I can understand, you want 3 obects all positioned in the horizontal center, no matter the vertical position.
So, I'd do that like this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="25dip"
>
<!-- The center object -->
<TextView
android:id="#+id/game1Step"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:contentDescription="#string/game1step1"
android:gravity="center"
android:textColor="#FFFFFF"
android:textSize="50sp"
android:textStyle="bold"
/>
<!-- The left object -->
<ImageButton
android:id="#+id/game1CheckButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/game1Step"
android:layout_gravity="center"
android:layout_margin="4dp"
android:background="#null"
android:contentDescription="#string/checkbutton"
android:gravity="center"
android:onClick="clickCheck"
android:src="#drawable/checkbutton"
/>
<!-- The right object -->
<ImageButton
android:id="#+id/game1NextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/game1Step"
android:layout_gravity="center"
android:layout_margin="4dp"
android:background="#null"
android:clickable="false"
android:contentDescription="#string/nextbutton"
android:enabled="false"
android:gravity="center"
android:onClick="clickNext"
android:src="#drawable/nextbutton"
/>
</RelativeLayout>

Related

Center Editext in a Linear Layout - Android

I'm trying to center the editText which you can see in this screen (the one on the black buttons):
This is my code. I set the android:gravity="center_horizontal" tag, but as you can see it doesn't works. I do not understand if I need to set it at the center of its linear layout or if I need to pay attention to the external linear layout:
<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:background="#FCFCFC"
android:orientation="vertical"
android:layout_marginTop="8dp"
tools:context=".PaintingActivity" >
<!-- Top Buttons -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/draw_btn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="50dp"
android:contentDescription="#string/brush"
android:background="#drawable/rounded_corners"
android:padding="16dp"
android:layout_marginTop="5dp"
android:src="#drawable/brush" />
<ImageButton
android:id="#+id/erase_btn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="50dp"
android:contentDescription="#string/erase"
android:background="#drawable/rounded_corners"
android:padding="16dp"
android:layout_marginTop="5dp"
android:src="#drawable/eraser" />
<ImageButton
android:id="#+id/undo_btn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:contentDescription="Undo"
android:layout_marginRight="50dp"
android:background="#drawable/rounded_corners"
android:padding="16dp"
android:layout_marginTop="5dp"
android:src="#drawable/undo" />
<ImageButton
android:id="#+id/colorPicker"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginTop="5dp"
android:background="#drawable/rounded_corners"
android:contentDescription="colorPicker"
android:padding="16dp"
android:src="#drawable/palette" />
</LinearLayout>
<!-- Custom View -->
<com.example.williamstest.DrawingView
android:id="#+id/drawing"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginBottom="3dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:layout_weight="1"
android:background="#FFFFFFFF" />
<!-- Menu in basso -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
<!-- Top Row -->
<LinearLayout
android:id="#+id/toprow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/_70sdp"
android:orientation="horizontal">
<EditText
android:id="#+id/edittitle"
android:layout_width="400dp"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:layout_marginBottom="25dip"
android:ems="10"
android:hint="Inserisci qui il titolo"
android:gravity="center_horizontal"
android:inputType="textNoSuggestions"
android:singleLine="true"/>
</LinearLayout>
<!-- Bottom Row -->
<LinearLayout
android:id="#+id/bottomrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="120dp"
android:orientation="horizontal">
<Button
android:id="#+id/bb_3"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginLeft="120dp"
android:layout_marginBottom="10dp"
android:background="#drawable/button_bg"
android:text="Indietro"
android:textColor="#FFFFFF" />
<Button
android:id="#+id/bb_2"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginLeft="120dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:background="#drawable/button_bg"
android:text="Avanti"
android:textColor="#FFFFFF" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
You have a redundant left margin
Remove android:layout_marginLeft="#dimen/_70sdp"from thetoprowlayout
or add an equal right margin
No need to use layout_marginLeft to align it center_horizontal, remove it. Just add android:gravity to center_horizontal in your LinearLayout enclosing EditText.
<LinearLayout
android:id="#+id/toprow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<EditText
android:id="#+id/edittitle"
android:layout_width="400dp"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:layout_marginBottom="25dip"
android:ems="10"
android:hint="Inserisci qui il titolo"
android:gravity="center_horizontal"
android:inputType="textNoSuggestions"
android:singleLine="true"/>
</LinearLayout>

Linear Layout shorten text in textview

I have a status bar with 6 elements in a row in a linear layout. This looks ok on a newer display with high resolution. But when I start the app on my old smartphone only the first text of the Textview in the status bar is shown. The imageViews are cut off and the status bar gets higher without extra content.
What I want is, that if the content does not fit, the text in the first textView is shorten, so that the ImageViews can all be shown. Is this possible?
<?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="wrap_content"
android:layout_margin="2dp"
android:gravity="right|center_vertical"
android:orientation="horizontal" >
<TextView
android:id="#+id/statusbar_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="Text der Statusbar" />
<TextView
android:id="#+id/statusbar_text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="TEXT1" />
<ImageView
android:id="#+id/some_state0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/stat_some_state0" />
<ImageView
android:id="#+id/some_state1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:src="#drawable/stat_some_state1" />
<ImageView
android:id="#+id/some_state2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:src="#drawable/stat_some_state2" />
<ImageView
android:id="#+id/some_state3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:src="#drawable/stat_some_state3" />
</LinearLayout>
Add weight for all elements . If weights are equal the Layout will be divided equally. if weight is twice the others then the division will be twice. if orientation is vertical and you had given weight then height should be 0dp and if it is horizontal , width should be 0dp
?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="wrap_content"
android:layout_margin="2dp"
android:gravity="right|center_vertical"
android:orientation="horizontal" >
<TextView
android:id="#+id/statusbar_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:layout_weight="1"
android:text="Text der Statusbar" />
<TextView
android:id="#+id/statusbar_text1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="5dp"
android:text="TEXT1" />
<ImageView
android:id="#+id/some_state0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/stat_some_state0" />
<ImageView
android:id="#+id/some_state1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="5dp"
android:src="#drawable/stat_some_state1" />
<ImageView
android:id="#+id/some_state2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="5dp"
android:src="#drawable/stat_some_state2" />
<ImageView
android:id="#+id/some_state3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="5dp"
android:src="#drawable/stat_some_state3" />
</LinearLayout>
you should make your layout like this. adding weight to the views will fix their widths. So no matter the text length it won't disturb your imageViews.
<?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="wrap_content"
android:layout_margin="2dp"
android:gravity="right|center_vertical"
android:orientation="horizontal">
<TextView
android:id="#+id/statusbar_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:paddingLeft="5dp"
android:text="Text der Statusbar" />
<TextView
android:id="#+id/statusbar_text1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="5dp"
android:text="TEXT1" />
<ImageView
android:id="#+id/some_state0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:src="#drawable/stat_some_state0" />
<ImageView
android:id="#+id/some_state1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:paddingLeft="5dp"
android:src="#drawable/stat_some_state1" />
<ImageView
android:id="#+id/some_state2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:paddingLeft="5dp"
android:src="#drawable/stat_some_state2" />
<ImageView
android:id="#+id/some_state3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:paddingLeft="5dp"
android:src="#drawable/stat_some_state3" />
</LinearLayout>

Center a TextView below an ImageView in a TableLayout

how can I center a TextView below an ImageView in a TableLayout? I see that I have to use center_horizontal with a specified layout_width in dp:
android:layout_width="90dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
...but my text value is not always the same, I set it programmatically and if it's too long:
So I set android:singleLine to true:
android:layout_width="90dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:singleLine="true"
So I set layout_width to wrap_content but in this way center_horizontal is not working :(
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:singleLine="true"
This is my TableRow layout:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/table_row_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp">
<RelativeLayout
android:id="#+id/relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<!-- Left column -->
<ImageView
android:id="#+id/left_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:src="#drawable/item_image" />
<TextView
android:id="#+id/left_name"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/left_image"
android:layout_alignStart="#+id/left_image"
android:layout_below="#+id/left_image"
android:gravity="center_horizontal"
android:text="ITEM TEXT VIEW"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/black" />
<!-- Right column -->
<ImageView
android:id="#+id/right_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/left_name"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="60dp"
android:layout_marginRight="60dp"
android:src="#drawable/item_image"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/right_name"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/right_image"
android:layout_alignStart="#+id/right_image"
android:layout_below="#+id/right_image"
android:gravity="center_horizontal"
android:text="ITEM TEXT VIEW" />
</RelativeLayout>
</TableRow>
Anyone can help me?
UPDATE: This my real layout element:
You can use two RelativeLayouts instead of one. Check this XML,
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/table_row_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp">
<RelativeLayout
android:id="#+id/relative_layout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:weightSum="0.5">
<!-- Left column -->
<ImageView
android:id="#+id/left_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:src="#drawable/item_image" />
<TextView
android:id="#+id/left_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/left_image"
android:gravity="center_horizontal"
android:layout_centerHorizontal="true"
android:text="ITEM TEXT VIEW"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/black" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relative_layout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:weightSum="0.5">
<!-- Right column -->
<ImageView
android:id="#+id/right_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:src="#drawable/item_image" />
<TextView
android:id="#+id/right_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/right_image"
android:gravity="center_horizontal"
android:layout_centerHorizontal="true"
android:text="ITEM TEXT VIEW"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/black" />
</RelativeLayout>
</TableRow>
RelativeLayout is not a best suit for this scenario.It's better to replace with two LinearLayouts. The advantage is, you can avoid most of the magic numbers like margins and paddings. Try this layout,
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/table_row_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<!-- Left column -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/left_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/circle" />
<TextView
android:id="#+id/left_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="ITEM TEXT VIEW" />
</LinearLayout>
<!-- Right column -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/right_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/circle"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/right_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="ITEM TEXT VIEW" />
</LinearLayout>
</TableRow>

Android button height is different

I have two buttons inside a linear layout which have same properties defined. But to my surprise, both have different height. Please see the xml code below
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingTop="5dp" >
<ImageView
android:id="#+id/imgUser"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="5dp"
android:layout_marginTop="10dp"
android:adjustViewBounds="true"
android:src="#drawable/default_people_thumb" />
<TextView
android:id="#+id/tvUploadHeader"
style="#style/Text.Quaternary.Small.Bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/choose_a_profile_photo" />
<TextView
android:id="#+id/tvDescription"
style="#style/Text.Quaternary.ExtraSmall.Bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:text="#string/photo_upload_initial" />
<Button
android:id="#+id/btnChoosePhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#drawable/login_button_selector"
android:text="#string/choose_photo_from_gallery" />
<Button
android:id="#+id/btnSkip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#drawable/login_button_selector"
android:text="#string/choose_photo_from_gallery" />
</LinearLayout>
Can you please help me to have the same height. I am wondering as to how both behaves differently. Also added includeFontPadding for the button. But no hope :(.
Your background drawables have different padding or height. check #drawable/btn_large_gray and #drawable/login_button_selector
put both the button inside linear layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingTop="5dp" >
<ImageView
android:id="#+id/imgUser"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="5dp"
android:layout_marginTop="10dp"
android:adjustViewBounds="true"
android:src="#drawable/default_people_thumb" />
<TextView
android:id="#+id/tvUploadHeader"
style="#style/Text.Quaternary.Small.Bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/choose_a_profile_photo" />
<TextView
android:id="#+id/tvDescription"
style="#style/Text.Quaternary.ExtraSmall.Bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:text="#string/photo_upload_initial" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingTop="5dp" >
<Button
android:id="#+id/btnChoosePhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#drawable/login_button_selector"
android:text="#string/choose_photo_from_gallery" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingTop="5dp" >
<Button
android:id="#+id/btnSkip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#drawable/btn_large_gray"
android:text="#string/skip" />
</LinearLayout>
</LinearLayout>
and adjust both linear layouts as you like it
Try defining property android:layout_weight for each element in your LinearLayout, so they can have the same height.
For example :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingTop="5dp" >
<ImageView
android:id="#+id/imgUser"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_weight="1"
android:layout_margin="5dp"
android:layout_marginTop="10dp"
android:adjustViewBounds="true"
android:src="#drawable/default_people_thumb" />
<TextView
android:id="#+id/tvUploadHeader"
style="#style/Text.Quaternary.Small.Bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/choose_a_profile_photo" />
<TextView
android:id="#+id/tvDescription"
style="#style/Text.Quaternary.ExtraSmall.Bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:text="#string/photo_upload_initial" />
<Button
android:id="#+id/btnChoosePhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="5dp"
android:background="#drawable/login_button_selector"
android:text="#string/choose_photo_from_gallery" />
<Button
android:id="#+id/btnSkip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="5dp"
android:background="#drawable/login_button_selector"
android:text="#string/choose_photo_from_gallery" />
</LinearLayout>
The idea is to give as much space to each element. Your LinearLayout contains different elements so by choosing wrap_content in the android:layout_height and android:layout_width, space available will be allocated for each element added. And as you TextViews and Buttons are not the same...
Hope it'll help
You are using different style for both the buttons,
i.e. "#style/Text.Quaternary.Small.Bold" and "#style/Text.Quaternary.ExtraSmall.Bold" you should use only one style for both buttons

How to disable or block TextView to not narrowed other elements of the Layout

I have problem with my TextView, when something is written in it. It narrows the right layout.
like here:
1 Without something is written: http://i.stack.imgur.com/zItJw.jpg
2.Without something is written in: http://i.stack.imgur.com/b5Nb4.jpg
My Layout Code:
1st bar:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="1"
android:background="#drawable/czas_punkty_bez_napisu"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="#+id/dzialanie"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="25sp"
android:text="X razy Y" />
<TextView
android:id="#+id/equal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="25sp"
android:text="=" />
<TextView
android:id="#+id/tylemabyc"
android:textSize="25sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout>
</LinearLayout>
2nd bar
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:background="#drawable/czas_punkty_bez_napisu"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/czas"
android:textSize="25sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
</LinearLayout>
3rd bar:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:background="#drawable/czas_punkty_bez_napisu"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/wynik"
android:textSize="25sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
</LinearLayout>
How to block 1st TextView to not narrows others ?
You can use layout_weight property to tell android how much space each textView takes on the screen. If you want all of them to have the same space you can add the layout_weight of 1 for all of them.
You will also need to readjust your layout_width too. You will have to trade off for the layout_heght. For example if i want 4 textviews in a horizontal line holding equal space "Horizontally" i would do something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="This is verrry long text" />
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="maybe not" />
<TextView
android:id="#+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="let us see" />
<TextView
android:id="#+id/textView4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ok" />

Categories

Resources