My app's layout is FrameLayout. I want to align an ImageButton to right top of the framelayout. I tried below code not worked.
mImageButton = new ImageButton(mContext);
LayoutParams ll = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT );
ll.gravity = Gravity.RIGHT;
mImageButton.setLayoutParams(ll);
mImageButton.setImageDrawable(getResources().getDrawable(R.drawable.ic_menu_moreoverflow_normal_holo_light));
this.addView(mImageButton);
expected output
I think you should change the construction code of LayoutParams to:
LayoutParams ll = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ll.gravity = Gravity.TOP | Gravity.RIGHT;
In your code, you set the layout_width and layout_height value to MATCH_PARENT , so your ImageView will fill all your parent FrameLayout.
Try this,
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.TOP | Gravity.RIGHT;
mImageButton.setLayoutParams(params);
I think you need to provide height width in layout params. Because with match_parent it will take width and height of parent or you can use WRAP_CONTENT.
mImageButton = new ImageButton(mContext);
LayoutParams ll = new LayoutParams(width,height);
ll.gravity = Gravity.RIGHT | Gravity.TOP;
mImageButton.setLayoutParams(ll);
mImageButton.setImageDrawable(getResources().getDrawable(R.drawable.ic_menu_moreoverflow_normal_holo_light));
this.addView(mImageButton);
Related
I want to play gif in alertdialog.
I write this code:
AlertDialog.Builder builder = new AlertDialog.Builder(myThis);
builder.setCancelable(false);
WebView view = new WebView(myThis);
view.loadUrl("file:///android_asset/gif_pokemon.gif");
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
params.gravity=Gravity.CENTER;
view.setLayoutParams(params);
builder.setView(view);
dlg_p=builder.create();
WindowManager.LayoutParams wlmp = dlg_p.getWindow().getAttributes();
wlmp.gravity = Gravity.CENTER ;
dlg_p.show();
but the problem is gif is not places in center. Its gravity is left.
How can I set it to center?
Wrap the WebView using LinearLayout,
the LayoutParams of the LinearLayout is set to match_parent for the width and wrap_content for the height, if needed fully center then use match_parent for the height too.
set the LinearLayout gravity to center, you can also use centerVertical or centerHorizontal.
Hope it helps :)
AlertDialog.Builder builder = new AlertDialog.Builder(myThis);
builder.setCancelable(false);
LinearLayout linearLayout = new LinearLayout(myThis);
linearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
linearLayout.setGravity(Gravity.CENTER);
WebView view = new WebView(myThis);
view.loadUrl("file:///android_asset/gif_pokemon.gif");
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity=Gravity.CENTER;
view.setLayoutParams(params);
linearLayout.addView(view);
builder.setView(linearLayout);
dlg_p=builder.create();
dlg_p.show();
How can I programmatically set button in right corner of EditText?
FrameLayout fl = new FrameLayout(this);
lv.addView(fl);
searchTxt = new EditText(this);
fl.addView(searchTxt);
btnSearch = new Button(this);
I tried this
btnSearch.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
fl.addView(btnSearch);
and
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0);
param.gravity = Gravity.RIGHT;
fl.addView(btnSearch, param);
But the button is still in the left corner.
You need to set layout param for button as following:
FrameLayout.LayoutParams param = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0);
param.gravity = Gravity.RIGHT;
and set it to button
btnSearch.setLayoutParam(param);
f1.addView(btnSearch);
Set the layoutparams to the LinearLayout to Fill_Parent in width
I have this code :
FrameLayout game = new FrameLayout(this);
LinearLayout gameWidgets = new LinearLayout (this);
game.addView(gameWidgets);
How to set the LinearLayout in the buttom of the FrameLayout ?
Try this,
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.BOTTOM;
game.addView(GameWidgets, params);
Use this code:
FrameLayout.LayoutParams p = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM
| Gravity.CENTER_HORIZONTAL);
game.addView(GameWidgets, p);
I have a linear layout, how can I add gravity to it so that it is horizontally centred?
LinearLayout textHolder = new LinearLayout(context);
textHolder.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
The above does not appeat to work.
This should work
LinearLayout textHolder = new LinearLayout(context);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
lp.gravity = Graviity.CENTER_HORIZONTAL;
textHolder.setLayoutParams(lp);
But this will only center the children of the linearlayout according to textHolder's size. If you want to center the textHolder completely, the you should apply the layout params to the parent of textHolder.
In xml file we can do;
android:layout_gravity="center_vertical|right"
I couldn't find how to set those params programmatically. I think something like this is not possible.
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
params.gravity = Gravity.BOTTOM;
params.gravity = Gravity.CENTER;
params.gravity = Gravity.BOTTOM | Gravity.CENTER;