I'm new in android programming.
I just discover that the xml is not the only way to set a layout. So, I'm trying to understand the programmatically set layout. I've been trying to change the position of the button. How can I set the layout gravity so that the button will be positioned in intended axes, let say bottom and center?
Btw, is the icicle same thing as savedInstanceState?
Here's the coding that I stumbled upon. Can you please show me the right way to set the gravity?
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout one = new LinearLayout(this);
myButton1 = new Button1(this);
one.addView(myButton1,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));
myButton2 = new Button2(this);
one.addView(mButton2,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));
setContentView(one);
I think, you are looking for
LinearLayout one = new LinearLayout(this);
one.setGravity(Gravity.BOTTOM | Gravity.CENTER); //to show LinearLayout gravity
Button myButton1 = new Button(this);
one.addView(myButton1,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));
myButton1.setText("First");
myButton1.setGravity(Gravity.CENTER); //to show text gravity in button
Button myButton2 = new Button(this);
one.addView(myButton2,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));
myButton2.setText("Second");
myButton2.setGravity(Gravity.CENTER); //to show text gravity
setContentView(one);
icicle is sometimes used as the name of the parameter.It's just a
placeholder for one of the formal parameters onCreate takes one Bundle parameter. It's up to you what to call it.
Some properties of LinearLayout https://stackoverflow.com/a/19065951/2826147
yourLinearLayout.setGravity(Gravity.BOTTOM);
Simple as that
You need to use setGravity(),
For example button.setGravity(Gravity.CENTER | Gravity.BOTTOM);
Related
I am adding a dynamic layout view which looks like this.
I am appending this view on button click. Here is the code for this view on button click
public void addDynamicView() {
RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
LinearLayout lHori = new LinearLayout(this);
lHori.setOrientation(LinearLayout.HORIZONTAL);
//lHori.setWeightSum(3);
lHori.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
LinearLayout lVertical = new LinearLayout(this);
lVertical.setOrientation(LinearLayout.VERTICAL);
//lVertical.setWeightSum(2);
lVertical.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
Button button = new Button(this);
button.setLayoutParams(new LinearLayout.LayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)));
button.setVisibility(View.VISIBLE);
button.setText("button");
TextView txtEmpName = new TextView(this);
txtEmpName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
txtEmpName.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
txtEmpName.setText("txt1");
TextView txtEmpDropTime = new TextView(this);
txtEmpDropTime.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
txtEmpDropTime.setText("txt2");
lVertical.addView(txtEmpName);
lVertical.addView(txtEmpDropTime);
lHori.addView(lVertical);
lHori.addView(button);
relativeLayout.addView(lHori);
linear.addView(relativeLayout);
}
The problem is I want all the buttons to be placed below the click button.
But I get output like this
How can I place all Button with text Button below the Click button?
If you two do with xml than it's so simple just make a layout which you want after click on click button after that set visibility gone for all layout accept click button next in Java on button click you can call visibility view to make that layout ...
Hope so it will works for you..
You can create a LinearLayout lButton:
LinearLayout lButton = new LinearLayout(this);
lButton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
then set its gravity as Gravity.END
lButton.setGravity(Gravity.END)
and add the button into it
lButton.addView(button);
lHori.addView(lVertical);
lHori.addView(lButton);
I am trying to add a button programmatically in a custom view implementation. App is API 15+.
Here I am facing issue with button text not getting centered. It sounds trivial, but it is not working. Below is code in question. output is
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
mSubmit = new Button(mContext);
mSubmit.setTextColor(Color.WHITE);
mSubmit.setText("Submit");
mSubmit.setTextSize(getResources().getDimension(R.dimen.headertxt));
addView(mSubmit, params);
Set equal padding for all sides of your button:
for example:
myButton.setPadding(2,2,2,2);
Before:
After adding our padding:
addContentView worked for me!
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
mSubmit = new Button(mContext);
mSubmit.setTextColor(Color.WHITE);
mSubmit.setText("Submit");
mSubmit.setTextSize(getResources().getDimension(R.dimen.headertxt));
addContentView(mSubmit, params);
It's not centered vertically, so try
mSubmit.setGravity(Gravity.CENTER_VERTICAL);
Could also be some custom background for the button that's not correctly set up -- it's not very clear from the image.
And note : The gravity you set on the layout params is a completely different thing than the gravity you set on the button.
Trying to dynamically add a textView and a button into a LinearLayout, then add this layout into a main layout that setContentView use
So its something like this
T - text view
B - button
Ltb - layout that contains T and B
Lm - Main layout that contains Ltb
Then use this.setContentView(Lm) to show to result
Roles:
T must be on the left.
B must be on right of the screen within the layout
All element above are declared dynamically, without using layout xml
Actual result:
Display fine. but when I type in text that is longer than the screen width, the Button got pushed outside of the screen and gone.
Problem, is it something my dynamic layout doing wrong ?
Code here:
public SearchBar(Context c){
et=new EditText(c);
bt=new Button(c);
et.setHint("added et");
bt.setText("added btn");
ll=new LinearLayout(c);
setLinearLayout();
et.setLayoutParams(flowLeft());
bt.setLayoutParams(flowRight());
ll.addView(et);
ll.addView(bt);
}
private RelativeLayout.LayoutParams flowRight(){
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
//params.weight = 1.0f;
//params.gravity=Gravity.RIGHT;
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
return params;
}
private RelativeLayout.LayoutParams flowLeft(){
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
//params.weight = 1.0f;
//params.gravity=Gravity.RIGHT;
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
return params;
}
private void setLinearLayout(){
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setLayoutParams(params);
}
// try this way, hope this will help you...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ltb = new LinearLayout(this);
TextView T = new TextView(this);
Button B =new Button(this);
LinearLayout.LayoutParams ltbParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams TParms = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT,1f);
LinearLayout.LayoutParams BParms = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
T.setLayoutParams(TParms);
B.setLayoutParams(BParms);
T.setText("Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text");
B.setText("Button");
ltb.addView(T);
ltb.addView(B);
ltb.setLayoutParams(ltbParams);
setContentView(ltb);
}
Do like this. This may help you.
Ltb.setWeightSum(100);
Ltb.setOrientation(LinearLayout.HORIZONTAL);
T.setLayoutParams(new LinearLayout.LayoutParams(0,LayoutParams.MATCH_PARENT,50));
B.setLayoutParams(new LinearLayout.LayoutParams(0,LayoutParams.MATCH_PARENT,50));
I have done some research, but the answer i found does not work for me. Here is some part of my code. the R.id.relative is the id of the relativelayout in the xml file
RelativeLayout RL = (RelativeLayout) findViewById(R.id.relative);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView title = new TextView(this);
title.setText(" History ");
title.setId(99099);
title.setTextSize(30);
title.setGravity(Gravity.CENTER);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
title.setLayoutParams(params);
RL.addView(title);
RelativeLayout.LayoutParams test_params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
Button test = new Button(this);
test.setText(" Back ");
test_params.addRule(RelativeLayout.BELOW,99099);
test.setId(199291);
test.setGravity(Gravity.CENTER);
test.setLayoutParams(test_params);
RL.addView(test);
RelativeLayout.LayoutParams test_params2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
Button test2 = new Button(this);
test2.setText(" Clear ");
test_params2.addRule(RelativeLayout.BELOW,test.getId());
test2.setGravity(Gravity.CENTER);
test.setLayoutParams(test_params2);
RL.addView(test2);
all 3 items did show up, but they stack together. I can't get them below another.
Could anyone help ?
From what I've been able to find out, you have to add the view using LayoutParams. Here's an example:
LinearLayout linearLayout = new LinearLayout(this);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
relativeParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
parentView.addView(linearLayout, relativeParams);
And to relatively position your items programmatically you have to assign ids to them, this stops them from 'overlapping'.
TextView tv1 = new TextView(this);
tv1.setId(1);
TextView tv2 = new TextView(this);
tv2.setId(2);
Then addRule(RelativeLayout.RIGHT_OF, tv1.getId());
Change test.setLayoutParams(test_params2); to test2.setLayoutParams(test_params2);
Explanation: You have inadvertently set the layout params of test a second time, with params that instruct it to be below itself, which I guess just puts it top-left (default placement in a RelativeLayout). Since you never give test2 any layout params, it also gets default placement. So everything is at the top and thus appear atop each other.
Incidentally, if you just want them arranged linearly, why not use a vertical LinearLayout?
The rule you have added is causing your view to be stacked together.If you need to add it should be used by the following rule.
test_params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);
Next, add the view to your RelativeLayout with your LayoutParams:
RL.addView(yourAdView, rLParams);
same goes for the each cases.try to run.It will solve your problem
I' m at this point stuck : I want to have a scrollview + a fixed button at the bottom, but Programatically Way ! I can' t go with XML for some technical reason.
Actually i have this :
//Is it really usefull Relative View?
RelativeLayout layout = new RelativeLayout(this);
ScrollView sv = new ScrollView(this);
sv.setId(2);
// What is it? RelativeLayout.LayoutParams?
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, sv.getId());
sv.setLayoutParams(new ViewGroup.LayoutParams(480, 800));
layout.addView(saveButton, lp);
layout.addView(sv);
I do the first 3 page Google on "fixed button and scrollview Android programatically"
Im beginner on Android, so, don' t hesitate to comment on my code some hints ;)
Thx for your help.
try this
LinearLayout layout = new LinearLayout(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(lp);
ScrollView scroll = new ScrollView(this);
LinearLayout.LayoutParams slp = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,0, 1.0f);
scroll.setLayoutParams(slp);
Button btn = new Button(this);
ViewGroup.LayoutParams blp = new ViewGroup.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
btn.setLayoutParams(blp);
btn.setText("Click Me");
layout.addView(scroll);
layout.addView(btn);
setContentView(layout);
If you are trying to create a view where the upper portion of the screen is a scrolling list, and the bottom portion is a button, put the scrollview inside a linearlayout, and put the button in the linearlayout below the scrollview.
http://developer.android.com/reference/android/widget/LinearLayout.html