I'm trying to integrate ads (admob) into my Android app. It's mostly working, except for one issue with the layout. My layout consists of:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:app="http://schemas.android.com/apk/res/com.example.photos">
<LinearLayout android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="#+id/status_layout">
<ImageView android:id="#+id/cardStatus"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="#string/hello"
android:id="#+id/cardStatusText" />
</LinearLayout>
<GridView android:id="#+id/imageGridView"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:padding="10dp" android:verticalSpacing="10dp"
android:horizontalSpacing="10dp" android:numColumns="auto_fit"
android:columnWidth="100dp" android:stretchMode="columnWidth"
android:gravity="center" android:layout_below="#id/status_layout" />
<!-- Place an AdMob ad at the bottom of the screen. -->
<!-- It has white text on a black background. -->
<!-- The description of the surrounding context is 'Android game'. -->
<com.admob.android.ads.AdView android:id="#+id/ad"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_alignParentBottom="true" app:backgroundColor="#000000"
app:primaryTextColor="#FFFFFF" app:secondaryTextColor="#CCCCCC"
app:keywords="Android Photo" />
</RelativeLayout>
The ads are shown at the bottom of the screen, just as I want. However, they seem to be overlayed or drawn on top of the bottom portion of the grid view. I would like the gridview to be shorter, so that the ad can fill the bottom portion of the screen, and not hide part of the gridview. The problem is most annoying when you scroll all the way to the bottom of the gridview, and you still cannot fully see the last row items in the grid due to the ad.
I'm not sure if this is the standard way that AdMob ads work. If this is the case, adding some padding to the bottom of the grid (if that's possible) would due the trick. That way the user can scroll a bit further, and see the last row in addition to the ad.
I just switched from using LinearLayout to RelativeLayout after reading some similar issues with ListViews. Now my ad is along the bottom instead of above the grid, so I'm getting closer.
Thoughts?
-Kevin
put this property in your gridview
android:layout_above="#+id/ad"
Related
I have a GridView within FrameLayout as shown below:
As seen, numbers are aligned closer to left side of the screen. I have tried to bring numbers closer so that i could solve the problem, but failed. Playing around with stretchMode like 'columnWidth' and 'none' couldn't solve my problem. Please suggest me any way that i can fit numbers within background white area.
.xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/paperbck"
android:columnWidth="30dp"
android:gravity="center"
android:horizontalSpacing="20dp"
android:numColumns="4"
android:stretchMode="columnWidth"
android:verticalSpacing="20dp" />
</FrameLayout>
</LinearLayout>
Try giving left margin to the Grid View.
I am thinking to build an app which will have the same grid layout like in the picture, but I cant figure out what layout I should use.
GridView, or something like that?
It is possible using StaggeredGridView . Check out below repos on github
https://github.com/maurycyw/StaggeredGridView
https://github.com/chrisjenx/StaggeredGridView/tree/master/demo
Simple enough to do it with LinearLayout:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Stack rows vertically -->
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<!-- First row, two items -->
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<!-- First item, 50% of width -->
<FooView android:id="#+id/foo1"
android:layout_width="0px"
android:layout_weight="1"
android:layout_height="wrap_content" >
<!-- Second item, 50% of width -->
<FooView android:id="#+id/foo2"
android:layout_width="0px"
android:layout_weight="1"
android:layout_height="wrap_content" >
</LinearLayout>
<!-- Next row, just one item -->
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<!-- Third item, 100% of width -->
<FooView android:id="#+id/foo2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</LinearLayout>
<!-- And so on... -->
</LinearLayout>
</ScrollView>
Obviously, the exact nature of "FooView" is up to you, as are decorative borders, shadows, etc. In practice, you'd want to do this programatically, but this static xml layout shows you what you're after.
GridLayout also would work, but it's only available in 4.0 and above, unless you want to start using the compatibility library. Since LinearLayout will do the job, that's what I would use. RelativeLayout would probably also work, but I've never been able to get the damn thing to work, myself.
I'm not sure exactly what they use, but you can achieve the same effect with either a GridLayout or a ListView.
I have a layout with a ListView. The list scrolls fine, but sometimes the other content on the page needs to be bigger and either the list or the other content take up too much of the screen so I need it all to scroll.
Can that be done?
Here is the layout I have so far:
<?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"
>
<include android:id="#+id/header"
layout="#layout/header"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
<TextView
android:id="#+id/view_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Business:"
/>
<TextView
android:id="#+id/business_privacy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Loading..."
/>
<TextView
android:id="#+id/think"
android:layout_width="fill_parent"
android:layout_marginTop ="5dp"
android:textColor="#color/light_best_blue"
android:paddingLeft="10px"
android:layout_height="wrap_content"
android:text="Fill out each section below"
/>
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:textSize="20px"
>
</ListView>
</LinearLayout>
What would be the way to make it easy for the user to be able to see both the text and the list even if the text takes up the whole screen? Can I make the text scrollable? It is especially bad in horizontal view so I think there must be some ux patters to make it play nicely.
Thanks for the advice!
1- Put textview which has a larger text in header of the list view so it get scroll with list...
or
2- put the textview in other scroll view with fixed size like 1/3 of the screen.
You could either make the content wrap, or wrap the content up using a HorizontalScrollView. Android docs on it can be found here.
The default screen of my app shows a ListView. When the user selects an entry a new Activity is displayed with a Title (TextView), some information gained from xml (WebView).
I want to place an Ad aligned to the bottom of this second Activity.
My layout currently is this:
<LinearLayout android:id="#+id/info" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" style="#style/info">
<TextView android:id="#+id/title" style="#style/title" />
<WebView android:id="#+id/info" style="#style/info" />
</LinearLayout>
This works perfectly, but I can't get the Ad to always be at the bottom.
I've tried RelativeLayout, but no matter what, I always get the same results!
If the HTML shown in the WebView is short enough for the AdView to be displayed (but not at bottom, underneath WebView) or its too long and the AdView is not shown at all!
I want the AdView always visible always, even if the WebView scrolls!
Hope this makes sense!
Neil
PS: I'll continue to look for answers and update if I find one....
Figured out a way to solve this, but is it the best way??
This is how I did it:
<LinearLayout android:id="#+id/linearlayout" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:neildeadman="http://schemas.android.com/apk/res/com.neildeadman.android" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" style="#style/Info">
<LinearLayout android:id="#+id/info" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" style="#style/info">
<TextView android:id="#+id/title" style="#style/title" />
<WebView android:id="#+id/info" style="#style/info" />
</LinearLayout>
</LinearLayout>
The "layout_weight" attributes allowed me to force the inner LinearLayout to fill the remaining gap left by the add, which then contained my layout from above!
Works lovely....
I have a gridview that is inflated by multiple buttons. I used just a gridview and it worked fine. The grid was in correct order and was centered. Although, I wanted to add a button below the grid of buttons that was different and centered. To do this I decided to section off the gridview and then add the button (therefore it wouldn't be affected by the gridview). I tried the following code to seperate the views:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout
android:id="#+id/tbl_gridcreator"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<TableRow>
<GridView
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="150px"
android:numColumns="7"
android:stretchMode="spacingWidthUniform"
android:gravity="center">
<!--
android:horizontalSpacing="0px" -->
</GridView>
</TableRow>
<TableRow android:gravity="center">
<Button android:text="Submit"
android:id="#+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</TableRow>
</TableLayout>
</LinearLayout>
The problem is that this code smushes the gridview to the left and oddly it flips it (Position 0 is ont he bottom right and the last button is on the top left pushed off screen). The following image shows what is the result:
Any idea why its pushed to the left and flipped? Or maybe how I could have just the Gridview with the button on the bottom?
I figured it out. Not sure why I was using a TableLayout to encapsulate the GridView and the Button but I fixed it by removing the TableLayout and just having the the GridView above the Button in a RelativeLayout instead of a LinearLayout. There still some bugs but the most of the issue has been solved.