Android Images in a gridView in wrong order - android

i have a problem with a image gridView.
I put all the images in an array, but when the grid is displayed the images aren’t in the order that I decided.
Here is my code taken from various tutorials.
The main activity:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Clicca sull'immagine per ingrandire"
android:id="#+id/textView2"
android:layout_gravity="center_horizontal"
android:textAlignment="center"
android:paddingBottom="10sp"
android:textColor="#color/white"
android:textSize="#dimen/text_size"/>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:columnWidth="300dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center"
android:stretchMode="columnWidth" >
</GridView>
Related java code:
package ...;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
public class extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_);
GridView gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
/**
* On Click event for Single Gridview Item
* */
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Sending image id to FullScreenActivity
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The Image Adapter:
package com.example.valeria.Flexibilia_abbigliamento_per_danze_caraibiche_by_DDLAB;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.max27a1ridotta, R.drawable.max27b1ridotta,
R.drawable.max27c1ridotta, R.drawable.max29a1ridotta,
R.drawable.max29b1ridotta, R.drawable.max30a1ridotta,
R.drawable.max30b1ridotta, R.drawable.max33a1ridotta,
R.drawable.max33b1ridotta, R.drawable.max36a1ridotta,
R.drawable.max38a1ridotta, R.drawable.max38b1ridotta,
R.drawable.max281ridotta, R.drawable.max311ridotta,
R.drawable.max321ridotta, R.drawable.max341ridotta,
R.drawable.max351ridotta, R.drawable.max371ridotta,
};
// Constructor
public ImageAdapter(Context c) {
mContext = c;
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setLayoutParams(new GridView.LayoutParams(350, 300));
} else {
imageView = (ImageView) convertView;
}
return imageView;
}
}
The gridView display the correct number of items, but the images are randomized.
I am new in android and in programming, and is possible that taking examples from various sources I put too much code.
Where did I go wrong?
Thanks and sorry for my English…

The problem is not in the order of images, the problem is in the way you work with convertView. even if it is not null you have to fill it, otherwise you will get a cached View for different position. So this code should solve the problem:
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setLayoutParams(new GridView.LayoutParams(350, 300));
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
And just for the future I would suggest you to read about ViewHolder patern

Related

Why this custom adapter does not show output

I'm new in android. I have tried a custom adapter but output is not visible. apparently there is no error.Kindly mention where I'm doing wrong.
Here is code
package com.example.customadapter;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;
import android.widget.TextView;
public class MainActivity extends Activity {
GridView gridview;
static final String[] Box_Clrs=new String[]{
"pink" , "red" , "blue"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridview= (GridView) findViewById(R.id.grid_view);
gridview.setAdapter(new ImageAdapter(this, Box_Clrs));
gridview.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id){
Toast.makeText(
getApplicationContext(),((TextView) v.findViewById(R.id.grid_text))
.getText(), Toast.LENGTH_SHORT).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
ImageAdapter.java
package com.example.customadapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ImageAdapter extends BaseAdapter {
Context context;
final String[] clrValues;
public ImageAdapter(Context context, String[] clrValues)
{
this.context=context;
this.clrValues=clrValues;
}
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if(convertView==null)
{
gridView = new View(context);
gridView = inflater.inflate(R.layout.mobile,null);
TextView textview=(TextView) gridView.findViewById(R.id.grid_text);
textview.setText(clrValues[position]);
ImageView imageview=(ImageView) gridView.findViewById(R.id.image);
String clr= clrValues[position];
if(clr.equals("pink"))
imageview.setImageResource(R.drawable.pink);
else if(clr.equals("red"))
imageview.setImageResource(R.drawable.red);
else
imageview.setImageResource(R.drawable.blue);
}
else
{
gridView=(View) convertView;
}
return convertView;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return clrValues.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position){
// TODO Auto-generated method stub
return 0;
}
}
activity_main.xml
<GridView 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:id="#+id/grid_view"
android:gravity="center"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
android:numColumns="auto_fit"
tools:context="com.example.customadapter.MainActivity" >
mobile.xml
<LinearLayout 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:padding="5dp"
tools:context="com.example.customadapter.MainActivity" >
<ImageView
android:id="#+id/image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:src="#drawable/blue">
</ImageView>
<TextView
android:id="#+id/grid_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp">
</TextView>
You are missing convertView = gridView; after inflating and populating your list item layout. I find it easier to do something like this:
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.list_item, parent, false);
}
// do your setup here
return convertView;
getView method should return item of your grid view instead of gridview itself
try to use something like this:
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)
View view
if(convertView==null)
{
view = inflater.inflate(R.layout.mobile,parent, false);
} else {
view = convertView;
}
TextView textview=(TextView) view.findViewById(R.id.grid_text);
textview.setText(clrValues[position]);
ImageView imageview=(ImageView) view.findViewById(R.id.image);
String clr= clrValues[position];
if(clr.equals("pink"))
imageview.setImageResource(R.drawable.pink);
else if(clr.equals("red"))
imageview.setImageResource(R.drawable.red);
else
imageview.setImageResource(R.drawable.blue);
return view;
}
You should also improve methods:
#Override
public Object getItem(int position) {
return clrValues[position];
}
#Override
public long getItemId(int position){
return position;
}
See a tutorial on custom adapters http://www.vogella.com/tutorials/AndroidListView/article.html#adapterown
Try this;
Change below;
View gridView;
if(convertView==null)
{
gridView = new View(context);
gridView = inflater.inflate(R.layout.mobile,null);
…………
}
To;
View gridView = convertView;
if(gridView==null)
{
gridView = inflater.inflate(R.layout.mobile,null);
………………
}

