0

I have the code as below, and I need 2 things: I wish to put tv[i] on the same line as txt[i] and I want keyboard to appear after touching tv[i].

I'm a complete beginner to Android. Thank you.

public void click2(View view) {
        Button button3 = findViewById(R.id.button2);
        button3.setText("hello");
       // Button[] buttons = new Button[10](this);
        /*
       LinearLayout linearLayout = (LinearLayout) findViewById(R.id.activity_main);
        Button txt1 = new Button(this);
       // linearLayout.setBackgroundColor(Color.TRANSPARENT);
        linearLayout.addView(txt1);
*/

        Integer.toBinaryString(12);

        // Put buttons into an array
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.activity_main);
       // GridLayout gridLayout=(GridLayout) findViewById(R.id.activity_main);

        // Put buttons into an array
     //   Button[] txt = {new Button(this), new Button(this)};
        Button[] txt = new Button[8];
        TextView[] tv = new TextView[8];

        // loop over all values of i between 0 to the end of the button array
        for (int i = 0; i < txt.length; i = i + 1) {
            // Access array elements by index so txt1 is txt[1], etc.
       txt[i]=new Button(this);
       txt[i].setText(Integer.toBinaryString(i));
            linearLayout.addView(txt[i]);
            tv[i]=new TextView(this);

            linearLayout.addView(tv[i]);

        }

        };

MAYBE I'm putting your line linearLayout.setOrientation(LinearLayout.HORIZONTAL); at a wrong place.

KOD MainActivity.java

public void click2(View view) {
        Button button3 = findViewById(R.id.button2);
        button3.setText("hello");

        // Put buttons into an array
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.activity_main);
       // GridLayout gridLayout=(GridLayout) findViewById(R.id.activity_main);

        // Put buttons into an array
     //   Button[] txt = {new Button(this), new Button(this)};
        Button[] txt = new Button[8];
        TextView[] tv = new TextView[8];

        // loop over all values of i between 0 to the end of the button array
        for (int i = 0; i < txt.length; i = i + 1) {
            linearLayout.setOrientation(LinearLayout.HORIZONTAL);

            // Access array elements by index so txt1 is txt[1], etc.
       txt[i]=new Button(this);
       txt[i].setText(Integer.toBinaryString(i));
            linearLayout.addView(txt[i]);
            tv[i]=new TextView(this);

            linearLayout.addView(tv[i]);


        }

        };
  • `LinearLayout linearLayout = (LinearLayout) findViewById(R.id.activity_main);` I think you are referencing your layout, and this is wrong please check your code. – RaBaKa 78 Feb 17 '22 at 23:32
  • @RaBaKa78 Sorry I didn't catch the point. What do you mean by the line `LinearLayout linearLayout = (LinearLayout) findViewById(R.id.activity_main);` ? Can I use LinearLayout ? I do not know what's wrong with my code, I've been using AS for one week only. – user865044 Feb 17 '22 at 23:36

2 Answers2

0

Call

linearLayout.setOrientation(LinearLayout.VERTICAL);

to make 2 items appear side by side.

For showing keyboard, take a look here: How do you close/hide the Android soft keyboard programmatically?

Filipe Oliveira
  • 850
  • 7
  • 13
0

Looks like you are referencing your xml file here: LinearLayout linearLayout = (LinearLayout) findViewById(R.id.activity_main);

You should go to your xml file, add a id (android:id="@+id/some_id") tag to your <LinearLayout> and use this id in your LinearLayout linearLayout =(LinearLayout) findViewById(R.id.HERE)

Other option is to go to your xml file and add sandroid:orientation="horizontal" like this:

    <LinearLayout
                    android:id="@+id/some_id"
                    android:orientation="vertical"
                    ...>