Difficulty understanding layout_alignWithParentIfMissing - android

I don't understand the meaning of layout_alignWithParentIfMissing for the TextView attribute.
I read the following documentation:
android:layout_alignWithParentIfMissing
If set to true, the parent will be used as the anchor when the anchor cannot be be found for layout_toLeftOf, layout_toRightOf, etc.
Must be a boolean value, either true or false.
Please explain to me.

This apply only when using RelativeLayout.
If You set element to be toLeftOf some other element it means it will be on the left of this element.
But if this element will be missing, because You delete it for example it will be align to parent.
Take this example
<?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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="102dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignWithParentIfMissing="true"
android:layout_alignBottom="#+id/textView1"
android:layout_toRightOf="#+id/textView1"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
See what happens If You delete textView1 but don't touch textView2.
It will go to the bottom, on left(not right because it is inside of parent)
Android will position element relative to parent instead of element it should be because it is missing.
If it is false parameters relating to missing elements will be ignored.

Another sample. I have just used.
<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"
android:padding="6dip" >
<Button
android:id="#+id/streamOverflow"
android:layout_width="50dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:background="#drawable/ic_action_overflow"
android:focusable="false" />
<TextView
android:id="#+id/delete_time"
style="?android:attr/textAppearanceSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Disappears "
android:layout_toLeftOf="#id/streamOverflow"
android:layout_alignWithParentIfMissing="true"
/>
</RelativeLayout>
if "streamOverflow" is missing, "delete_time" textview behaves like having android:layout_alignParentRight="true" property.

In Relative Layouts, the position of a View can be specified relative to another View.
The tags layout_toLeftOf and layout_toRightOf position the view relative to an 'anchor' view object. When this anchor View is missing, the tag alignWithParentIfMissing, will align your view with the parent view.

Related

LinearLayout selector doesn't cover the whole View's children

The following is my XML code, I need to make a selector for the LinearLayout, but when I tap on the child TextView it doesn't show the selector backgroud as tapping other areas of the LinearLayout!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:clickable="true"
android:focusable="true"
android:descendantFocusability="blocksDescendants"
android:background="?android:attr/selectableItemBackground">
<ImageView android:paddingLeft="35dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/chart"/>
<TextView android:paddingLeft="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/tv_title" />
<ImageView android:paddingRight="15dp"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/ic_star_border_black_48dp"
android:id="#+id/iv_bookmark" />
</LinearLayout>
Try this:
<TextView android:paddingLeft="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/tv_title"
android:clickable="false"
android:focusable="false" />
I think the reason for the error is because descendantFocusability is only for focusing state. It does not affect touch events. Since the TextView is still clickable it can still consume the touch event, and thus keep the parent from consuming the click.
Edit:
Try setting android:descendantFocusability to beforeDescendants instead of blocksDescendants.
Edit 2:
Changing the parent to android:addStatesFromChildren="true" and removing the clickable and focusable from the child TextView seemed to do the trick. This works because according to the ViewGroup android docs:
addStatesFromChildren: Sets whether this ViewGroup's drawable states also include its children's drawable states.
So when the child will set its state it also sets its parents state.
Try setting android:focusable and android:focusableInTouchMode to false in your TextView.

Elements superposed using Android RelativeLayout

