Aligning margins correctly in android layout - android

Given below is the screenshot of my screen which contains a progressbar. As you can see from the screen, the spacing between the left and right of the screens and this progressbar is not equal. It is less towards the right. How can I make this the same as the left with less gap?
Any help is appreciated
Code and screenshot below:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical" >
<TextView
android:id="#+id/textViewContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/blue"
android:textSize="18sp" />
<TextView
android:id="#+id/textLink"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:textSize="16sp" />
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="10dp"
android:progressDrawable="#drawable/green_progress_color" />
</LinearLayout>
</TableRow>
EDIT
Main Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/bottom_layout_installing"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="#string/total_progress" />
<ProgressBar
android:id="#+id/total_installed_package_installer"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_margin="10dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp" >
<Button
android:id="#+id/buttonAbort"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/button_bg"
android:onClick="onAbortClick"
android:text="#string/abort"
android:textColor="#android:color/white" />
<Button
android:id="#+id/buttonLaunch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/button_bg"
android:text="#string/install"
android:textColor="#android:color/white" />
</LinearLayout>
</LinearLayout>
<ScrollView
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_above="#id/bottom_layout_installing" >
<TableLayout
android:id="#+id/installingListview"
style="#android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:groupIndicator="#android:color/transparent"
android:listSelector="#android:color/transparent" >
</TableLayout>
</ScrollView>

Looks like your '#drawable/green_progress_color' doesn't stretch. Try with '#android:drawable/progress_horizontal' to check if this is to your layout or your progress drawable.

Don't use a TableRow just on its own like that, remove it and just have the LinearLayout. The TableRow is causing the problem. TableRow must always be nested within a TableLayout(http://developer.android.com/reference/android/widget/TableRow.html)
<?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="vertical" >
<TextView
android:id="#+id/textViewContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/blue"
android:textSize="18sp" />
<TextView
android:id="#+id/textLink"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:textSize="16sp" />
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="10dp"
android:progressDrawable="#drawable/green_progress_color" />
</LinearLayout>

Related

How to align LinearLayout at bottom in a RelativeLayout?

I'm trying to get the LinearLayout to be aligned at the bottom of this RelativeLayout. I tried setting alignParentBottom="true", but then I can't even see the contents of the LinearLayout. Is there another way to accomplish what I need?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="90dp"
android:layout_height="200dp"
android:minHeight="200dp"
android:background="#color/colorPrimaryDark"
android:elevation="2dp"
android:gravity="center">
<TextView
android:id="#+id/textView_bookName"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:padding="10dp"
android:textSize="20dp"
android:textColor="#color/white"
/>
<TextView
android:id="#+id/textView_bookAuthor"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="1dp"
android:text="Author"
android:textSize="10dp"
android:textColor="#color/white"
android:layout_below="#+id/textView_bookName"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_below="#+id/textView_bookAuthor"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/button_download_book"
android:text="#string/download"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/button_read_book"
android:text="Read"
android:visibility="gone"/>
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/progressbar_book_download"
style="#android:style/Widget.ProgressBar.Horizontal"
android:progress="0"
android:visibility="gone"
/>
</LinearLayout>
</RelativeLayout>
Add to LinearLayout: android:layout_alignParentBottom="true"
eg.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:layout_below="#+id/textView_bookAuthor">
[...]
<LinearLayout
android:layout_alignParentBottom="true"
android:layout_height="100dp"
android:layout_width="wrap_content">
</LineaLayout>
Try using a LinearLayout that contains both layouts and setting them an android:layout_weight and android:layout_height="0dp".
Don't forget to set the ratio between the weights to one you'd like and setting the container's android:orientation to vertical.
BTW, for textSize you better use sp and not dp...
Please try below xml which I have modified. It will solve the issue. I have removed the android:layout_below="#+id/textView_bookAuthor" and added android:layout_alignParentBottom="true".
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="90dp"
android:layout_height="200dp"
android:minHeight="200dp"
android:background="#color/colorPrimaryDark"
android:elevation="2dp"
android:gravity="center">
<TextView
android:id="#+id/textView_bookName"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:padding="10dp"
android:textSize="20dp"
/>
<TextView
android:id="#+id/textView_bookAuthor"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="1dp"
android:text="Author"
android:textSize="10dp"
android:layout_below="#+id/textView_bookName"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/button_download_book"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/button_read_book"
android:text="Read"
android:visibility="gone"/>
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/progressbar_book_download"
style="#android:style/Widget.ProgressBar.Horizontal"
android:progress="0"
android:visibility="gone"
/>
</LinearLayout>
</RelativeLayout>

