how can I set gravity to center in webview to play gif? - android

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();

Related

LinearLayout inside a ScrollView doesn't show

Here's my Code
mainView = new MainView(this, imageId, text, 3);
mainView.setOnTouchListener(this);
LinearLayout.LayoutParams mainParams = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
LinearLayout mainLinear = new LinearLayout(this);
ScrollView.LayoutParams scrollParams = new
ScrollView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
ScrollView scrollView = new ScrollView(this);
LinearLayout.LayoutParams myViewParams = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mainLinear.addView(mainView, myViewParams);
scrollView.addView(mainLinear, mainParams);
addContentView(scrollView, scrollParams);
Of course it works within LinearLayout only. Please tell me how to put it inside a ScrollView.
ScrollView's child should only have height as WRAP_CONTENT. If you try the same layout using xml AS will give you a lint warning saying exactly this.
If you need your child view to occupy full height of scroll view when content is less you could use setFillViewport().
This should work for you:
mainView = new MainView(this, imageId, text, 3);
mainView.setOnTouchListener(this);
LinearLayout.LayoutParams mainParams = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LinearLayout mainLinear = new LinearLayout(this);
ScrollView.LayoutParams scrollParams = new
ScrollView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
ScrollView scrollView = new ScrollView(this);
LinearLayout.LayoutParams myViewParams = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mainLinear.addView(mainView, myViewParams);
scrollView.addView(mainLinear, mainParams);
scrollView.setFillViewport(true);
addContentView(scrollView, scrollParams);

How to align an ImageButton in Android FrameLayout programmatically

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);

How to create a linear layout with textview and image button dynamically

i want to create a linear layout where inside it it has text view and image button. And i want to create it dynamically.
LinearLayout layoutSection = new LinearLayout(getActivity());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
params.gravity = Gravity.CENTER;
params.setMargins(5,5,5,5);
imageView.setImageResource(R.drawable.image);
imageView.setLayoutParams(params);
TextView textView = newTextView(getActivity());
TextView.setText(("Enter text here");
layoutSection.addView(textView);
layoutSection.addView(imageView);

How to set child weight in the code?

I want that button(child) will have 25% of full width of horizontal LinearLayout (its parent). The code:
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
ll.setWeightSum (1.0f);
Button b = new Button (this);
ll.addView(b);
How can I do it? Thanks.
Try this:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENTS,LayoutParams.FILL_PARENT));
lp.weight = 0.25;
b.setLayoutParams(lp);
You can pass it in as part of the LinearLayout.LayoutParams constructor:
LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f);

Center a layout in Android?

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.

Categories

Resources