Home > Mobile >  Get text length in characters number Android Java
Get text length in characters number Android Java

Time:01-29

I have a text box (EditText) where I will insert text, and a button that should calculate the lentgh of that text and display it in the another text box. How should I code that button?

CodePudding user response:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button b1=findViewById(R.id.b1);
    Button b2=findViewById(R.id.b2);
    EditText t1=findViewById(R.id.t1);
    TextView t2;
    t2 = findViewById(R.id.t2);


    b1.setOnClickListener(view -> {
        int nb=0;
        for(int i=0; i<t1.length();   i){
            nb  ;
        }
        t2.setText(Integer.toString(nb));
    });

    b2.setOnClickListener(view -> {
        t2.setText(t1.getText().toString());
    });
}
  •  Tags:  
  • Related