Android Beginner: Prevent admob from pushing content up - java

I am a beginner so bear with me. I have a layout with several buttons and then an adview at the bottom. When the ad loads, it pushes the buttons up and makes them very small. Is there anyway to prevent the content from being pushed up?
Here is my code
<?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/background_hdpi" >
<com.google.ads.AdView
android:id="#+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="a14fc541226f07b"
ads:loadAdOnCreate="true" >
</com.google.ads.AdView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/ad"
android:orientation="vertical"
android:weightSum="7.0" >
<Button
android:id="#+id/basics1"
android:layout_width="150.0dip"
android:layout_height="0.0dip"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="2.0dip"
android:layout_marginTop="100.0dip"
android:layout_weight="1.0"
android:text="Overview"
android:textSize="16.0sp" />
<Button
android:id="#+id/basics2"
android:layout_width="150.0dip"
android:layout_height="0.0dip"
android:layout_gravity="center_horizontal"
android:layout_margin="2.0dip"
android:layout_weight="1.0"
android:text="Campaign"
android:textSize="16.0sp" />
<Button
android:id="#+id/basics3"
android:layout_width="150.0dip"
android:layout_height="0.0dip"
android:layout_gravity="center"
android:layout_margin="2.0dip"
android:layout_weight="1.0"
android:text="Special Ops"
android:textColor="#ff000000"
android:textSize="16.0sp" />
<Button
android:id="#+id/basics4"
android:layout_width="150.0dip"
android:layout_height="0.0dip"
android:layout_gravity="center_horizontal"
android:layout_margin="2.0dip"
android:layout_weight="1.0"
android:text="Zombies"
android:textColor="#ff000000"
android:textSize="16.0sp" />
<Button
android:id="#+id/basics5"
android:layout_width="150.0dip"
android:layout_height="0.0dip"
android:layout_gravity="center_horizontal"
android:layout_margin="2.0dip"
android:layout_weight="1.0"
android:text="Modes"
android:textColor="#ff000000"
android:textSize="16.0sp" />
<Button
android:id="#+id/basics6"
android:layout_width="150.0dip"
android:layout_height="0.0dip"
android:layout_gravity="center_horizontal"
android:layout_margin="2.0dip"
android:layout_weight="1.0"
android:text="Ranks/Unlocks"
android:textColor="#ff000000"
android:textSize="16.0sp" />
</LinearLayout>
</RelativeLayout>

You have a few options.
Make your buttons a fixed size instead of weight dependent. Weights make your Buttons take up a fraction of the space available. So when the ad loads, the space available decreases and then your buttons become smaller.
Change your RelativeLayout into a FrameLayout and put the ads after the button LinearLayout.
Put everything inside ScrollView, ie:
RelativeLayout -> ScrollView -> LinearLayout (height:wrap_content,width:fill_parent) -> AdView + LinearLayout with Buttons

Related

ImageView covering other contents in android

