Tell me how to implement such a design element in Android? (exactly the white figure is interested).

At first I thought, just make an ImageView and display text on top of it, but the fact is that the text will come from the server and it can be of different lengths, therefore it is necessary that the figure expand or become smaller depending on the length of the text. Can I draw this shape with onDraw() ?

enter image description here

    1 answer 1

    Maybe I did not quite understand the question, but it's very simple.

    drawable / round_rectangle.xml

     <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#90CAF9"/> <corners android:radius="10dp"/> 

    Further:

     <?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:padding="16dp"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="16dp" android:background="@drawable/round_rectangle"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." </RelativeLayout> </LinearLayout> 

    As a result, you get:

    enter image description here

    UPD. Programmatically:

     RelativeLayout relativeLayout = new RelativeLayout(this); relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT)); relativeLayout.setPadding(16, 16, 16, 16); relativeLayout.setBackground(getDrawable(R.drawable.round_rectangle)); setContentView(relativeLayout); TextView textView = new TextView(this); textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); textView.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."); relativeLayout.addView(textView); 
    • Oh, it seems, and if I need to dynamically create these rectangles in the Activity? - Lucky_girl
    • @Lucky_girl, Look UPD (but it's much easier to do all this in a separate markup file). - s8am
    • And another question, for example, I would need to dynamically create several such rectangles on one screen and what would they be under one another, how to do it correctly? Create a loop? - Lucky_girl
    • 2
      @Lucky_girl; Try using RecyclerView and, for example, CardView . With the help of RecyclerView you can organize some list, the elements of which will be CardView - your rectangles. - s8am
    • It's a good idea, thanks! - Lucky_girl