How to create a on-screen keyboard using java with different rows

this is the screenshot of the current app so that is the code that i currently have, but its only for a single line from A-Z using horizontal scroll, can somebody tell me how to convert it into QWERTY layout with different rows for each set of lines.

 String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";     for (int i = 0; i < chars.length(); i++) {         final Button charBtn = new Button(this);         charBtn.setText(String.valueOf(chars.charAt(i)));         charBtn.setTextColor(getResources().getColor(android.R.color.white));         charBtn.setBackgroundResource(R.drawable.background_button);         charBtn.setLayoutParams(new LinearLayout.LayoutParams(100, 100));         dialogCharView.addView(charBtn);                   charBtn.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 String query = searchQueryTxt.getText().toString() + charBtn.getText();                 searchQueryTxt.setText(query);      
Add Comment
1 Answer(s)

So you haven’t stated whether you are using awt, swing, or javafx. And there appears to be some code missing. I’ve just created a JavaFX project and made a similar set up.

I would add another String variable with "QWERTYUIOP…", this way you can switch between layouts by via the variable.

    String abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";     String qwerty = "QWERTYUIOPASDFGHJKLZXCVBNM"; 

So since your keys are on the one line, you need set their layout conditionally – allowing only 10 on the first row, 9 on second row, etc. This will give you the look of most keyboards.

Here’s my logic:

    // column variable - will be incremented each for loop iteration     //  for positioning X layout     double c = 0;      // row variable     //  for positioning Y layout     double rowOffset = 0;      for (int i = 0; i < qwerty.length(); i++) {          // change our column/row variables - 10 keys on top row,         if (i == 10) {             rowOffset += 30;             // 0.5 will give the appearance of the A key between Q and W             c = 0.5;         // 9 keys on second row         } else if (i == 19) {             rowOffset += 30;             c = 1.5;         }          final Button charBtn = new Button();         charBtn.setText(String.valueOf(qwerty.charAt(i)));         // use our column and row variables to set key position         charBtn.setLayoutX(c * 30);         charBtn.setLayoutY(rowOffset);          // set Button click event here         //  charBtn.setOnMouseClicked() ... ;          // add to your view port         anchorPane.getChildren().add(charBtn);          // increment our column variable         c++;     } 

Set the button sizes to a constant to make it look better, and play around with the ‘c’ variable in the IF / ELSE IF blocks to set the indentation.

layout of the keyboard

Answered on July 16, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.