I have the following Android layout
<?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:paddingBottom="16dp"
android:orientation="vertical" >
<TextView
android:id="#+id/slideTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:padding="#dimen/px20"
android:text="#string/login_message"
android:textSize="#dimen/px25"
android:textStyle="bold" />
<TextView
android:id="#+id/slideDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/password"
android:drawablePadding="#dimen/px20"
android:drawableStart="#drawable/password"
android:padding="#dimen/px20"
android:text="#string/login_message_body"
android:textSize="#dimen/px20" />
<TextView
android:id="#+id/swipeMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="" />
</RelativeLayout>
But all the elements are positioned at the top of the screen one on top of the other, like if they didn't occupy any space.
That's not what what it seems to happen in the RelativeLayout documentation, where all elements are vertically positioned one below the other.
What's going on here?
So you need to use the id of the other components to align then properly.
For example the TextView with the id #+id/slideDescription should also have
android:layout_below="#+id/slideTitle" as one of the properties of the xml.
And the TextView with the id #+id/swipeMessage should also have
android:layout_below="#+id/slideDescription" as one of the properties of the xml.
In order to place one view below another in RelativeLayout you have to use layout_below property and set the ID of View you want to be above the specified one. But actually in order to place views vertically below each other it is more convenient to use LinearLayout with orientation set to vertical
layout_below is missed in the above xml code.I replaced the code with that please use that.
In Relative layout elemnets will be arranged relative to other elements in order to do this we should use id values of individual view elments
android:layout_below="#id/slideTitle" should be placed in description text view
android:layout_below="#id/slideDescription" should be placed in message text view
in order to get the output you desired please use the below code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="16dp" >
<TextView
android:id="#+id/slideTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:padding="#dimen/px20"
android:text="#string/login_message"
android:textSize="#dimen/px25"
android:textStyle="bold" />
<TextView
android:id="#+id/slideDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/slideTitle"
android:drawableLeft="#drawable/password"
android:drawablePadding="#dimen/px20"
android:drawableStart="#drawable/password"
android:padding="#dimen/px20"
android:text="#string/login_message_body"
android:textSize="#dimen/px20" />
<TextView
android:id="#+id/swipeMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#id/slideDescription"
android:text="" />

Scrollview with Linear and Relative Layout is all boxed up, so I cannot reposition items

I am creating a ScrollView to house my relativelayout. The RelativeLayout seems to need to be inside a LinearLayout (from online). However it has gone all boxed up, with the items overlapping each other. This means I cannot reposition stuff. How can I fix this? My code is like this:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:scrollbarAlwaysDrawVerticalTrack="true"
tools:context=".AddParty" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/addparty_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="65dp"
android:onClick="switch_peoplechoice"
android:text="#string/addparty_switch" />
<Button
android:id="#+id/addparty_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/addparty_switch"
android:layout_alignLeft="#+id/addparty_switch"
android:layout_marginBottom="10dp"
android:onClick="cancel"
android:text="#string/addparty_cancel" />
<EditText
android:id="#+id/addparty_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/addparty_venue"
android:layout_marginTop="11dp"
android:ems="10"
android:hint="#string/addparty_title"
android:inputType="text" />
<EditText
android:id="#+id/addparty_venue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/addparty_title"
android:layout_marginTop="26dp"
android:ems="10"
android:hint="#string/addparty_venue"
android:inputType="text" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/addparty_venue"
android:layout_alignBottom="#+id/addparty_venue"
android:layout_marginLeft="13dp"
android:layout_toRightOf="#+id/addparty_venue"
android:text="TextView" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/addparty_venue"
android:layout_alignLeft="#+id/textView2"
android:text="TextView" />
<TimePicker
android:id="#+id/addparty_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/addparty_venue"
android:layout_below="#+id/addparty_venue"
android:layout_marginTop="16dp" />
<DatePicker
android:id="#+id/addparty_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/addparty_time"
android:layout_below="#+id/addparty_time"
android:layout_marginTop="19dp" />
</RelativeLayout>
And like all properly filled out. I actually just inserted the LinearLayout and ScrollView around my relativelayout I already had. Is there a better solution to needing a RelativeLayout to scroll, or what am I doing wrong.
It get's all bunched up in the UI viewer in Eclipse...
Thanks for the help!!
Your items are overlapping one another in the RelativeLayout as you most likely did not specifiy the positions of each item individualy, though it is difficult to determine without seeing the entire layout xml.
Please read http://developer.android.com/guide/topics/ui/layout/relative.html for more information on how RelativeLayouts work.
As far as the XML is concerned, it seems that their is a conception mistake on how your Views are related to each others.
Let's start with the RelativeLayout that has a wrap_content height. No problem on that but it must be noted.
The first element inside this RelativeLayout is positioned at the bottom of the RelativeLayout. Still no problem on this point.
Thing is, the second View is positioned ABOVE the first one, this means that the height of the RelativeLayout is getting bigger when the second View is inserted, but the first View should be at the bottom of the layout so it is repositioned again thus overlap with the second View.
Can you try to position the first View with layout_alignParentTop="true" ?
Maybe this will fixe your problem.

