Reputation: 4196
How easily can you layout elements in Android programmatically, and is this a practical alternative to using the XML / code split typically used in Android?
Coming from an iOS background, where similarly you can connect UI elements in a storyboard with code in the body files, I've found that creating all elements in code itself offers greater overview and reduces coding time. There's also a lot less toggling between the two files.
Android seems to place greater importance on the use of XML for layout than Apple does for storyboards, and I'm wondering if it's a bridge too far to force element creation into the activities themselves.
I get that to a limited extent the question refers to coding style (thereby inviting off-topic discussion), but not exclusively so and insofar it influences UX (and maybe performance), it's relevant.
Upvotes: 0
Views: 71
Reputation: 5339
using xml
is easier than using java and more flexible to control : example of Button
inside LinearLayout
Button button = new Button(this);
button.setText("My Button");
button.setWidth(100);// in pixel not recommended
button.setHeight(100);
button.setBackgroundColor(Color.RED);
button.setX(100);
button.setY(100);
button.setTextSize(TypedValue.COMPLEX_UNIT_PX, 20);// to convert into sp
LinearLayout container = new LinearLayout(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
container.setLayoutParams(layoutParams);
container.addView(button);
it may take 15min of coding will using xml is just drag and drop and using the xml Editor
Upvotes: 1
Reputation: 389
Using layout files let you better manage the UI as compared to adding them programmatically which would include the aligning the views from all the sides and setting params and rules. This would make java file more cumbersome. Better approach would be keeping the two separate unless required.
Upvotes: 2