Textview in Linear Layout Appears on the wrong position

I have the Below Layout. I want the linear layout that holds the Textviews to appear below the PieChart. However it appears on top and on smaller screens the piechart overlays over the text.
<?xml version="1.0" encoding="utf-8"?>
<uk.co.senab.actionbarpulltorefresh.extras.actionbarsherlock.PullToRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/catexprpt_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.echo.holographlibrary.PieGraph
android:id="#+id/piegraph"
android:paddingTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="#+id/tableRowN"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="15dp"
android:layout_below="#+id/piegraph"
android:orientation="horizontal" >
<TextView
android:id="#+id/titleN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/rounded_edges"
android:text="#string/note"
android:textColor="#ffffff"
android:textSize="13sp"
android:typeface="serif" />
<TextView
android:id="#+id/notice3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:typeface="serif" />
</LinearLayout>
Can you try with the PieGraph as a child of the LinearLayout.
I'm not sure about ActionBarSherlock's PullToRefreshLayout but the Android implementation SwipeRefreshLayout supports only one direct child view.
Edit:
I'm suggesting something like this as an only child to the Refresh layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.echo.holographlibrary.PieGraph
android:id="#+id/piegraph"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip" />
<TextView
android:id="#+id/titleN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/piegraph"
android:background="#drawable/rounded_edges"
android:text="#string/note"
android:textColor="#ffffff"
android:textSize="13sp"
android:typeface="serif" />
<TextView
android:id="#+id/notice3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/piegraph"
android:textColor="#000000"
android:typeface="serif" />
</RelativeLayout>
More on layout optimization here.
I managed to solve it as below thanks to # LordRaydenMK :
<?xml version="1.0" encoding="utf-8"?>
<uk.co.senab.actionbarpulltorefresh.extras.actionbarsherlock.PullToRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/catexprpt_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/tableRowM"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.echo.holographlibrary.PieGraph
android:id="#+id/piegraph"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/tableRowN"
android:paddingBottom="20dp" />
<LinearLayout
android:id="#+id/tableRowN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true">
<TextView
android:id="#+id/titleN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/rounded_edges"
android:text="#string/note"
android:textColor="#ffffff"
android:textSize="13sp"
android:typeface="serif" />
<TextView
android:id="#+id/notice3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="Heloooooo Mr. Fish"
android:typeface="serif" />
</LinearLayout>

ViewPager/Fragment overlapping LinearLayout at bottom

I have a ViewPager which shows Fragments and I want that on the top, and a simple LinearLayout on the bottom. But, the Fragments are overlapping the layout at the bottom. I want the Fragment to end right about the LinearLayout. See picture:
XML with ViewPager
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="#+id/steps_pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<Button
android:id="#+id/finish_checklist_btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/btn_white_glossy"
android:layout_margin="20dp"
android:text="#string/finish_checklist" />
</LinearLayout>
XML of Fragment
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
android:background="#fff" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26sp"
android:text="#string/step_order_label" />
<TextView
android:id="#+id/step_order"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26sp" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#000" />
<TextView
android:id="#+id/step_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:textSize="26sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="40dp"
android:textSize="20sp"
android:text="#string/result_label" />
<TextView
android:id="#+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" />
</LinearLayout>
<!-- YES/NO BUTTONS -->
<Button
android:id="#+id/button_no"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/btn_white_glossy"
android:padding="20dp"
android:visibility="gone"
android:text="#string/button_no" />
<Button
android:id="#+id/button_yes"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/button_no"
android:background="#drawable/btn_white_glossy"
android:padding="20dp"
android:layout_marginBottom="30dp"
android:visibility="gone"
android:text="#string/button_yes" />
<!-- END YES/NO BUTTONS -->
I used android:layout_below element here.
layout_below your ViewPager id.
and this layout as vertical
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="fill_vertical"
android:orientation="vertical" >
In all your xml files, should define the background color for it, it will solve the problem :
Add this android:background="#android:color/black" in to the View tag you defined.
p/s : This case only avoid Background was overlapped.

