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.
Related
I need to create a button programatically and have it centered on the layout, both horizontally and vertically. I am trying with the following code:
LinearLayout ll = (LinearLayout)findViewById(R.id.layoutItem);
Button b = new Button(this);
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.button));
b.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
b.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
ll.addView(b);
But it's not working. The button comes out on top all to the left.
Any clues on how to fix this?
I would do something like:
LinearLayout.LayoutParams ll = (LinearLayout.LayoutParams)b.getLayoutParams();
ll.gravity = Gravity.CENTER;
b.setLayoutParams(ll);
see if that works.
Or you can use a RelativeLayout as your parent View and do the following:
this.testButton= (Button) this.findViewById(R.id.testButton);
RelativeLayout.LayoutParams testLP = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
testLP.addRule(RelativeLayout.CENTER_IN_PARENT);
this.testButton.setLayoutParams(testLP);
You can set several rules for the RelativeLayout.
I'm creating a LinearLayout programmatically and I'm adding to this layout three buttons, but they're showed one on top of the other.
How I can show the buttons in line?
Reading around I probably understood that I need to set up a LayoutParams but I didn't figured out how..
I've tried with this but it didn't did the trick..
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
);
ll.addView(b1, layoutParams);
ll.addView(b2, layoutParams);
ll.addView(b3, layoutParams);
Thanks for any help!
EDIT:
Probably I needed to add more details.
I have also other stuff in the Layout but I does'n matter, I've created an additional layout just for the buttons.
Now the buttons are in line but they have different width.. : /
I've tried with this but it didn't help..
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
1f
);
ll.setOrientation(LinearLayout.HORIZONTAL);
Lets you set buttons in horizontal line allignment.
But to give balanced space to all three buttons. you must set weight property for all the three Button objects to 1.
Edit:
Do this for all buttons.
LinearLayout.LayoutParams params = button.getLayoutParams();
params.weight = 1;
button.setLayoutParams(params);
to set weight for all buttons.
Regards,
Aqif Hamid
Try this:
ll.setOrientation(LinearLayout.HORIZONTAL);
All have MATCH_PARENT should have LayoutParams.WRAP_CONTENT in width or height (in at least one as per LinearLayout orientation )
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
Try it:
LinearLayout gvDivisao = (LinearLayout) findViewById(R.id.gvDivisao);
LayoutInflater inflater = getLayoutInflater();
LayoutParams btDivLayoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f);
Button btDivA = (Button)inflater.inflate(R.layout.button_divisao, null);
btDivA.setText("A");
gvDivisao.addView( btDivA, btDivLayoutParams);
I have this code:
ImageButton call = new ImageButton(context);
call.setId(9001+result.index);
call.setBackgroundResource(R.drawable.small_call);
LinearLayout.LayoutParams call_params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
call.setLayoutParams(call_params);
It renders the button the way I want it, but when I do this:
ImageButton call = new ImageButton(context);
call.setId(9001+result.index);
call.setBackgroundResource(R.drawable.small_call);
LinearLayout.LayoutParams call_params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT,
Gravity.CENTER_VERTICAL | Gravity.RIGHT
);
call.setLayoutParams(call_params);
It stretches the image, messing up the aspect ratio, and makes it blurry/pixelated.
So what is the correct way to render the image as in the first code snippet, but place the button where the second code snippet puts it?
Thanks in advance.
Use RelativeLayout or FrameLayout instead of LinearLayout.
See also layout_gravity in LinearLayout.
This is a tutorial: A Visual Guide to Relative Layouts In Android.
I need to create a button in Java. Below is my code:
Button b = new Button(MyClass.this);
b.requestLayout();
LayoutParams lp = b.getLayoutParams();
lp.height = LayoutParams.WRAP_CONTENT;
lp.width = LayoutParams.WRAP_CONTENT;
b.setLayoutParams(lp);
b.setText("bla");
b.setTextSize(16);
b.setOnClickListener(myListener);
I then add this button to the bottom of a ListView:
getListView().addFooterView(b);
However this crashes, because getLayoutParams returns null.
Even if I create new LayoutParams instead of getLayoutParams, i.e.:
Button b = new Button(MyClass.this);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
b.setLayoutParams(lp);
b.setText("bla");
b.setTextSize(16);
b.setOnClickListener(myListener);
then the application crashes. Without setLayoutParams, it runs fine, but my button is not sized properly.
How can I size my button?
You have to add this button to a view if you want to get LayoutParams from button. Or just create new LayoutParams and set it.
It's returning null because when you programmatically create a widget, it has no layout params! (Until you add it to a view, then it receives defaults from the LayoutManager)
edit: above is referring to line 3 of your code
Set them like this:
TextView moneyTV = new TextView(this);
LayoutParams lp1 = new LayoutParams(HeightParamHere, WidthParamHere, WeightParamHere);
moneyTV.setLayoutParams(lp1);
Edit2: here's some readybake replacement code.
Button b = new Button(MyClass.this);
b.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
b.setText("bla");
b.setTextSize(16);
b.setOnClickListener(myListener);
Assuming you have defined myListener, this should work.
Because I'm adding this button via ListView::addFooterView, it turns out I had to use the ListView type.
b.setLayoutParams(new ListView.LayoutParams(ListView.LayoutParams.WRAP_CONTENT, ListView.LayoutParams.WRAP_CONTENT));
Using this instead of just LayoutParams resolves my crash. Hope this helps others.
I Have this simple Code:
layout = new RelativeLayout(this);
bottom = new LinearLayout(this);
Button close = new Button(this);
close.setText("Close");
bottom.addView(close);
layout.addView(bottom);
setContentView(layout);
How can i put my button at the bottom of the window i prefer to determine the x,y cords but any solution will be good.
since the absoluteLayout is deprecated i tried using the Relativelayout as the api adviced with no succes.
i'm not using XML at all is that a problem ?
You can set LayoutParametrs (RelativeLayout.LayoutParams) to your elements. And don't forget to set LayoutParams to parent view.
RelativeLayout.LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
lp.addRule(ALIGN_PARENT_BOTTOM);
button.setLayoutParams(lp)