Aharon Manne
Aharon Manne

Reputation: 712

alignParentRight in xml not working

I have a generic layout, to which I attach fragments:

<?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:background="@drawable/back"
     android:orientation="vertical" >

<include 
    android:id = "@+id/login_header"
    layout="@layout/header_layout"/>

<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

I have a fragment which I want to display on the right side of the screen.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="@dimen/two_third_width"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_margin="@dimen/small_margin"
android:background="@drawable/stock_price_bg"
tools:context=".FoundItemFragment" >

</RelativeLayout>

The layout is displayed left-justified rather than right justified. When I color the fragment_container FrameLayout, I see that it takes the entire width of the screen. I attach the fragment with the following code in my "FoundItemFragment extends Fragment implements OnClickListener" class:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View fragView = null;
    try {
        fragView = inflater.inflate(layoutID, container, false);
        //LoadData(fragView);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return fragView;
}

The container parameter passed to this routine has the id of the fragment_container layout, so I am not attaching it to some other object which might have settings that override the alignParentRight setting. Why is the fragment displayed left-justified rather than right-justified?

Upvotes: 0

Views: 3260

Answers (3)

Gaurav Gupta
Gaurav Gupta

Reputation: 4701

<?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:background="@drawable/back"
     android:orientation="vertical" >

<include 
    android:id = "@+id/login_header"
    layout="@layout/header_layout"/>

<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true" />

</RelativeLayout>

Since generic layout hold the container of the fragment, you should align/place the fragment there. Placement of the views of the fragment should be done in fragment specific xml file

Upvotes: 0

youssefhassan
youssefhassan

Reputation: 1065

Just edit your container xml to

<?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:background="@drawable/back" >

<include 
    android:id = "@+id/login_header"
    layout="@layout/header_layout"
    android:layout_alignParentTop = "true"/>

<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:below = "@id/login_header"
    android:layout_alignParentRight="true"/>

</LinearLayout>

Upvotes: 1

Viswanath Lekshmanan
Viswanath Lekshmanan

Reputation: 10093

The layout parent should be Relative Layout so that alignParentRight will work .

You are using the alignParentRight for a root element RelativeLayout

Upvotes: 2

Related Questions