I want to design
like this. how to set first charecter to that circle and should circle be in random color.
Asked
Active
Viewed 67 times
2 Answers
0
TRY this Change color as per your requirement.
you will add colors programmatically.
and text add pragmatically using this link
Textview -
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/ic_baseline_circle_24"
android:gravity="center"
android:maxLength="1"
android:padding="10dp"
android:text="M"
android:textColor="@android:color/white"
android:textSize="12sp"
android:textStyle="normal" />
add in res/drawable/ic_baseline_circle_24.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="#41B582"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M12,2C6.47,2 2,6.47 2,12s4.47,10 10,10 10,-4.47 10,-10S17.53,2 12,2z" />
</vector>

Sandesh Khutal
- 1,712
- 1
- 4
- 17
-
yeah I tried thisbut problem is that im not able to set this first charecter to the imageview. – Shweta S May 22 '22 at 04:07
0
You can't directly set a text to image view but you can use the Relative layout and Text-View to do so. let's see how.
This is the result:-
This is the code
here is the code for your xml layout file
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardCornerRadius="100dp"
app:cardBackgroundColor="@color/transparent">
<RelativeLayout
android:layout_width="150dp"
android:layout_height="150dp">
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@null"
android:background="@color/grey"
/>
<TextView
android:id="@+id/person_name_first_letter_txt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="K or whatever your person name is"
android:maxLength="1"
android:textColor="@color/white"
android:textSize="80dp"
android:gravity="center"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
here is the array of colors for picking a random color in colors.xml
<array name="random_color_array">
<item>#37BFA8</item>
<item>#F50057</item>
<item>#3D5AFE</item>
<item>#FF3D00</item>
<!-- add more colors according to your need-->
</array>
Now here is the code for the activity in java
TextView name_text = findViewById(R.id.person_name_first_letter_txt);
ImageView background_image = findViewById(R.id.img);
String user_name = "John Doe";
String[] colorsTxt = getApplicationContext().getResources().getStringArray(R.array.allcolors);
List<Integer> colors = new ArrayList<Integer>();
for (int i = 0; i < colorsTxt.length; i++) {
int newColor = Color.parseColor(colorsTxt[i]);
colors.add(newColor);
}
Random random = new Random();
int color = colors[random.nextInt(colors.length)];
background_image.setBackgroundColor(getResources().getColor(color));
name_text.setText(user_name);

Nimantha
- 6,405
- 6
- 28
- 69

block king
- 68
- 6