matghazaryan
matghazaryan

Reputation: 5896

how to set border to layout

I want set a border to Relativelayout. I make relativelayout clickable and use selector for changing selected color, also I use shape for rounding corners of layout. But when I select the layou the shape takes all background including the border. I can't find a way for solving this problem.

This is my main.xml

<RelativeLayout android:id="@+id/map_refresh"
                android:layout_height="fill_parent" android:layout_width="wrap_content" 
                android:background="@drawable/settings_selector_up" 
                android:padding="15dp"
                android:layout_marginLeft="15dip" android:layout_marginRight="15dip" android:layout_marginTop="5dip">
                <TextView android:id="@+id/Text1" 
                    android:layout_height="wrap_content" android:layout_width="wrap_content"
                    android:text="Map refresh period">
                </TextView>
                <TextView android:id="@+id/TextView09" 
                    android:layout_height="wrap_content" android:layout_width="wrap_content"
                    android:text="1 min" 
                    android:layout_alignParentRight="true"
                    android:paddingRight="5dp">
                </TextView>
            </RelativeLayout>
</LinearLayout>

this is my settings_selector_up.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Non focused states -->
    <item android:state_focused="false" android:state_selected="false"
        android:state_pressed="false" android:background="@drawable/settings_border_roundup" />
    <item android:state_focused="false" android:state_selected="true"
        android:state_pressed="false" android:drawable="@drawable/settings_border_roundup" />

    <!-- Focused states -->
    <item android:state_focused="true" android:state_selected="false"
        android:state_pressed="false" android:drawable="@drawable/settings_border_roundup" />
    <item android:state_focused="true" android:state_selected="true"
        android:state_pressed="false" android:drawable="@drawable/settings_border_roundup" />

    <!-- Pressed -->
    <item android:state_selected="true" android:state_pressed="true"
        android:drawable="@drawable/selector_pressed" > 
        </item>
    <item android:state_pressed="true" android:drawable="@drawable/list_selector_pressed" />
</selector>

And this is my selector_pressed

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#E6E6E6"></solid>
    <corners android:radius ="10dp" />
</shape>

Is there a way receive same result as we go for example using listselector in listView (in listview when I select the listitem only background color changed but border stayed unchanged).

Upvotes: 3

Views: 11007

Answers (1)

Zsombor Erdődy-Nagy
Zsombor Erdődy-Nagy

Reputation: 16914

Put the whole thing in another parent layout that handles the drawing of the border, like this:

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="your background 9-patch or shape of the border"
    android:padding="some padding value so the border looks ok">

    < Your RelativeLayout without the border stuff >

</FrameLayout>

Upvotes: 10

Related Questions