0

I have an XML layout having a single TextView

Now I want to add 50 buttons which I want to add dynamically in my java file !.

Is it possible to add attributes to an XML file via java code ?? Or can an activity have 2 layouts at a time ??

for eg,

public class Options extends Activity
{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.options);
    Button but=new Button(this);
    but.setText("Wassup");
    // How do I add this button to the layout ?
}

}
Vaido
  • 111
  • 1
  • 7
  • see this... http://stackoverflow.com/questions/5631913/add-a-button-dynamically-to-a-linearlayout-in-android may be a duplicate. – Sai Kiran Jul 03 '13 at 16:35

4 Answers4

3

Is it possible to add attributes to an XML file via java code ??

No, but you can add properties to Views and Layouts as you are doing with setText(). resource files themselves cannot be changed after compiled.

Or can an activity have 2 layouts at a time ??

The simple answer is no but you can inflate another layout and add it to the current layout.

Example of what you can do to add a Button

Inflate your root layout and add the Buttons to it with addView(). Something like

Layoutinflater inflater = (LayoutInflater) getSystemService
  (Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.layout_file);
Button but=new Button(this);
but.setText("Wassup");
// How do I add this button to the layout ?
ll.addView(but);

LayoutInflater

Or if you want to add it to a layout in the current file you can just use findViewById() and use addView() on that to add your Buttons to.

codeMagic
  • 44,549
  • 13
  • 77
  • 93
1

Considering you have an xml layout as below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@id/mainlayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
</TextView>

</LinearLayout>

In your java code after setContentView(R.layout.options); you can do the following:

LinearLayout linearLayout=(LinearLayout)findViewById(R.id.mainlayout);
Button button=new Button(this);
linearLayout.addView(button);

Now you can add as many buttons you like into the linear layout as seen above.

S.A.Norton Stanley
  • 1,833
  • 3
  • 23
  • 37
0

Yes it is possible. After setContentView(R.layout.options); get your buttons container with findViewById(). You will have a reference to a LinearLayout, RelativeLayout or something else. After that use Layout inflater and programmatically you can add other layouts or components.

Hope it helps!

  • @MarkBasler I wrote in my profile: I don't give fish, I am whiling to teach a fishing little. I know that would be apreciated, but use his brain a little and the Google. Ofc if he is whiling to pay I will write code :) –  Jul 03 '13 at 16:44
  • Notice the accepted answer had an example. It doesn't need to be exactly what they need. – yams Jul 03 '13 at 16:47
  • oh, yes, usually Iam telling what to do, others read that give code they answer is accepted and up-voted and mine, not always, who cares? :) –  Jul 03 '13 at 16:49
  • Helping others regardless of what you get is always the right answer. – yams Jul 03 '13 at 17:09
0

just use layout.addView() where layout is a ViewGroup that you get by calling findViewById(R.id.layoutId)

pskink
  • 23,874
  • 6
  • 66
  • 77