How to create a button dynamically?

I know that many people asked about this, but my question matters:

I already have the layout in xml (using tabs), but I need to create the buttons according to the reading line.

For example: A query returns four words. I need to create four buttons so that when I click, a toast with the word appears.

Caution: I do not know how many words will find.

The code looks something like this:

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

Edit:

Check out my new code to 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> 

+4
source share
5 answers

You can create a button in the code with:

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

To display Toast, add 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 will need to have a container defined in xml to hold them, like LinearLayout. Get LinearLayout with:

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

You probably want this code outside of your loop, perhaps right in front of it, so you don't get a layout at every iteration.

Then, in a loop, after your button is configured:

 myContainer.addView(myButton); 

Finally, a note on the myLayoutParams variable. When you set the options for the view, they must match the parent view of your widget. Therefore, if you put a Button in a LinearLayout, it will 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 your loop and reuse it for each button.

+4
source

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 do not know your layout, so I can not clarify it.

The ViewGroup will be your layout and it should have the identifier found by findByViewId.

In addition, you will need to put a button if you want.

+2
source
  while (Content.indexOf("<glossary>") != -1) { Content = Content.substring(Content.indexOf("<glossary>") + 9); //create button with name "Content" Button b = new Button(this); b.setText(Content); // add this new view to your parent layout parent.addView(b); } 
+1
source

You want to include the layout in your XML to hold down the buttons you add (you can do it dynamically, but it doesn't seem necessary). When you want to add a button, find this layout. Then you can use ViewGroup.addView (View a child) to add your buttons to the view hierarchy.

You will also want to tell the system that the layout has changed, which you can do with the View.invalidate () method.

+1
source

I changed the way tabs are created and it will work!

+1
source

Source: https://habr.com/ru/post/1412064/


All Articles