Issue
I want to design
like this. how to set first charecter to that circle and should circle be in random color.
Solution
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);
Answered By - block king
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.