Android-Aligning a element + margin with relative to parent element - android-layout

To align a element with respect to an other element using their I can use.
android:layout_alignLeft etc..
But how to add a margin or a padding:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="#string/students"
android:textSize="30sp"
android:typeface="normal"
android:layout_marginTop="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:id="#+id/student" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/student"
android:paddingLeft="20dp"
android:paddingTop="30dp"
android:text="#string/stud_describe"
android:textSize="10sp"
android:textStyle="italic"
android:typeface="sans" />
</RelativeLayout>
Padding top in the second TextView is not having any effect.

Looks like you're getting padding and margin mixed up. Change the second TextView to:
layout_marginTop="30dp"
instead of
paddingTop="30dp".
Padding pads the View itself, making it bigger. Margin changes the position of the TextView within its container (RelativeLayout) and doesn't alter its size.

Related

how to align text of text view to right side and also text should be in center of text view

I want to align my text in text view to right side and also it should be centered in the text view.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="right">
<ImageView
android:layout_height="60dp"
android:layout_width="60dp"
android:id="#+id/img_campaign"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:paddingTop="5dp">
</ImageView>
<TextView
android:id="#+id/txt_campaign"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_gravity="center"
android:paddingLeft="5dp"
android:gravity="right"
android:text="Dummy TEXT Dummy TEXT "
android:textSize="20dp">
</TextView>
</LinearLayout>
As you have only applied android:gravity="right" in your Text View. If you want itRight with Center in Text View then you need to change it to android:gravity="right|center_vertical" in your Text View.
This android:gravity="right|center_vertical" will Center your Text's in Text View.
Follow this.
<TextView
android:id="#+id/txt_campaign"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="right|center_vertical"
android:paddingLeft="5dp"
android:text="Dummy TEXT Dummy TEXT "
android:textSize="20dp">
</TextView>

Child Views do not expand to the same height as their parent

I've got the following setup:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/list_item"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
<!--otherwise nothing important is happening here -->
</LinearLayout>
<LinearLayout
android:id="#+id/sliding_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignTop="#+id/list_item"
android:layout_alignBottom="#+id/list_item"
android:visibility="gone"
android:orientation="horizontal" >
<TextView
android:id="#+id/undo_acknowledge"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="fill_vertical|start"
android:layout_weight="0.5"
android:textSize="20sp"
android:text="#string/undo"
android:singleLine="false"
android:textColor="#color/black" />
<TextView
android:id="#+id/acknowledge"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="fill_vertical|end"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:textSize="20sp"
android:text="#string/delete"
android:singleLine="false"
android:textColor="#color/black" />
</LinearLayout>
</RelativeLayout>
Some background:
I've got a Recycler View containing these layout as list items. When I slide an item, the LinearLayout (I've tried also RealtiveLayout) "sliding_layout" becomes visible and it's two TextViews appear.
The "list_item" layout's height is calculated using wrap_content and therefore the "sliding_layout" uses
android:layout_alignTop="#+id/list_item"
android:layout_alignBottom="#+id/list_item"
to match it's height.
Up to this point it works fine. But I want the two child TextViews of "sliding_layout" to have the same height as their parent (and therefore as "list_item"). As you can see, I tried to achieve this by multiple ways (all of them separate and combined, as far as possible):
android:layout_height="match_parent"
android:gravity="fill_vertical"
android:layout_alignParentTop="true" & android:layout_alignParentBottom="true" (with RealtiveLayout as parent)
But none of those seems to do anything: The vertical size of the TextViews seems to be calculated according to the text size.
My Build configuration is the following:
minSdkVersion 16
targetSdkVersion 21
Do you have any suggestions what I could do to achieve getting my TextViews the same height as their parent?

android layout - need to match the height of a table row to match width using layout weights

I want to display 18 buttons in a grid 0f 6x3. I am using six table rows and each row has 3 buttons. the width of each button is set to 0dp and given a layout_weight=1.
as the background image I want to use is a circular object I need the width and height of the button to be same! How can I achieve this.
here is the basic xml
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/tableRow">
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/imageButton4"
android:layout_weight="1"
android:background="#drawable/sh_commonroom" />
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/imageButton5"
android:layout_weight="1"
android:background="#drawable/sh_bedroom" />
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/imageButton6"
android:layout_weight="1"
android:background="#drawable/sh_kitchen" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
........................
Currently it looks like this.