How to launch app when clicked on icon in Android?

Hey guys right now I'm working on a launcher app. I've made a code where I can display all installed apps in a GridView but when I click the icon nothing happens, so how can I launch app from it? Here's my code:
MainActivity:
package com.example.appgrid;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class MainActivity extends Activity {
GridView mGrid;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loadApps();
setContentView(R.layout.activity_main);
mGrid = (GridView) findViewById(R.id.myGrid);
mGrid.setAdapter(new AppsAdapter());
}
private List<ResolveInfo> mApps;
private void loadApps() {
// TODO Auto-generated method stub
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mApps = getPackageManager().queryIntentActivities(mainIntent, 0);
}
public class AppsAdapter extends BaseAdapter {
public AppsAdapter() {
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i;
if (convertView == null) {
i = new ImageView(MainActivity.this);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new GridView.LayoutParams(95, 95));
i.setPadding(10, 10, 10, 10);
} else {
i = (ImageView) convertView;
}
ResolveInfo info = mApps.get(position);
i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));
return i;
}
public final int getCount() {
return mApps.size();
}
public final Object getItem(int position) {
return mApps.get(position);
}
public final long getItemId(int position) {
return position;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
main.xml:
<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="com.example.appgrid.MainActivity" >
<GridView
android:id="#+id/myGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="4"
android:gravity="center">
</GridView>
</RelativeLayout>
Thanks in advance!
Try this
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.package.address");
startActivity(launchIntent);

GridView items are too small - Android

I am following this tutorial, it is very simple, and yet my GridView images are much much smaller than the one in the tutorial. Does anything stand out as wrong code? Below I've included my main activity, my adapter class, and my xml with the GridView.
It should be like this:
Instead, mine is like this:
SitesActivity.java
package org.azurespot.cutelinks;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.GridView;
import org.azurespot.R;
public class SitesActivity extends ActionBarActivity {
private GridView gridView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sites);
// with fragments, make sure you include the rootView when finding id
gridView = (GridView) findViewById(R.id.sites_grid);
// Set the Adapter to GridView
gridView.setAdapter(new GridViewSitesAdapter(this));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_sites, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
GridViewSitesAdapter.java
package org.azurespot.cutelinks;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import org.azurespot.R;
/**
* Created by mizu on 2/11/15.
*/
public class GridViewSitesAdapter extends BaseAdapter {
public Context mContext;
public GridViewSitesAdapter(Context c) {
mContext = c;
}
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.cute_overload, R.drawable.attack_of_the_cute,
R.drawable.zoo_borns, R.drawable.cutest_paw,
R.drawable.mochimochiland, R.drawable.baby_mugging,
R.drawable.cutest_food, R.drawable.tiny_cute_things,
R.drawable.etsy_robot_plush
};
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
return imageView;
}
}
activity_sites.xml
<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"
tools:context="org.azurespot.cutelinks.SitesActivity"
android:background="#2198bb">
<GridView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/sites_grid"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:layout_margin="10dp"
android:columnWidth="90dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:background="#drawable/button_border">
</GridView>
</RelativeLayout>
Why don't you just calculate the width of gridview item based on the screenwidth
columnWidth = (int) ((getScreenWidth() - ((AppConstant.NUM_OF_COLUMNS + 1) * padding)) / AppConstant.NUM_OF_COLUMNS);
Hope this sample line of code will help you understand. Let me know if you need any help on it.
Thanks :)

