Android screen shifts down with AdView - android

How do I prevent my Android screen from shifting down when a page is view is displayed and receives the first add?
When the view is first displayed the screen is normal, then a ad is displayed and the screen shifts down a few lines.
Here is my code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background3"
android:orientation="vertical" >
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_gravity="top"
ads:adSize="BANNER"
ads:adUnitId="xxxxxxxxxx"
android:gravity="center"
ads:loadAdOnCreate="true" >
</com.google.ads.AdView>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginTop="34dp" >
<EditText
android:id="#+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="Start" />
</TableRow>
</LinearLayout>
TIA, Trey

I don't have much experience with the ads. But from strictly a View standpoint it seems like what is happening is that the adView is set to View.GONE before it loads. Which means that its space is ignored when placing the other views into the parent.
I don't know for sure how you could get it to not do that. But I would try explicitly setting it to android:visibility="invisible" in your xml, then in your java you'll have to make it visible after it has loaded. "invisible" is like gone, but it tells the parent to respect the space that you will take up when you are visible.
Perhaps the loadOnCreate attribute causes the behavior you are seeing? Might also be worth trying it without that and just load it manually in your onCreate.

Related

Focus an element on main view while showing ProgressDialog

I Have been looking around this for a while now and could not find solution;
When you create ProgressDialog, the Main View is turned to grey and loses focus,What I want to do is to keep an element from the Main View focused (lets say Admob) and keep it usable while the ProgressDialog is shown
UPDATE : CODE !
View mainview = LayoutInflater.from(MainAct).inflate(R.layout.activity_main, null);
View adView = mainview.findViewById(R.id.adView);
mProgress = new ProgressDialog(MainAct);
mProgress.setMessage("Downloading...");
mProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgress.setCancelable(false);
mProgress.setCanceledOnTouchOutside(false);
mProgress.show();
adView.requestFocus();
it seems like using reaquestFocus doesn't work and is only intended for form elements, not bringing elements on TOP
UPDATE 2:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linlaHeaderProgress"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal"
android:visibility="gone" >
<ProgressBar
android:id="#+id/pbHeaderProgress"
style="#style/Spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp" >
</ProgressBar>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="left|center"
android:padding="2dp"
android:text="Loading...."
android:textColor="#F1F1F1"
android:textSize="20sp" >
</TextView>
</LinearLayout>
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:cacheColorHint="#android:color/transparent"
android:divider="#000000"
android:dividerHeight="0dp"
android:fadingEdge="none"
android:persistentDrawingCache="scrolling" >
</ListView>
<TextView
android:id="#android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:padding="10dp"
android:textColor="#f1f1f1"
android:textSize="15sp"
android:textStyle="bold"
android:visibility="gone" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom|center"
android:orientation="horizontal" >
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="MY_AD_UNIT_ID"
ads:loadAdOnCreate="true"
ads:testDevices="TEST_EMULATOR, MY_TEST_DEVICE_ID" />
</LinearLayout>
</LinearLayout>
The code above is for a layout that is used almost exclusively in own of my apps. What I do while loading the data is quite simple.
Look at how the ProgressBar is defined in the XML. While still loading data, I change the LinearLayout linlaHeaderProgress to VISIBLE which effectively hides the ListView below it because of the attributes set to it. After I am done loading the data, I change the visibility of the LinearLayout linlaHeaderProgress to GONE. A point to note here is, the Admob XML right at the bottom of the XML, is always visible to the user and can be interacted with anytime before or after the data is loading / done loading.
If you need to show this XML in the form of a Dialog, you can always do so by attaching a Theme to this activity.
Hope some of this helps in your endeavour.
UPDATE
Check these tutorials. These are a few I could find with a quick search for showing a Horizontal ProgressBar. You are using a ProgressDialog.
http://www.techrepublic.com/blog/app-builder/androids-batterymanager-and-progressbar-two-for-one-tutorial/703
http://theandroid.in/example-of-progressbar-in-android/
And finally, use this Google Search to see a few examples of making an activity look like a dialog.
This should help you achieve what your screenshot suggests.

Right layout for basic android browser

I need to create a basic browser for the android: a WebView a toolbar at the top with a back button, and a toolbar at the bottom with some more buttons.
What would be the best way to go about doing this?
This is what I have so far:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/browserBackButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back" />
<WebView
android:id="#+id/webView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/backButton"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/back" />
<ImageButton
android:id="#+id/forwardButton"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/forward" />
<ImageButton
android:id="#+id/reloadButton"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/reload" />
<ImageButton
android:id="#+id/browserButton"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/safari" />
</LinearLayout>
</LinearLayout>
The bottom LinearLayout of buttons doesn't show at all. Only the top back Button and the WebView.
LinearLayout is ok.
You should change the height of the WebView control and set the weight to 1
Do it so:
<WebView android:layout_height="fill_parent" android:layout_weight="1" android:layout_width="fill_parent"
or even so, as it is recommended by the Lint tool:
<WebView android:layout_height="0dip" android:layout_weight="1" android:layout_width="fill_parent"
The weight is important, it forces the view to fill the remaining space.
If it doesn't help, you can also try to set the height of the bottom panel to some constant value, or set the height of image buttons to wrap_content, then you can be sure that it will work.
Personally, I don't like doing nested LinearLayouts it can hinder performance when your layouts become more complex. It might not be a big deal for something small; but, it is still good practice to use RelativeLayouts.
Also, lose the xmlns:android="http://schemas.android.com/apk/res/android" in your second LinearLayout. You don't need it.
Hope this helps.

