Using LayoutInflater to add the same view multiple times below each other - android

i've been working on a simple UI which displays a few photos and some other info!
I have a simple layout for displaying each photo in a box and I wanted to add this layout dinamicaly multiple times (the number of photos I have).
This is my info_wrapper.xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/info_wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:paddingLeft="1dp"
android:paddingRight="1dp"
>
<TextView
android:id="#+id/usernamePosting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"/>
<ImageView
android:below="#+id/usernamePosting"
android:id="#+id/photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"/>
</RelativeLayout>
And my main_screen.xml file:
<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="match_parent" >
<RelativeLayout
android:id="#+id/menuBar"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:background="#color/green">
<ImageView
android:id="#+id/locationIcon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/location_place"
></ImageView>
<TextView
android:id="#+id/userLocationTxtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toRightOf="#+id/locationIcon"
android:text="#string/app_name"
android:textColor="#color/white"
android:textSize="15dp" />
</RelativeLayout>
<RelativeLayout
android:layout_below="#id/menuBar"
android:id="#+id/bodyView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/light_gray">
</RelativeLayout>
</RelativeLayout>
Right now when I try to add multiple times the info_wrapper view - all the view ar on top of each other - but I want to be one below other

Just use a ListView with your own implementation of e.g. BaseAdapter or ArrayAdapter.
Have a look at getItemViewType and getViewTypeCount.
If you have two different types of views, have getViewTypeCount return 2 and getItemViewType(position) either 0 or 1 depending on which element is at position.
You could for example have a custom Adapter implementation which inflates/reuses different views for different types of objects:
class ExampleAdapter extends BaseAdapter
{
[...]
View getView(int position, View convertView, ViewGroup parent)
{
Object o = getItemFromSomeDataSource(position);
if(o instanceof Type1)
{
if(convertView == null)
{
convertView = inflater.inflate(item_layout_1);
[...]
}
else
{
[...] // reuse existing View
}
}
else if(o instanceof Type2)
{
[...]
}
[...]
}
int getViewTypeCount(){ return n; } // where n = no. of different types to display
int getItemViewType (int position){ [...] } // mapping of item-position and item-type
}

Related

Android ListView Customization (title, description, details, footer)

As I'm new to Android, I'm struggling to design a custom layout which is going to be my listview row. I like my list view row should contain title description, details and footer. My listview row hold data like the image I attached. Please see the attached image
You must need to create your_layout_row.xml first
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal"
android:layout_margin="14dp"
>
<LinearLayout
android:layout_width="60dp"
android:layout_height="60dp"
>
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:paddingLeft="10dp"
android:layout_height="60dp"
/>
</LinearLayout>
<TextView
android:gravity="center_vertical"
android:paddingBottom="10dp"
android:paddingRight="5dp"
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="20dp"
android:text="TextView"
android:textStyle="normal|italic"
android:textSize="17dp" />
</LinearLayout>
now here you have your listview layout.
<?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="#color/TaskList_layout_background_color"
>
<ListView
android:id="#+id/lv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:dividerHeight="10dp"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp" >
</ListView>
</RelativeLayout>
now create your class Model
for example :
class ModelClass
{
String text;
public ModelClass(String _text)
{
text = _text;
}
public String getText()
{
return this.text;
}
}
now create your custom adapter class
public class CustomAdapter extends ArrayAdapter<ModelClass> {
// declaring our ArrayList of items
private ArrayList<ModelClass> objects;
/* here we must override the constructor for ArrayAdapter
* the only variable we care about now is ArrayList<Item> objects,
* because it is the list of objects we want to display.
*/
public CustomAdapter(Context context, int textViewResourceId, ArrayList<ModelClass> objects) {
super(context, textViewResourceId, objects);
this.objects = objects;
}
/*
* we are overriding the getView method here - this is what defines how each
* list item will look.
*/
#Override
public View getView(int position, View convertView, ViewGroup parent){
// assign the view we are converting to a local variable
View v = convertView;
// first check to see if the view is null. if so, we have to inflate it.
// to inflate it basically means to render, or show, the view.
if (v == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.your_layout_row null);
}
/*
* Recall that the variable position is sent in as an argument to this method.
* The variable simply refers to the position of the current object in the list. (The ArrayAdapter
* iterates through the list we sent it)
*
* Therefore, i refers to the current Item object.
*/
ModelClass i = objects.get(position);
if (i != null) {
// This is how you obtain a reference to the TextViews.
// These TextViews are created in the XML files we defined.
TextView tt = (TextView) v.findViewById(R.id.textView);
// check to see if each individual textview is null.
// if not, assign some text!
if (tt != null){
tt.setText( objects.get(position).getText());
}
}
// the view must be returned to our activity
return v;
}
}
now in your main activity set your listview adapter to this customAdapter.
ArrayList<MyModel> myList = new new ArrayList<MyModel>();
myList.add("1");
myList.add("2");
myList.add("3");
myList.add("4");
ListView myListView = (ListView)findViewById(R.id.lv_list);
CustomAdapter adapter= new CustomAdapter(acitivity, 0, myList);
myListView.setAdapter(adapter);
In this case, you have to custom adapter.
Actually Relative is more suitable for designs. But now I'll write with LinearLayout to be easy.
here is the custom layout for listview item...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="Title"
android:background="#android:color/holo_red_light"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:background="#android:color/holo_green_light"
android:text="DESC"/>
<TextView
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:background="#android:color/holo_blue_light"
android:text="Other Detais"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="Footer"
android:background="#android:color/holo_red_light"
android:gravity="center"/>
</LinearLayout>
copy and modify above answer by Hein Htet Aung
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="Title"
android:background="#android:color/holo_red_light"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:padding="20dp"
android:background="#android:color/holo_green_light"
android:text="DESC"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="20dp"
android:layout_weight="1"
android:gravity="center"
android:background="#android:color/holo_blue_light"
android:text="Other Detais"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:text="Footer"
android:background="#android:color/holo_red_light"
android:gravity="center"/>

