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>
Related
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>
I came across this problem while making a launch pad activity. I have a GridView layout that is populated with app icons. I want to center it on the screen but just can not no matter what I've tried. If the icons are not enough to cover the whole screen width the GridView is aligned to the left. I put a white background in the GridView Layout to show the problem.
I change programmaticaly the size of the ImageView and the widthof the GridView column to offer more functionality so I see that increaasing the image size enough makes it look centered. But I want it to be centered regardless of the icons number or size. Can someone help?
(I also tried using a linear layout instead of a reletive for the GridView. no difference)
GridView Layout
<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="wrap_content"
android:background="#ffffff" >
<GridView
android:id="#+id/gridView1"
android:numColumns="auto_fit"
android:gravity="center"
android:layout_gravity="center"
android:columnWidth="70dp"
android:stretchMode="columnWidth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
</GridView>
</RelativeLayout>
Single grid-item layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_height="wrap_content" >
</ImageView>
</LinearLayout>
For centering in the middle of another layout (RelativeLayout), use
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
e.g. for a GridView:
<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="wrap_content" >
<GridView
android:id="#+id/gridView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" >
</GridView>
</RelativeLayout>
In order to make your Items fill the whole screen at all times, use match_parent for your item-width and use weights.
I have this cell item:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="#+id/titleText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:background="#AA000000"
android:gravity="center"
android:textColor="#FFFFFF"/>
</LinearLayout>
and this GridView:
<GridView android:id="#+id/gridView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:numColumns="auto_fit"
android:gravity="center"
android:columnWidth="100dp"
android:layout_weight="1"/>
I want to set linear layout's background image but, according to image size, I get different heigths for cells (I use different images) which is not very good looking...how can I stretch the background to a fixed heigth?
Unfortunately gridview seems not have a columnHeigth attribute...
Solved thanks to chet 's advice: I set heigth and width of linear layout.
What i want is to have the gridview filling whole screen. And when you scroll it down, at the end would appear a layout with 2 buttons. Ive been reading and couldnt figure how to get it. I could fix it on the top of the screen and the bottom (over the gridview). And also trying the layout with the button wont even appear when the gridview is filled.
Ive been playing with weights, aligns, etc. But cant find whats wrong. Heres the xml:
<?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:background="#drawable/fondo">
<GridView android:layout_width="fill_parent"
android:id="#+id/gridView1"
android:layout_height="wrap_content"
android:numColumns="2"
android:listSelector="#00000000">
</GridView>
<LinearLayout android:id="#+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_below="#id/gridView1">
<ImageView android:id="#+id/botonAtras"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="1"
android:onClick="onBotonAtrasClick">
</ImageView>
<ImageView android:id="#+id/botonAdelante"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="1"
android:onClick="onBotonAdelanteClick">
</ImageView>
</LinearLayout>
</RelativeLayout>
As far as I can tell you can't easily do this. You can do it with a plain ListView using addFooter(), but sadly GridView doesn't have this method. Also you can't implement your own GridView because it uses a lot of private APIs.
However, there is a horrible hack used here that might work:Android Pull to refresh
Apparently it uses a LinearLayout containing the header view and the ListView and moves them both. (According to this page.) As that guy says, a lot of work for a horrible horrible hack.
The only other way I can think to do it, is to give up on GridView and use a ListView where each row is a horizontal LinearLayout. Still very hacky though.
set LinearLayout below GridView by setting android:layout_below:"#+id/gridView" inside id/linearLayout
I solve this problem like this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fillViewport="true" >
<GridView
android:id="#+id/myGrid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="60dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:padding="10dp"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
</LinearLayout>
<Button
android:id="#+id/bacth_button"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:layout_alignParentBottom="true"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:layout_marginTop="2dip"
android:text="Uninstall Selected Apps"
android:textColor="#000000"
android:textSize="16dip"
android:textStyle="bold" />
</LinearLayout>
include GridView in linearlayout,then i's work as you like.
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.