How to launch app when clicked on icon in Android? - 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);

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);
………………
}

Android Images in a gridView in wrong order

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

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 :)

How can i put images in a listView?

I have a ListView but I can´t put images on it.
The error is in Activity_listView at holder.image.setImageDrawable(datos[position].getImage());
The error message is
The method setImageDrawable(Drawable) in the type ImageView is not applicable
for the arguments (ImageView)
Any idea for solve this?
Activity (Activity_listView):
package com.simarro.asteroids;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class Activity_listView extends ActionBarActivity {
private ListView lstOpciones;
ImageView asteroid = (ImageView)findViewById(R.id.img_asteroid);
private puntuacion[] datos = new puntuacion[] {
new puntuacion(asteroid,"Pepito Dominguez", "15489"),
new puntuacion(asteroid,"Pedro Martínez", "16598"),
new puntuacion(asteroid,"Paco Perez", "16332"),
new puntuacion(asteroid,"Rosana Fernandez", "18792"),
new puntuacion(asteroid,"Paco Jones", "960") };
class AdaptadorTitulares extends ArrayAdapter<puntuacion> {
Activity context;
AdaptadorTitulares(Activity context) {
super(context, R.layout.puntuacion, datos);
this.context = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
View item = convertView;
ViewHolder holder;
if (item == null) {
LayoutInflater inflater = context.getLayoutInflater();
item = inflater.inflate(R.layout.puntuacion, null);
holder = new ViewHolder();
holder.image=(ImageView)item.findViewById(R.id.img_asteroid);
holder.jugador = (TextView) item.findViewById(R.id.LblJugador);
holder.puntuacion = (TextView) item.findViewById(R.id.LblPuntuacion);
item.setTag(holder);
} else {
holder = (ViewHolder) item.getTag();
}
holder.image.setImageDrawable(datos[position].getImage());
holder.jugador.setText(datos[position].getJugador());
holder.puntuacion.setText(datos[position].getPuntuacion());
return (item);
}
}
static class ViewHolder {
ImageView image;
TextView jugador;
TextView puntuacion;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);
AdaptadorTitulares adaptador = new AdaptadorTitulares(this);
lstOpciones = (ListView) findViewById(R.id.LstOpciones);
//¡FUUUUUUUUU-SIÓN!
lstOpciones.setAdapter(adaptador);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_list_view, 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);
}
}
The xml of content for layout with listView (puntuacion.xml):
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="3"
android:orientation="horizontal" >
<ImageView
android:id="#+id/img_asteroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/asteroid" />
<TextView
android:id="#+id/LblJugador"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0"
android:textSize="30px"
android:textStyle="bold" />
<TextView
android:id="#+id/LblPuntuacion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30px"
android:textStyle="normal" />
</GridLayout>
The layout where is the listView (listView.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView android:id="#+id/LstOpciones"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
The class for get values (Puntuacion)
package com.simarro.asteroids;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;
public class puntuacion
{
private ImageView image;
private String jugador;
private String puntuacion;
public puntuacion(ImageView img, String tit, String sub){
image=img;
puntuacion = sub;
jugador = tit;
}
public ImageView getImage(){
return image;
}
public String getJugador(){
return jugador;
}
public String getPuntuacion(){
return puntuacion;
}
}
Instead of returning ImageView in getImage(), return a Drawable and change it to
public Drawable getImage() {
return image.getDrawable();
}
getDrawable() Docs
As the error says, the method setImageDrawable() takes a Drawable type but you are passing it an ImageView

GridView items are not selectable in Fragment

I have a Fragment in an Activity and I'm trying to display a GridView` from a custom adapter.I'm able to display the GridView but unable to select any item of the gridview.
Can any one help me in identifying the issue.
Please find the code I have used :
//MainActivity
package com.example.frag;
import java.util.ArrayList;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.os.Build;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
GridView optionsGridView;
ArrayList<InfoItems> items = new ArrayList<InfoItems>();
OptionsAdapter adapter;
String items2[] = {"1","2","3","4"};
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
// implentation of display of options in GridView
optionsGridView = (GridView) rootView.findViewById(R.id.optionsGrid);
/*items.add(new InfoItems("Passport"));
items.add(new InfoItems("Aadhar"));
items.add(new InfoItems("Pan"));
items.add(new InfoItems("DL"));*/
adapter = new OptionsAdapter(this.getActivity(), items2);
optionsGridView.setAdapter(adapter);
return rootView;
}
}
}
Custom Adapter
package com.example.frag;
import java.util.ArrayList;
import dalvik.bytecode.OpcodeInfo;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class OptionsAdapter extends BaseAdapter{
private Context context;
private ArrayList<InfoItems> infoItems;
String items[];
public OptionsAdapter(Context context,ArrayList<InfoItems> items) {
// TODO Auto-generated constructor stub
this.context = context;
this.infoItems = items;
}
public OptionsAdapter(Context contsxt,String txt[]) {
// TODO Auto-generated constructor stub
this.context = contsxt;
this.items = txt;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return items.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return infoItems.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.info_options, null);
}
TextView option = (TextView) convertView.findViewById(R.id.optionsItem);
option.setText(items[position]);
return convertView;
}
}
fragment xml -- layout which will be displayed in the fragment
<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.frag.MainActivity$PlaceholderFragment" >
<GridView android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/optionsGrid"
android:horizontalSpacing="5dp"
android:verticalSpacing="5dp"
android:numColumns="2"
android:gravity="center"
android:layout_marginTop="10dp"/>
</RelativeLayout>
item xml -- list item of the custom adapter
<?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:background="#000"
android:clickable="true">
<TextView android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:gravity="center"
android:textColor="#f000ff"
android:textSize="20sp"
android:textStyle="bold"
android:id="#+id/optionsItem"
android:text="passport"
android:clickable="true"
android:layout_gravity="center"/>
</LinearLayout>
Squonk is correct, you need to call optionsGridView.setAdapter(new OnItemClickListener....). You could also implement OnItemClickListener in your fragment and call optionsGridView.setAdapter(this).
Inside your PlaceholderFragment put this like below :
optionsGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//do your stuff!
}
});

Categories

Resources