listview and edittext softkeyboard

I have custom template with edittext field. When I click on "next" button on softkeyboard it move focus only two time - than button changed to "OK". List have 12 items.
Any way to navigate to all items, not only 2?
Can you help me please?
Im use this template for listview:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="3dp" >
<TextView
android:id="#+id/head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="true"
android:paddingBottom="3dp"
android:paddingTop="3dp" >
<EditText
android:id="#+id/value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:gravity="left"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceSmall"
android:width="100dp" />
</LinearLayout>
</LinearLayout>
And this xml for listview:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/calk_1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="382dp"
android:divider="#color/reddivider"
android:dividerHeight="#dimen/twodp"
android:focusable="true"
android:focusableInTouchMode="true"
android:smoothScrollbar="true" >
</ListView>
</LinearLayout>
Also, here my adapter right now:
public View getView(int position, View convertView, ViewGroup parent){
// assign the view we are converting to a local variable
View v = convertView;
// first check to see if the view is null. if so, we have to inflate it.
// to inflate it basically means to render, or show, the view.
if (v == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_view, null);
}
/*
* Recall that the variable position is sent in as an argument to this method.
* The variable simply refers to the position of the current object in the list. (The ArrayAdapter
* iterates through the list we sent it)
*
* Therefore, i refers to the current Item object.
*/
CalcItem i = objects.get(position);
int last=getCount()-1;
if (i != null) {
// This is how you obtain a reference to the TextViews.
// These TextViews are created in the XML files we defined.
TextView hd = (TextView) v.findViewById(R.id.head);
TextView tx = (TextView) v.findViewById(R.id.text);
TextView ds = (TextView) v.findViewById(R.id.description);
EditText vl = (EditText) v.findViewById(R.id.value);
if (position==0){
vl.setNextFocusUpId(last);
vl.setNextFocusDownId(1);
} else if (position==last){
vl.setNextFocusDownId(0);
} else {
vl.setNextFocusDownId(position+1);
}
if (hd != null){
hd.setText(i.getHead());
}
if (tx != null){
tx.setText(i.getText());
}
if (ds != null){
ds.setText(i.getDescription());
}
if (vl != null){
vl.setText(Integer.toString(i.getValue()));
}
}
// the view must be returned to our activity
return v;
}
Use android:nextFocusUp="id" and android:nextFocusDown="id" - as described in the documentation.
Here's an example from the docs:
<LinearLayout
android:orientation="vertical"
... >
<Button android:id="#+id/top"
android:nextFocusUp="#+id/bottom"
... />
<Button android:id="#+id/bottom"
android:nextFocusDown="#+id/top"
... />
</LinearLayout>
As far as I know, Edit texts doesnt work well in ListViews and Recycler views,
I'll Recommend you to inflate separate views multiple times instead of making a ListView if you are dealing with edit texts.

android:how to make infinite scrollview with endless scrolling

