I have tried adding a grid view to a custom dialog. When displaying
the dialog it crashes.
But when tried displaying the grid view in normal activity it was
working.( without dialog )
I took the examples from developer android website.Took grid view and
tried to integrate in custom dialog.
I used two xml main.xml & category.xml.
Here is my code:
package com.android.test;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class test extends Activity {
/** Called when the activity is first created. */
public final static int CATEGORY_ID=0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.categories);
button.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
showDialog(CATEGORY_ID);
}
});
}
protected Dialog onCreateDialog(int id) {
Dialog dialog;
switch(id) {
case CATEGORY_ID:
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = this;
LayoutInflater inflater = (LayoutInflater)
mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.categorydialog,
(ViewGroup)
findViewById(R.id.layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView)
layout.findViewById(R.id.image);
image.setImageResource(R.drawable.find);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(test.this, "" + position,
Toast.LENGTH_SHORT).show();
}
});
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
dialog = builder.create();
break;
default:
dialog = null;
}
return dialog;
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the
Adapter
public View getView(int position, View convertView, ViewGroup
parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled,
initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new
GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.info, R.drawable.find,
R.drawable.info, R.drawable.find,
R.drawable.info, R.drawable.find,
R.drawable.info, R.drawable.find,
R.drawable.info, R.drawable.find,
R.drawable.info, R.drawable.find,
R.drawable.info, R.drawable.find,
R.drawable.info, R.drawable.find,
R.drawable.info, R.drawable.find,
R.drawable.info, R.drawable.find,
R.drawable.info, R.drawable.find
};
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="3dip"
android:orientation="vertical">
<Button android:id="#+id/categories"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Choose categories"
/>
</LinearLayout>
categorydialog.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
android:id="#+id/layout_root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
>
<ImageView android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
/>
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF"
/>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</LinearLayout>
Could anyone please help me out here.
Change this line:
GridView gridview = (GridView) findViewById(R.id.gridview);
to something like this:
GridView gridview = (GridView) layout.findViewById(R.id.gridview);
Related
I am new to Android and I'm trying to add setOnItemClickListener to my gridview and i am getting the following error:
setOnItemClickListener in AdapterView cannot by applied to MainActivity
for this code line: grid.setOnItemClickListener(this);
What am I doing wrong? Could you help me please?
Here is my code
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
GridView grid;
TextView tvTest;
TextView tvTest2;
Random rnd = new Random();
int color;
ImageView imageView;
ArrayList<Integer> colorInts = new ArrayList<Integer>();
ArrayList<Integer> itemsList = new ArrayList<Integer>();
Integer[] items;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
grid = (GridView) findViewById(R.id.grid);
tvTest =(TextView) findViewById(R.id.tvTest);
tvTest2 =(TextView) findViewById(R.id.tvTest2);
adjustGridView();
itemsList.add(R.drawable.lemon);
itemsList.add(R.drawable.lemon);
itemsList.add(R.drawable.lemon);
itemsList.add(R.drawable.lemon);
itemsList.add(R.drawable.lemon);
itemsList.add(R.drawable.lemon);
// ArrayList into Array
items = new Integer[ itemsList.size() ];
itemsList.toArray( items );
grid.setAdapter(new CustomGridAdapter(this, items));
grid.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "Clicked postion is " + arg2,
Toast.LENGTH_SHORT).show();
tvTest.setText("Number of color: "+ colorInts);
tvTest2.setText("Number of color: "+ colorInts.get(arg2));
}
// Here is your custom Adapter
public class CustomGridAdapter extends BaseAdapter {
private AppCompatActivity mContext;
// Keep all Images in array
public Integer[] mThumbIds;
// Constructor
public CustomGridAdapter(MainActivity mainActivity, Integer[] items) {
this.mContext = mainActivity;
this.mThumbIds = items;
}
#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 = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
imageView.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
colorInts.add(color);
return imageView;
}
}
private void adjustGridView() {
grid.setNumColumns(3); //(GridView.AUTO_FIT);
grid.setColumnWidth(80);
grid.setVerticalSpacing(5);
grid.setHorizontalSpacing(5);
grid.setStretchMode(GridView.NO_STRETCH);
}
}
and here is the activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="#+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.testrk.basiclayout001.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/myColor02"
android:layout_weight="2"
>
<TextView
android:id="#+id/tvTest"
android:background="#color/myColor04"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/myColor01"
android:focusable="false"
android:layout_weight="1"
>
<GridView
android:id="#+id/grid"
android:focusable="true"
android:clickable="true"
android:background="#color/myColor03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
</GridView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/myColor02"
android:layout_weight="2"
>
<TextView
android:id="#+id/tvTest2"
android:background="#color/myColor04"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
and cell.xml:
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/imgv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
</ImageView>
The setOnItemClickListener method does not take in the context as an argument, it takes in an OnItemClickListener object. What you need to do is pass in a new OnItemClickListener in the method call and then override the OnItemClick method.
Example:
grid.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
//code to be executed on click
}
I hope this answers your question.
I've downloaded the Navigation Drawer example from the developers guide, I try to implement a GridView into an Activity of one of the options of the Drawer, but in execution time the GridView doesn't appears, the IDE doesn't show me any errors and in an example apart I can do it de GridView successfuly, How do I correct this problem?
My code is here:
In the MainActivity I use the same like the example with
Fragment fragment;
FragmentManager fragmentManager;
fragment = new inicioFragment(R.layout.inicio);
fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
InicioActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;
public class InicioActivity extends Activity {
TextView tv1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inicio);
tv1=(TextView)findViewById(R.id.tv1);
GridView gv = (GridView)findViewById(R.id.gridView1);
gv.setAdapter(new ImageAdapter(this.getApplicationContext()));
gv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id){
String g=Integer.toString(position);
tv1.setText(g);
}
});
}
}
ImageAdapter.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 ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(13, 13, 13, 13);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
private Integer[] mThumbIds = {
R.drawable.image_2, R.drawable.image_3,
R.drawable.image_4, R.drawable.image_5,
R.drawable.image_6, R.drawable.image_7,
R.drawable.image_0, R.drawable.image_1,
R.drawable.image_2, R.drawable.image_3,
R.drawable.image_4, R.drawable.image_5,
R.drawable.image_6, R.drawable.image_7,
R.drawable.image_0, R.drawable.image_1,
R.drawable.image_2, R.drawable.image_3,
R.drawable.image_4, R.drawable.image_5,
R.drawable.image_6, R.drawable.image_7
};
}
inicio.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="230dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout1"
android:orientation="vertical" >
<TextView
android:id="#+id/tv2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Aqui van las notificaciones push de facebook" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="230dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="horizontal" >
<GridView
android:id="#+id/gridView1"
android:layout_width="156dp"
android:layout_height="192dp"
android:numColumns="2" >
</GridView>
<TextView
android:id="#+id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:gravity="center_horizontal|center_vertical"
android:text="InformaciĆ³n" />
</LinearLayout>
</RelativeLayout>
Thanks for your collaboration!!
Try to change InicioActivity into a fragment.
And check this question. I think you are facing the same problem.
The following worked for me. In Android Studio:
1) Create a new Navigation Drawer Activity from the list of activity templates.
2) Note that several layout files will be created for you, including a content_main.xml. Replace the XML in the content_main.xml file with your <GridView> XML.
3) Create the relevant XML files or Java classes for the GridView, eg. the ImageAdapter class and an XML file for the GridView item layout.
Can someone help me with the code-I am having image slider containing multiple images and if I select any particular image and click on button that selected image should open on another page/activity.
I tried a lot but unable to display selected image on another page/activity.
So, please help me.
Thanks in advance.
**Main.java**
package com.example.imagesliderdemo;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
#SuppressWarnings("deprecation")
public class Main extends Activity {
private Gallery topsgallery;
private ImageView imgView;
int GalItemBg;
private Integer[] Imgid = {
R.drawable.a_1, R.drawable.a_2, R.drawable.a_3, R.drawable.a_4, R.drawable.a_5 };
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
imgView = (ImageView)findViewById(R.id.ImageViewTops);
imgView.setImageResource(Imgid[0]);
Button btnNextScreen = (Button) findViewById(R.id.btnNextScreen);
btnNextScreen.setOnClickListener(new View.OnClickListener() {
private int position;
public void onClick(View v) {
// TODO Auto-generated method stub
//Intent nextScreen = new Intent(Main.this, OpenImage.class);
Intent nextScreen=new Intent(getApplicationContext(),OpenImage.class);
nextScreen.putExtra("image",R.drawable.ic_launcher);
startActivity(nextScreen);
}
});
topsgallery = (Gallery) findViewById(R.id.gallery1);
topsgallery.setAdapter(new AddImgAdp (this));
topsgallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(#SuppressWarnings("rawtypes") AdapterView parent, View v, int position, long id) {
imgView = (ImageView)findViewById(R.id.ImageViewTops);
}
});
}
public class AddImgAdp extends BaseAdapter {
int GalItemBg;
private Context cont;
public AddImgAdp(Context c) {
cont = c;
TypedArray typArray = obtainStyledAttributes(R.styleable.Gallery1);
GalItemBg = typArray.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
typArray.recycle();
}
public int getCount() {
return Imgid.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(cont);
imageView.setImageResource(Imgid[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(80, 70));
imageView.setBackgroundResource(GalItemBg);
return imageView;
}
}
}
**Other activity file OpenImage.java**
package com.example.imagesliderdemo;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class OpenImage extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
ImageView image = (ImageView) findViewById(R.id.imageview);
int image_link = getIntent().getIntExtra("image_url", R.drawable.ic_launcher);
image.setImageResource(image_link);
Button btnClose = (Button) findViewById(R.id.btnClose);
btnClose.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Closing SecondScreen Activity
finish();
}
});
}
}
**activity_main.xml**
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:overScrollMode="always"
android:isScrollContainer="true"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarStyle="outsideInset"
android:scrollbars="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:scrollbars="vertical">
<LinearLayout
android:id="#+id/LinearLayoutTops"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:orientation="vertical">
<TextView
android:gravity="center"
android:text="#string/tops"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="#+id/LinearLayoutTopsGallery"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<Gallery
android:id="#+id/gallery1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:spacing="10dp" />
<ImageView
android:id="#+id/ImageViewTops"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/imageslide"/>
</LinearLayout>
</LinearLayout>
<Button android:id="#+id/btnNextScreen"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Send to Next Screen"
android:layout_marginTop="15dip"/>
</LinearLayout>
</ScrollView>
**Another Activity screen2.xml**
<?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" >
<Button
android:id="#+id/btnClose"
android:layout_width="151dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="15dip"
android:text="Close" />
<ImageView
android:id="#+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
You can use fragment,
http://developer.android.com/guide/components/fragments.html
this page have a example, maybe can help you.
Create a global variable to get the position of Slider(Let suppose that variable be pos).Now in getView of your slider adapter code like this
public View getView(final int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(cont);
pos=position;
imageView.setImageResource(Imgid[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(80, 70));
imageView.setBackgroundResource(GalItemBg);
return imageView;
}
Now on button click code like this:
btnNextScreen.setOnClickListener(new View.OnClickListener() {
private int position;
public void onClick(View v) {
// TODO Auto-generated method stub
//Intent nextScreen = new Intent(Main.this, OpenImage.class);
Intent nextScreen=new Intent(getApplicationContext(),OpenImage.class);
//Sending data to another Activity
nextScreen.putExtra("id",Imgid[pos]);
Log.e("n", Imgid[pos]);
startActivity(nextScreen);
}
});
Just pass the selected image id value in the Gallery on click listener.
topsgallery.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long arg3)
{
// TODO Auto-generated method stub
Intent fullImage = new Intent(YourActivity.this,FullImageView.class)
fullImage.putExtra("image_url",R.drawable.image);
startActivity(fullImage);
}
});
consider the following steps :
#1 Create a new activity say for ex : FullImageView with the following view and declare that in manifest.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/android" />
</ImageView>
</LinearLayout>
3 : Receive the intent in onCreate of FullImageView activity
ImageView image = (ImageView) findViewById(R.id.image);
int image_link = getIntent().getIntExtra("image_url", R.drawable.default);
4 : Set the URL to your ImageView.
imageView.setImageResource(image_link);
Please feel free if you face any issues regarding this.
You can put the intent inside the adapter... and then do something like this (Note that you need to pass the context to the adapter):
public View getView(final int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(cont);
imageView.setImageResource(Imgid[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(80, 70));
imageView.setBackgroundResource(GalItemBg);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent nextScreen=new Intent(context,OpenImage.class);
nextScreen.putExtra("image",ImgId[pos]);
startActivity(nextScreen);
}
});
return imageView;
}
I am working on a application which require image and text group together in horizontal scroll bar.
I have tried few things but i am unable to get this, can anyone guide me with this guys.
Here is what i a have done,
main.xml
<?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">
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="horizontal" >
<GridView
android:layout_height="wrap_content"
android:id="#+id/gridView1"
android:layout_width="wrap_content"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:scrollbars="horizontal"
android:verticalSpacing="10dp">
</GridView>
</HorizontalScrollView>
</LinearLayout>
gridview_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/border"
android:padding="5dp">
<ImageView
android:layout_height="64dp"
android:id="#+id/imageView1"
android:layout_width="64dp"
android:src="#drawable/icon"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</ImageView>
<TextView
android:text="TextView"
android:layout_height="wrap_content"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_below="#+id/imageView1"
android:layout_marginTop="2dp"
android:layout_centerHorizontal="true"
android:textSize="18sp"></TextView>
</RelativeLayout>
GridviewExampleActivity.java
package com.paresh.gridviewexample;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
public class GridViewExampleActivity extends Activity {
/** Called when the activity is first created. */
private GridviewAdapter mAdapter;
private ArrayList<String> listCountry;
private ArrayList<Integer> listFlag;
private GridView gridView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
prepareList();
// prepared arraylist and passed it to the Adapter class
mAdapter = new GridviewAdapter(this,listCountry, listFlag);
// Set custom adapter to gridview
gridView = (GridView) findViewById(R.id.gridView1);
gridView.setAdapter(mAdapter);
// Implement On Item click listener
gridView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
Toast.makeText(GridViewExampleActivity.this, mAdapter.getItem(position), Toast.LENGTH_SHORT).show();
}
});
}
public void prepareList()
{
listCountry = new ArrayList<String>();
listCountry.add("india");
listCountry.add("Brazil");
listCountry.add("Canada");
listCountry.add("China");
listCountry.add("France");
listCountry.add("Germany");
listCountry.add("Iran");
listCountry.add("Italy");
listCountry.add("Japan");
listCountry.add("Korea");
listCountry.add("Mexico");
listCountry.add("Netherlands");
listCountry.add("Portugal");
listCountry.add("Russia");
listCountry.add("Saudi Arabia");
listCountry.add("Spain");
listCountry.add("Turkey");
listCountry.add("United Kingdom");
listCountry.add("United States");
listFlag = new ArrayList<Integer>();
listFlag.add(R.drawable.india);
listFlag.add(R.drawable.brazil);
listFlag.add(R.drawable.canada);
listFlag.add(R.drawable.china);
listFlag.add(R.drawable.france);
listFlag.add(R.drawable.germany);
listFlag.add(R.drawable.iran);
listFlag.add(R.drawable.italy);
listFlag.add(R.drawable.japan);
listFlag.add(R.drawable.korea);
listFlag.add(R.drawable.mexico);
listFlag.add(R.drawable.netherlands);
listFlag.add(R.drawable.portugal);
listFlag.add(R.drawable.russia);
listFlag.add(R.drawable.saudi_arabia);
listFlag.add(R.drawable.spain);
listFlag.add(R.drawable.turkey);
listFlag.add(R.drawable.united_kingdom);
listFlag.add(R.drawable.united_states);
}
}
GridviewAdapter.java
package com.paresh.gridviewexample;
import java.util.ArrayList;
import android.app.Activity;
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 GridviewAdapter extends BaseAdapter
{
private ArrayList<String> listCountry;
private ArrayList<Integer> listFlag;
private Activity activity;
public GridviewAdapter(Activity activity,ArrayList<String> listCountry, ArrayList<Integer> listFlag) {
super();
this.listCountry = listCountry;
this.listFlag = listFlag;
this.activity = activity;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return listCountry.size();
}
#Override
public String getItem(int position) {
// TODO Auto-generated method stub
return listCountry.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public static class ViewHolder
{
public ImageView imgViewFlag;
public TextView txtViewTitle;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
if(convertView==null)
{
view = new ViewHolder();
convertView = inflator.inflate(R.layout.gridview_row, null);
view.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);
view.imgViewFlag = (ImageView) convertView.findViewById(R.id.imageView1);
convertView.setTag(view);
}
else
{
view = (ViewHolder) convertView.getTag();
}
view.txtViewTitle.setText(listCountry.get(position));
view.imgViewFlag.setImageResource(listFlag.get(position));
return convertView;
}
}
My ouput should be
------------ ---------- ------------ ----
Image Image Image Image Image Image Image Image Image Image Image
Text Text Text Text Text Text Text Text Text Text Text
------------ ---------- ------------ --
Please Help me guys
Hi there just have a look at CustomArrayAdapter used for ListViews and Gridviews especially point 10 at this Tutoiral there you get to know how to customize your GridView/ListView (handled same way)
This might also help
If you have problems with the hotrizontal way, try this project: https://github.com/jess-anders/two-way-gridview
hi guys i need to place a single image in multiple places.
suppose i need to place one image in 5 places in a layout but using only one <ImageView> id
<ImageView
android:id="#+id/img"
android:layout_width="340dip"
android:layout_height="240dip"
android:layout_marginBottom="60dip" />
how to acheive this please help.
Use This code for which uses Grid View :
Main.java:
package sra.gri.vie;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(Main.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.icon, R.drawable.icon,
R.drawable.icon, R.drawable.icon,
R.drawable.icon,
};
}
}
main.xml: // the layout
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
Lat me know if this post is of any help :)
you can do like this
RelativeLayout rl = (RelativeLayout) findViewById(R.id.my_relative_layout);
ImageView iv = new ImageView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 50; params.topMargin = 60;
rl.addView(iv, params);
and then add this r1 in your main view then again change margin, height and other param and add again like this you do n no of times
Do you try include tag for it?
img.xml
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_home"/>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<include layout="#layout/img"/>
<include layout="#layout/img"/>
<include layout="#layout/img"/>
<include layout="#layout/img"/>
</LinearLayout