The image is covering the textView and buttons(I cant even see them at all so I'm guessing they are behind the image). Image is attached. I want the textview and buttons to appear below the imageview. Below is my xml file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="#+id/imageView9"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitStart"
android:src="#drawable/bw" />
<EditText
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button2" />
</LinearLayout>
You can change the main layout to RelativeLayout, set the ImageView at the top and wrap the views below the ImageView, inside a separate layout.
By setting android:layout_above="#+id/linear" for the ImageView, you will be sure that the ImageView will never cover the other views.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/linear"
android:layout_alignParentTop="true"
android:scaleType="fitStart"
android:src="#drawable/bw" />
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:gravity="center_horizontal">
<EditText
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button" />
</LinearLayout>
</RelativeLayout>
Try changing the parent layout to a RelativeView, or a ConstraintView. If you don't want to redo your positioning of your layout, you can wrap everything except the Image in a LinearLayout.
<RelativeView>
<ImageView>
<LinearLayout>
//...All your stuff
</LinearLayout>
<RelativeView>
This isn't one way of course, try to experiment. Feel free to ask if you still encounter problems.

android - button under a layout is still touchable

I have a layout with 5 buttons and each of them has a OnTouchListener that slides a frame layout on screen (on top of the buttons) but after that all of my 5 buttons is still touchable.
How to disable touch for views that is under other layouts such as
FrameLayout that filled with a fragment ?
<Button
style="#style/tab_btn"
android:background="#drawable/village_button"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_height="70dp"
android:layout_width="70dp"
android:text=""
android:onClick="buttonPress"/>
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/sidebar_btn"
android:id="#+id/sidebar_btn4"
android:layout_marginTop="80dp"
android:layout_marginLeft="-18dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/sidebar_btn"
android:id="#+id/sidebar_btn3"
android:layout_marginTop="120dp"
android:layout_marginLeft="-18dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/sidebar_btn"
android:id="#+id/sidebar_btn2"
android:layout_marginTop="160dp"
android:layout_marginLeft="-18dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/sidebar_btn"
android:id="#+id/sidebar_btn"
android:layout_marginTop="200dp"
android:layout_marginLeft="-18dp" />
<FrameLayout
android:layout_width="160dp"
android:layout_height="match_parent"
android:layout_marginTop="-17dp"
android:layout_marginLeft="-176dp"
android:layout_marginBottom="-17dp"
android:id="#+id/sidebar"
android:background="#color/black">
</FrameLayout>
I think you must add android:clickable="true" to FrameLayout
So it can capture the touch or click event when it's on above the button's
<FrameLayout
android:layout_width="160dp"
android:layout_height="match_parent"
android:layout_marginTop="-17dp"
android:layout_marginLeft="-176dp"
android:layout_marginBottom="-17dp"
android:id="#+id/sidebar"
android:clickable="true"
android:background="#color/black">
try use
bt.setVisible(View.GONE);
on the buttons you need to disable and if you need use it again you can use
bt.setVisible(View.VISIBLE);

How to resize the layout properly? - Android

Im trying to make an app for the sony smart watch 2.
Im trying to make a basic converter app which consists of one text box, two radio buttons and one button. Yet when testing on the watch everything is massive. If I resize things in eclipse things disappear and other things what is happening? Picture of it on watch:http://content.screencast.com/users/jeremyc12/folders/Jing/media/050d6158-1daa-4ebe-8eb2-ed8a46e3d024/2014-04-23_1334.png
Here is my xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="#dimen/smart_watch_2_control_width"
android:layout_height="#dimen/smart_watch_2_control_height"
android:background="#color/myColor"
android:onClick="calculate"
tools:ignore="PxUsage" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:inputType="numberSigned"
android:width="5px" >
<requestFocus />
</EditText>
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/editText1"
android:layout_alignBottom="#+id/editText1"
android:layout_alignParentRight="true"
android:layout_marginRight="16dp"
android:checked="true"
android:text="#string/kmh" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button2"
android:layout_alignLeft="#+id/radioButton1"
android:text="#string/miles" />
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/editText1"
android:text="#string/calc" />
Two issues I see here:
As Aritra pointed out, make sure in your XML layout that you aren't doing any scaling. So don't use "dp" just "px".
There are only a few Views in Android supported on SW2. See here for details:
http://developer.sonymobile.com/reference/sony-addon-sdk/com/sonyericsson/extras/liveware/aef/control/Control.Intents#EXTRA_DATA_XML_LAYOUT
EditText, RadioButton and Button views are not included there so thats why you're seeing some strange issues even without scaling.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff0000"
android:onClick="calculate" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:inputType="numberSigned"
android:width="5px" >
<requestFocus />
</EditText>
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_toRightOf="#+id/radioButton2"
android:checked="true"
android:text="kmh"
android:textSize="8sp" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:text="miles"
android:textSize="8sp"/>
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/radioButton2"
android:layout_toRightOf="#+id/editText1"
android:text="calculate"
android:textSize="8sp"/>
</RelativeLayout>

Get two buttons 50/50 width in my layout

i've got a layout that looks like below. But ideally I want the buttons Save and Cancel to be 50% width each, next to each other. Can't quite seem to get them to work though.
Any suggestions?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<Button
android:id="#+id/btnMinus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="-" />
<EditText
android:id="#+id/txtCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/btnPlus"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/btnPlus"
android:layout_toRightOf="#+id/btnMinus"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<Button
android:id="#+id/btnPlus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="+" />
<Button
android:id="#+id/btnGo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnMinus"
android:layout_toRightOf="#+id/button1"
android:maxWidth="100dp"
android:text="Save"
android:width="100dp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/btnMinus"
android:text="Cancel" />
</RelativeLayout>
You shouldn't be using a RelativeLayout for this design. You should be using a LinearLayout.
Something like this outline (very stripped-down):
<LinearLayout android:orientation=vertical>
<LinearLayout android:orientation=horizontal>
<Button android:layout_width=100 /> <!-- Minus -->
<EditText android:layout_width=0 android:layout_weight=1 />
<Button android:layout_width=100 /> <!-- Plus -->
</LinearLayout>
<LinearLayout android:orientation=horizontal>
<Button android:layout_width=0 android:layout_weight=1 /> <!-- Cancel -->
<Button android:layout_width=0 android:layout_weight=1 /> <!-- Save -->
</LinearLayout>
</LinearLayout>
Your looking for android:layout_weight="0.5". This only works with LinearLayouts, though.
So what you could do is add a LinearLayout that contains your two buttons and set the weights then.
You should use a LinearLayout, inside the RelativeLayout, only for buttons, and give them the same android:layout_weight (1 and 1, 2 and 2, as you wish).

Android XML Issue - images not displaying in AVD

I'm fairly new to android but I've managed to figure out how all the layout stuff works with XML. That's all fine.
In the Graphical Layout tab in Eclipse I have a few buttons and a logo which is a .gif image. I have place the .gif image into my XML file as an ImageView within a LinearLayout and it shows up on the Graphical Layout.
But when I run it in the Android Virtual Device (AVD) everything shows up but the image. Anyone know why this is happening? See XML code below...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/natlib"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="-8dp"
android:padding="#dimen/padding_medium"
android:text="#string/welsh_libs"
android:textColor="#79438F"
android:textSize="22dip"
android:textStyle="bold"
tools:context=".MainActivity" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="137dp"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:background="#A4C81C"
android:text="#string/ask_lib" />
<Button
android:id="#+id/button2"
android:layout_width="137dp"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:background="#FF0066"
android:text="#string/find_book" />
<Button
android:id="#+id/button3"
android:layout_width="137dp"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:background="#3F83F1"
android:text="#string/find_lib" />
<Button
android:id="#+id/button4"
android:layout_width="137dp"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:background="#FE0002"
android:text="#string/register" />
<Button
android:id="#+id/button5"
android:layout_width="137dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="#FBFC3F"
android:text="#string/login" />
<ImageView
android:id="#+id/image1"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_marginLeft="220dp"
android:layout_marginTop="20dp"
android:layout_weight="0.37"
android:contentDescription="#string/desc"
android:src="#drawable/wag_logo"
android:visibility="visible" />
</LinearLayout>
set android:layout_height greater than 0...try this
<ImageView
android:id="#+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="220dp"
android:layout_marginTop="20dp"
android:layout_weight="0.37"
android:contentDescription="#string/desc"
android:src="#drawable/wag_logo"
android:visibility="visible" />
Which folder did you place the image in? /res/drawable?
Also, which folder did you place the layout in? /res/layout?
The /res/ folder and it's subfolders are used in Localization.
So if you only have it in /res/drawable-xhdpi/, it'll only show up on very large devices.
Also, if you modified your layout file at /res/layout-large/ and didn't modify the one at /res/layout, only the large version would get the modifications.

Categories