Positioning a WebView - java

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

Related

How to display a simple ImageButton on a customly created view?

I've been browsing the entire internet, but I could not find any answer to this question. I want to create a background, some image buttons, along with a bunch of moving graphics (circles). Since I do not know how to overwrite a xml layout with moving graphics, I chose to customly create my view, draw the background, the imageButtons and the circles (2D graphics).
public class GameView extends View implements UtilConstants
since I extended the View class, I had to call the super class constructor in my constructor
GameView(Context context) {
super(context);
this.context = context;
this.paint = new Paint();
}
and to implement the onDraw method, which acts like the java paintComponent
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas); // necessary for some reason
setGridBackground(); // sets the background
paint.setColor(Color.parseColor("#FF0000"));
canvas.drawCircle(this.x += 10, 300, 20, paint); //moves my graphics (ball) within the screen
drawScoreInLeftUpperCorner(canvas, 20); // paints a string text on the screen
setAbilitiesImages(); // places some imageButtons
try {
Thread.sleep(THREAD_REFRESH_RATE_MS);
} catch (InterruptedException ex) {
System.out.println(ex);
}
invalidate();
}
Now, to get to my problem:
I do not know how to set those ImageButtons !!! I am trying
private void setAbilititesImages() {
ImageButton imageButton = new ImageButton(this.context); // is this ok to put the argument this.context??
imageButton.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
imageButton.setImageResource(R.drawable.monster); // set my resource
RelativeLayout relativeLayout = findViewById(R.id.in_game_layout); // what is the purpose of this???
if (relativeLayout != null) { // it never enters this if, relativeLayout is always null
relativeLayout.addView(imageButton);
}
}
Aaaaand the image button never shows up...why is it necessary to use Relative/Linear layouts? That R.id.in_game_layout I created is just an empty RelativeLayout xml. Can't I just use something like
imageButton.setImageResource(R.drawable.monster); // set my resource
imageButton.setBounds(.....);
(View)this.add(imageButton); ???
ImageButton requires a parent view to be hosted in it. The parent View can be a RelativeLayout, a linearlayout or a ConstraintLayout.
In your case, the instance relativeLayout is always null because you not specify the view parent in which you do findViewById.
You should make something like :
RelativeLayout relativeLayout = view.findViewById(R.id.in_game_layout);
You need to add layout param to your parent Relative layout also to display your Image button appropriately.
Example
RelativeLayout relativeLayout = findViewById(R.id.relativeLayout);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
ImageButton imageButton = new ImageButton(this);
imageButton.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher_background,null));
relativeLayout.addView(imageButton,params);

Make Layout Non Clickable But Children Clickable

I've been working on a system overlay and I found that moving my imageview around the screen is a hassle using WindowManager.LayoutParams x and y, so I've decided to make a RelativeLayout with the height of the device to hold my imageview. The only problem is I want to make the Layout non clickable so that the activity below it can be clicked, but not the imageview itself as it launches an activity. Is there anyway to do this? Here is my code so far
final RelativeLayout floaterLayout = new RelativeLayout(getApplicationContext());
//this layout makes the relativelayout non clickable so that the activity below it can be clicked but it doesn't allow any children to be clicked either.
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED + WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
+ WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE + WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN + WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL + WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, PixelFormat.TRANSLUCENT);
params.height = 2560;
params.y = 0;
floaterLayout.setBackgroundColor(Color.RED);
floaterLayout.setAlpha(0.5f);
floaterLayout.setLayoutParams(params);
((WindowManager) getSystemService(Context.WINDOW_SERVICE)).addView(floaterLayout, params);
ImageView view = new ImageView(getApplicationContext());
Util.setImageDrawable(view, R.drawable.floater_dots);
floaterLayout.addView(view);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do stuff
}
});
#Nicolas you are creating a relative layout that's not added to your parent layout.Simply create a xml with a relativeLayout and an imageView. Set that imageView's onClickListener in java code. Through this the parent layout will be nonclickable and only imageview will be clickable

Android Add margins programmatically to Webview

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

Admob ad not showing in my app

Admob ad not showing in my application. I am sensing ad is loading but somehow its not showing because of my layout arrangement. but i couldn't locate it. here is my code
protected void onSetContentView() {
final RelativeLayout relativeLayout = new RelativeLayout(this);
final FrameLayout.LayoutParams relativeLayoutLayoutParams = new FrameLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
this.mRenderSurfaceView = new RenderSurfaceView(this);
mRenderSurfaceView.setRenderer(mEngine);
final FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.FILL_PARENT,
FrameLayout.LayoutParams.FILL_PARENT,Gravity.CENTER);
final android.widget.RelativeLayout.LayoutParams surfaceViewLayoutParams = new RelativeLayout.LayoutParams(layoutParams);
surfaceViewLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
relativeLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams);
FrameLayout frameLayout = new FrameLayout(this);
adView = new AdView(this, AdSize.BANNER, "##############");
adView.refreshDrawableState();
adView.setVisibility(AdView.VISIBLE);
frameLayout.addView(adView);
relativeLayout.addView(frameLayout);
this.setContentView(relativeLayout, relativeLayoutLayoutParams);
}
You have told your SurfaceView to FILL_PARENT, so it is taking all the space leaving none for the AdView sibling.
final FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.FILL_PARENT,
FrameLayout.LayoutParams.FILL_PARENT,Gravity.CENTER);
Note also there is no need for the FrameLayout wrapping the AdView.
I suggest you follow jquery404's advice and define the layout in XML. You'll get a much better understanding of your ui design.

Add imageview to layout on specific coordinates

I am dynamically adding ImageViews to layout. Here is the code, which is doing what I want:
setContentView(R.layout.main);
RelativeLayout main = (RelativeLayout) findViewById(R.id.main_view);
ImageView smokeImage = new ImageView(this);
Drawable smoke = getResources().getDrawable(R.drawable.smoke);
smokeImage.setBackgroundDrawable(smoke);
main.addView(smokeImage, 800, 800);
How to add ImageView to specific coordinates? For example. I want it to appear on x=25px and y=43px . Are there any special functions?
The easiest way is to set margins to your image view. You can use create RelativeLayout.LayoutParams, set margins to params
params.setMargins(left, top, right, bottom);
and set params to your image view
smokeImage.setLayoutParams(params);

Categories

Resources