0

i'm trying to create buttons dynamically though code using a linear layout to set one button after the other, my code runs doesn't give any errors, nor throwing any exceptions or anything, but all i get is an empty screen. Thanks in advance.

private void runThreadCreateButton(final List<Stop> stops) {

    new Thread() {
        public void run() {
            int i=0;
            while (i++ < 1) {
                try {
                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            for(int j=0;j<stops.size();j++)
                            {
                                Button myButton = new Button(getApplicationContext());
                                myButton.setText(stops.get(j).getName());

                                LinearLayout ll = new LinearLayout(getApplicationContext());

                                LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                                ll.addView(myButton, lp);
                            }
                        }
                    });
                    Thread.sleep(1000);
                } catch (Exception e) {
                   System.out.println(e.getMessage());
                }
            }
        }
    }.start();
}
2
  • What is your activities layout? Where do you attach your LinearLayout ll, to another layout? Commented Feb 1, 2014 at 23:44
  • The activity layout for this activity is called display_stops_activity how do i atach the linear layout ll to it, sorry i'm kinda new to android. Commented Feb 1, 2014 at 23:57

2 Answers 2

1

you create a new Layout, but you Activity is not set to show your layout. Call setContentView() on your Main Activity with your new layout.

LinearLayout ll = new LinearLayout(getApplicationContext());
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);
//add something like this
yourMainActivity.setContentView(ll);
Sign up to request clarification or add additional context in comments.

2 Comments

@Diego: you need to call display_stops_activity.addView(ll) inside your for loop.
It kinda worked added one button (should add like 20) do i have to change the position of each button in each iteration for it to work?
1

Well, if you didn't add your layout to your activity this was your main problem.

However you should learn about composing your layouts in xml. This makes your code a lot more readable and maintainable. Also you don't need to worry about switching Threads all the time.

If you need to populate your views with runtime data, you should use list views and list adapters. ListViews and ListAdapters are essentials to almost every android app, so you you should really learn about them. If you want your list items to hold more complex layout you can do so by implementing your own custom list adapters.

There are also lots of performance tweaks you can do, when using list views. For most use cases generating and managing your layout from code is not a good approach.

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.