Linear Layout weight not working in android

I have a problem in my android layout.
I have an Image as following contains two parts:
(green border) will contains a descriptive meaning
(red border) I have to position a text in this part to be centered
I want the text to be in the bottom 1/3 of the image.
I have tried to make it programmatically but it doesn't work as I can't get (x, y, w, h) of an Image except it has rendered to the screen.
I have tried also the following code:
<RelativeLayout
android:id="#+id/item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1" >
<ImageView
android:id="#+id/item1_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:src="#drawable/item1_icon" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="3" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="2" >
</LinearLayout>
<TextView
android:id="#+id/item1_title"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:ellipsize="end"
android:gravity="center"
android:maxLength="10"
android:singleLine="true"
android:text="My Day"
android:textColor="#android:color/white"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
How can I do that?
Note that the layout_weight attribute is assigned to the RelativeLayout:
<RelativeLayout
android:id="#+id/item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1" >
It won't work this way. Only a LinearLayout can have a layout_weight attribute. For RelativeLayout it is ignored.
I guess that you forgot to add this namespace to root view of your layout:
xmlns:android="http://schemas.android.com/apk/res/android"
And also I guess your problem will be solved if add it to your code.I added that to your code and try it.It was true.This is it's all:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1" >
<ImageView
android:id="#+id/item1_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:src="#drawable/ic_launcher" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="3"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:background="#android:color/white" >
</LinearLayout>
<TextView
android:id="#+id/item1_title"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:ellipsize="end"
android:gravity="center"
android:maxLength="10"
android:singleLine="true"
android:text="My Day"
android:textColor="#android:color/white"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
This should work
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/item1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp">
<ImageView
android:id="#+id/item1_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="#drawable/ic_launcher" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:background="#android:color/white" >
</LinearLayout>
<TextView
android:id="#+id/item1_title"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:ellipsize="end"
android:gravity="center"
android:maxLength="10"
android:singleLine="true"
android:text="My Day"
android:textColor="#android:color/white"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>

Fill RelativeLayout parent with LinearLayout child

This seems simple but I've exhausted my Google search patience.
I have a RelativeLayout that has two sub LinearLayouts side-by-side and I want them both to vertically fill the parent, no matter the height of the two LinearLayouts. This is being used as a ListView row and the content under #id/item_left is generally shorter than the one on the right.
The background drawable doesn't fill the entire row.
Any ideas on making both sides fill?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent">
<LinearLayout android:id="#+id/item_left"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:paddingRight="2dp"
android:paddingLeft="2dp"
android:background="#drawable/item_left"
android:orientation="vertical">
<TextView android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/item_left_text"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/item_right_background"
android:layout_toRightOf="#id/item_left">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/item_right_text"
android:text="Row1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/item_right_text"
android:text="Row2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/item_right_text"
android:text="Row3" />
</LinearLayout>
</RelativeLayout>
try this code .... sorry but I changed the color to so the impact when you will change the text of first TextView
<?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="fill_parent"
android:background="#ffffffff" android:weightSum="1">
<LinearLayout android:id="#+id/item_left"
android:layout_width="wrap_content" android:layout_height="fill_parent"
android:paddingRight="2dp" android:paddingLeft="2dp"
android:background="#ff0000ff" android:orientation="vertical" >
<TextView android:id="#+id/time" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="#ff555555"
android:text="sdfdsf s ada d a asd sad sad" />
</LinearLayout>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="match_parent" android:orientation="vertical"
android:background="#ffff0000"
android:layout_weight="1" >
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="#ff555555"
android:text="Row1" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="#ff00ff00"
android:text="Row2" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="#ff00ff00"
android:text="Row3" />
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/layout1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FF0000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="helloooooooooooooo" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#id/layout1"
android:background="#0000FF"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Row1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Row2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Row3" />
</LinearLayout>
</RelativeLayout>

Categories

Resources