3

i want to create a view like this i have posted in screen shot, in circle shape of layout with one image view with some background colour and one text view just below the image view with white background and the parent of the complete should be in blue colour as shown in picture, i have tried but not able to get the result I will post my code below, please guide me? my required view is enter image description here

and i am getting this output with the layout i created enter image description here

my layout code is

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/parent_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#202230"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <RelativeLayout
            android:id="@+id/circle_layout"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/whitecircle" >

            <RelativeLayout
                android:id="@+id/circle_layoutinner"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_above="@+id/rating_viewtv"
                android:layout_alignParentTop="true"
                android:background="@drawable/circletwo"
                android:layout_marginTop="1dp"
                android:layout_centerHorizontal="true" >

                <TextView
                    android:id="@+id/ratingcup_viewtv_fonts"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:text="M"
                    android:gravity="center"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:textColor="@android:color/holo_purple" />
            </RelativeLayout>

            <View android:id="@+id/seprater_viewtv"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_above="@+id/rating_viewtv"
                android:background="#2b2c3a" />

            <TextView
                android:id="@+id/rating_viewtv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="3dp"
                android:text="4.5"
                android:textColor="@android:color/holo_purple" />
        </RelativeLayout>


        </LinearLayout>
    </LinearLayout>

</LinearLayout>

and my whitecircle.xml is 
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadius="0dp"
    android:shape="ring"
    android:thicknessRatio="2"
    android:useLevel="false">

    <solid android:color="@color/white" />

</shape>

and my circletwo.xml is
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadius="0dp"
    android:shape="ring"
    android:thicknessRatio="2"
    android:useLevel="false">

    <solid android:color="#ff9546" />

</shape>
Achin
  • 1,252
  • 4
  • 28
  • 63

2 Answers2

3

Change your declaration of the circle_layoutinner RelativeLayout to specify a height in dp instead of wrap_content and get rid of the marginTop:

<RelativeLayout
    android:id="@+id/circle_layoutinner"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:layout_above="@+id/rating_viewtv"
    android:layout_alignParentTop="true"
    android:background="@drawable/circle_inset_drawable"
    android:layout_centerHorizontal="true" >

define circle_inset_drawable.xml to offset the orange circle by the correct amount:

<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/circletwo"
    android:insetTop="20dp"
    android:visible="true" />

insetTop should be the height of circle_layout minus the height of circle_layoutinner

You can set the color of the drawable in code like this. You just need to start with your layout object and then keep on burrowing down through the objects until you get to the one that lets you set the color:

RelativeLayout rl = (RelativeLayout)findViewById(R.id.circle_layoutinner);
InsetDrawable id = (InsetDrawable)rl.getBackground();
GradientDrawable gd = (GradientDrawable)id.getDrawable(); // API 19+ only!
gd.setColor(0xffff0000);   // set to red

Or you can create the InsetDrawable in code like this:

RelativeLayout rl = (RelativeLayout)findViewById(R.id.circle_layoutinner);
GradientDrawable gd = (GradientDrawable)getResources().getDrawable( R.drawable.circletwo );
gd.setColor(0xffff0000);   // set to red
int dpInPixels = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, getResources().getDisplayMetrics());
InsetDrawable id = new InsetDrawable(gd, 0, dpInPixels, 0, 0);
rl.setBackground(id);
samgak
  • 23,944
  • 4
  • 60
  • 82
  • 2
    Wow nice one. Never knew about inset. Thank You :-) – Dhinakaran Thennarasu Jan 16 '15 at 06:47
  • it is working for me, but one more query is it possible to change the orange colour to dynamically, i mean in my case it is not static, colours are coming from web service ? any help on this ? – Achin Jan 16 '15 at 06:55
  • i am running on ICS 4.0 and it is giving me error java.lang.NoSuchMethodError: android.graphics.drawable.InsetDrawable.getDrawable ? – Achin Jan 16 '15 at 07:36
  • ah I see, that requires API 19 apparently: http://developer.android.com/reference/android/graphics/drawable/InsetDrawable.html#getDrawable() In that case you'll have to create the drawables in code – samgak Jan 16 '15 at 07:38
  • hi i have never created the drawables using code,i don't know how to use this and please can u explore your comment a bit more for below 19 api? – Achin Feb 09 '15 at 04:58
  • see the edit to the answer. the last code snippet shows how to do it – samgak Feb 09 '15 at 06:00
  • it dosent work. if you change the height of outer circle to 160 dp & inner circle to 120 dp with inset as 40 dp. it gives this image https://www.dropbox.com/s/py7bxhxv37kv79r/Screenshot_2016-12-30-14-51-14.jpg?dl=0 . Please help. – Ankit Ostwal Dec 30 '16 at 11:01
  • hi @samgak it dosent work. if you change the height of outer circle to 160 dp & inner circle to 120 dp with inset as 40 dp. it gives this image https://www.dropbox.com/s/py7bxhxv37kv79r/Screenshot_2016-12-30-14-51-14.jpg?dl=0 . Please help – Ankit Ostwal Jan 03 '17 at 05:15
  • @AnkitOstwal I don't think I can debug it just from looking at an image. I don't even know if you're using XML or code – samgak Jan 03 '17 at 05:19
  • hi @samgak using xml just . this link http://stackoverflow.com/questions/41393750/create-customise-button-with-image-at-top-and-text-at-bottom/41393884?noredirect=1#comment69990565_41393884 – Ankit Ostwal Jan 03 '17 at 05:27
  • hi @samgak it would be great if you can help me out of this. Cant figure out since last 3 days. – Ankit Ostwal Jan 03 '17 at 05:34
  • hi @Achin , it dosent work. if you change the height of outer circle to 160 dp & inner circle to 120 dp with inset as 40 dp. it gives this image https://www.dropbox.com/s/py7bxhxv37kv79r/Screenshot_2016-12-30-14-51-14.jpg?dl=0 . Please help. I have posted my question here http://stackoverflow.com/questions/41393750/create-customise-button-with-image-at-top-and-text-at-bottom/41393884?noredirect=1#comment69990565_41393884 . Just using XML. Help is appreciated thanks. – Ankit Ostwal Jan 03 '17 at 05:43
  • @AnkitOstwal remove android:layout_marginTop="10dp" from circle_layout RelativeLayout – samgak Jan 03 '17 at 06:35
  • @samgak that dosent help. no change in output view. – Ankit Ostwal Jan 03 '17 at 07:02
  • @AnkitOstwal try changing imageview parameters to match the textview parameters in this example. For e.g. width is wrap_content and height is match parent, not the other way around. Also, can you get it to work for text before trying an image? – samgak Jan 03 '17 at 07:08
  • @samgak ya achieved it but i have to keep textview height as constant. (37dp) , then its doing well. But if i make it as wrap_content it misbehaves. – Ankit Ostwal Jan 03 '17 at 07:50
0

Try to create the image with orange n white colour u expected.

set it as background to LL or RL

Simply use an ImageView and Textview with appropriate dimension to display the info.

kevz
  • 2,727
  • 14
  • 39
  • the orange colour is not static , it has to changed , i can not make it as static image. any other idea? – Achin Jan 16 '15 at 06:29