vnshetty
vnshetty

Reputation: 20132

which one is better? -Creating buttons at Run time or design time in android

i need to create around 26 buttons for simple task like display alphabets. i can do this by using layout design.

if i create this button at run time will it give more performance(Considering memory, speed,apk size!)?

Important Requirements:

edit: This layout is like pop up window for other four activities. user can press any alphabets in this layout. As soon as alphabets get selected layout will be closed

Upvotes: 0

Views: 178

Answers (5)

Josh
Josh

Reputation: 10738

Yikes. While XML is the best practice answer, 26 of anything screams for some dynamic run-time creation, or at least a combination of the two. You're not going to see much difference in processing time or apk size either way - it will come down to code maintenance down the line.

For instance, consider what will happen when you want to change or add a new attribute, say padding, for each of your letter buttons. Do you want to have to manually go change all those XML elements, or think about a clever regex to properly find/replace?

I'd go with a combination of styles, <include> statement, and run time modification for a comprehensive, maintainable solution. First create a single button styled how you think you want all your buttons to look. Extract your "LetterButton" style out to style.xml and use the android:style="@style/LetterButton" attribute on your button instead. This will allow you to change your style in a single file and have it affect all your letter buttons.

Next, extract the button itself into an <include> file. You can do this by right clicking on the GUI version of the button and choosing "Extract include...". Then arrange your <include>-buttons however you need to, perhaps in a <TableLayout>. Make sure you give each one a unique id, like @+id/letter_button_0 up through _25. The text attribute for all these buttons can be anything, you'll set those dynamically later.

Finally, in your onCreate, define an array of ints of the form {R.id.letter_button_0, ...}, and an array of Strings of the form {"A", ...}, and iterate over those, doing a button = findbyId(int), button.setText(String) to put a letter on each of your buttons.

It may seem like more work this way, but you're doing all the heavy lifting creating a smart UI, so that down the line you can change code in a single place (style or include) and all your buttons will be updated.

Upvotes: 1

Karthik Thangarajan
Karthik Thangarajan

Reputation: 91

You could create a layout with having static assets in it & have dynamic text content & for dynamic backgrounds.You can have use the button properties of gone & visible in it.

Upvotes: 0

SnowyTracks
SnowyTracks

Reputation: 1985

Strongly recommend XML layouts for best practice and more understandable code. Also, If you are worried about performance for large view, use relative layouts, these are faster to render than other types of layouts such as LinearLayouts.

Showing XML is best practice: http://developer.android.com/guide/topics/ui/declaring-layout.html

Also to increase performance keep the Buttons as Activity member variables, then they only need to be loaded once from resources.

Hope that helps, Marc

Upvotes: 1

Sherif elKhatib
Sherif elKhatib

Reputation: 45942

Since everything is static residing in your assets, it is fine to have everything in xml files.

Still, one can argue that the 'notion' of parsing the xml layout files of your project introduces an overhead to the process of creating the views.

I would go with a well-designed layout defined in xml.

Upvotes: 2

Vineet Shukla
Vineet Shukla

Reputation: 24031

You should create it at xml file and make visible and invisible as you need.

Upvotes: 0

Related Questions