I want a code to make the ScrollView show the same images with endless scrolling over and over.
This is excelent for the layout and I want to know what is the code necessary for adding it to a ScrollView with infinite scrolling.
<RelativeLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</HorizontalScrollView>
Use a ListView and an Adapter that is modified slightly to have "infinite" elements
Here are the changes to your the Adapter that would support this behavior:
#Override
public int getCount()
{
return Integer.MAX_VALUE;
}
#Override
public ImageItem getItem(int position)
{
return mItems.get(position % mItems.size());
}
Essentially you trick it by telling it that the count is MAX_INT and then when you go to get an item use mod to get correct item in the sequence.
Several people have proposed different solutions to this already as well.
See Here: Android Endless List
and CommonsWare has a component that supports this behavior as well: https://github.com/commonsguy/cwac-endless
FoamGuy's answer is correct.
But in order to make the list go backwards, as in an infinite carrousel, I also trick the system by setting the default element to be Integer.MAX_VALUE/2 by calling:
listView.setSelection( Integer.MAX_VALUE/2 );
It is pretty unlikely the user will scroll back 1 billion elements back, which makes the effect of infinite carrousel in both directions.
I also have some other modifications to the BaseAdapter custom implementation:
#Override
public Object getItem(int position) {
if ( model.getSize()==0 ) {
return null;
}
// mod the list index to the actual element count
return model.getElementAtPosition( position%model.getSize() );
}
#Override
public long getItemId(int position) {
if ( model.getSize()==0 ) {
return -1;
}
// same as above
return model.getElementAtPosition( position%model.getSize() ).id;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if ( model.getSize()==0 ) {
return null;
}
// also make sure to point to the appropriate element in your model otherwise you
// would end up building millions of views.
position%=model.getSize();
View front= convertView;
if ( convertView==null ) {
// inflate/build your list item view
}
...
}
This way the list will spin around like in a carousel w/o extra memory allocation for unneeded views.

problem with custom ListView showing 3 vertical TextViews

