0

How can I set the gravity and margin for auto-generated(within the code) TextViews and Buttons? They are inside a LinearLayout.

Here is my generation code:

        for(int i=0; i<num_enter; i++){
    final int cuco = i;
    LinearLayout linlay = new LinearLayout(this);
    linlay.setOrientation(0);
    TextView text = new TextView(this);
    text.setText(name[cuco] + "        ");
    linlay.addView(text);
    TextView num = new TextView(this);
    num.setId(cuco);
    num.setText("" + current[cuco]);
    linlay.addView(num);
    Button minus = new Button(this);
    minus.setText("-");
    minus.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            int cval = current[cuco];
            int currentF = current[cuco] - 1;
            current[cuco] = current[cuco] -1;
            SetSql update = new SetSql(SpellCast.this);
            update.open();
            update.changeCurrent(currentF, cval, name[cuco]);
            update.close();
            ((TextView) findViewById(cuco)).setText("" + currentF);
        }
    });
    LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT);
    minus.setLayoutParams(p);
    linlay.addView(minus);
    Button plus = new Button(this);
    plus.setText("+");
    plus.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            int cval = current[cuco];
            int currentF = current[cuco] + 1;
            current[cuco] = current[cuco] + 1;
            SetSql update = new SetSql(SpellCast.this);
            update.open();
            update.changeCurrent(currentF, cval, name[cuco]);
            update.close();
            ((TextView) findViewById(cuco)).setText("" + currentF);
        }
    });
    LinearLayout.LayoutParams w = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT);
    plus.setHeight(LayoutParams.WRAP_CONTENT);
    plus.setWidth(50);
    plus.setLayoutParams(w);
    linlay.addView(plus);

    main.addView(linlay);
    }

Hope you find a way to set the button gravity without changing it size.

rel-s
  • 6,108
  • 11
  • 38
  • 50
  • Lots of people are trying to help... but we are all just guessing until you post some code. I know that the code seems obvious to you... but you are saying all of the given answers don't work even though they answer your question perfectly – ByteMe May 04 '12 at 20:30
  • What is the result of this vs. the result you want? – ByteMe May 04 '12 at 22:39
  • The result of this is both buttons and text fields stuck to the left – rel-s May 05 '12 at 07:53

3 Answers3

1

1) Get Reference of TextView say tv.

2) tv.setGravity(Gravity.CENTER);

It is not sure work with LinearLayout.. Try Relative Or Absolute

Antony
  • 603
  • 6
  • 14
0

Use View.setLayoutParams(ViewGroup.LayoutParams params). Look at http://developer.android.com/reference/android/R.styleable.html#ViewGroup_Layout for the LayoutParams.

techi.services
  • 8,473
  • 4
  • 39
  • 42
  • sorry, but like the answer below you it doesn't work. is there another way this might work? i am using a lot of buttons and textviews, and using this method while online giving a left margin makes them stand on each other... – rel-s May 04 '12 at 20:08
  • Could you post an example of how you want them layed out? – techi.services May 04 '12 at 20:20
  • I cant post any example, all i want is a way to margin auto-generated buttons and textviews from the left – rel-s May 04 '12 at 20:23
0

Since nobody has addressed margins

LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
p.setMargins(10, 10,10,10);
p.gravity = Gravity.CENTER;

textView.setLayoutParams(p);

When using setLayoutParams make sure the type of layout params matches the parent view which, in this case, is LinearLayout

setGravity(Gravity.CENTER); Will set the gravity of the text within the view

dymmeh
  • 22,247
  • 5
  • 53
  • 60
  • The idead sounds nice, but in reality it doesn't work. it just makes all the buttons to stick to each other so no one is seen. isn't there any other simple solution? – rel-s May 04 '12 at 20:05
  • well, i don't know what you mean by "stick to each other". If you posted some code of how you're adding the buttons/textviews we could probably help out a little more :) – dymmeh May 04 '12 at 20:08
  • Well, there really is nothing to post. i mean that the buttons keep forming on each other, like one ontop of the other. – rel-s May 04 '12 at 20:19
  • That should be impossible if you're using a linear layout. Views should never overlap like that when using a linear layout. If you're using a RelativeLayout it makes sense, though – dymmeh May 04 '12 at 20:21
  • Do you have the orientation set properly in the linear layout? Did you try changing the width of the TextViews / Buttons to be wrap_content instead of match_parent like i have? – dymmeh May 04 '12 at 20:29
  • You are setting the layout weight = Gravity.RIGHT using the layoutParam contructor LinearLayout.LayoutParams (int width, int height, float weight) . Use p.gravity = Gravity.RIGHT; like in my answer if you want to set the gravity. There are no constructors for layoutparams that take gravity – dymmeh May 04 '12 at 21:01
  • In that case it just does nothing. buttons stay in their places. – rel-s May 04 '12 at 21:06
  • what type of view is "main" that you are adding the linear layout to? – dymmeh May 04 '12 at 21:16
  • Main is a linear layout as well, that holds all my other linear layouts – rel-s May 05 '12 at 07:55