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

前端 未结 4 621
粉色の甜心
粉色の甜心 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 05:09

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

    create profile_bg.xml

    
    
    
        
    
        
    
    

    then main_activity.xml

    
    
    
        

    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());
            }
        }
    

提交回复
热议问题