0

I just want to dynamicly add buttons to my Layout when i want to. The buttons should be like this XML Button:

 <Button android:text="Text" 
 android:gravity="bottom" 
 android:textSize="10dp" 
 android:textColor="#FFFFFF" 
 android:layout_width="wrap_content"
 android:background="@drawable/attack1"
 android:layout_height="wrap_content" 
 android:id="@+id/workingButton">
 </Button>

.

public class GravityIssueActivity extends Activity
{
    LinearLayout layout;
    Button newButton;
    Button buttonByXml;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //the button in the xml file
        buttonByXml = (Button)findViewById(R.id.workingButton);
        layout = (LinearLayout)findViewById(R.id.layoutToInsert);
        //my new programatically "born" button
        newButton = new Button(this);
        //Setting the properties as i want
        newButton.setText("Text");
        newButton.setTextSize(10);
        newButton.setTextColor(Color.WHITE);
        newButton.setBackgroundResource(R.drawable.attack1);
        // Gravity = Bottom !!!!!!!!!!
        newButton.setGravity(Gravity.BOTTOM);
        // getting the XML buttons params just for case...
        newButton.setLayoutParams(new LayoutParams(buttonByXml.getLayoutParams()));
        //Adding my new Button to the layout
        layout.addView(newButton);
    }
}

And here is an image of the results:

enter image description here

How is it possible to became different result when I copy all the attributes?

casperOne
  • 73,706
  • 19
  • 184
  • 253
Adam Varhegyi
  • 11,307
  • 33
  • 124
  • 222
  • To be honest, I only got problems with gravity. I cannot make text to be in bottom. LinearLayout.LayoutParams params = new LinearLayout.LayoutParams (LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT); params.gravity=80 is just not working, – Adam Varhegyi Nov 10 '11 at 13:07
  • to you need gravity to align text inside button or to align button inside it's parent? – Vladimir Nov 10 '11 at 13:09
  • 1
    I just want the Button's text to be aligned to the bottom of the Button :) – Adam Varhegyi Nov 10 '11 at 13:11
  • possible duplicate of [Android: How to programmatically add button to view](http://stackoverflow.com/questions/4776057/android-how-to-programmatically-add-button-to-view) – Johan Nov 10 '11 at 14:18
  • I've just tried it - but with default background obviously - two buttons look virtually identical. HTC Desire S, Android 2.3.5 (same result on 2.1 emulator) – Vladimir Nov 10 '11 at 19:11

5 Answers5

1

If you want to create dynamic view (like Button,textview etc) then just use this code and run it in your application.

MyActivity.java://your java file

 LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout1);
 Button btn = new Button(this)
 btn.setText("My Dynamic Button);
 btn.setMinLines(1);
 btn.setMaxLines(3);
 ll.addView(et);

In XML File:

 <LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="@+id/TextView01"
android:layout_below="@+id/relativeLayout1"
android:orientation="vertical" >

Pir Fahim Shah
  • 10,505
  • 1
  • 82
  • 81
0

You can absolutely create buttons in code but it's not considered a best-practice unless you have a good reason for dynamically creating the controls. Check out this post Add an array of buttons to a GridView in an Android application.

Community
  • 1
  • 1
Carth
  • 2,303
  • 1
  • 17
  • 26
0

Try using

 Button b = new Button();

This gives you a View instance that can be added to your current parent activity or fragmnet view. For a full reference of possible settings look at http://developer.android.com/reference/android/widget/Button.html

You can use all the set methods provided by parent views in the object hierarchy.

Johnnycube
  • 1,060
  • 1
  • 10
  • 25
0

If you need to align text to the bottom of the button, all you need is:

Button button = ...
//apply required paramteres
button.setGravity(Gravity.BOTTOM);
Vladimir
  • 9,683
  • 6
  • 36
  • 57
  • Something is wrong with my code because this snipet leaves an empty space betwen button's bottom and the text. This why I posted this question... I just dont get it. – Adam Varhegyi Nov 10 '11 at 13:15
  • Could you provide result you are getting? Without seeing it I can only assume that you've set `setLineSpacing()` with value greater than required or you have `\n` at the end of the String you use to set button's text. – Vladimir Nov 10 '11 at 13:19
  • Im not using setLineSpacing(), If I use the XML code above, I got a nice image button with text on its bottom. If I try to create one with code as like .setGravity(Gravity.BOTTOM), there is an empty space betwen text and Button's bottom. – Adam Varhegyi Nov 10 '11 at 14:07
  • @AdamVarhegyi Is `layoutToInsert` the LinearLayout containing first (working) button? – Vladimir Nov 10 '11 at 19:30
  • Yes. It contains both buttons. Fustrating error, i stuck for a day now. – Adam Varhegyi Nov 11 '11 at 14:05
  • That's really strange. I asked about buttons being in different layouts because looking at your image it seems like buttons' heights are different, and so background is moved down on the second one. Try getting buttons' height after both are fully drawn and put them to log - if they are different there might be something wrong with they way their height is set. – Vladimir Nov 11 '11 at 14:38
  • @AdamVarhegyi perhaps you have something like `[linearlayout1 [xml_button][linearlayout2 [code_button]]]` then if height of linearlayout2 is less than height of linearlayout1 codded buton will shrink, moving backgorund down – Vladimir Nov 11 '11 at 14:53
  • All the heights, widths, gravities and even weights are the same both for the buttons. I cannot upload picture becouse the emulator is freezing, so i only tested it on my phone. Thanks for your advices Vladimir, but i dont think this will ever work, i think i just continue my work where i stopped. – Adam Varhegyi Nov 12 '11 at 11:58
0

use below code.you also add other parameters

    Button submit=new Button(this);
    LinearLayout.LayoutParams params= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
    params.setMargins(25, 0, 25, 0);
    submit.setLayoutParams(params);
    submit.setText("Attack");
    submit.setTextSize(10);
    submit.setTextColor(getResources().getColor(R.color.white));
    submit.setBackgroundResource(R.drawable.attack);
Dharmendra Barad
  • 949
  • 6
  • 14