Reputation: 2292
I am trying to show installed apps list in dialog , onclick of a button. The apps list should be in gridview.
My code is as follows
mapapp.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
loadApps();
final Dialog dialog = new Dialog(getApplicationContext());
LayoutInflater inflater = (LayoutInflater)
getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.categorydialog,
(ViewGroup) findViewById(R.id.layout_root));
dialog.setTitle("Title...");
GridView mGrid = (GridView) layout.findViewById(R.id.gridview);
mGrid.setAdapter(new AppsAdapter(MainActivity.this));
mGrid.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
}
});
}
});
the methods
private void loadApps() {
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mApps = getPackageManager().queryIntentActivities(mainIntent, 0);
}
adapter is as follows
public class AppsAdapter extends BaseAdapter {
private Context mContext;
public AppsAdapter(Context context) {
mContext = context;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i;
if (convertView == null) {
i = new ImageView(mContext);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new GridView.LayoutParams(50, 50));
} 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;
}
}
No error and nothing is shown. Please suggest me where I might be doing wrong.
Upvotes: 1
Views: 746
Reputation: 2292
I got the answer
for any one who need this
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
loadApps();
showDialog(CATEGORY_ID);
}
});
and the methods used are as follows
private void loadApps() {
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mAppsold=getPackageManager().getInstalledPackages(PackageManager.GET_PERMISSIONS);
mApps = getUserInstalledApps(this);
}
private boolean isSystemPackage(PackageInfo pkgInfo) {
return ((pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) ? true
: false;
}
protected Dialog onCreateDialog(int id) {
switch(id) {
case CATEGORY_ID:
AlertDialog.Builder builder;
Context mContext = this;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.categorydialog,(ViewGroup) findViewById(R.id.layout_root));
GridView gridview = (GridView)layout.findViewById(R.id.gridview);
gridview.setAdapter(new AppsAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView parent, View v,int position, long id) {
String now=mApps.get(position).getPackageName();
Intent mainIntent = getPackageManager().getLaunchIntentForPackage(now);
if(mainIntent!=null)
{
mainIntent = mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET)
.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
startActivity(mainIntent);
}
}
});
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
dialog = builder.create();
dialog.setTitle("Select the Map App" );
break;
default:
dialog = null;
}
return dialog;
}
public class AppsAdapter extends BaseAdapter {
private Context mContext;
LayoutInflater inflater;
public AppsAdapter(Context context) {
mContext = context;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.content, null);
convertView.setLayoutParams(new GridView.LayoutParams(100, 100));
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView )convertView.findViewById(R.id.image);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.icon.setAdjustViewBounds(true);
holder.icon.setScaleType(ImageView.ScaleType.CENTER_CROP);
holder.icon.setPadding(8, 8, 8, 8);
holder.title.setText(mApps.get(position).getAppName());
holder.icon.setImageDrawable(mApps.get(position).getAppIcon());
return convertView;
}
class ViewHolder {
TextView title;
ImageView icon;
}
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;
}
}
public boolean isPackageExisted(String targetPackage){
List<ApplicationInfo> packages;
PackageManager pm;
pm = getPackageManager();
packages = pm.getInstalledApplications(0);
for (ApplicationInfo packageInfo : packages) {
if(packageInfo.packageName.equals(targetPackage)) return true;
}
return false;
}
public ArrayList<ApplicationInfoStruct> getUserInstalledApps(Context context)
{
try
{
mApps = new ArrayList<ApplicationInfoStruct>();
packs = context.getPackageManager().getInstalledPackages(0);
for(int i = 0; i<packs.size();i++)
{
p1 = packs.get(i);
if (p1.versionName == null) {
continue ;
}
if((packs.get(i).applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) ==1)
{
}else
{
appInfoStruct= new ApplicationInfoStruct();
appInfoStruct.setAppName(p1.applicationInfo.loadLabel(context.getPackageManager()).toString().trim());
appInfoStruct.setPackageName(p1.packageName);
appInfoStruct.setActivityInfo(p1.activities);
appInfoStruct.setVersionName(p1.versionName.toString().trim());
appInfoStruct.setVersionCode(p1.versionCode);
appInfoStruct.setSourceDir(p1.applicationInfo.sourceDir);
//appInfoStruct.setDate(p1.lastUpdateTime);
appInfoStruct.setAppIcon(p1.applicationInfo.loadIcon(context.getPackageManager()));
mApps.add(appInfoStruct);
}
}
return mApps;
}catch(Exception e)
{
Log.e("Exception in getUserInstallApps()", e.getMessage());
return mApps;
}
}
Thats it. This generates a list of app view in gridview inside a dialog.
Upvotes: 1