how to use scrollfview in a popup window

I am trying to use a scroll view in my pop up window, and it works to an extent, but when I scroll down the text is still visible - how do I use this code in a pop up window?
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#333333">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp" >
<TextView
android:id="#+id/popupView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="#string/helpmenu"
android:textSize="1600pt" />
<Button
android:id="#+id/close"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:text="Close" />
</LinearLayout>
jcw i found solution to this problem i know its to late, but still for any future use.
Make use of any parent layout like Frame or linear, relative above scroll View and the problem you are facing will be gone.

ListView, AdMob and built in backup Ad Layout issue

What I am trying to do is build a layout which if an AdMob Ad fails to load it shows one of my own Ads (ImageView) which are built into the app.
I cant seem to get the listview not to cover up my Ad.
I have no issue getting it to work if I just use the AdMob Ad alone.
Here is my layout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bgdark"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/actionbarLayout"
android:layout_width="fill_parent"
android:layout_height="#dimen/actionbar_height"
android:layout_alignParentTop="true"
android:layout_marginBottom="2dip"
android:background="#drawable/actionbar_background" >
//Top bar is here
</RelativeLayout>
<LinearLayout
android:id="#+id/layoutAds"
android:layout_width="match_parent"
android:layout_height="50dip"
android:background="#00FF00"
android:layout_alignParentBottom="true"
android:layout_centerVertical="true" >
<ImageView
android:id="#+id/ImageAlternativeAd"
android:layout_width="wrap_content"
android:layout_height="50dip"
android:layout_gravity="center_vertical"
android:src="#drawable/anad"
android:visibility="visible" >
</ImageView>
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="123456abcd"
ads:loadAdOnCreate="true"
ads:testDevices="TEST_EMULATOR,CFFFFFFF9A836FD749F3EF439CD"
android:visibility="visible" />
</LinearLayout>
<ListView
android:id="#+id/listCategories"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/adView"
android:layout_below="#id/actionbarLayout"
android:cacheColorHint="#00000000" >
</ListView>
This is exactly what you want.
https://stackoverflow.com/a/5188181/563306
Also, did you try with,
<ListView
android:id="#+id/listCategories"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/layoutAds"
android:layout_below="#id/actionbarLayout"
android:cacheColorHint="#00000000" >
</ListView>
I think a clean way to implement this would be to have a component that initially holds your com.google.ads.AdView view. Then, when you notice that your ad did not load, you can swap it with your ImageView on the fly.
Even better: make that a custom component. So from the "outside" (your layout) you are just including an instance of this component and internally, this component is implemented by either displaying an ad from AdMob or your own ad depending on whether the AdMob ad fails to load or not.

How to change the height of a webview on Runtime?

I have a webview and a table containing 4 buttons. I have set the height of the webview to 400px, so that the buttons are displayed at the bottom of the screen, in portrait mode. My problem is, when changed to landscape mode the buttons are not visible. So I need to change the height of the webview on runtime, when orientation is changed. Do anybody know how to achieve this? I am able to capture the orientation change, using 'onConfigurationChanged' function.
Thanks CommonsWare, I gave the xml as follows. The application crashes on launch.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout android:layout_alignParentTop="true">
<WebView
android:id="#+id/appView"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
</RelativeLayout>
<RelativeLayout
android:layout_alignParentBottom="true">
<TableLayout
android:id="#+id/TableLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<TableRow
android:id="#+id/TableRow01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center">
<ImageButton
android:id="#+id/more"
android:padding="1dip"
android:src="#drawable/more1"
android:layout_marginRight="15dip">
</ImageButton>
<ImageButton
android:id="#+id/albums"
android:padding="1dip"
android:src="#drawable/albums1"
android:layout_marginRight="15dip">
</ImageButton>
<ImageButton
android:id="#+id/videos"
android:padding="1dip"
android:src="#drawable/videos"
android:layout_marginRight="15dip">
</ImageButton>
<ImageButton
android:id="#+id/artists"
android:padding="1dip"
android:src="#drawable/artists1"
android:layout_marginRight="15dip">
</ImageButton>
</TableRow>
</TableLayout>
</RelativeLayout>
</LinearLayout>
Design your layout to avoid the hard-wired value of 400px. Not only will that not work in landscape mode on whatever emulator/device you are testing on, it will not work on smaller or larger screens the way you expect.
Please use a RelativeLayout, with attributes like android:layout_alignParentBottom for the buttons and android:layout_alignParentTop and android:layout_above for the WebView. Then, your layout will dynamically adapt to the available space.

Categories

Resources