I'm making a launcher for android with a feature where the user selects an alphabet, then apps starting with that alphabet will be displayed. I'm having a class that stores name, icon, etc of apps and can successfully display it using Gridiew. But I can't seem to figure out how to display only the selected apps that starts with the selected alphabet.
MainActivity/Dashboard.java
public class Dashboard extends Activity {
DrawerAdapter drawerAdapterObject;
GridView drawerGrid;
class Pac {
Drawable icon;
String name;
String label;
}
Pac[] pacs;
PackageManager pm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
drawerGrid = (GridView) findViewById(R.id.content);
pm = getPackageManager();
set_pacs();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_PACKAGE_ADDED);
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
filter.addDataScheme("package");
registerReceiver(new PacReciever(), filter);
}
public void set_pacs() {
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mainIntent.hasExtra("facebok");
List<ResolveInfo> pacsList = pm.queryIntentActivities(mainIntent, 0);
pacs = new Pac[pacsList.size()];
for(int i = 0; i < pacsList.size(); i++) {
pacs[i] = new Pac();
pacs[i].icon = pacsList.get(i).loadIcon(pm);
pacs[i].name = pacsList.get(i).activityInfo.packageName;
pacs[i].label = pacsList.get(i).loadLabel(pm).toString();
}
new SortApps().exchage_sort(pacs);
drawerAdapterObject = new DrawerAdapter(this, pacs);
drawerGrid.setAdapter(drawerAdapterObject);
drawerGrid.setOnItemClickListener(new DrawerClickListner(this, pacs, pm));
}
public class PacReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
set_pacs();
}
}
public void test(){
Intent test = new Intent(this,MainActivity.class);
startActivity(test);
}
}
activity_dashboard.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_dashboard"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.zyconut.socio.Dashboard">
<SlidingDrawer
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:handle="@+id/handle"
android:content="@+id/content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<ImageView
android:id="@+id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/ic_launcher"/>
<GridView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimarytrans"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="50dp"
android:horizontalSpacing="50dp"/>
</SlidingDrawer>
</RelativeLayout>
DrawerAdapter.java
public class DrawerAdapter extends BaseAdapter {
Context mContext;
Dashboard.Pac[] pacsForAdapter;
public DrawerAdapter(Context c, Dashboard.Pac pacs[]) {
mContext = c;
pacsForAdapter = pacs;
}
@Override
public int getCount() {
return pacsForAdapter.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
static class ViewHolder {
TextView text;
ImageView icon;
}
@Override
public View getView(int pos, View convertView, ViewGroup viewGroup) {
ViewHolder viewHolder;
LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView == null) {
convertView = li.inflate(R.layout.drawer_item, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.icon_text);
viewHolder.icon = (ImageView) convertView.findViewById(R.id.icon_image);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.icon.setImageDrawable(pacsForAdapter[pos].icon);
viewHolder.text.setText(pacsForAdapter[pos].label);
return convertView;
}
}
DrawerClickListner.java
public class DrawerClickListner implements OnItemClickListener {
Context mContext;
Dashboard.Pac[] pacsForAdapter;
PackageManager pmForListner;
public DrawerClickListner(Context c, Dashboard.Pac[] pacs, PackageManager pm) {
mContext = c;
pacsForAdapter = pacs;
pmForListner = pm;
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
Intent launchIntent = pmForListner.getLaunchIntentForPackage(pacsForAdapter[pos].name);
mContext.startActivity(launchIntent);
}
}
SortApps.java
public class SortApps {
public void exchage_sort(Dashboard.Pac[] pacs){
int i,j;
Dashboard.Pac temp;
for(i = 0; i < pacs.length; i++) {
for(j = i + 1; j < pacs.length; j++) {
if(pacs[i].label.compareToIgnoreCase(pacs[j].label) > 0) {
temp = pacs[i];
pacs[i] = pacs[j];
pacs[j] = temp;
}
}
}
}
}
drawer_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<ImageView
android:id="@+id/icon_image"
android:layout_width="65dp"
android:layout_height="65dp"
android:padding="3dp"/>
<TextView
android:id="@+id/icon_text"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:maxLines="2"
android:gravity="center_horizontal"/>
</LinearLayout>
This is my first post, please do forgive me for any mistakes.
I just need someone to point me to how i can display apps starting with 'A' only. Rest I may be able to figure it out.
Thanks in advance
You can get the list of installed applications using the following snippet.
Then you can use this List to filter applications based on starting alphabet.