im trying to display 3 TextViews as a custom row design in a ListView. Unfortunatly, only 2 TextViews ( row_title and row_postcode_city) are visible...
Maybe somebody has an idea?
Regards,
float
Code of my ArrayAdapter:
private class PartnerAdapter extends ArrayAdapter<Partner> {
private ArrayList<Partner> items;
public PartnerAdapter(Context context, int textViewResourceId, ArrayList<Partner> items) {
super(context, textViewResourceId, items);
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.partner_suche_ergebnis_row, parent, false);
}
Partner o = items.get(position);
if (o != null) {
TextView title = (TextView) v.findViewById(R.id.row_title);
TextView subtitle = (TextView) v.findViewById(R.id.row_subtitle);
TextView postcode_city = (TextView) v.findViewById(R.id.row_postcode_city);
if (title != null) {
title.setText(o.getTitle());
}
if(subtitle != null){
subtitle.setText(o.getSubtitle());
}
if(postcode_city != null){
subtitle.setText(o.getPostcode() + " " + o.getCity());
}
}
return v;
}
}
XML of the custom row design:
<?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="?android:attr/listPreferredItemHeight"
android:padding="0dip"><!--
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:src="#drawable/icon" />
--><LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="#+id/row_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="bold"/>
<TextView
android:id="#+id/row_subtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/row_postcode_city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
Try to use the following xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="0dip">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="#+id/row_title"
android:text="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="bold"/>
<TextView
android:id="#+id/row_subtitle"
android:text="2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:text="3"
android:id="#+id/row_postcode_city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
Check it now.
Change your LinearLayout to "wrap_content" on layout_height
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content">
Also, I don't know why you have a LinearLayout inside a LinearLayout. Since this xml is only for individual rows. Each row will be a horizontal LinearLayout, the second one being unnecessary as ListView automatically lists rows vertically.
Finally, the root LinearLayout should use minHeight for the default Item Height, not layout_height.
android:minHeight="?android:attr/listPreferredItemHeight"
This should be your xml file (unless you really need the second LinearLayout for some odd reason)
<?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="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:padding="0dip">
<TextView
android:id="#+id/row_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="bold"/>
<TextView
android:id="#+id/row_subtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/row_postcode_city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Sorry for offtopic, but I here some useful tips from me
You have a LOT of needless (and flat out wrong) stuff going on here:
Partner o = items.get(position);
if (o != null) {
You should get something for every position, if item retuns null that means you are doing something wrong. In your case, if you get null you just return reused old, or brand new row, which will confuse the user by displaying the old data, or no data at all.
if (title != null) {
You have 2 ways of getting View object of your row, by inflating, or getting it from recycler (convertView). And recycler will ALLWAYS return the right type of View, so those check are useless since your components will always be found.

A grid layout of icon/text buttons

I am attempting to create a 3 x 3 grid of items. Each Item consists of an ImageView on top of a TextView. Unfortunately, I am having issues getting everything to play nicely.
Here is my attempt to get 2 such items side by side. The text views don't even show, and the icons are squished together (instead of evenly spaced)
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1" android:gravity="center"
android:paddingLeft="40px" android:paddingRight="40px" >
<TableRow>
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/usertoolsimage"
android:src="#drawable/ftnicon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:text="User Accounts"
android:gravity="right"
android:padding="3dip" android:textColor="#ffffff" />
</LinearLayout>
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/queueimage"
android:src="#drawable/ftnicon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:text="Queue Management"
android:gravity="right"
android:padding="3dip" android:textColor="#ffffff" />
</LinearLayout>
</TableRow>
<TableRow>
<TextView
android:text="test 3"
android:padding="3dip" android:textColor="#ffffff" />
<TextView
android:text="test 4"
android:gravity="right"
android:padding="3dip" android:textColor="#ffffff" />
</TableRow>
</TableLayout>
My goal in the end is to have a grid of clickable items where the item is an image and text for a main menu. Can anyone guide me on what layouts I should use to achieve this?
Your best bet in my opinion would be to use the gridView that way it supports scrolling and spacing and you can be very dynamic in what each items layout and events are. Another option is to just create a lay out the images with a combination of Relative/Linear Layouts.
GridView layout:
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:columnWidth="60dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
and then in your activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
mGame.status = GameStatus.PLAYING;
setContentView(R.layout.gridLayout);
GridView grid = (GridView) findViewById(R.id.myGrid);
grid.setAdapter(new customAdapter());
grid.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
//do some stuff here on click
}
});
}
public class customAdapter extends BaseAdapter {
public View getView(int position, View convertView, ViewGroup parent) {
//create a basic imageview here or inflate a complex layout with
//getLayoutInflator().inflate(R.layout...)
ImageView i = new ImageView(this);
i.setImageResource(mFams.get(position).imageId);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
final int w = (int) (36 * getResources().getDisplayMetrics().density + 0.5f);
i.setLayoutParams(new GridView.LayoutParams(w * 2, w * 2));
return i;
}
public final int getCount() {
return 9;
}
public final Family getItem(int position) {
return mFams.get(position);
}
public final long getItemId(int position) {
return position;
}
}
Or the basic layout using linear layouts:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout android:id="#+id/linearLayout1"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_alignParentLeft="true"
android:layout_width="fill_parent"
android:orientation="horizontal">
<ImageView>...</ImageView>
<ImageView>...</ImageView>
<ImageView>...</ImageView>
</LinearLayout>
<LinearLayout android:id="#+id/linearLayout2"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_alignParentLeft="true"
android:layout_width="fill_parent"
android:orientation="horizontal">
<ImageView>...</ImageView>
<ImageView>...</ImageView>
<ImageView>...</ImageView>
</LinearLayout>
<LinearLayout android:id="#+id/linearLayout3"
android:layout_height="fill_parent""
android:layout_weight="1"
android:layout_alignParentLeft="true"
android:layout_width="fill_parent"
android:orientation="horizontal">
<ImageView>...</ImageView>
<ImageView>...</ImageView>
<ImageView>...</ImageView>
</LinearLayout>
You should really use a GridView to do grids, and not TableRows. Have you seen Android's tutorial for GridView's? To be able to achieve what you want with an image overlayed with text, you would need to utilize the FrameLayout as shown in Android's FrameLayout example. The tricky part here though, is that you need to apply this layout to each item that is going to be in the Gridview.
So for example, lets say you create a layout file called image_text_view.xml which looks like this:
<FrameLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:id="#+id/gridImage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_horizontal">
<ImageView
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="false">
</ImageView>
<CheckedTextView
android:id="#+id/imageTick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textColor="#000000"
android:layout_gravity="center_horizontal|bottom"
android:checkMark="#drawable/icon"
android:checked="false"
android:visibility="invisible"
>
</CheckedTextView>
</FrameLayout>
You need to apply this layout in each of your GridView item. To do this (editing Android's GridView example) You would need to redefine the getView in the ImageAdapter as follows:
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if(convertView==null){
v = LayoutInflater.from(mContext).inflate(R.layout.image_text_view,null);
v.setLayoutParams(new GridView.LayoutParams(100,100));
ImageView imageView = (ImageView)v.findViewById(R.id.image);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
imageView.setImageResource(mThumbsIds[position]);
CheckedTextView checkedTextView = (TextView) v.findViewById(R.id.imageTick);
checkedTextView.setEnabled(true);
checkedTextView.setVisibility(View.VISIBLE);
}
else
{
v = convertView;
}
return v;
Using this, for example, you will have whatever you set (whether its text or icons) overlaying the images for each item in the grid. You may need to do minor tweaks to this example to meet your exact needs, but this is the strategy i would go with.

Categories

Resources