简体   繁体   中英

Custom UI object android

Im quite new to android development and in fact also Java. I have created an activity where I have several "rounded corner" transparable gradient-"buttons" that is made up of an absolutelayout, and two texfields. Now I want these "buttons" to be added to my screen based on some values in a database. So the UI is dynamic. It must be fetched from DB. So it can be 10 "buttons" and it can be 1 "button". How can I add these "buttons" dynamically Adding text from DB? and still have click-events etc. This is how the "buttons" are defined in XML: (I have removed some of the elements)

Hope someone out there understands the problem, and are able to help.

Button newButton = (Button) findViewById(R.id.new_button);
newButton.setText("Click me");

Get the values from database and create a loop. Instead of setting the text in xml, set the text programmatically in your activity class.

What's the reason you need so many buttons? And how do you want them to be located on the screen? Actually, it's a bad practice to create undefined amount of buttons. May be you just need a ListView with CursorAdapter attached. You will get the list of items, the text could be set from databse automatically by CursorAdapter, and you can make items to be "rounded corner" transparable gradient attaching this layout in adapter's getView(). And handling onItemClick() you will get the Button functionality.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM