• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Dynamically added buttons

kevinp88

Lurker
Apr 24, 2011
3
0
Hi,

I'm writing a application for Android which you can use when you are
on holiday with friends and you want to split the expenses during your
trip. You can add a new trip (using a sqlite DB for this). By editing
the trip you can add expenses and put which person paid for it.

However i'm having some trouble with the list of all trips that you
get when you want to edit one trip.
android_problem.png


As you can see in the pictures, I can make the buttons without any
problem.
However when I make the text inside the button longer (this should be
<name of the trip and location>)
(for example: Name: Brussels - Location: Belgium)
When I do this some buttons go off-screen (so I can't see them) I
would like that they are drawn underneath eachother and not next to
each other.
Here is the code that I have to add them dynamically:

Code:
       DBAdapter db = new DBAdapter(this);
       //---get all trips---
       db.open();
       Cursor c = db.getAllTrips();
       if (c.moveToFirst())
       {
               LinearLayout layout = (LinearLayout)
findViewById(R.id.layout);
               do {
                       Button buttonView = new Button(this);
               buttonView.setText(c.getString(1));
               //buttonView.setText("Name: " + c.getString(1) + "-" +
"Location: " + c.getString(2));
               layout.addView(buttonView);
           } while (c.moveToNext());
       }
       db.close();

Also how can I add some styles to dynamically added buttons?
The first button that you see is a button that I declared in the XML-
file.

If you need more information about something please let me know ;)

Thx in advance guys!

Kevin
 
Thx Jordi that helped ;)

Just a little question..Do you guys have a nice tutorial or do you guys know how I can get the phone numbers of all my contacts from my contactlist? I've been trying to do this with serveral solutions of peoples but they dont seem to work or the imports are decrepated!

Thx in advance ;)
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones