I would like to create a GridView with squares items on two columns which would take the totality of the screen (match_parent).
Every item consists of an imageView and a textView.
Here is the result in image :
How do I make the items width to be equal to their variable height?
Thank you in advance!!
In your activity or Fragment layout you should have GridView like this:
<GridView
android:id="#+id/grid_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2"
/>
And then you should have an Adapter that extends BaseAdapter for example and it's item should be as follow:
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawableTop="#drawable/yourDrawable"
android:layout_margin="20dip"
android:gravity="center" />
You need more explanation ?
Use these link, they will help you
Option 1
Option 2
This is Gridview.
<GridView
android:numColumns="2"
android:id="#+id/related_contact_grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
This is item. You will inflate this to gridview in Adapter.
<?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">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/imageView"
android:text="Tendance"
android:layout_centerHorizontal="true"
</RelativeLayout>
If you want, I can support your work on this Gridview.
Related
I have GridView in one of my android application layouts. I am trying to set Item in center of Gridview but its always stay on the left side. I have searched many similar questions in StackOverflow but not got my problem solved. My XML is like below. Let me know if anyone can help me to make my GridView items in the center.
<FrameLayout
android:id="#+id/framecaregory"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/second"
android:layout_alignParentTop="true"
android:layout_marginTop="?actionBarSize">
<GridView
android:id="#+id/gridviewcategory"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/lite_color"
android:numColumns="5"
android:visibility="gone">
</GridView>
</FrameLayout>
Thanks
The thing you want does not depends on grid Layout, but to its child layout which is adapted to it. First create another layout grid_item_row.xml and put:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/framecaregory"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:gravity="center"
android:layout_height="wrap_content">
<TextView
android:text="Text in the center"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
After that adapt this layout to grid layout
Is it possible to make a gridview that has instead of a grid of pictures, a grid of pictures with a small imagebutton below every one of them?
Short answer: Yes. You can have an ImageView and an ImageButton in a GridView.
Long answer:
You will naturally have to create a custom GridView for that purpose.
For example:
Create an XML which will hold the container GridView, say, grid.xml:
<GridView
android:id="#+id/gridFriends"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clipChildren="true"
android:columnWidth="100dp"
android:fastScrollEnabled="true"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" >
</GridView>
And, to define the contents of the GridView, create another XML layout which will hold the ImageView and the ImageButton. Say, grid_items.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" >
<FrameLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center" >
<ImageView
android:id="#+id/imgProfilePicture"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#null" />
<ImageButton
android:id="#+id/imgbtnDemo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:background="#null"
android:gravity="center"
android:src="#drawable/ic_contact_picture" >
</ImageButton>
</FrameLayout>
</RelativeLayout>
Finally, if you are familiar with the concept of custom ListViews, with a few modifications, you will be able to implement a custom GridView too. If you are not familiar with custom ListViews or GridViews, follow this tutorial to see how to create a custom GridView: http://www.coderzheaven.com/2012/02/29/custom-gridview-in-android-a-simple-example/. Or use this Google Search for more tutorials on the same.
An important point here would be, if you need the ImageButton's to do a function when they are clicked, the onClickListener will need to be setup in the Adapter.
GridView shows a grid of Views. It can show anything that extends View class. It can show a LinearLayout with an ImageView and an ImageButton inside it.
i have an arraylist of data ( bluetooth devices ) and i want to list those devices in a list,
For this i have made a customized view (item.xml) :
<?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" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/images" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_margin="10dip" />
</LinearLayout>
Composed of an imageview and a textview, so i can put the name of the device and a standard image int the list, here is the main.xml file :
<?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="vertical" >
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
the progress bar is for the scanning operation in the beginning of the activity.
any help would be appreciated, thank you
A listview can support different layouts(views) and can be built dynamically. This is done through a custom adapter.
you need listview to hold those data in the list. in your main layout i cant see any listview. after taking list view you need to define adapter for that listview and use this adapter in your listview.
Hope this will help you
I have a linearlayout with 3 imageView and a listview in vertical orientation.i want to scroll the full screen including the images when the user scrolls the list.is that possible?thanks
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#222222"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ph2" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ph1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ph2" />
<ListView
android:id="#+id/ll4_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.5"
android:cacheColorHint="#00000000" />
</ScrollView>
You definitely don't want to put the ListView inside of the ScrollView, but it's certainly possible to include the images. I would recommend considering putting the layout with the images in a header to the ListView. See addHeaderView. This related SO post may also be of use to you.
You should search api , the scrollView can not be inner more than one child , must has one direct child
I want to make Activity like this using GridView.
And this Activity have only one scrollbar include all text & gridview.
for it, using scrollview.
<ScrollView>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<GridView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<GridView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</ScrollView>
I wrote xml and class like attached using ScrollView to have only one
scroll.
GridView has android:layout_height="fill_parent", but just visible one row.
And each GridView has scroll.
Same result layout_height was changed to wrap_content.
Is it bug? or Do I have some mistake? or Is it impossible?
Thanks.
You need to use the parameter weight :
0 for TextView
1 for GridView