0

I have created an alert dialog with a list of apps (in text). So when I click on any of them, that particular app launches. I did this using AlertDialog fragment and using this code:

apps = new String[]{"App1","App2","App3"};
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Choose the app");
        builder.setItems(apps, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int which) {
                   // The 'which' argument contains the index position
                   // of the selected item

                      //Code to launch the apps
               }
        });
        return builder.create();

Is it possible to display the app icons along with the app names? If yes, what changes should I make to the code?

How do I get the app icons of installed apps on the phone?

SoulRayder
  • 5,072
  • 6
  • 47
  • 93
  • For that you have to make one `Custom Dialog` and inflated(Contains your `ListView`) one view to that `Dialog` then you fetch all the apps info using `RetrievePackages` to one `ArrayList` and Bind to your `ListView` with `Custom Row` – M D Apr 14 '14 at 06:36

3 Answers3

1

1.Create custom layout for dialog like you want then

Layout custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp" />

    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#FFF" 
        android:layout_toRightOf="@+id/image"/>


</RelativeLayout>

2.Make adapter with your text , icon(i.e items object )and your custom layout

 ArrayAdapter<String> adapter  = new ArrayAdapter<String> (this, R.layout.custom_dialog.xml);

3.set this adapter to your dialog like this

  builder.setAdapter(adapter  , new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {

           }
    });

Update:

Best option is to use listview in activity with android:theme="@android:style/Theme.Dialog" so that activity will look like dialog(as u want list of apps in dialog only) and also it will be easy to handle the data in listview compared to dialog with adapter.This will fulfill all your requirement in easy and smart way.

Shakeeb Ayaz
  • 6,200
  • 6
  • 45
  • 64
0
  • call your app like this

     Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.package.address");
     startActivity(LaunchIntent);
    

i got reference from this

  • add your icon dynamically like this

    see this answer

Community
  • 1
  • 1
MilapTank
  • 9,988
  • 7
  • 38
  • 53
0

I'd create a new xml layout, then inflate it using:

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflate.inflate(R.layout.your_file, null,false);

And then on AlertDialog

builder.setView(view)

I'd add a list view to layout and then handle it like every other listview. Use view.findViewById to get references.

How get list of apps? http://javatechig.com/android/how-to-get-list-of-installed-apps-in-android

RobertM
  • 287
  • 1
  • 6