I want to design
like this. how to set first charecter to that circle and should circle be in random color.
CodePudding user response:
TRY this Change color as per your requirement.
you will add colors programmatically.
and text add pragmatically using this
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);
If this works for you then please accept the answer