Element gets pushed to the right. Why? - android

When my LinearLayout is coded like this:
<LinearLayout ...
<!-- SOME CODE HERE -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="17">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/inputText"
android:inputType="textCapCharacters|textMultiLine"
android:hint="#string/input_text_hint"
android:gravity="top|left"/>
</LinearLayout>
<!-- MORE CODE HERE -->
</LinearLayout>
My activity looks like this:
But when I add this adMob code:
<LinearLayout ...
<!-- SOME CODE HERE -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="17">
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="#string/top_banner_1">
</com.google.android.gms.ads.AdView>
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/inputText"
android:inputType="textCapCharacters|textMultiLine"
android:hint="#string/input_text_hint"
android:gravity="top|left"/>
</LinearLayout>
<!-- MORE CODE HERE -->
</LinearLayout>
I get this:
How do I get the ad to be on top of the EditText?

The default orientation for a LinearLayour is horizontal, which makes the add pushing your EditText to the right. If you add android:orientation="vertical" to your parent LinearLayout the issue is solved.
I think that the correct code you have to use is:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="17">
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="#string/top_banner_1">
</com.google.android.gms.ads.AdView>
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/inputText"
android:inputType="textCapCharacters|textMultiLine"
android:hint="#string/input_text_hint"/>
</LinearLayout>
I have removed some properties that can make your layout look ugly in the image you provided.

Related

How to make the ads appear on an activity in android?

I have an android activity with a xml layout as follows:
<RelativeLayout>
...
<GridLayout>
...
</GridLayout>
....
</RelativeLayout>
to which I would like to add the ads-thingy. I have tried the following setup:
<LinearLayout>
<RelativeLayout>
...
<GridLayout>
...
</GridLayout>
....
</RelativeLayout>
<com.google.android.gms.ads.AdView>
...
</com.google.android.gms.ads.AdView>
</LinearLayout>
but it does not show the ads (the dots represent other content I believe is not important for this question). Is there some special trick to use to be able to show the ads-thingy? Make the layout even more complex? Is there a special layout-things I have to use?
My main activity does show the ads; the structure here is as follows:
<LinearLayout>
...
<android.support.design.widget.CoordinatorLayout>
<LinearLayout>
...
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
<com.google.android.gms.ads.AdView>
....
</com.google.android.gms.ads.AdView>
<LinearLayout
A more complete example of the real layout xml file (stripped off the really uneccessary parts...)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cccccc"
>
<GridLayout
android:id="#+id/grid"
android:layout_margin="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="#+id/new_cancel"
android:alignmentMode="alignBounds"
android:columnCount="10"
android:columnOrderPreserved="false"
android:useDefaultMargins="true">
// REMOVED TEXTVIEW, EDITTEXT, IMAGEBUTTONS etc HERE
</GridLayout>
<Button
android:id="#+id/new_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:background="#color/colorPrimaryDark"
android:text="Cancel"
android:layout_margin="5dp"
/>
<Button
android:id="#+id/new_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:background="#color/colorPrimaryLight"
android:text="Ok"
android:layout_margin="5dp"
/>
</RelativeLayout>
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
</LinearLayout>
P.S. What is the general name of things like LinearLayout or RelativeLayout?
As far as I know you have to initialize your ad in your Activity code using an AdRequest object:
AdView adView = (AdView) findViewById( R.id.adView );
AdRequest request = new AdRequest.Builder()
.build();
adView.loadAd( request );
Discarding a problem with your ADS initialization, as you say it works in another activity, and focusing on layout problems, I'd say the problem is the following:
You have a Vertical LinearLayout that covers the whole screen (match_parent on a top level) with two childs
The first child is a RelativeLayout that covers the whole parent (match_parent)
The second child is the Ad View that is probably being displayed, but you can't see it because it starts at the first pixel after the screen height.
So to correct this, I'd suggest you to try:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#cccccc"
>
.
.
.
</RelativeLayout>
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
I'd also suggest to give the proper dimensions to the Ad View as you know them (https://developers.google.com/android/reference/com/google/android/gms/ads/AdSize#constant-summary) . Then, I'd suggest to use a FrameLayout as is way lighter than a linearlayout. You would do it like this:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cccccc"
android:layout_marginBottom = "50dp">
.
.
.
</RelativeLayout>
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="bottom|center_horizontal"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
</FrameLayout>
Make sure that your Admob layout is displaying in xml view. Put your admob view inside RelativeLayout and try to use android:alignparentBottom:true
You can check this answer.
https://stackoverflow.com/a/28578320/2022000

ScrollView does not show in relativelayout

This is what I am trying to implement:
The visibility of the adview can be toggled to either visible or gone. This is a layout which I used and is working without the two buttons.
This is the xml for with the scrollview and the adview, without the buttons. (scrollview works)
<RelativeLayout
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id"
android:visibility="visible" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/adView"
android:paddingBottom="12dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="12dp">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="0dp"
android:layout_marginLeft="18dp"
android:layout_marginRight="18dp"
android:layout_marginTop="0dp"
android:stretchColumns="0">
<TableRow>
//scrollable content here
</TableRow
</TableLayout>
</ScrollView>
I try to implement scenario A by encapsulating the adView element inside a TableLayout, so I can attempt to use TableRows to implement the buttons.
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tableLayout">
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id"
android:visibility="visible" />
</TableLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/tableLayout"
.....
When I use this approach, my scrollview disappears. Where am I going wrong and how can I resolve this and get to scenario A?
You are missing the attribute 'android:layout_alignParentBottom="true"' from TableLayout that you had in AdView. This aligns the table in top of the parent relative layout although your intention was to put it in the bottom.

