Home > Net >  can LinearLayout put two items on the same line?
can LinearLayout put two items on the same line?

Time:02-19

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]);


        }

        };

CodePudding user response:

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?

CodePudding user response:

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"
                    ...>
  • Related