I want to add a view comprising of 4 buttons and a textview to a linear layout and set that newly made view to a viewflipper...Client's requirement is that i need to create the layout the programmatically and not using an xml file
Asked
Active
Viewed 2,629 times
-3
-
1And what have you tried? – Sandip Armal Patil Feb 12 '13 at 06:35
-
1What's the question here? – Sean Feb 12 '13 at 06:35
-
http://stackoverflow.com/questions/6216547/android-dynamically-add-views-into-view – Jignesh Ansodariya Feb 12 '13 at 06:44
-
I want to add a view comprising of 4 buttons and a textview to a linear layout and set that newly made view to a viewflipper...Client's requirement is that i need to create the layout the programmatically and not using an xml file – HelpNeeded Feb 12 '13 at 06:44
-
Your client doesn't know what they're talking about and it's your job to advise them on the best approach. – Simon Feb 12 '13 at 07:41
2 Answers
0
It's recommended to define UI in XMl. But for your client requirement you can do it dynamically. In android, UI in xml and at run time (.java file) are optional each other. So use java methods to create LinearLayout and add newly created views to it. Finally you can add this LinearLayout to ViewFlipper.

Sandeep Kharat
- 500
- 1
- 4
- 12
-
Thats what I am asking how do i add this newly created view to the layout ! – HelpNeeded Feb 12 '13 at 06:51
0
See this is sample code, this might helpful for you. Instaed of LockView you can mention other views..
lockLayout = (LinearLayout) findViewById(R.id.quick_lock_layout);
private void renderLockLayout() {
lockLayout.removeAllViews();
lockLayout.invalidate();
lockLayout.setLayoutParams(new LinearLayout.LayoutParams(
lockLayoutWidth + 7, (height / 6)));
/*
* Toast.makeText(ApplicationContext.getContext(), "Num count is :" +
* _totalLocks, Toast.LENGTH_SHORT).show();
*/
Log.i(getClass().getSimpleName(), "Total :" + _totalLocks);
lockViewArray = new LockView[_totalLocks];
for (int index = 0; index < _totalLocks; index++) {
LockView lockview = (LockView) inflater.inflate(R.layout.lockview,
null);
lockview.setLayoutParams(new LayoutParams((width),
LayoutParams.WRAP_CONTENT));
lockLayout.addView(lockview);
lockViewArray[index] = lockview;
}
lockLayout.invalidate();
}

Sandeep Kharat
- 500
- 1
- 4
- 12