How to set min/max lenght of Random Name created?

when I run this programm the emulator only gives me one letter when random name is generated by clicking a button. I want to set the minimum generated lenghtname of chars in my string by 4 and maximum by 8. So the button generates a random Name by min 4 and max 8 letters from the String. Cant find any solution.

public class MainActivity extends AppCompatActivity {
private Switch Switch;
private EditText Name;
private Button btn;
private String items[] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","1","2","3","4","5","6","7","8","9","0"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);





Switch = (Switch) findViewById(R.id.switch1);
    Switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            openActivity2();
        }
    });


    Name = findViewById(R.id.editTextTextPersonName);
    btn = findViewById(R.id.button);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Random rndm = new Random();
            int i = rndm.nextInt(items.length);
            Name.setText(items [i]);
        }
    });





}



public void openActivity2() {

    Intent intent = new Intent(this, MainActivity2.class);
    startActivity(intent);
}
}

Thanks in advance.

Answer

You are using Name.setText(items[i]) which is only setting one character from items array to Name that’s why you were getting one letter on clicking of button.

 public class MainActivity extends AppCompatActivity {
    private Switch Switch;
    private EditText Name;
    private Button btn;
    private String items[] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","1","2","3","4","5","6","7","8","9","0"};
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
    
    
    
    
    Switch = (Switch) findViewById(R.id.switch1);
        Switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                openActivity2();
            }
        });
    
    
        Name = findViewById(R.id.editTextTextPersonName);
        btn = findViewById(R.id.button);
    
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Random rndm = new Random();

                String out = "";

                // here length is will be a random number between 4 and 8 

                // this is used to get random number between 4(inclusive) and 9(exclusive)
                int low = 4;
                int high = 9;
                int length = rndm.nextInt(high-low) + low;
                
                for (int i=0;i<length;i++) {
                  int idx=rndm.nextInt(items.length);
                  out += items[idx];
                }
               
               
                Name.setText(out);


            }
        });
    
    
    
    
    
    }



public void openActivity2() {

    Intent intent = new Intent(this, MainActivity2.class);
    startActivity(intent);
}
}