masmic masmic - 5 months ago 72
Android Question

Gridview app launcher inside fragment

As the title says, I need to create a custom app launcher inside a fragment. I've based my code in several answers I've readed around here, anyway, I don't get to show any app on it.

This is the adapter class:

public class ShortcutAdapter extends BaseAdapter {

private Context mContext;
private List<ResolveInfo> appsList;
PackageManager mPackageManager;


public ShortcutAdapter(List<ResolveInfo> appsList, Context mContext) {
this.appsList = appsList;
this.mContext = mContext;
mPackageManager = mContext.getPackageManager();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
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;
}

ResolveInfo resolveInfo = appsList.get(position);
imageView.setImageDrawable(resolveInfo.loadIcon(mPackageManager));

return imageView;
}
}


And this is the fragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.settings_layout, container, false);

GridView mGrid = (GridView) view.findViewById(R.id.gridview);

//Load list on adapter
mGrid.setAdapter(new ShortcutAdapter(loadApps(), getContext()));

//Listener gridview items
mGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ResolveInfo checkedResolveInfo = (ResolveInfo) parent.getItemAtPosition(position);
ActivityInfo clickedActivityInfo = checkedResolveInfo.activityInfo;

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setClassName(
clickedActivityInfo.applicationInfo.packageName,
clickedActivityInfo.name);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
startActivity(intent);
}
});
return view;
}


/*Load apps*/
private List<ResolveInfo> loadApps() {
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

List<ResolveInfo> appsList = getActivity().getPackageManager().queryIntentActivities(mainIntent, 0);
return appsList;
}


The code is the same that you can find in several tutorials about this around internet, but in my case, I don't achieve to show any app in the GridView.

EDIT- xml gridview layout

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="120dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center_horizontal"
android:layout_marginTop="60dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"/>
</LinearLayout>

Answer

Any of below will cause no app icon to display.

  1. ShortcutAdapter#getCount() return 0;

    @Override public int getCount() { return 0; }

  2. (GridView) view.findViewById(R.id.gridview); GridView have a narrow width. You can add a background and check it.

  3. Fragment add in a ViewGroup which has a wrong narrow width. You can add a background and check it.

Comments