Search code examples
androidviewalignmentandroid-relativelayout

views won't align right in RelativeLayout (width from view set in java code)


I have an issue with my android app. I'm setting the width of three views at the same size as three buttons above of the views. The views are indicators and are showing, which button is pressed. Maybe not the smartest solution, but I'm just the trainee...

The Code from my XML is:

<RelativeLayout
        android:id="@+id/statusIndicatorTop"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/status_indicator_height"
        android:background="@color/colorPrimaryDark"
        android:layout_below="@+id/toolBar"
        android:orientation="horizontal"
        android:gravity="right">

        <View
            android:id="@+id/statusPreise"
            android:layout_width="0dp"
            android:layout_height="@dimen/status_indicator_height"
            android:background="@color/colorAccent"
            android:visibility="invisible"
            android:layout_alignLeft="@id/statusShops" />

        <View
            android:id="@+id/statusShops"
            android:layout_width="0dp"
            android:layout_height="@dimen/status_indicator_height"
            android:background="@color/colorAccent"
            android:visibility="invisible"
            android:layout_alignLeft="@id/statusKategorie"/>

        <View
            android:id="@+id/statusKategorie"
            android:layout_width="0dp"
            android:layout_height="@dimen/status_indicator_height"
            android:background="@color/colorAccent"
            android:visibility="invisible"
            android:layout_alignParentRight="true" />

    </RelativeLayout>

My JAVA Code in OnCreate is:

@Override
    public void onStart() {
        super.onStart();

        fragmentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {

                fragmentView.getViewTreeObserver().removeGlobalOnLayoutListener(this);

                Button btnKat = (Button) fragmentView.findViewById(R.id.button_Kategorie);
                int btnKatWidth = btnKat.getWidth();
                RelativeLayout.LayoutParams paramsKat = (RelativeLayout.LayoutParams) statusKategorie.getLayoutParams();
                paramsKat.width = btnKatWidth;
                statusKategorie.setLayoutParams(paramsKat);

                Button btnShops = (Button) fragmentView.findViewById(R.id.button_Shops);
                int btnShopsWidth = btnShops.getWidth();
                RelativeLayout.LayoutParams paramsShops = (RelativeLayout.LayoutParams) statusShops.getLayoutParams();
                paramsShops.width = btnShopsWidth;
                statusShops.setLayoutParams(paramsShops);

                Button btnPreise = (Button) fragmentView.findViewById(R.id.button_Search);
                int btnPreiseWidth = btnPreise.getWidth();
                RelativeLayout.LayoutParams paramsPreise = (RelativeLayout.LayoutParams) statusSuche.getLayoutParams();
                paramsPreise.width = btnPreiseWidth;
                statusSuche.setLayoutParams(paramsPreise);
            }
        });
    }

In LOG I get this error messages:

Error:(185, 39) No resource found that matches the given name (at 'layout_alignLeft' with value '@id/statusShops').
Error:(193, 39) No resource found that matches the given name (at 'layout_alignLeft' with value '@id/statusKategorie').

When I'm not using alignLeft, on the first and second view, my app is starting and the third indicator is doing what I want. I can imagine why the app isn't working, but I can't fix it with my lack of experience. I hope it's enough code to solve my problem.

Thank you in advance.


Solution

  • The order it's important, you can't refer to a view that hasn't been drawn yet (i.e you can't do layout_aling=@id/statusKategorie if statusKategory hasn't been drawn yet). Try to reorder your declarations in the xml, for example like this:

    <RelativeLayout
        android:id="@+id/statusIndicatorTop"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/status_indicator_height"
        android:background="@color/colorPrimaryDark"
        android:layout_below="@+id/toolBar"
        android:orientation="horizontal"
        android:gravity="right">
    
        <View
            android:id="@+id/statusKategorie"
            android:layout_width="0dp"
            android:layout_height="@dimen/status_indicator_height"
            android:background="@color/colorAccent"
            android:visibility="invisible"
            android:layout_alignParentRight="true" />
       <View
            android:id="@+id/statusShops"
            android:layout_width="0dp"
            android:layout_height="@dimen/status_indicator_height"
            android:background="@color/colorAccent"
            android:visibility="invisible"
            android:layout_alignLeft="@id/statusKategorie"/>
       <View
            android:id="@+id/statusPreise"
            android:layout_width="0dp"
            android:layout_height="@dimen/status_indicator_height"
            android:background="@color/colorAccent"
            android:visibility="invisible"
            android:layout_alignLeft="@id/statusShops" />
    </RelativeLayout>