Relative layout - Right justify the data in textview

I am using the following xml to generate a row in a list view. Though i have set the gravity as center_Vertical and right. Only center_Vertical is applied but not the "right" gravity. I have even tried inside the code. the "right" gravity is not working. Kindly help me to correct the code.
Thanks in Advance
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tvTicketID"
android:layout_width="wrap_content"
android:layout_height="52dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:gravity="center_vertical|left"
android:text="Ticket ID"
android:textSize="15dp" />
<TextView
android:id="#+id/tvCreationHour"
android:gravity="center_vertical|right"
android:layout_width="wrap_content"
android:layout_height="52dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="40dp"
android:layout_toRightOf="#+id/tvTicketID"
android:text="Hour" />
<TextView
android:id="#+id/tvTableNumber"
android:gravity="center_vertical|right"
android:layout_width="wrap_content"
android:layout_height="52dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="24dp"
android:layout_toRightOf="#+id/tvCreationHour"
android:text="Table #" />
<TextView
android:id="#+id/tvAmount"
android:gravity="center_vertical|right"
android:layout_width="wrap_content"
android:layout_height="52dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_toRightOf="#+id/tvTableNumber"
android:text="Amount" />
</RelativeLayout>
The code I use to create a row using the above xml
holder.tvTicketID.setText(String.valueOf(ticket.getId())) ;
holder.tvCreationHour.setText(String.valueOf(ticket.getCreationHour())) ;
holder.tvTableNumber.setText(String.valueOf(ticket.getTableNumber())) ;
holder.tvAmount.setText(String.valueOf(ticket.getTotalAmount())) ;
Try setting android:layout_gravity="right"
In general, android:gravity="right" is different from android:layout_gravity="right".
The first one affects the position of the text itself within the View, so if you want it to be right-aligned, then layout_width= should be either "fill_parent" or "match_parent".
The second one affects the View's position inside its parent, in other words - aligning the object itself (edit box or text view) inside the parent view.
Finally, I fonund the answer. I have changed the android:layout_width="wrap_content" in the TEXTVIEW to android:layout_width="30dp". Now it works..

layout_toRightOf works and layout_toLeftOf doesn't, how to solve?

I have simple relative layout - an ImageView (iv1) and a TextView (tv1) to the left of iv1. Unfortunately, I see no tv1. What is more, even hierarchyViewer can't help me find it.
<?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"
android:gravity="right"
android:background="#android:color/white"
>
<ImageView
android:id="#+id/iv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/tv1"
android:layout_toLeftOf="#+id/iv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</RelativeLayout>
It looks like that:
However, if I change android:layout_toLeftOf="#+id/iv1" to android:layout_toRightOf="#+id/iv1", my text view becomes positioned to right of image view. It seems like toRightOf works and toLeftOf doesn't.
Here how it looks:
What is the matter? How to make layout_toLeftOf work?
Change:
<TextView
android:id="#+id/tv1"
android:layout_toLeftOf="#+id/+iv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
to:
<TextView
android:id="#+id/tv1"
android:layout_toLeftOf="#id/iv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
You are using #+id/iv1 in your toLeftOf in the TextView instead of #id/iv1.
Also you should give the imageview a specific place in the view. Try to align it: layout_parentTop = true
set imageview property align_parent_right= true;
In my case (not in this layout) a problem was in android:layout_width of RelativeLayout. When it was wrap_content or match_parent, a layout became scattered. When I set it as, for instance, 300dp, everything was good.

Categories

Resources