Android Programming ViewPager from a GridView

I am new to android programming and am looking to implement a gallery which has a grid view that then pushes on to a view pager but I am having a few problems getting this to work. I have it working currently just using a static Image Adapter but am not sure how to change this to use a view pager instead. I also want to be able to add my own titles to each screen but I wasn't sure how to do this or whether it had to be the image name that would appear?
The code I have at the moment is...
the .xml with the grid view
<?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"
android:background="#3d3d3e" >
<GridView
android:id="#+id/gridview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/phone"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" >
</GridView>
</RelativeLayout>
the grid view .java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
public class Marble extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.marble);
GridView gridView = (GridView) findViewById(R.id.gridview1);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter1(this));
/**
* On Click event for Single Gridview Item
* */
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Sending image id to FullScreenActivity
Intent i = new Intent(getApplicationContext(), FullImageActivity1.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
}
});
}
}
the image adapter .java
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter1 extends BaseAdapter {
private Context mContext;
// Keep all Images in array
Integer[] mThumbIds = {
R.drawable.arabescato, R.drawable.biancocarrara,
R.drawable.botticinoclassico, R.drawable.calacattaoro,
R.drawable.cremamarfil, R.drawable.cremavalencia,
R.drawable.emperadordark, R.drawable.jurabeige,
R.drawable.neromarquina, R.drawable.perlatoolympo,
R.drawable.rojoalicante
};
// Constructor
public ImageAdapter1(Context c){
mContext = c;
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(265, 265));
return imageView;
}
}
and finally the image activity .java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
public class FullImageActivity1 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter1 imageAdapter1 = new ImageAdapter1(this);
ImageView imageView = (ImageView) findViewById(R.id.fullimage);
imageView.setImageResource(imageAdapter1.mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
}
}
All help on this will be massively appreciated as I'm absolutely stumped!
Thanks in advance!
Before you proceed, add the android.support.v4 jar file to your project.
You need 2 things: the ViewPager in your layout, and an adapter that extends PagerAdapter
First change your full_image.xml layout to this:
<?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.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Then create an adapter that extends PagerAdapter (required by ViewPager)
public class ImagePagerAdapter extends PagerAdapter {
private List<ImageView> images;
public ImagePagerAdapter(List<ImageView> images) {
this.images = images;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = images.get(position);
container.addView(imageView);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(images.get(position));
}
#Override
public int getCount() {
return images.size();
}
#Override
public boolean isViewFromObject(View view, Object o) {
return view == o;
}
}
In your FullImageActivity1 class:
public class FullImageActivity1 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
// Loop through the ids to create a list of full screen image views
ImageAdapter1 imageAdapter1 = new ImageAdapter1(this);
List<ImageView> images = new ArrayList<ImageView>();
for (int i = 0; i < imageAdapter1.getCount(); i++) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(imageAdapter1.mThumbIds[i]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
images.add(imageView);
}
// Finally create the adapter
ImagePagerAdapter imagePagerAdapter = new ImagePagerAdapter(images);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
viewPager.setAdapter(imagePagerAdapter);
// Set the ViewPager to point to the selected image from the previous activity
// Selected image id
int position = getIntent().getExtras().getInt("id");
viewPager.setCurrentItem(position);
}
}
The general idea is to create an adapter that extends PagerAdapter to supply data to the ViewPager. For more information, you can visit Android Docs on PagerAdapter

Display blank item in grid view

