Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a button dynamically?

I know that many people have asked about this, but my question has a difference:

I already have a layout in xml (using tabs), but I need to create buttons according to a reading of a string.

For example: The query returns four words. I need to create four buttons, that when clicked appear a toast with the word.

Attention: I don't know how many words will find.

The code would look something like:

    while (Content.indexOf("<glossary>") != -1) {
        Content = Content.substring(Content.indexOf("<glossary>") + 9);
        //create button with name "Content"
    }

Edit:

Look my new code for create just one Button:

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.showphyto);
    intent = getIntent();
    Title = intent.getStringExtra("Title");
    Content = intent.getStringExtra("Content");
    setTitle(Title);
    //if(getIntent().getData().getQueryParameter("author") != null)
    TabSpec descritor;

    /************************** Tab General **************************/
    descritor = getTabHost().newTabSpec("tag1");
    descritor.setContent(R.id.General);

    Button myButton = new Button(ShowAllegationActivity.this);
    myButton.setText("test");
    LinearLayout myContainer = (LinearLayout) findViewById(R.id.General);
    myContainer.addView(myButton);

    descritor.setIndicator("General", getResources().getDrawable(R.drawable.icon));
    getTabHost().addTab(descritor);

    /************************** Tab 2 **************************/
    descritor = getTabHost().newTabSpec("tag2");
    descritor.setContent(R.id.Recipe);
    Teste = (TextView) findViewById(R.id.teste2);
    Teste.setText("Teste2");
    descritor.setIndicator("Tab2", getResources().getDrawable(R.drawable.icon));
    getTabHost().addTab(descritor);

    /************************** Tab3 3 **************************/
    descritor = getTabHost().newTabSpec("tag3");
    descritor.setContent(R.id.Related);
    Teste = (TextView) findViewById(R.id.teste3);
    Teste.setText("Teste3");
    descritor.setIndicator("Tab3", getResources().getDrawable(R.drawable.icon));
    getTabHost().addTab(descritor);

    getTabHost().setCurrentTab(0);

}

}

xml file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TabHost android:id="@android:id/tabhost" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent">
    <LinearLayout android:id="@+id/linearLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical">
        <TabWidget android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:id="@android:id/tabs"></TabWidget>
        <FrameLayout android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:id="@android:id/tabcontent">
            <LinearLayout android:layout_width="fill_parent" 
            android:layout_height="fill_parent" 
            android:id="@+id/General">
                    <TextView android:layout_width="fill_parent"
                    android:layout_height="wrap_content" 
                    android:id="@+id/teste" />
                    <LinearLayout android:layout_width="fill_parent" 
                    android:layout_height="fill_parent" 
                    android:id="@+id/General2">
                    </LinearLayout>
            </LinearLayout>
            <LinearLayout android:layout_width="fill_parent" 
            android:layout_height="fill_parent" 
            android:id="@+id/Recipe">
                    <TextView android:layout_width="fill_parent"
                    android:layout_height="wrap_content" 
                    android:id="@+id/teste2" />
            </LinearLayout>
            <LinearLayout android:layout_width="fill_parent" 
            android:layout_height="fill_parent" 
            android:id="@+id/Related">
                    <TextView android:layout_width="fill_parent"
                    android:layout_height="wrap_content" 
                    android:id="@+id/teste3" />
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>

like image 239
Fabiano Palaoro Avatar asked Jan 17 '23 17:01

Fabiano Palaoro


2 Answers

You can create a button in code with:

Button myButton = new Button();
myButton.setLayoutParams(myLayoutParams);
myButton.setText(myText);

To make it display the Toast, add an onClickListener:

myButton.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View view)
                    {
                        //The parameter view is the button that was clicked 
                        Toast.makeText(context, ((Button) view).getText(), Toast.LENGTH_LONG).show()
                        //TypeCasting to Button is important, because a normal View doesn't have a getText method
                    }
                });

Then, to add them to your screen, you'll need to have a container defined in xml to hold them, for example a LinearLayout. Get the LinearLayout with:

LinearLayout myContainer = findViewById(R.id.myButtonId); //Make sure you set the android:id attribute in xml

You'll probably want this code to be outside of your loop, perhaps just before it, so you're not getting the Layout on each iteration.

Then, in the loop, after your button is set up:

myContainer.addView(myButton);

Finally, a note about the myLayoutParams variable. When you set parameters for a view, they need to correspond to the parent view for your widget. So if you're putting the Button in a LinearLayout it would look like

//This will make LayoutParameters with layout_width="wrap_content" and layout_height="fill_parent"
LinearLayout.LayoutParams myLayoutParams = new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.WRAP_CONTENT, 
    LinearLayout.LayoutParams.FILL_PARENT);

You can also put this outside of your loop, too, and reuse it for each Button.

like image 180
MattDavis Avatar answered Jan 20 '23 04:01

MattDavis


What you could do would be something like this:

    Button button = new Button(this);
    ViewGroup viewGroup = (ViewGroup) findViewById(R.id.container);

    viewGroup.addView(button);

I dont know your layout so i cant get more specific.

The ViewGroup would be your layout and it should have the id to be found by findByViewId.

In addition you would have to place the button were you want it to be.

like image 38
Ostkontentitan Avatar answered Jan 20 '23 04:01

Ostkontentitan