Why can't I add elements after a GridView? - android

I've got the following layout XML file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">
<GridView android:id="#+id/main_grid_screen"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:numColumns="auto_fit" android:verticalSpacing="30dp"
android:horizontalSpacing="10dp" android:columnWidth="90dp"
android:stretchMode="columnWidth" android:gravity="center" />
<Button android:id="#+id/clickable_area" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/view_items_area" />
</LinearLayout>
The GridView shows up perfectly fine; however, the Button below it is completely absent. If I place the Button tag above the GridView, it appears. What gives?

I think it is because of the fill_parent attributes. The GridView is most likely taking up the entire screen and blocking your view of the button. You could try wrap_content instead.

Related

How to set the screens size of Grid view to match all screen sizes?

Here i have used a gridview but it is not changing according to the screen size.I think there is a problem with height.the xml used for gridview is given below.
<?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="fill_parent"
android:background="#drawable/bg"
android:orientation="vertical" >
<GridView
android:id="#+id/gridView1"
android:numColumns="3"
android:gravity="center"
android:paddingTop="20dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:stretchMode="columnWidth"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/linearLayout1"
android:layout_centerVertical="true"
>
</GridView>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical" >
<com.assuretech.ku.HorizontalMenuBar
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
From what I can see. You need to close the RelativeLayout at the end. I've included my screenshot of your code with it closed. I changed the background to red to see the text.
I guess you are looking for
numColumns = "auto_fit"
as GridView attribute
Firstly you must be using some layout to put your gridview in. Do the following:
Make height and width of the layout to fill parent:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
then, set height and width of your gridview to fill_parent as well.
Well, this is just a supposed solution that you might have been working this way
Hope this works for you.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="30dp">
<GridView
android:id="#+id/grid_images"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="100dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="15dp">
</GridView>
</LinearLayout>

Using Gridview and Button in LinearLayout