admob ads overlapping textview

I'm trying to add an Admob banner (at the top) to a the main activity (my main app screen), but I face tiny problem which is the admob ad overlapping textview
this is my main activity xml code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/adView"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_top_margin"
tools:context="${relativePackage}.${activityClass}" >
<com.google.android.gms.ads.AdView android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="ca-app-pub-xxxxxxxxx/xxxxxx"
ads:adSize="BANNER"/>
<LinearLayout
android:id="#+id/linearLayoutHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<!-- First header line -->
<TextView android:id="#+id/header_priority"
android:layout_width="31dp"
android:layout_height="wrap_content"
android:textColor="#00F"
android:gravity="center"
android:textSize="#dimen/font_size_tiny"
android:text="#string/pri_label"
/>
<!-- First header line -->
<TextView android:id="#+id/header_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#00F"
android:textSize="#dimen/font_size_tiny"
android:text="#string/desc_label"
/>
</LinearLayout>
<ListView
android:id="#+id/lvItems"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/llPriority"
android:layout_below="#+id/linearLayoutHeader" >
</ListView>
<LinearLayout
android:id="#+id/llPriority"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<EditText
android:id="#+id/etPriority"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="#string/hint_priority"
android:inputType="number"
android:text="#string/num_1" />
<EditText
android:id="#+id/tvItemText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:ems="10"
android:hint="#string/add_hint">
<requestFocus />
</EditText>
<Button
android:id="#+id/btnAdd"
android:layout_width="85dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:onClick="onClickAddItem"
android:text="#string/add_button_label" />
</LinearLayout>
</RelativeLayout>
Yes, RelativeLayout allows overlapp, but within RelativeLayout you can arrange the alignments of the view elements, too. Just figure out the id of the view element that overlaps with your adview and add the following attribute to your AdView (I am assuming it's overlapping with linearLayoutHeader)
android:layout_above="#+id/linearLayoutHeader"
You (or someone else that works on the same project?) actually already did make use of these attributes to arrange the views. Study the for example the following part of the code ;-)
<ListView
android:id="#+id/lvItems"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/llPriority"
android:layout_below="#+id/linearLayoutHeader" >
</ListView>
That's because the root layout is a relativelayout which allows overlap, the other layouts you use are linear which are meant for stacking controls.

Place adMob banner inside view

I am trying to make the ads look as part of the app by placing them inside a view with a custom background. This is the result that I want to achieve - actually it's a preview from the layout editor:
When I run the app, however, the background and the "Sponsorized" text are not shown. How can I make it work?
This is my current layout for the view:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.gms.ads.AdView android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adUnitId="#string/admob_id"
ads:adSize="MEDIUM_RECTANGLE"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sponsorized"
/>
</LinearLayout>
Create an another inner LinearLayout as parent to AdView . Use this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="10dp"
android:background="#color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp">
<com.google.android.gms.ads.AdView android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adUnitId="#string/admob_id"
ads:adSize="MEDIUM_RECTANGLE"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sponsorized"/>
</LinearLayout>

Android ScrollView not scrolling

I have solved my question. I chose to instead have a header and a footer that are always located at the bottom and top of the screen respectively. Then I created a "center content" which I enclosed within a ScrollView layout. I have updated the code below if people are interested in seeing what it now looks like.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:background="#FFFFFF">
<LinearLayout android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#layout/header"
android:layout_alignParentTop="true">
<ImageView android:src="#drawable/logo"
android:contentDescription="#string/logo_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dip"/>
</LinearLayout>
<ScrollView android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="350dip"
android:layout_below="#id/header">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="20dip" >
<!-- Email Label -->
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#372C24"
android:text="#string/email"/>
<EditText android:id="#+id/email_field"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:singleLine="true"
android:inputType="textEmailAddress"/>
<!-- Password Label -->
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:textColor="#372C24"
android:text="#string/password"/>
<EditText android:id="#+id/password_field"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:inputType="textPassword"/>
<!-- Login button -->
<Button android:id="#+id/login_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dip"
android:text="#string/login"/>
<!-- Register button -->
<Button android:id="#+id/register_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dip"
android:text="#string/register_button"/>
</LinearLayout>
</ScrollView>
<LinearLayout android:id="#+id/footer"
android:layout_width="fill_parent"
android:layout_height="90dip"
android:background="#layout/footer"
android:layout_alignParentBottom="true">
</LinearLayout>
</RelativeLayout>
For me, remove android:windowSoftInputMode="adjustPan" of the <activity> tag in AndroidManifest.xml file solved the problem.
I removed this line from the AndroidManifest.xml file
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
and added this line
android:theme="#android:style/Theme.NoTitleBar"
it has both fixed my scrolling app and made my app look more visually appealing. However, I am not sure if this is the correct fix.
for me the problem was that the LinearLayout in the ScrowView had android:layout_height="wrap_content".
<ScrollView android:id="#+id/scroll_view1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<LinearLayout android:id="#+id/layout_inside"
android:layout_width="match_parent"
android:layout_height="wrap_content" --> This should not we wrap_content, but specific height.
android:orientation="vertical" >
The ScrollView is not working when used as the root element of an XML layout.
It has to be wrapped inside a LinearLayout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView android:id="#+id/scroll_view1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<LinearLayout android:id="#+id/layout_inside"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
Have a fun ! !
ScrollView must have Linearlayout control as a immediate child, you have relative layout, I think that might be causing the problem.
Check this link out, also this. Look at their layout xmls..

Categories

Resources