How To Create A Listview With 2 Columns Showing All Installed Android Apps And The Permissions Along With It?
Solution 1:
Firstly you need load apps list and permission for every apps. I do it inside Activity.onCreate to simplify example. The better way to do it inside AsyncTask
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final PackageManager packageManager = getPackageManager();
final List<Pair<String, List<String>>> appsWithPermission = new ArrayList<>();
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
for (ResolveInfo info : apps) {
final ApplicationInfo applicationInfo = info.activityInfo.applicationInfo;
final String appName = (String) applicationInfo.loadLabel(packageManager);
final List<String> permissions = new ArrayList<>();
if (appName != null) {
try {
final PackageInfo packageInfo = packageManager.getPackageInfo(applicationInfo.packageName, PackageManager.GET_PERMISSIONS);
final String[] requestedPermissions = packageInfo.requestedPermissions;
if (requestedPermissions != null) {
permissions.addAll(Arrays.asList(requestedPermissions));
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
appsWithPermission.add(new Pair<>(appName, permissions));
}
final ListView listView = (ListView) findViewById(R.id.list_view);
final AppsAdapter appsAdapter = new AppsAdapter(this, appsWithPermission);
listView.setAdapter(appsAdapter);
}
}
Secondly you need xml layout list_item.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="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/list_item_appname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textAppearance="@android:style/TextAppearance.Medium" />
<TextView
android:id="@+id/list_item_apppermissions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
and finally AppsAdapter.java
public class AppsAdapter extends BaseAdapter {
private final Context mContext;
private List<Pair<String, List<String>>> mAppsWithPermission;
public AppsAdapter(Context context, List<Pair<String, List<String>>> appsWithPermission) {
mContext = context;
mAppsWithPermission = appsWithPermission;
}
static class ViewHolder {
public TextView appName;
public TextView appPermissions;
}
@Override
public int getCount() {
return mAppsWithPermission.size();
}
@Override
public Object getItem(int position) {
return mAppsWithPermission.get(position);
}
@Override
public long getItemId(int position) {
return mAppsWithPermission.get(position).hashCode();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
holder = new ViewHolder();
holder.appName = (TextView) convertView.findViewById(R.id.list_item_appname);
holder.appPermissions = (TextView) convertView.findViewById(R.id.list_item_apppermissions);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Pair<String, List<String>> item = mAppsWithPermission.get(position);
holder.appName.setText(item.first);
holder.appPermissions.setText(item.second.toString());
return convertView;
}
}
Solution 2:
I see you've already figured out how to get a list of all installed applications and their respective permissions, which is good. In order to make a ListView with multiple columns, though, you'll need to implement a ListViewAdapter.
A really great resource for figuring out how to do this can be found here.
If you're confused about anything on that link, or something doesn't work, let me know.
Code:
PopAnalysis.java
public class PopAnalysis extends Activity {
public static final String FIRST_COLUMN = "First";
public static final String SECOND_COLUMN = "Second";
ListView appList;
private TextSwitcher mSwitcher;
TextView myText;
private ArrayList results = new ArrayList();
private ArrayList<HashMap<String, String>> list;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popupanalysis);
appList = (ListView)findViewById(R.id.listViewApp);
HashMap<String,String> t1 = new HashMap<String, String>();
t1.put(FIRST_COLUMN, "App 1");
t1.put(SECOND_COLUMN, "Permission 1");
list.add(t1);
ListViewAdapter adapter = new ListViewAdapter(this, list);
listView.setAdapter(adapter);
}
}
ListViewAdapter.java
public class ListViewAdapter extends BaseAdapter {
public static final String FIRST_COLUMN = "First";
public static final String SECOND_COLUMN = "Second";
public ArrayList<HashMap<String, String>> list;
Activity activity;
TextView txtFirst;
TextView txtSecond;
public ListViewAdapter(Activity activity,ArrayList<HashMap<String, String>> list){
super();
this.activity=activity;
this.list=list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=activity.getLayoutInflater();
if(convertView == null){
convertView=inflater.inflate(R.layout.column_row_layout, null);
txtFirst=(TextView) convertView.findViewById(R.id.application);
txtSecond=(TextView) convertView.findViewById(R.id.permissions);
}
HashMap<String, String> map=list.get(position);
txtFirst.setText(map.get(FIRST_COLUMN));
txtSecond.setText(map.get(SECOND_COLUMN));
return convertView;
}
}
column_row_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView android:id="@+id/application"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:textStyle="bold" />
<TextView android:id="@+id/permissions"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"/>
</LinearLayout>
In PopAnalysis.java I added only one entry to simply show how it's done. Certainly you shouldn't have any issues implementing the data you want into it.
Solution 3:
Instead of 2 column listview , you can also try ExpandableListView. Clicking on the item with expand a detail view just below to it. detail view will contain the permissions of that app.
BTW, there is no way in SDK's listview to have multiple column. What you can do is to have a pseudo-multicolumn list view like using multiple textview(or other kind of view) in the same row. In that case you will need to make a custom adapter for your list view and a custom row layout file.
If you really want to have something multi-column, then either 3rd party libraries or TableLayout
is your only option.
Solution 4:
to make two column listview you have to implement custom listview. please read this link
in program_list.xml instead of imageview take textview it will resolve your problem.
Post a Comment for "How To Create A Listview With 2 Columns Showing All Installed Android Apps And The Permissions Along With It?"