I tried add button below gridview in linearlayout at following code. But when i execute my app i only see gridview there is no button item. So it may basic mistake but i can't see it.
<?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"
android:orientation="horizontal"
android:background="#2a2a2a"
>
<GridView
android:id="#+id/grid_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnWidth="150dp"
android:numColumns="auto_fit"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"></GridView>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="#string/cam_string" />
</LinearLayout>
Change the orientation of your LinearLayout to vertical. I suspect you're not seeing the Button because the GridView takes up all horizontal space, then the LinearLayout adds the Button in a place off the screen that's not visible. You also may want to change the layout_height of the GridView to "0dip" and give it the `android:layout_weight=1' attribute so that it will fill all space except that taken up by the Button.
Other than the suggested Solution by others you can try the alternative using relative layout.
You can use a RelativeLayout for this purpose instead of LinearLayout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#2a2a2a" >
<GridView
android:id="#+id/grid_view"
android:layout_above="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnWidth="150dp"
android:numColumns="auto_fit"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"></GridView>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="#string/cam_string" />
</RelativeLayout>

Group More Than One Controls At Android

I had created a window with RelativeLayout and insterted two controls in it; one is GridView and the other one is WebView.
I will bind images to gridview to use like menu and i will load a html page in webview for ads.
But i have problems with these controls.
For example, if user scrolls gridview vertically it is disappearing behind webview however, in that i was expecting webview was slide with gridview too when user scroll gridview.
So i wonder, can i group these controls together to act simultaneously?
My another problem with orientation, when i turn my device to landscape mode webview is disappearing from screen.
Lastly if i set GridView's layout_height attribute fill_parent, i can't see WebView, so i set it as wrap_content. But this time WebView doesn't align at bottom, it aligns at GridView's after and there is space between screen bottom and webview.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg480480">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="auto_fit"
android:verticalSpacing="45dp"
android:horizontalSpacing="10dp"
android:columnWidth="150dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_marginTop="30dp"
android:scrollbars="none"
android:layout_alignParentTop="true" />
<WebView
android:id="#+id/webView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:gravity="bottom"
android:layout_below="#id/gridview" />
</RelativeLayout>
Here are some screen shots.
I guess the main problem is that both the GridView and the Webview specify layout_height="fill_parent". I guess you probably don't want the GridView to fill the whole parent area, so use "wrap_content" instead.
I don't know enough of the desired layout of the page but I would recommend using a LinearLayout to start and get comfortable. It really depends what the grid view and the web view should show and where your main content is.
In the example below I have simply assumed that your web view wants to take up the majority of the screen.
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff0000"
android:numColumns="auto_fit"
android:verticalSpacing="45dp"
android:horizontalSpacing="10dp"
android:columnWidth="150dp"
android:stretchMode="columnWidth"
android:layout_marginTop="30dp"
android:scrollbars="none"
/>
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ff00"
android:scaleType="centerInside" />
</LinearLayout>
If the assumption is not correct then you would probably specify it the other way round (Webview=>wrap_conent)
Improve the WebView layout code as below:
<GridView
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/webView1"
android:layout_alignParentTop="true"
android:layout_marginTop="30dp"
android:columnWidth="150dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:scrollbars="none"
android:stretchMode="columnWidth"
android:verticalSpacing="45dp" />
<WebView
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="bottom"
android:scaleType="centerInside" />
FYI, android:layout_alignParentBottom="true" will display WebView at the bottom of the screen. and include android:layout_above="#+id/webView1" inside the GridView.
Give a try to this layout code.
You need to add
android:layout_above="#+id/webView1"
in your gridview
also
android:layout_alignParentBottom="true"
in your webview
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg480480"
android:orientation="vertical"
>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="auto_fit"
android:verticalSpacing="45dp"
android:horizontalSpacing="10dp"
android:columnWidth="150dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_above="#+id/webView1"
android:layout_marginTop="30dp"
android:scrollbars="none"
android:layout_alignParentTop="true" />
<WebView
android:id="#+id/webView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#id/gridview" />
</RelativeLayout>

Android: Set layout as background of view

Is it possible to set another layout as the background for a view?
What I'm looking to do is have a text view that performs a countdown behind my grid view.
I've tried setting my layout as background resource for my grid view but it seems to expect a drawable only.
What is best workaround for this? How would I go about stacking this layout behind the grid view possibly with a transparent background?
FrameLayout can be used to stack things on top of other things.
Here is an example where I combined the HelloGridView code and my own layout of text views (that tries to emulate your counter). The result is a that the GridView displays dog pictures with the counter TextViews sort of floating above.
Hope this helps you.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/frameLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<GridView
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
<LinearLayout
android:id="#+id/counterLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/counterLabel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="center"
android:text="Counter" />
<TextView
android:id="#+id/counterValue"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="center"
android:text="128"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</FrameLayout>

Android XML Layout Help: How to layout UI so an ad is always visible

I have playing around with my XML layout for what seems like weeks now, and i just cannot get it to work how i would like it to.
I would like the admob ad to always been visible at the bottom of the screen, I have tried to do it like this, but because the gridview is graphically intensive i think the admob ad is being left behind somewhat.(It only shows 30% of the time, roughly)
Hopefully someone will be able to show me how to lay it out correctly. I.E Force the ad at the bottom of the screen!
Thanks
Lucy
<?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="wrap_content">
<ImageView android:id="#+id/ImageView03"
android:src="#drawable/banner_gallery"
android:layout_height="60dp"
android:layout_width="fill_parent"></ImageView>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:columnWidth="90dp"
android:numColumns="auto_fit" android:verticalSpacing="7dp"
android:horizontalSpacing="7dp" android:stretchMode="columnWidth"
android:gravity="center" android:layout_gravity="bottom"
android:layout_marginTop="10dp"></GridView>
<RelativeLayout
android:layout_marginTop="-50dip"
android:gravity="bottom"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<com.admob.android.ads.AdView
android:id="#+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
</LinearLayout>
Use Relative Layout as parent layout instead of linear layout and use android:layout_alignParentBottom="true" to the relative layout of addview
Try something like this. You only need one RelativeLayout and then layout the different views according to each other,
<?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">
<ImageView android:id="#+id/ImageView03"
android:src="#drawable/banner_gallery"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_width="fill_parent"/>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:columnWidth="90dp"
android:numColumns="auto_fit" android:verticalSpacing="7dp"
android:horizontalSpacing="7dp" android:stretchMode="columnWidth"
android:layout_below="#id/ImageView03"
android:layout_above="#id/ad"
android:layout_marginTop="10dp"/>
<com.admob.android.ads.AdView
android:layout_alignParentBottom="true"
android:id="#+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>

Categories

Resources