I have this gridView with imageView items, but i can't make it clickable. I'm kind of new in android and i don't understand how to make theses images clickable. I tried some answers that I have read in other posts but it doesn't work.
Help...
Another question! If i want to pass a hard coded String to another activity, but this string changes every time I click on an imageView item. Can I declare a static string and then I say string="word" and then call it from the other activity with Activity.string? Would i have the value in the other activity?
Please find below my xml files, gridViewAdapter and activity!
services_list_activity.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=".ServicesListActivity">
<GridView
android:id="#+id/gridView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="4dp"
android:columnWidth="80dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" />
</RelativeLayout>
gris_row.xml
<LinearLayout 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:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="#+id/item_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:src="#drawable/atm">
</ImageView>
<TextView
android:id="#+id/item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textSize="15sp" >
</TextView>
</LinearLayout>
CustomGridViewAdapter.java
public class CustomGridViewAdapter extends ArrayAdapter<Item> {
Context context;
int layoutResourceId;
ArrayList<Item> data = new ArrayList<Item>();
public CustomGridViewAdapter(Context context, int layoutResourceId,
ArrayList<Item> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
RecordHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new RecordHolder();
holder.txtTitle = (TextView) row.findViewById(R.id.item_text);
holder.imageItem = (ImageView) row.findViewById(R.id.item_image);
row.setTag(holder);
} else {
holder = (RecordHolder) row.getTag();
}
Item item = data.get(position);
holder.txtTitle.setText(item.getTitle());
holder.imageItem.setImageBitmap(item.getImage());
return row;
}
static class RecordHolder {
TextView txtTitle;
ImageView imageItem;
}
}
ServicesListActivity.java
public class ServicesListActivity extends Activity implements AdapterView.OnItemClickListener{
protected static final Toast text = null;
//String to pass to another activity
public static String types;
GridView gridView;
ArrayList<Item> gridArray = new ArrayList<Item>();
CustomGridViewAdapter customGridAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.services_list_activity);
//set grid view item
Bitmap atmIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.atm);
Bitmap bankIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.bank);
Bitmap barIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.bar);
Bitmap cafeIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.cafe);
gridArray.add(new Item(atmIcon,"ATMs"));
gridArray.add(new Item(bankIcon,"Banks"));
gridArray.add(new Item(barIcon,"Bars"));
gridArray.add(new Item(cafeIcon,"Cafés"));
gridView = (GridView) findViewById(R.id.gridView1);
customGridAdapter = new CustomGridViewAdapter(this, R.layout.grid_row, gridArray);
gridView.setAdapter(customGridAdapter);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//intent
Intent data = new Intent(getApplicationContext(), ListActivity.class);
switch (position) {
case 0:
//String to send to another activity
types= "restaurant";
startActivity(data);
break;
case 1:
startActivity(data);
break;
case 2:
startActivity(data);
break;
case 3:
startActivity(data);
break;
default:
break;
}
}
}
Add this to your ImageView:
android:clickable="true"
With feedback:
android:background="?attr/selectableItemBackground"
Then set an OnClickListener in your Activity:
yourImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
Related
I am creating food shop app in which click on menu button on home page it should redirect to grid view ,
i have created image grid layout xml and image grid class , but not able to map it with button on home page
this is my main activity
public class TimmyRestaurantActivity extends Activity {
Button go_to_menu,go_to_order_list,findstore,info;
//Button custinfo;
String user_name;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent myintent = getIntent();
Bundle extras = myintent.getExtras();
user_name = extras.getString("cust_name");
Toast.makeText(TimmyRestaurantActivity.this, "Welcome " + user_name ,Toast.LENGTH_LONG ).show();
// initialise form widget
go_to_menu=(Button)findViewById(R.id.Go_To_Menu);
go_to_menu.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i=new Intent(TimmyRestaurantActivity.this,
ImageAdapter.class);
startActivity(i);
}
}
this is my gridlayout
<?xml version="1.0" encoding="utf-8"?>
<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="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center"
android:stretchMode="columnWidth" >
</GridView>
this is adaptor class for my grid
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.img15, R.drawable.img15,
R.drawable.img15, R.drawable.img15
};
// 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 = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
return imageView;
}
}
and on menuscreen.java i am trying call my gridadapater
public class MenuScreen extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
GridView gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
}
on click button , this error comes
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.restaurant/com.restaurant.MenuScreen}:
java.lang.RuntimeException: Your content must have a ExpandableListView whose id attribute is 'android.R.id.list'
How can i properly redirect from home page button to grid view page , please suggest
Add a placeholder in your main laout(R.layout.main)
<FrameLayout
android:layout_height="match_parent"
android:id="#+id/place_men"
android:layout_width="match_parent">
</FrameLayout>
and chnage the code like
go_to_menu.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//starting new fragment on button click
MenuScreen menuScreen =new MenuScreen();
getSupportFragmentManager().beginTransaction().replace(R.id.place_men, menuScreen).commit();
}
});
Also MenuScreen extends android.support.v4.app.Fragment
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0f0f0">
<GridView
android:id="#+id/gridView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:columnWidth="100dp"
android:drawSelectorOnTop="true"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp"
android:focusable="true"
android:clickable="true"/>
</RelativeLayout>
grid_item_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#drawable/grid_color_selector"
android:orientation="vertical"
android:padding="5dp">
<ImageView
android:id="#+id/image"
android:layout_width="100dp"
android:layout_height="100dp" />
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:textSize="12sp" />
</LinearLayout>
GridViewAdapter.class
public class GridViewAdapter extends ArrayAdapter {
private Context context;
private int layoutResourceId;
private ArrayList data = new ArrayList();
public GridViewAdapter(Context context, int layoutResourceId, ArrayList data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.imageTitle = (TextView) row.findViewById(R.id.text);
holder.image = (ImageView) row.findViewById(R.id.image);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
ImageItem item = data.get(position);
holder.imageTitle.setText(item.getTitle());
holder.image.setImageBitmap(item.getImage());
return row;
}
static class ViewHolder {
TextView imageTitle;
ImageView image;
}
}
ImageItem.class pojo class
public class ImageItem {
private Bitmap image;
private String title;
public ImageItem(Bitmap image, String title) {
super();
this.image = image;
this.title = title;
}
public Bitmap getImage() {
return image;
}
public void setImage(Bitmap image) {
this.image = image;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
MainActivity.class
public class MainActivity extends ActionBarActivity {
private GridView gridView;
private GridViewAdapter gridAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridView);
gridAdapter = new GridViewAdapter(this, R.layout.grid_item_layout, getData());
gridView.setAdapter(gridAdapter);
}
// Prepare some dummy data for gridview
private ArrayList<ImageItem> getData() {
final ArrayList<ImageItem> imageItems = new ArrayList<>();
TypedArray imgs = getResources().obtainTypedArray(R.array.image_ids);
for (int i = 0; i < imgs.length(); i++) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), imgs.getResourceId(i, -1));
imageItems.add(new ImageItem(bitmap, "Image#" + i));
}
return imageItems;
}
}
Please follow this code implementation for Gridview
And in your button click event
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(YourActivity.this, MainActivity.class);
startActivity(intent);
}
});
Follow this link for working example
Image GridView
I have a custom list view that fill with an image and text view in each row, and it shown correct :[1
but my problem is that I want to show the text that in a Toast in setOnItemClickListener. here is my codes:
public class Showing {
public int icon;
public String title;
public Showing(){
super();
}
public Showing(int icon, String title) {
super();
this.icon = icon;
this.title = title;
}}
and my adapter is:
public class ShowingAdapter extends ArrayAdapter<Showing> {
Context context;
int layoutResourceId;
Showing data[] = null;
public ShowingAdapter(Context context, int layoutResourceId, Showing[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ShowingHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ShowingHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
row.setTag(holder);
}
else
{
holder = (ShowingHolder)row.getTag();
}
Showing showing = data[position];
holder.txtTitle.setText(showing.title);
holder.imgIcon.setImageResource(showing.icon);
return row;
}
static class ShowingHolder
{
ImageView imgIcon;
TextView txtTitle;
}}
And here are my layouts for Header:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/txtHeader"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:textStyle="bold"
android:textSize="22dp"
android:textColor="#FFFFFF"
android:padding="10dp"
android:text="Action Photos"
android:background="#336699" />
</LinearLayout>
And each row
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
<ImageView android:id="#+id/imgIcon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_gravity="left" />
<TextView android:id="#+id/txtTitle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:textStyle="bold"
android:textSize="22dp"
android:textColor="#000000"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
</LinearLayout>
So here is My Main code :
public class MainActivity extends Activity {
private ListView listView1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Showing action_data[] = new Showing[]
{
new Showing(R.drawable.cut, "Cut"),
new Showing(R.drawable.delete, "Delete"),
new Showing(R.drawable.favorites, "Favorites"),
new Showing(R.drawable.redo, "Redo"),
new Showing(R.drawable.up, "Up")
};
ShowingAdapter adapter = new ShowingAdapter(this,R.layout.listview_item_row, action_data);
listView1 = (ListView) findViewById(R.id.listView1);
View header = (View) getLayoutInflater().inflate(R.layout.listview_header_row, null);
listView1.addHeaderView(header);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
***// I want to make a toast here...
// but the 'VIEW' is LinearLayout and
// I want to retrive the 'TEXT VIEW.getText'***
}
});
}}
You don't need to act on the layout. You can simply retrieve the object associated with your row. So inside the onItemClick() you can get it in this way:
Showing showing = (Showing)adapter.getitem(position)
now, showing contains all the information you need. So you can get the text:
showing.title
or if you make title private (as it supposed to be), you would use a getter:
showing.getTitle();
This question already has answers here:
Custom Adapter getView() method is not called
(6 answers)
Closed 7 years ago.
the image is not showing , the app is launching but no image is dispalying , can you help me to fix that ? I guess problem with gridimage. plesae not that the place holder of picasso is not showing in the list view.
public class GridViewAdapter extends ArrayAdapter<GridImages> {
private Context mContext;
private int layoutResourceId;
private ArrayList<GridImages> mGridImages = new ArrayList<GridImages>();
public GridViewAdapter(Context mContext, int layoutResourceId, ArrayList<GridImages> mGridImages) {
super(mContext, layoutResourceId, mGridImages);
this.layoutResourceId = layoutResourceId;
this.mContext = mContext;
this.mGridImages = mGridImages;
}
/**
* Updates grid data and refresh grid items.
* #param mGridData
*/
public void setGridData(ArrayList<GridImages> mGridImages) {
this.mGridImages = mGridImages;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null){
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, parent, false);
holder.imageView = (ImageView)convertView.findViewById(R.id.grid_item_image);
holder.titleTextView = (TextView)convertView.findViewById(R.id.grid_item_title);
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
}
String url = "http://www.justedhak.comlu.com/images/uploaded_images.jpg";
Picasso.with(mContext)
.load(url)
.placeholder(R.drawable.ic_launcher)
.error(R.drawable.ic_launcher)
.noFade().resize(150,150)
.centerCrop()
.into(holder.imageView);
return convertView;
}
static class ViewHolder {
TextView titleTextView;
ImageView imageView;
}
}
this is main activity.
public class MainActivity extends ActionBarActivity {
GridViewAdapter mGridAdapter;
private ArrayList<GridImages> mGridImages;
private ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list1);
mGridImages = new ArrayList<>();
mGridAdapter = new GridViewAdapter(this, R.layout.grid_item_layout, mGridImages);
listView.setAdapter(mGridAdapter);
}
activity main
<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.justedhak.MainActivity" >
<ListView
android:id="#+id/list1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:columnWidth="100dp"
android:drawSelectorOnTop="true"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp"
android:focusable="true"
android:clickable="true"/>
</RelativeLayout>
grid item layout
<ImageView
android:id="#+id/grid_item_image"
android:layout_width="500dp"
android:layout_height="500dp"
/>
<TextView
android:id="#+id/grid_item_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:maxLines="2"
android:layout_below="#+id/grid_item_image"
android:ellipsize="marquee"
android:textSize="12sp" />
</RelativeLayout>
Check your layout once.
try to put the specific height for image view and see.
If you didn't succeed add you layout code.
I have been searching everywhere for similar solutions but none seem to work for me.
On my first screen I will have a gridview which consists of 1 column and 3 rows vertically. Each row will have an imageview and a partially transparent textview on top of the text view. The imageviews span the screen width fine. My only problem is that the 3 image views do not span the full screen vertically, there is space inbetween rows, although I have tried many methods to fix this. I will post up my xml files and code:
row_layout.xml - this is the gridview items
<RelativeLayout 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/item_image"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:layout_width="fill_parent"
android:layout_height="180dp"
android:src="#drawable/season1">
</ImageView>
<TextView
android:id="#+id/item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/item_image"
android:layout_alignTop="#+id/item_image"
android:layout_alignRight="#+id/item_image"
android:layout_alignBottom="#+id/item_image"
android:gravity="bottom|left"
android:textSize="30sp"
android:textAlignment="center"
android:lines="1"
android:layout_marginTop="145dp"
android:background="#99000000">
</TextView>
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="fill_parent"
tools:context=".MainActivity" >
<GridView
android:id="#+id/gridView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:padding="0dp"
android:gravity="center"
android:numColumns="1"
android:fitsSystemWindows="true"
android:stretchMode="columnWidth" >
</GridView>
My custom adapter:
public class CustomGridViewAdapter extends ArrayAdapter<Item> {
Context context;
int layoutResourceId;
ArrayList<Item> data = new ArrayList<Item>();
public CustomGridViewAdapter(Context context, int layoutResourceId, ArrayList<Item> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
RecordHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
row.setMinimumHeight(MainActivity.height1/3);
holder = new RecordHolder();
holder.txtTitle = (TextView) row.findViewById(R.id.item_text);
holder.imageItem = (ImageView) row.findViewById(R.id.item_image);
row.setTag(holder);
} else {
holder = (RecordHolder) row.getTag();
}
Item item = data.get(position);
holder.txtTitle.setText(item.getTitle());
holder.imageItem.setImageBitmap(item.getImage());// my image
return row;
}
static class RecordHolder {
TextView txtTitle;
ImageView imageItem;
}
}
MainActivity:
public class MainActivity extends ActionBarActivity {
GridView gridView;
ArrayList<Item> gridArray = new ArrayList<Item>();
CustomGridViewAdapter customGridAdapter;
public static int height1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
ActionBar ab = getActionBar();
ab.setDisplayShowTitleEnabled(false);
ab.setDisplayShowHomeEnabled(false);
Resources res = this.getResources();
Bitmap bMap = BitmapFactory.decodeResource(res, R.drawable.actionbar);
BitmapDrawable actionBarBackground = new BitmapDrawable(res, bMap);
ab.setBackgroundDrawable(actionBarBackground);
//set grid view item
Bitmap season1Icon = BitmapFactory.decodeResource(res, R.drawable.season1);
Bitmap season2Icon = BitmapFactory.decodeResource(res, R.drawable.season2);
Bitmap season3Icon = BitmapFactory.decodeResource(res, R.drawable.season3);
gridArray.add(new Item(season1Icon,"Season 1"));
gridArray.add(new Item(season2Icon,"Season 2"));
gridArray.add(new Item(season3Icon,"Season 3"));
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
height1 = metrics.heightPixels;
gridView = (GridView) findViewById(R.id.gridView1);
customGridAdapter = new CustomGridViewAdapter(this, R.layout.row_grid, gridArray);
gridView.setAdapter(customGridAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Intent a = new Intent(MainActivity.this, House.class);
a.putExtra("Position", position);
startActivity(a);
}
});
}
}
Any help will be greatly appreciated, I've spent hours trying to fix this!
Dynamically set the layout params of your imageview, once you know the height of the screen.
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
//row.setMinimumHeight(MainActivity.height1/3); //don't need this, since wrap content will make the row height match your image view's
holder = new RecordHolder();
holder.txtTitle = (TextView) row.findViewById(R.id.item_text);
holder.imageItem = (ImageView) row.findViewById(R.id.item_image);
holder.imageItem.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, MainActivity.height1/3);
row.setTag(holder);
} else {
holder = (RecordHolder) row.getTag();
}
i have implemented a list view its working fine but when i click on the list it gives me raw data that getItemAtPosition(position) returns me.
following is the code that i am using
public class FirstTab extends Activity{
private TabHostProvider tabProvider;
private TabView tabView;
private static String[] country1;
private ListView speakerList;
final Handler mHandler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Context context = getApplicationContext();
tabProvider = new TabbarView(this);
tabView = tabProvider.getTabHost("main");
tabView.setCurrentView(R.layout.firsttab);
setContentView(tabView.render(0));
String temp[] = new String[]{"furqan","furqan1","furqan2"};
Weather weather_data[] = new Weather[temp.length];
for(int i = 0;i<temp.length;i++)
{
weather_data[i] = new Weather(R.drawable.ic_launcher, temp[i]);
}
WeatherAdapter adapter = new WeatherAdapter(this,R.layout.listview_item_row, weather_data);
speakerList = (ListView)findViewById(R.id.speakerList);
View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
speakerList.addHeaderView(header);
speakerList.setAdapter(adapter);
final ViewFlipper viewflipper = (ViewFlipper) findViewById(R.id.viewflipper);
speakerList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(final AdapterView<?> arg0, View arg1, final int arg2,long arg3) {
// TODO Auto-generated method stub
String item = speakerList.getItemAtPosition(arg2).toString();
Log.d("the value is", item);
/*View item = (View) (speakerList.getItemAtPosition(arg2));
String item = arg0.getItemAtPosition(arg2).toString();
Log.d("the value is", item);*/
//Toast.makeText(getApplicationContext(), arg2, 1).show();
}
});
}
}
The above is the main activity.Following is the adapter
public class weatheradapter {
public static class WeatherAdapter extends ArrayAdapter<Weather>{
Context context;
int layoutResourceId;
Weather data[] = null;
public WeatherAdapter(Context context, int layoutResourceId, Weather[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
static class WeatherHolder
{
ImageView imgIcon;
TextView txtTitle;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
WeatherHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new WeatherHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
row.setTag(holder);
}
else
{
holder = (WeatherHolder)row.getTag();
}
Weather weather = data[position];
holder.txtTitle.setText(weather.title);
holder.imgIcon.setImageResource(weather.icon);
return row;
}
}
}
this is the xml file
<?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="fill_parent"
android:layout_gravity="center" android:gravity="center" android:background="#00FFcc">
<ViewFlipper android:id="#+id/viewflipper" android:layout_width="fill_parent" android:layout_height="fill_parent" >
<ListView android:id="#+id/speakerList" android:layout_width="fill_parent" android:layout_height="fill_parent" />
<ListView android:id="#+id/categoryList" android:layout_width="fill_parent" android:layout_height="wrap_content" />
</ViewFlipper>
</LinearLayout>
The following is the layout for the list view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
<ImageView android:id="#+id/imgIcon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
<TextView android:id="#+id/txtTitle"
android:text="#+id/txtTitle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:textStyle="bold"
android:textSize="22dp"
android:textColor="#000000"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
</LinearLayout>
need help thanks
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Cursor c = (Cursor) parent.getAdapter().getItem(position);
c.getString(c.getColumnIndex("col_name")));
}