I need to add margins to a webview programmatically. I would like to do something like below:
public void setSideMargin(final int sideMargin, int id) {
WebView webView = (WebView) ((Activity) context)
.findViewById(id);
WebView.LayoutParams p = new WebView.LayoutParams(
WebView.LayoutParams.MATCH_PARENT,
WebView.LayoutParams.WRAP_CONTENT);
p.leftMargin = sideMargin;
p.rightMargin = sideMargin;
webView.setLayoutParams(p);
}
This is obviously wrong I know but is there anything like this that I can do to add the margins programmatically? Thanks
The thing to understand with LayoutParams is this: The LayoutParams does not depend on the element it is set to, but on the parent.
It is an indication given to the parent regarding the positioning of the element.
Therefore, if your WebView is in a LinearLayout, getLayoutParams will get you a LinearLayout.LayoutParams.
Which means that, in order to have a descendant of MarginLayoutParam, a LayoutParams that supports margins, your WebView must be placed in a ViewGroup that supports margins, such as LinearLayout or RelativeLayout. (see the list of descendants http://developer.android.com/reference/android/view/ViewGroup.MarginLayoutParams.html ). In other words, WebView itself does not support margins, its parent does.
In which case, you should cast the LayoutParams from the WebView to:
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) webView.getLayoutParams();
Once you have that, you can modify the margins :
p.leftMargin = sideMargin;
p.rightMargin = sideMargin;
webView.setLayoutParams(p);
Try with this code:
WebView.LayoutParams layoutParams = (WebView.LayoutParams) webView.getLayoutParams();
webView.layoutParams.leftMargin = sideMargin;
webView.layoutParams.rightMargin = sideMargin;
webView.setLayoutParams(layoutParams);
If doesn't work , then take linearlayout parent of this view and insert padding for it.
Example code:
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) webView.getParent().getLayoutParams();
layoutParams.setMargins(sideMargin, 0, sideMargin, 0);
((LinearLayout)webView.getParent()).setLayoutParams(layoutParams);
2th Example code:
LinearLayout webviewLayout = (LinearLayout) webView.getParent();
webviewLayout.setPadding(sideMargin,0,sideMargin,0);
Related
I have the following code to generate button objects with a set text, onClick command (more or less), height, width, and margins
private Button generateButton(String text, char command, int height, int width, int left, int top){
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(getDP(width),getDP(height));
lp.setMargins(getDP(left),getDP(top),0,0);
Button button = new Button(this.getContext());
button.setText(text);
button.setLayoutParams(lp);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("Command",""+ command);
}
private char command;
private View.OnClickListener init(char var){
command = var;
return this;
}
}.init(command));
return button;
}
Along with this method to get the dp
private int getDP(int size){
return (int) (size * this.getContext().getResources().getDisplayMetrics().density);
}
But when I run the app the button have the proper height and width, along with the proper text and onclick action, but they have no margins, they're all bunched up into one corner
The view hierchy according to the Layout Inspector in Android studio goes
DecorView
LinearLayout
FrameLayout
FitWindowLinearLayout
ContentFrameLayout
CoordinatorLayout
ViewPager
ConstraintLayout
ConstraintLayout
Buttons
And from what I've read online, LayoutParams has to be from the same class as the layout, as is LinearLayout.LayourParams, or ConstraintLayout.LayoutParams and I've tried all the layout types that made sense to me and still no margins
It might be worth noting this is in a fragment
For views that are children of ConstraintLayout, you need to add horizontal and vertical constraints. Otherwise they layout will not know how to place them, and places them at the top left corner.
You can use the ConstraintLayout.LayoutParams, and set the constraints by
lp.leftToLeft, lp.leftToRight and similar methods here
So for example if you want to place these buttons one after the other vertically, you will constraint each button horizontally to the parent, and vertically to the previous button.
public void setMargins (int left, int top, int right, int bottom)
This is the signature for setMargins method. You call the method with right = 0 and bottom = 0. So you will not have the margins on right and bottom. Try to set some values for right and bottom.
You should use LayoutParams to set your button margins:`
LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
params.setMargins(left, top, right, bottom);
yourbutton.setLayoutParams(params);
Depending on what layout you're using you should use RelativeLayout.LayoutParams or LinearLayout.LayoutParams.
And to convert your dp measure to pixel, try this:
Resources r = mContext.getResources();
int px = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
yourdpmeasure,
r.getDisplayMetrics()
);
`
I want to play a gif animation and this view will move on the screen every second, but if I start an activity I need to add this view again and the position will change.
See the attached image below. I need to stick this over every view. Any way to do it?
Create one BaseActivity for all activities in your app to show Overlay View or Layout.
you can inherit BaseActivity on other Activity
You can add gif supported views or layouts
for example i added overlay_layout in my showOverlay method.
you can call showOverlay method where ever you want to show and you can remove with removeOverlay with conditions.
Please note that showOverlay and removeOverlay should be in BaseActivity
void showOverlay(){
LayoutInflater inflater = activity.getLayoutInflater();
layout = inflater.inflate(R.layout.overlay_layout, null);
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.gravity = Gravity.BOTTOM;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.type = WindowManager.LayoutParams.TYPE_APPLICATION;
final WindowManager mWindowManager = (WindowManager);
activity.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
mWindowManager.addView(layout, params);
}
void removeOverlay(){
windowManager.removeView(view);
}
set this view into WindowManager and it will always show on top
I am trying to create a method that will add a new LinearLayout to an existing LinearLayout. I would like to send a parameter to this method telling it whether the new LinearLayout should be VERTICAL or HORIZONTAL. This is what I have so far but 'LinearLayout.Orientation' is not the correct name for this parameter type.
private LinearLayout addLinearLayout(View linearLayout, LinearLayout.Orientation orientation)
{
//Create new horizontal linear layout and add to the specified linear layout
LinearLayout newLinearLayout = new LinearLayout(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(20, 10, 0, 10);
newLinearLayout.setOrientation(LinearLayout.orientation);
((LinearLayout) linearLayout).addView(newLinearLayout, layoutParams);
return newLinearLayout;
}
Is this possible? If so, what is the type of the parameter I am trying to pass?
Thanks for any help.
From the documentation:
public void setOrientation (int orientation)
(...)
orientation Pass HORIZONTAL or VERTICAL. Default value is HORIZONTAL.
private LinearLayout addLinearLayout(LinearLayout linearLayout, int orientation) {
//Create new horizontal linear layout and add to the specified linear layout
LinearLayout newLinearLayout = new LinearLayout(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(20, 10, 0, 10);
newLinearLayout.setOrientation(orientation);
linearLayout.addView(newLinearLayout, layoutParams);
return newLinearLayout;
}
Call this method like this:
addLinearLayout(yourLinearlayout, LinearLayout.HORIZONTAL);
The orientation of a Layout seems to be represented by an Integer...
private LinearLayout addLinearLayout(LinearLayout linearLayout, int orientation)
{
//Create new horizontal linear layout and add to the specified linear layout
LinearLayout newLinearLayout = new LinearLayout(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(20, 10, 0, 10);
newLinearLayout.setOrientation(orientation);
linearLayout.addView(newLinearLayout, layoutParams);
}
// Then you can call your function like this
addLinearLayout(layout, LinearLayout.VERTICAL);
I have a surface (which extends SurfaceView), an AdView and now I want to add a WebView, which I'm going to need to position dynamically.
For start, I'm trying to add my web view at the center and make its size the device's width and half its height. I'm with a 800X480 device and landscape mode, so for now I want to set its border at left=0, top=120, right=800, bottom=360, just for the sake of testing. Later on I'll want to be able to set it dynamically, but so far this doesn't even work:
private MySurface surface;
private AdView adView;
private WebView
.
.
.
// create the ad view
adView = new AdView(this, AdSize.SMART_BANNER, AD_MOB_ID);
adView.setAdListener(new MyAdListener());
// create the surface
surface = MySurface.getRef(this);
// set a listener for touch event
surface.setOnTouchListener(this);
// create the web view
webView = new WebView(this);
webView.setWebViewClient(new WebViewClient());
webView.setVisibility(View.GONE);
LayoutParams webViewParams = new LayoutParams(800,240); // this works
webViewParams.setMargins(0, 120, 800, 360); // this does nothing...
// create a relative layout
RelativeLayout l = new RelativeLayout(this);
// add the surface
l.addView(surface);
// add the ad view at the bottom
AdView.LayoutParams adViewParams = new AdView.LayoutParams(
AdView.LayoutParams.WRAP_CONTENT,
AdView.LayoutParams.WRAP_CONTENT);
adViewParams.addRule(AdView.ALIGN_PARENT_BOTTOM);
l.addView(adView, adViewParams);
l.addView(webView, webViewParams);
setContentView(l);
// load an ad
//loadAdMob();
when I later set the webview visibility to visible:
webView.loadUrl(url);
webView.setVisibility(View.VISIBLE);
It appears the right width and height but starts at the very top of the screen :(
Can anyone help ? Thanks.
Ok, I think I got it:
webView = new WebView(this);
webView.setWebViewClient(new WebViewClient());
RelativeLayout.LayoutParams webViewParams= new RelativeLayout.LayoutParams(800, 240);
webViewParams.addRule(RelativeLayout.CENTER_IN_PARENT);
RelativeLayout l = new RelativeLayout(this);
l.addView(surface);
l.addView(adView, adViewParams);
l.addView(webView, webViewParams);
setContentView(l);
credit goes here
I have set a linear layout inside a alert dialog in which I basically have a few Edit texts.
The problem I am facing is that I am not able to scroll down to see every edit text created dynamically.
Here's the snippet:
Context context = this.getApplicationContext();
LinearLayout layoutCreateMerch = new LinearLayout(context);
layoutCreateMerch.setOrientation(LinearLayout.VERTICAL);
layoutCreateMerch.setVerticalScrollBarEnabled(true);
final AlertDialog.Builder alert = new AlertDialog.Builder(Act.this);
alert.setTitle("Submit");
final EditText Name = new EditText(Act.this);
final EditText Desc = new EditText(Act.this);
...
...
So, How can I add a scroll view at runtime in this alert dialog's Linear Layout?
I suppose LinearLayouts don't need a scroll bar/view but this isn't working out for me :|
Also, inflating this would be other option but for now..can I do something abt this?
Thanks for reading.. :)
In theory it goes like this:
ScrollView scroll = new ScrollView(context);
scroll.setBackgroundColor(android.R.color.transparent);
scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
scroll.addView(yourLinearLayout);
sO basically just put you linearLayout to ScrollView and set it as dialogView in the end like,
alertDialog.setView(scroll);
Please use <ScrollView ></ScrollView> to wrap your layout.