0

I'm relatively new to android and I'm trying to create a linear layout with 2 buttons and text view. The buttons will allow me to increase and decrease the value of the text view in the middle of them.

Basically I want to achieve something like this:
Layout

I have already created a function for the creation of textviews and it works fine. Now I want to create 2 buttons which I managed to do on my own but I seem to fail when it comes to making them align with eachother as shown in the image above.

The code I wrote to create textviews dynamically is given below:

 void create_text(int i, int amountid, int H, int W)
{
    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.imgLayout2);
    LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(340, 340);
    parms.gravity = Gravity.CENTER;
    parms.setMargins(20, 0, 20, 0);

    final TextView textView = new TextView(this);
    textView.setLayoutParams(parms);
    textView.setText("            x "+i);
    textView.setTypeface(null, Typeface.BOLD);

    linearLayout.addView(textView);
    textView.setId(amountid);

    textView.getLayoutParams().height = H;
    textView.getLayoutParams().width = W;

}

I want to create buttons in the same function that will be before and after the text view and perfectly aligned with it (my actual problem and something that I have failed to do so far). Any help is very much appreciated.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
JBJ
  • 288
  • 2
  • 13

2 Answers2

1

You should be building your layouts in the xml not programatically. In there you can easily 'draw' your layouts and make them aligned and responsive to any device.

To make sure they keep aligned you can use an Horizontal LinearLayout or even the new ConstraintLayout.

You should choose the second one (the ConstraintLayout), it came with the new Android SDK.

0

You can create a buttons same as you created textviews.

Button mButton = new Buttonthis);
mButton.setLayoutParams(parms);
mButton.setText("x "+i);

Then you have to add TextView and Button in one LinearLayout with horizontal orientation and add the layout to your parent view.

Naitik
  • 796
  • 11
  • 32
  • I know that but that returns massive, distorted button I want a result similar to the linked image. Basically I'm looking forward to someone helping me align them properly. – JBJ Jan 09 '17 at 10:54