Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create a button dynamically?
    primarykey
    data
    text
    <p>I know that many people have asked about this, but my question has a difference:</p> <p>I already have a layout in xml (using tabs), but I need to create buttons according to a reading of a string.</p> <p>For example: The query returns four words. I need to create four buttons, that when clicked appear a toast with the word.</p> <p>Attention: I don't know how many words will find.</p> <p>The code would look something like:</p> <blockquote> <pre><code> while (Content.indexOf("&lt;glossary&gt;") != -1) { Content = Content.substring(Content.indexOf("&lt;glossary&gt;") + 9); //create button with name "Content" } </code></pre> </blockquote> <p>Edit:</p> <p>Look my new code for create just one Button:</p> <pre><code> 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); } </code></pre> <p>}</p> <p>xml file:</p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"&gt; &lt;TabWidget android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@android:id/tabs"&gt;&lt;/TabWidget&gt; &lt;FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@android:id/tabcontent"&gt; &lt;LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/General"&gt; &lt;TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/teste" /&gt; &lt;LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/General2"&gt; &lt;/LinearLayout&gt; &lt;/LinearLayout&gt; &lt;LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/Recipe"&gt; &lt;TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/teste2" /&gt; &lt;/LinearLayout&gt; &lt;LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/Related"&gt; &lt;TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/teste3" /&gt; &lt;/LinearLayout&gt; &lt;/FrameLayout&gt; &lt;/LinearLayout&gt; &lt;/TabHost&gt; </code></pre> <p></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload