how to make thumbnail image with initials two char from name android?

前端 未结 4 618
粉色の甜心
粉色の甜心 2021-01-06 04:14

I want to thumbnail initials with two word for my image view like \"Peter Parker\" but am able to get only one word \"P\"while running code how can get second word after spa

相关标签:
4条回答
  • 2021-01-06 04:54

    Hi you can using following way

    String str = "FirstWord SecondWOrd";
            String[] strArray = str.split(" ");
            StringBuilder builder = new StringBuilder();
            if (strArray.length > 0)
                builder.append(strArray[0], 0, 1);
            if (strArray.length > 1)
                builder.append(strArray[1], 0, 1);
            Log.d("New Text" , builder.toString());
    
    0 讨论(0)
  • 2021-01-06 05:00

    You can do it functional way:

    val peterParker = "Peter Parker"
    
    val initials = peterParker
            .split(' ')
            .mapNotNull { it.firstOrNull()?.toString() }                     
            .reduce { acc, s -> acc + s }
    
    println(initials) //PP
    

    This would cover cases when a person's name consists of more than 2 words.

    0 讨论(0)
  • 2021-01-06 05:09

    I have done some Trick & implemented this avatar with a Button lol ;p

    create profile_bg.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
    
        <solid
            android:color="@color/colorWhite"/>
    
        <corners
            android:radius="500dp"/>
    </shape>
    

    then main_activity.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#4300313A"
        tools:context=".MainActivity">
    
        <Button
            android:onClick="clicked"
            android:id="@+id/avatar"
            android:clickable="false"
            android:focusable="false"
            android:textColor="@color/colorPrimary"
            android:textSize="65sp"
            android:focusableInTouchMode="false"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:background="@drawable/profile_bg"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="50dp"/>
    
        <EditText
            android:id="@+id/edtname"
            android:layout_below="@+id/avatar"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="50dp"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:hint="Enter your name"/>
    
        <Button
            android:onClick="clicked"
            android:textColor="@color/colorBackground"
            android:text="Submit Name"
            android:textStyle="bold"
            android:focusableInTouchMode="false"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_below="@+id/edtname"
            android:layout_marginTop="50dp"/>
    
    </RelativeLayout>
    

    then in MainActivity.java (to split the string and get the first letter of each word ~ name in if condition with stringbuilder)

        public class MainActivity extends AppCompatActivity {
    
            EditText editText;
            Button button;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
    
                editText = (EditText) findViewById(R.id.edtname);
                button = (Button) findViewById(R.id.avatar);
    
            }
    
            public void clicked(View view) {
    
                String str = editText.getText().toString();
    
                String[] strArray = str.split(" ");
                StringBuilder builder = new StringBuilder();
    
    //First name
                if (strArray.length > 0){
                    builder.append(strArray[0], 0, 1);
                }
    //Middle name
                if (strArray.length > 1){
                    builder.append(strArray[1], 0, 1);
                }
    //Surname
                if (strArray.length > 2){
                    builder.append(strArray[2], 0, 1);
                }
    
                button.setText(builder.toString());
            }
        }
    
    0 讨论(0)
  • 2021-01-06 05:11

    it look's like your using substring to only grab the letters from position 0 to position 1, This is P in Petter

    holder.imgName?.text=teamData[position].userImage  
    

    .substring(0,1)

    If you'd like to grab the words Petter Parker, you have a few options.
    • IndexOf & Substring - find the position of a string and get the subtext after.
    • Substring - Subtext of string based on parameters

    If you plan to change the text length at any stage, you'll need to find the start of the word ( int start = yourString.indexOf("Petter"));
    and end of the word ( int end = yourString.indexOf(" "))

    IndexOf will return the position of the first letter in your query - Your case it's P in Petter --- So start+"petter".length()

    Here's an example of a barcode price checker app I'm working on

                        // STRING  FORMAT 00000899999:0.99   
    
                        String currentLine = "00000899999:0.99";
                        int breakPoint = currentLine.indexOf(":");
                        int end = currentLine.length();
                        int start = breakPoint + 1;
                        String Price = currentLine.substring(start,end);
    

    Price will be starting after (:) With +1 or include (:) with no + 1 and end at the lines length.

    0 讨论(0)
提交回复
热议问题