1. Download our Official Android App: Forums for Android!

Apps Dynamically added buttons

Discussion in 'Android Development' started by kevinp88, Apr 24, 2011.

  1. kevinp88

    kevinp88 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    3
    Joined:
    Apr 24, 2011

    Apr 24, 2011
    3
    0
    5
    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.
    [​IMG]

    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 (Text):
    1.        DBAdapter db = new DBAdapter(this);
    2.        //---get all trips---
    3.        db.open();
    4.        Cursor c = db.getAllTrips();
    5.        if (c.moveToFirst())
    6.        {
    7.                LinearLayout layout = (LinearLayout)
    8. findViewById(R.id.layout);
    9.                do {
    10.                        Button buttonView = new Button(this);
    11.                buttonView.setText(c.getString(1));
    12.                //buttonView.setText("Name: " + c.getString(1) + "-" +
    13. "Location: " + c.getString(2));
    14.                layout.addView(buttonView);
    15.            } while (c.moveToNext());
    16.        }
    17.        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
     

    Advertisement

  2. kevinp88

    kevinp88 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    3
    Joined:
    Apr 24, 2011

    Apr 24, 2011
    3
    0
    5
    UPDATE: I found the way to align my buttons vertically instead of horizontally when I add them to the View...It was just a parameter you have to add in your xml file!

    I'm still searching how I can add styles to a dynamically added button.! Any1 has a clue? :)
     
  3. JordiDroid

    JordiDroid Member
    Rank:
    None
    Points:
    36
    Posts:
    38
    Joined:
    Apr 20, 2011

    Apr 20, 2011
    38
    2
    36
    Software Engineer
    Netherlands
  4. kevinp88

    kevinp88 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    3
    Joined:
    Apr 24, 2011

    Apr 24, 2011
    3
    0
    5
    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 ;)
     
  5. JordiDroid

    JordiDroid Member
    Rank:
    None
    Points:
    36
    Posts:
    38
    Joined:
    Apr 20, 2011

    Apr 20, 2011
    38
    2
    36
    Software Engineer
    Netherlands

Share This Page

Loading...