SoulRayder
SoulRayder

Reputation: 5166

How to create an alert dialog with app icons next to the items(apps)

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?

Upvotes: 0

Views: 1620

Answers (3)

Shakeeb Ayaz
Shakeeb Ayaz

Reputation: 6096

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.

Upvotes: 1

RobertM
RobertM

Reputation: 297

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

Upvotes: 0

MilapTank
MilapTank

Reputation: 10076

  • 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

Upvotes: 0

Related Questions