4

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>

5 Answers 5

4

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Hello Matt, thanks for reply. I could not, I edited the post with the complete code, you can tell me what's wrong? Thx!
I need create a Button in xml?
You shouldn't need a button in xml. What is currently going wrong? Is the program crashing, or just not working how you intended? If it's crashing, post your logcat for the error. If not, it might be useful to see the "showphyto" xml file. Since it looks like you're using a tab layout, this tutorial from google may also provide some help developer.android.com/resources/tutorials/views/… Tabs are not set up like normal buttons, which may be part of your problem.
Just not working my intended! I edited with the xml file. Look please.
Take a look at #4-6 on the link I posted in my previous comment. It looks like, rather than buttons, you use the TabHost to load a bunch of Tabs that fire off intents. The tabs are located inside your TabWidget and the intent is created in the FrameLayout. I've never used this particular layout before, but setting up your "buttons" the way the tabs are set up in #6 may help. What do you want your tabs to do once clicked? If it's just fire off a Toast, you may be better off using a less complicated layout.
|
2

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.

Comments

1
 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);

}

Comments

1

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

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

Comments

1

I changed the way to create tabs and it worked!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.