Hello everyone please i need help. I'm a biginner in Android programming so since i was trying to solve this problem it took me all my days that why i want you to help me please. I'm trying to display a Linear layout according to the loop condition but the issue is that when i'm doing that it's just displaying the last one and i'm not able to see others when it's displaying. Here are my codes
ScrollView sv = new ScrollView(this);
sv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
sv.setPadding(5, 5, 5, 5);
sv.setBackgroundColor(getResources().getColor(R.color.colorWhite));
LinearLayout ll = null;
for (int i=0; i < 2; i++){
try {
//Here iam defining the LinearLayout
ll = new LinearLayout(this);
ll.setId(i);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
ll.setLayoutParams(params);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setBackground(getDrawable(R.drawable.circle_view));
//Here iam defining the RelativeLayout
RelativeLayout Rl = new RelativeLayout(this);
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
Rl.setLayoutParams(param);
//Defining a layout params for widgets
RelativeLayout.LayoutParams image = new RelativeLayout.LayoutParams(100, 90);
image.addRule(RelativeLayout.ALIGN_LEFT);
//Creating widgets
ImageView im = new ImageView(this);
im.setImageResource(R.mipmap.airtel);
im.setId(R.id.image1);
im.setLayoutParams(image);
//----------------
RelativeLayout.LayoutParams txtone = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
txtone.addRule(RelativeLayout.RIGHT_OF, R.id.image1);
txtone.setMargins(0, 5, 0, 45);
TextView txtVone = new TextView(this);
txtVone.setText("ArkaL"+i);
//txtVone.setTextSize(R.dimen.MainTextSize);
txtVone.setTextSize(15);
txtVone.setTypeface(txtVone.getTypeface(), Typeface.BOLD);
txtVone.setLayoutParams(txtone);
//------------------------------------------------
RelativeLayout.LayoutParams txttwo = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
txttwo.addRule(RelativeLayout.RIGHT_OF, R.id.image1);
txttwo.setMargins(0, 45, 0, 0);
TextView txtVtwo = new TextView(this);
txtVtwo.setText("République Démocratique du Congo");
txtVtwo.setTypeface(txtVtwo.getTypeface(), Typeface.SERIF.getStyle());
txtVtwo.setTextSize(10);
txtVtwo.setLayoutParams(txttwo);
Rl.addView(im);
Rl.addView(txtVone);
Rl.addView(txtVtwo);
RelativeLayout.LayoutParams recycler = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RecyclerView rv = new RecyclerView(this);
rv.setLayoutParams(recycler);
ll.addView(Rl);
ll.addView(rv);
}catch (Exception e){
}
}
//Here for assigning Linear to ScrollView
sv.addView(ll);
this.setContentView(sv);
Please help me.
your problem is that you created one LinearLayout and changed it in a loop and then added it to a ScrollView so at the end you will have just one LinearLayout. as you may know an ScrollView should have just one View as a child view so you should add a LinearLayout for a ScrollView and add as many LinearLayout as you want to the primary LinearLayout. your code can be like this:
LinearLayout llMain = new LinearLayout(this);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llMain.setLayoutParams(params2);
llMain.setOrientation(LinearLayout.VERTICAL);
sv.addView(llMain);
LinearLayout ll = null;
for (int i = 0; i < 2; i++) {
try {
//Here iam defining the LinearLayout
ll = new LinearLayout(this);
ll.setId(i);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ll.setLayoutParams(params);
ll.setOrientation(LinearLayout.VERTICAL);
...
llMain.addView(ll);
} catch (Exception e) {
...
note that the Height of primary linearlayour should wrap its content;
I have to set image on Right Top of textview programmatically. But its setting on leftside of textview. Can any one tell me how to deal with this?
My code is below:
FrameLayout frameLayout = KaHOUtility.generateFrameLayout(mContext);
frameLayout.setPadding(5, 5, 5, 5);
LinearLayout linearLayout = KaHOUtility.generateLinearLayout(mContext);
KaHOTextView textView = KaHOUtility.generatePanelHeadingTextViews(mContext);
textView.setText(name);
linearLayout.addView(textView);
frameLayout.addView(linearLayout);
ImageView imageView = KaHOUtility.generateImageView(mContext, 15, 15, R.drawable.cancel_mark);
LinearLayout.LayoutParams rPrams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
rPrams.gravity = Gravity.RIGHT|Gravity.TOP ;
imageView.setLayoutParams(rPrams);
frameLayout.addView(imageView);
You are creating layout params from the LinearLayout.LayoutParams class. However, the imageview is being added to a FrameLayout. This is incorrect because you only apply the layout params of the immediate parent to a view. So, in your case, it should be:
ImageView imageView = KaHOUtility.generateImageView(mContext, 15, 15, R.drawable.cancel_mark);
FrameLayout.LayoutParams rPrams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
rPrams.gravity = Gravity.RIGHT | Gravity.TOP ;
imageView.setLayoutParams(rPrams);
frameLayout.addView(imageView);
GridLayout glSameLoactions;
#OnClick(R.id.tvAddLocation)
void addLocationOnClickListener(){
if(i==0) {
glSameLoactions = new GridLayout(this);
glSameLoactions.setColumnCount(3);
glSameLoactions.setBackgroundResource(R.drawable.shape_rounded_corner_blurfilled);
glSameLoactions.setPadding(16,16,16,16);
}
RelativeLayout rlLocation = new RelativeLayout(this);
rlLocation.setPadding(16, 16, 16, 16);
LinearLayout.LayoutParams relativeParams = new LinearLayout.LayoutParams(300,200);
relativeParams.setMargins(16, 16, 16, 16);//<<<<----- NOT WORKING
rlLocation.setLayoutParams(relativeParams);
rlLocation.requestLayout();
rlLocation.setBackgroundResource(R.drawable.shape_rounded_corner_blurfilled);
TextView tvLocationName = new TextView(this);
tvLocationName.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
tvLocationName.setId(R.id.tvLocationName);
tvLocationName.setText("amritsar,PB");
tvLocationName.setTextColor(getResources().getColor(R.color.white));
tvLocationName.setTextSize(14f);
ImageView ivRadiobtn = new ImageView(this);
tvLocationName.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
ivRadiobtn.setImageResource(R.drawable.ic_circle_empty);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rlLocation.addView(tvLocationName);
rlLocation.addView(ivRadiobtn,lp);
glSameLoactions.addView(rlLocation);
if (i==0) {
llLocations.addView(glSameLoactions);
llLocations.setVisibility(View.VISIBLE);
}
i++;
}
I am making a layout programmatically, in which on a click of button, a new relative layout is added in a grid layout. All my code is working fine, but the problem is that i am unable to set margins to the relative layout inside the grid layout. Please help!!
Thanks
You should use RelativeLayout.LayoutParams instead of LinearLayout.LayoutParams.
Try this:
RelativeLayout.LayoutParams relativeParams =new RelativeLayout.LayoutParams(300, 300);
relativeParams.setMargins(16, 16, 16, 16);
rlLocation.setLayoutParams(relativeParams);
I dynamically declared some horizontal layouts witch contains some elements like ImageButtons, Buttons, and TextViews, these elements centrally oriented, all of the elements are oriented perfectly but the TextViews. I tried both declaring them directly like the rest of the elements and declaring them each inside a vertical layout but both didn't work:
that's my code:
// the layout params for the Horizontal Layout
LinearLayout.LayoutParams lp_icon = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
0, 1);
lp_icon.setMargins(10, 15, 5, 0);
// the layout params for the elements that contained in the horizontal LinearLayout
LinearLayout.LayoutParams lp_ineer_ver = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT, Gravity.CENTER);
// the layout params for the TextViews
LinearLayout.LayoutParams tests = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
// the layout params for the vertical layouts that contaion TextViews
LinearLayout.LayoutParams temp_lay = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
temp_lay.weight = 1;
// the elements declaration
icon1.setLayoutParams(lp_icon);// icon1 is then horizontal LinearLayout
icon1.setBackgroundResource(R.drawable.ac_overlay);
icon1.setOrientation(LinearLayout.HORIZONTAL);
icon1.setTag(NORMAL);
// TextView contained in a vertical Layout
LinearLayout lay = new LinearLayout(this);
icon1.addView(lay);
lay.setLayoutParams(temp_lay);
lay.setBackgroundColor(Color.TRANSPARENT);
lay.setOrientation(LinearLayout.VERTICAL);
TextView text1 = new TextView(this);
lay.addView(text1);
text1.setLayoutParams(tests);
text1.setText("Master Bedroom");
text1.setTextSize(12);
text1.setTextColor(Color.WHITE);
// other elements
ImageButton image = new ImageButton(this);
icon1.addView(image);
image.setLayoutParams(lp_ineer_ver);
image.setImageResource(R.drawable.grpbuttonfocus6);
image.setBackgroundColor(Color.TRANSPARENT);
Button testbut = new Button(this);
icon1.addView(testbut);
testbut.setLayoutParams(lp_ineer_ver);
testbut.setText(" 8");
testbut.setTextSize(12);
testbut.setBackgroundColor(Color.TRANSPARENT);
testbut.setTextColor(Color.WHITE);
ImageButton testcol = new ImageButton(this);
icon1.addView(testcol);
testcol.setLayoutParams(lp_ineer_ver);
testcol.setImageResource(R.drawable.home_cool);
testcol.setBackgroundColor(Color.TRANSPARENT);
// the TextView that's declared directly in the Horizontal Layout
TextView text2 = new TextView(this);
icon1.addView(text2);
text2.setLayoutParams(lp_ineer_ver);
text2.setText("00");
text2.setTextSize(12);
Try this..
Juse two changes in your code like below for first textview icon1.addView(text1); and the last textview text2.setLayoutParams(tests);
First
TextView text1 = new TextView(this);
icon1.addView(text1); //correction here
text1.setLayoutParams(tests);
text1.setText("Master Bedroom");
text1.setTextSize(12);
and Second
TextView text2 = new TextView(this);
icon1.addView(text2);
text2.setLayoutParams(tests); //correction here
text2.setText("00");
text2.setTextSize(12);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
and you can have Try adding the following code for applying the layout params to the TextView
LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(LinearLayout.CENTER_IN_PARENT);
textView.setLayoutParams(lp);
in XML file :
android:gravity = "center"
In my android app I need a layout as shown below
A relative layout is used as the parent layout inside I use 4 relative layout to arrange the contents- header layout 1,header layout 2, content layout(objRLScrollView -scroll view is inside this layout) and a footer layout
I use the below code to create the above layout
public class Login1 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
RelativeLayout objRLBody=new RelativeLayout(this);
objRLBody.setId(1001);
RelativeLayout.LayoutParams objLLBodyParams=new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
objRLBody.setBackgroundColor(Color.parseColor("#d8e4f3"));
objRLBody.setLayoutParams(objLLBodyParams);
/* header portion starts here*/
RelativeLayout objRLActionBar=new RelativeLayout(this);
objRLActionBar.setId(1002);
RelativeLayout.LayoutParams objRLActionBarParams=new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,50);
objRLActionBarParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
objRLActionBar.setLayoutParams(objRLActionBarParams);
objRLActionBar.setBackgroundColor(Color.parseColor("#2e4862"));
objRLBody.addView(objRLActionBar);
RelativeLayout objRLSelectedActionBar=new RelativeLayout(this);
objRLSelectedActionBar.setId(1003);
RelativeLayout.LayoutParams objRLSelectedActionBarParams=new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
objRLSelectedActionBarParams.addRule(RelativeLayout.BELOW,objRLActionBar.getId());
objRLSelectedActionBar.setLayoutParams(objRLSelectedActionBarParams);
objRLSelectedActionBar.setBackgroundColor(Color.WHITE);
objRLSelectedActionBar.setGravity(Gravity.CENTER|Gravity.LEFT);
TextView objTVSelectedAction = new TextView(this);
RelativeLayout.LayoutParams objTVSelectedActionParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
objTVSelectedAction.setLayoutParams(objTVSelectedActionParams);
objTVSelectedAction.setGravity(Gravity.CENTER);
objTVSelectedAction.setTextColor(Color.BLACK);
objTVSelectedAction.setTextSize(TypedValue.COMPLEX_UNIT_DIP,18);
objTVSelectedAction.setText("Scroll Test");
objTVSelectedAction.setTypeface(null, Typeface.BOLD);
objTVSelectedAction.setPadding(4, 0, 4, 0);
objTVSelectedAction.setId(1004);
objRLSelectedActionBar.addView(objTVSelectedAction);
objRLBody.addView(objRLSelectedActionBar);
/* header portion ends here*/
/* Relative layout which hold scroll view*/
RelativeLayout objRLScrollView = new RelativeLayout(this);
objRLScrollView.setId(1005);
objRLScrollView.setBackgroundColor(Color.parseColor("#d8e4f3"));
objRLScrollView.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL);
RelativeLayout.LayoutParams objRLScrollViewParams = new RelativeLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
objRLScrollViewParams.addRule(RelativeLayout.BELOW ,objTVSelectedAction.getId());
ScrollView objScrollView=new ScrollView(this);
objScrollView.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
objScrollView.setFillViewport(true);
/* contents */
RelativeLayout objRLContent = new RelativeLayout(this);
objRLContent.setId(1006);
objRLContent.setBackgroundColor(Color.parseColor("#d8e4f3"));
objRLContent.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL);
RelativeLayout.LayoutParams objRLContentParams = new RelativeLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
// objRLContentParams.addRule(RelativeLayout.BELOW ,objTVSelectedAction.getId());
objRLContent.setLayoutParams(objRLContentParams);
/* adding layouts containing Edittext (for testing scroll view) */
RelativeLayout rl2= addEditText(objRLContent,null,1);
RelativeLayout rl3= addEditText(objRLContent,rl2,2);
RelativeLayout rl4= addEditText(objRLContent,rl3,3);
RelativeLayout rl5= addEditText(objRLContent,rl4,4);
RelativeLayout rl6= addEditText(objRLContent,rl5,5);
RelativeLayout rl7= addEditText(objRLContent,rl6,6);
RelativeLayout rl8= addEditText(objRLContent,rl7,7);
RelativeLayout rl9= addEditText(objRLContent,rl8,8);
RelativeLayout rl10= addEditText(objRLContent,rl9,9);
RelativeLayout rl11= addEditText(objRLContent,rl10,10);
RelativeLayout rl12= addEditText(objRLContent,rl11,11);
RelativeLayout rl13= addEditText(objRLContent,rl12,12);
objScrollView.addView(objRLContent); // adding contents to scroll view
objRLScrollView.addView(objScrollView); // Adding scroll view to a relative layout
objRLBody.addView(objRLScrollView); // adding relative layout to body layout
// adding footer to body
RelativeLayout objRLFooter=new RelativeLayout(this);
RelativeLayout.LayoutParams objRLFooterParams=new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,25);
objRLFooterParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,objRLBody.getId());
objRLFooter.setLayoutParams(objRLFooterParams);
objRLFooter.setBackgroundColor(Color.parseColor("#2e4862"));
objRLFooter.setGravity(Gravity.CENTER);
objRLBody.addView(objRLFooter);
this.setContentView(objRLBody);
}
private RelativeLayout addEditText(RelativeLayout objRLContent,RelativeLayout layoutAbove,int i ) {
RelativeLayout objRLEditText = new RelativeLayout(this);
objRLEditText.setId(1100+i);
RelativeLayout.LayoutParams objRLEditTextParams = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
if(layoutAbove!=null)
objRLEditTextParams.addRule(RelativeLayout.BELOW,layoutAbove.getId());
objRLEditText.setLayoutParams(objRLEditTextParams);
objRLEditText.setPadding(8, 2, 8, 2);
EditText objETData = new EditText(this);
objETData.setId(1300+i);
RelativeLayout.LayoutParams objETDataParams=new RelativeLayout.LayoutParams(200,LayoutParams.WRAP_CONTENT);
objETData.setLayoutParams(objETDataParams);
objETData.setPadding(8, 0, 8, 0);
objETData.setTextSize(TypedValue.COMPLEX_UNIT_DIP,18);
objETData.setText(""+i);
objETData.setSingleLine(true);
objRLEditText.addView(objETData);
objRLContent.addView(objRLEditText);
return objRLEditText;
}
}
and the above code gives out put as shown below
The header layouts becomes invisible due to the presence of scrollView. What is wrong with my code? what should i do to get a layout with scrollable contents with fixed header and footer? plz help.
1- Set your ScrollView's height and width to match_parent.
2- Set your Scrollview's Top and Bottom padding equal to your top and bottom bar height.
3- Set your top bar's alignment to alignparenttop
4- Set your bottom bar's alignment to alignparentbottom
Note: If you want a good compatibility library for action bars consider using ActionBarSherlock.
Use a LinearLayout instead of a RelativeLayout for your Parent Container. Set the orientation to the LinearLayout to vertical and set a LayoutWeight to 1 for the ScrollView.
Try followin I have edited your code, which working Ok.
public class Scrolling2Activity extends Activity {
int myid = 1000, pos=-1, etpos=-1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout rlBody = new RelativeLayout(this);
rlBody.setId(++myid);
RelativeLayout.LayoutParams bodyParams = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
rlBody.setLayoutParams(bodyParams);
rlBody.setBackgroundColor(Color.BLACK);
// Header-1
RelativeLayout head1 = new RelativeLayout(this);
head1.setId(++myid);
RelativeLayout.LayoutParams head1Params = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, 25);
head1Params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
head1.setLayoutParams(head1Params);
head1.setBackgroundColor(Color.BLUE);
rlBody.addView(head1, ++pos);
// Header-2
RelativeLayout head2 = new RelativeLayout(this);
head2.setId(++myid);
RelativeLayout.LayoutParams head2Params = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, 25);
head2Params.addRule(RelativeLayout.BELOW, head1.getId());
head2.setLayoutParams(head2Params);
head2.setBackgroundColor(Color.CYAN);
// TextView in Header-2
TextView tv1 = new TextView(this);
tv1.setId(++myid);
RelativeLayout.LayoutParams tv1Params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tv1.setLayoutParams(tv1Params);
tv1.setGravity(Gravity.CENTER);
tv1.setTextColor(Color.BLACK);
tv1.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
tv1.setText("Scroll Test");
tv1.setTypeface(null, Typeface.BOLD);
tv1.setPadding(4, 0, 4, 0);
//tv1.setBackgroundColor(Color.DKGRAY);
head2.addView(tv1);
rlBody.addView(head2, ++pos);
// RelativeLayout For ScrollView
RelativeLayout scrollHolder = new RelativeLayout(this);
scrollHolder.setId(++myid);
RelativeLayout.LayoutParams scrollHolderParams = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
scrollHolderParams.addRule(RelativeLayout.BELOW, head2.getId());
scrollHolder.setLayoutParams(scrollHolderParams);
scrollHolder.setBackgroundColor(Color.DKGRAY);
scrollHolder.setGravity(Gravity.CENTER_VERTICAL);
// ScrollView
ScrollView scroll = new ScrollView(this);
scroll.setId(++myid);
scroll.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
scroll.setBackgroundColor(Color.GREEN);
// RelativeLayout
RelativeLayout etHolder = new RelativeLayout(this);
etHolder.setId(++myid);
RelativeLayout.LayoutParams etHolderParams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
etHolder.setLayoutParams(etHolderParams);
etHolder.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
etHolder.setPadding(0, 0, 0, 25);
// Creating EditTextes and Adding to etHolder AS you have done
RelativeLayout rl2 = addEditText(etHolder, null, 1);
RelativeLayout rl3 = addEditText(etHolder, rl2, 2);
RelativeLayout rl4 = addEditText(etHolder, rl3, 3);
RelativeLayout rl5 = addEditText(etHolder, rl4, 4);
RelativeLayout rl6 = addEditText(etHolder, rl5, 5);
RelativeLayout rl7 = addEditText(etHolder, rl6, 6);
RelativeLayout rl8 = addEditText(etHolder, rl7, 7);
RelativeLayout rl9 = addEditText(etHolder, rl8, 8);
RelativeLayout rl10 = addEditText(etHolder, rl9, 9);
RelativeLayout rl11 = addEditText(etHolder, rl10, 10);
RelativeLayout rl12 = addEditText(etHolder, rl11, 11);
RelativeLayout rl13 = addEditText(etHolder, rl12, 12);
scroll.addView(etHolder); // adding RelativeLayout = etHolder to ScrollView = scroll
scrollHolder.addView(scroll); // adding ScrollView = scroll to RelativeLayout = scrollHolder
rlBody.addView(scrollHolder, ++pos);
// RelativeLayout for Footer
RelativeLayout footer = new RelativeLayout(this);
footer.setId(++myid);
RelativeLayout.LayoutParams footerParams = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, 25);
footerParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
footer.setLayoutParams(footerParams);
footer.setBackgroundColor(Color.MAGENTA);
rlBody.addView(footer, ++pos);
setContentView(rlBody);
}
// Method as you have done
private RelativeLayout addEditText(RelativeLayout objRLContent,
RelativeLayout layoutAbove, int i) {
RelativeLayout objRLEditText = new RelativeLayout(this);
objRLEditText.setId(1100 + i);
RelativeLayout.LayoutParams objRLEditTextParams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
if (layoutAbove != null)
objRLEditTextParams.addRule(RelativeLayout.BELOW,
layoutAbove.getId());
objRLEditText.setLayoutParams(objRLEditTextParams);
objRLEditText.setPadding(8, 2, 8, 2);
EditText objETData = new EditText(this);
objETData.setId(1300 + i);
RelativeLayout.LayoutParams objETDataParams = new RelativeLayout.LayoutParams(
200, LayoutParams.WRAP_CONTENT);
objETData.setLayoutParams(objETDataParams);
objETData.setPadding(8, 0, 8, 0);
objETData.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
objETData.setText("" + i);
objETData.setSingleLine(true);
objRLEditText.addView(objETData);
objRLContent.addView(objRLEditText);
return objRLEditText;
}
}
once try this one
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(new BallBounce(this));
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
// this.requestWindowFeature(Window.FEATURE_NO_TITLE);
RelativeLayout objRLBody=new RelativeLayout(this);
objRLBody.setId(1001);
RelativeLayout.LayoutParams objLLBodyParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
objRLBody.setBackgroundColor(Color.parseColor("#d8e4f3"));
objRLBody.setLayoutParams(objLLBodyParams);
/* header portion starts here*/
RelativeLayout objRLActionBar=new RelativeLayout(this);
objRLActionBar.setId(1002);
RelativeLayout.LayoutParams objRLActionBarParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,50);
objRLActionBarParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
objRLActionBar.setLayoutParams(objRLActionBarParams);
objRLActionBar.setBackgroundColor(Color.parseColor("#2e4862"));
objRLBody.addView(objRLActionBar);
RelativeLayout objRLSelectedActionBar=new RelativeLayout(this);
objRLSelectedActionBar.setId(1003);
RelativeLayout.LayoutParams objRLSelectedActionBarParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
objRLSelectedActionBarParams.addRule(RelativeLayout.BELOW,objRLActionBar.getId());
objRLSelectedActionBar.setLayoutParams(objRLSelectedActionBarParams);
objRLSelectedActionBar.setBackgroundColor(Color.WHITE);
objRLSelectedActionBar.setGravity(Gravity.CENTER|Gravity.LEFT);
TextView objTVSelectedAction = new TextView(this);
RelativeLayout.LayoutParams objTVSelectedActionParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
objTVSelectedAction.setLayoutParams(objTVSelectedActionParams);
objTVSelectedAction.setGravity(Gravity.CENTER);
objTVSelectedAction.setTextColor(Color.BLACK);
objTVSelectedAction.setTextSize(TypedValue.COMPLEX_UNIT_DIP,18);
objTVSelectedAction.setText("Scroll Test");
objTVSelectedAction.setTypeface(null, Typeface.BOLD);
objTVSelectedAction.setPadding(4, 0, 4, 0);
objTVSelectedAction.setId(1004);
objRLSelectedActionBar.addView(objTVSelectedAction);
objRLBody.addView(objRLSelectedActionBar);
/* header portion ends here*/
/* Relative layout which hold scroll view*/
RelativeLayout objRLScrollView = new RelativeLayout(this);
objRLScrollView.setId(1005);
objRLScrollView.setBackgroundColor(Color.parseColor("#d8e4f3"));
objRLScrollView.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL);
RelativeLayout.LayoutParams objRLScrollViewParams = new RelativeLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
objRLScrollViewParams.addRule(RelativeLayout.BELOW ,objTVSelectedAction.getId());
ScrollView objScrollView=new ScrollView(this);
objScrollView.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
objScrollView.setFillViewport(true);
/* contents */
RelativeLayout objRLContent = new RelativeLayout(this);
objRLContent.setId(1006);
objRLContent.setBackgroundColor(Color.parseColor("#d8e4f3"));
objRLContent.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL);
RelativeLayout.LayoutParams objRLContentParams = new RelativeLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
// objRLContentParams.addRule(RelativeLayout.BELOW ,objTVSelectedAction.getId());
objRLContent.setLayoutParams(objRLContentParams);
/* adding layouts containing Edittext (for testing scroll view) */
RelativeLayout rl2= addEditText(objRLContent,null,1);
RelativeLayout rl3= addEditText(objRLContent,rl2,2);
RelativeLayout rl4= addEditText(objRLContent,rl3,3);
RelativeLayout rl5= addEditText(objRLContent,rl4,4);
RelativeLayout rl6= addEditText(objRLContent,rl5,5);
RelativeLayout rl7= addEditText(objRLContent,rl6,6);
RelativeLayout rl8= addEditText(objRLContent,rl7,7);
RelativeLayout rl9= addEditText(objRLContent,rl8,8);
RelativeLayout rl10= addEditText(objRLContent,rl9,9);
RelativeLayout rl11= addEditText(objRLContent,rl10,10);
RelativeLayout rl12= addEditText(objRLContent,rl11,11);
RelativeLayout rl13= addEditText(objRLContent,rl12,12);
objScrollView.addView(objRLContent); // adding contents to scroll view
objRLScrollView.addView(objScrollView); // Adding scroll view to a relative layout
objRLBody.addView(objRLScrollView); // adding relative layout to body layout
// adding footer to body
RelativeLayout objRLFooter=new RelativeLayout(this);
RelativeLayout.LayoutParams objRLFooterParams=new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,25);
objRLFooterParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,objRLBody.getId());
objRLFooter.setLayoutParams(objRLFooterParams);
objRLFooter.setBackgroundColor(Color.parseColor("#2e4862"));
objRLFooter.setGravity(Gravity.CENTER);
objRLBody.addView(objRLFooter);
this.setContentView(objRLBody);
}
private RelativeLayout addEditText(RelativeLayout objRLContent,RelativeLayout layoutAbove,int i ) {
RelativeLayout objRLEditText = new RelativeLayout(this);
objRLEditText.setId(1100+i);
RelativeLayout.LayoutParams objRLEditTextParams = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
if(layoutAbove!=null)
objRLEditTextParams.addRule(RelativeLayout.BELOW,layoutAbove.getId());
objRLEditText.setLayoutParams(objRLEditTextParams);
objRLEditText.setPadding(8, 2, 8, 2);
EditText objETData = new EditText(this);
objETData.setId(1300+i);
RelativeLayout.LayoutParams objETDataParams=new RelativeLayout.LayoutParams(200,LayoutParams.WRAP_CONTENT);
objETData.setLayoutParams(objETDataParams);
objETData.setPadding(8, 0, 8, 0);
objETData.setTextSize(TypedValue.COMPLEX_UNIT_DIP,18);
objETData.setText(""+i);
objETData.setSingleLine(true);
objRLEditText.addView(objETData);
objRLContent.addView(objRLEditText);
return objRLEditText;
}
}
finally I got the desired layout by simply rearrange the order of adding inner layout to the body layout in the above code, and the order is as follows
objRLBody.addView(objRLScrollView);
objRLBody.addView(objRLActionBar);
objRLBody.addView(objRLSelectedActionBar);
objRLBody.addView(objRLFooter);
also need to set scrollview padding, if we not set padding to scrollview (header layout width as top padding and footer with as bottom) the components view may blocked by header or footer layout.
This worked for me well....
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/light_gradient">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
//put your UI here
</RelativeLayout>
</ScrollView>