How to alight two textviews in a relativelayout

I am playing around with a listview with two text views. It looks very bad at the moment.
I would like the text (Eight, Five ,etc) to begin some margin on the left. More importantly I want the Numbers on the right to be aligned with the text on the left. which is in the center.
What is the best way to align both the items in one line with some gap in the middle. Right now the text on the right is in top right.
This is my layout.
<TextView
android:id="#+id/num_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16dip"
android:textColor="#000000"
android:paddingTop="15dip"
android:paddingBottom="15dip"
android:paddingLeft="0dip"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView android:id="#+id/num_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textStyle="bold"
android:background="#9ed321"
android:paddingRight="13dip"
android:paddingLeft="13dip"
android:layout_alignBottom="#+id/cat_id"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
Have a LinearLayout which is horizontal. Have the two in TextViews inside it with equal 0.5 weight. Keep both left aligned. You may have the left_margin on the whole Layout.
<LinearLayout
android:orientation="horizontal"
android:margin_left="10dp"
>
<TextView
android:weight = "0.5"
/>
<TextView
android:weight = "0.5"
/>
</LinearLayout>
P.S: I am just showing the concept. You may complete the layout as you desire.
To add a margin to the left use this in the XML file :
android:layout_marginLeft="10dp"
You can use the following to centre the TextView :
android:layout_centerHorizontal="true"
To add the Count to the right of the name :
android:layout_toRightOf="#id/num_name"
I hope that helps.
Both LinearLayour or RelativeLayout can be used within the list.
Try this out..
It will work. I have tried this in my project.
<RelativeLayout
<TextView
android:id="#+id/num_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dip"
android:textColor="#000000"
android:paddingTop="15dip"
android:paddingBottom="15dip"
android:paddingLeft="0dip"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView android:id="#+id/num_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/num_name"
android:textColor="#ffffff"
android:textStyle="bold"
android:background="#9ed321"
android:paddingRight="13dip"
android:paddingLeft="13dip"
android:layout_alignBottom="#+id/cat_id"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
I have changed the layout_width from "fill_parent" to "wrap_content"
Give it a try. If it is still not working, let me know.
Try This
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:orientation="horizontal" >
<TextView
android:id="#+id/num_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.9"
android:gravity="left"
android:textColor="#000000"
android:textSize="16dip"
android:text="fgth"
android:textStyle="bold" />
<TextView
android:id="#+id/num_count"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:background="#9ed321"
android:gravity="right"
android:textColor="#ffffff"
android:textStyle="bold" />
</LinearLayout>

Unable to right-align text in Android ListView row layout

I am trying to design a ListView row layout with the following:
A check box
An image
3 text items. 2 at the top, 1 at the bottom. Where one of the top text items is right aligned.
An image
It would look something like this:
Chk Img Left aligned small text Right aligned small text Img
Box Btn1 Left aligned medium text Btn2
No matter what I try, I cannot get the right aligned text to work properly. If I put the 2 top text items into a RelativeLayout and set the 2nd one to android:layout_alignParentRight="true" it gets aligned to the edge of the row, overlaying the 2nd image. The following is one of my many attempts:
<?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="wrap_content">
<CheckBox
android:id="#+id/cbSelect"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_alignParentLeft="true"/>
<ImageView
android:id="#+id/imgType"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_toRightOf="#id/cbSelect"
android:src="#android:drawable/btn_star_big_on"/>
<LinearLayout
android:id="#+id/itmDetails"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_toRightOf="#id/imgType"
android:gravity="center_vertical"
android:orientation="vertical"
android:paddingLeft="8dp" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/txtTopLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:textSize="12sp"
android:text="Top Left"/>
<TextView
android:id="#+id/txtTopRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textColor="#android:color/black"
android:textSize="12sp"
android:text="Top Right"/>
</RelativeLayout>
<TextView
android:id="#+id/txtMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Main Item Text"
android:textColor="#android:color/black"
android:textSize="16sp" />
</LinearLayout>
<ImageView
android:id="#+id/imgStar"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_alignParentRight="true"
android:src="#android:drawable/btn_star"/>
</RelativeLayout>
Any suggestions?

Resources