Search code examples
androidxmlandroid-layoutimage-clipping

Android layout clipping


I am trying to make a layout as described below. I don't understand how to exactly implement the layout. enter image description here

The layout and the above hexagon will have some text that i need to change dynamically from code.

I have tried a similar way mentioned here

But the hexagon is still clipping. I'm using the following code.

  <RelativeLayout
        android:id="@+id/Llfirstamount"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:background="@color/layoutcolor1"
        android:clickable="true"
        android:clipChildren="false" >

        <TextView
            android:id="@+id/my_first_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:android:layout_alignParentBottom="true"
            android:android:layout_alignParentLeft="true"
            android:padding="15dp"
            android:text="Amount"
            android:textColor="@android:color/white" />

        <TextView
            android:id="@+id/my_second_text"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="-20dp"
            android:background="@drawable/hexagon"
            android:clipToPadding="false"
            android:contentDescription="@string/contentdesc_peso_logo"
            android:gravity="center"
            android:text="x5" />
    </RelativeLayout>

I don't know is this the only way or the right way or there is a much better way to implement this.I'm very confused. Please Help!!! thank you..

EDIT:

Ok thanks for your answers using two text views with custom background is is a good and clean idea. Now I'm using the edited code but the hexagon is clipping like below.. enter image description here

Am i missing something i have also added

android:clipToPadding="false"

and

android:clipChildren="false"  

in my code.


Solution

  • This will do it..

    <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="match_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"
        tools:context=".MainActivity">
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:background="#ccc"
            android:gravity="center_vertical"
            android:layout_centerInParent="true"
            android:padding="5dp"
            android:text="@string/hello_world" />
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/textView2"
            android:layout_marginLeft="-25dp"
            android:layout_marginBottom="-25dp"
            android:layout_toRightOf="@+id/textView2"
            android:background="@drawable/ic_launcher" />
    
    
    </RelativeLayout>
    

    OUTPUT: enter image description here

    Good Luck :)