On home scree of application, how to display the menu which is similar to android menu but no items needs to be displayed in specific cells. Considering grid of 3 x 3, five items only needs to be displayed at (Row, Col): [0,1], [1,0], [1,1], [1,2], [2,1].
We have tried GridView and set visibility to GONE (convertView.setVisibility(View.GONE);) for items which need not be displayed. Following this, item is not displayed in grid but when user browses through blank item using up and down keys or click directly on blank item, that icon is hihglighted and selected as if it is blank item in grid. We want as it is blank it should not repond to user events neither highlighted not selected.
Code for Grid View:
package org.XXX;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class XXXActivity extends Activity {
GridView MyGrid;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.maingrid);
MyGrid = (GridView)findViewById(R.id.MyGrid);
MyGrid.setAdapter(new ImageAdapter(this));
MyGrid.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Toast.makeText(arg0.getContext(), position + " selected", Toast.LENGTH_LONG).show();
switch(position) {
case 0:break;
case 1:
//Browse
Intent newIntent = new Intent(XXXActivity.this, YYYListItemIcons.class);
startActivity(newIntent);
break;
case 2:break;
case 3:
//Saved Searches
newIntent = new Intent(XXXActivity.this, ZZZListItemIcons.class);
startActivity(newIntent);
break;
case 4:
//Sign in
break;
case 5:
//Reminders
break;
case 6:break;
case 7:
//Sign up
break;
case 8:break;
}
}
});
//onSearchRequested(); //to open search by default
}
public class ImageAdapter extends BaseAdapter
{
Context MyContext;
public ImageAdapter(Context _MyContext)
{
MyContext = _MyContext;
}
#Override
public int getCount()
{
return 9;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater mInflater = LayoutInflater.from(MyContext);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.grid_item, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.grid_item_text);
holder.icon = (ImageView) convertView.findViewById(R.id.grid_item_image);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// Bind the data efficiently with the holder.
holder.text.setText(getTextId(position));
holder.icon.setImageBitmap(BitmapFactory.decodeResource(MyContext.getResources(), getIconId(position)));
if(getIconId(position) == R.drawable.nothing) {
convertView.setVisibility(View.GONE);
}
return convertView;
}
#Override
public Object getItem(int arg0) {
return arg0;
}
#Override
public long getItemId(int arg0) {
return arg0;
}
private int getIconId(int position) {
int iconImages[] = {
R.drawable.nothing,
R.drawable.browse,
R.drawable.nothing,
R.drawable.saved_searches,
R.drawable.sign_in,
R.drawable.reminders,
R.drawable.nothing,
R.drawable.sign_up,
R.drawable.nothing
};
return iconImages[position];
}
private int getTextId(int position) {
int iconNames[] = {
R.string.nothing,
R.string.browse,
R.string.nothing,
R.string.saved_searches,
R.string.sign_in,
R.string.reminders,
R.string.nothing,
R.string.sign_up,
R.string.nothing
};
return iconNames[position];
}
}
static class ViewHolder {
TextView text;
ImageView icon;
}
}
GridLayout:
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/MyGrid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="5dp"
android:verticalSpacing="5dp"
android:horizontalSpacing="20dp"
android:numColumns="auto_fit"
android:columnWidth="60dp"
android:stretchMode="columnWidth"
android:gravity="center">
</GridView>
PerItemIconLayout in Grid:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/GridItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal">
<ImageView android:id="#+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
<TextView android:id="#+id/grid_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:gravity="center_horizontal"
android:textColor="#FFFFFF">
</TextView>
</LinearLayout>
if(getIconId(position) == R.drawable.nothing) {
convertView.setVisibility(View.GONE);
}
replace the above lines by below and try....
if(getIconId(position) == R.drawable.nothing) {
convertView.setVisibility(View.GONE);
convertView.setClickable(false);
convertView.setEnabled(false);
}
try this code in getview().
Now that you have posted your code, I am not sure if you can technically remove but you can disable the "highlighted" click that you are talking about, this way, when a user clicks on the one of the icons, it will no longer highlight.
This can be done via XML or in your code:
https://stackoverflow.com/questions/2865683/android-disable-highlighting-in-gridview
Code: GridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
XML: android:listSelector="#00000000"
However, this will affect all the icons in your gridview.
Also take a look at this:
https://stackoverflow.com/questions/5514629/how-to-disable-item-click-for-particular-positions-in-grid-view-in-android

Categories

Resources