mwieczorek
mwieczorek

Reputation: 2252

Alert Dialog with Array Adapter is not capturing clicks

I'm creating (or trying to) an alert dialog that pops up when a User clicks to play a video, whereby if there is a saved resume spot, they are given the choice to resume, start over or cancel the action. Since I want to include the resume time in the dialog (e.g. "Resume Playback from 08:32")

The dialog shows up with the correct items, correctly formatted, but nothing happens when clicked. I'm wondering where I've gone wrong.

Java code:

final ArrayAdapter<String> itemVals = new ArrayAdapter<>(Docket.CURRENT_CONTEXT,
        R.layout.adapter_simple_list_item, R.id.list_item);

String optionStartBeginning = getString(R.string.media_play_no_resume);
itemVals.add(optionStartBeginning);

if (CastingBridge.getHasResume()) {
    String optionResume = "Resume playback from "+ StringFormatter.intMsTimeToString(CastingBridge.RESUME_TIME);
    itemVals.add(optionResume);
}

itemVals.add(getString(R.string.cancel));

AlertDialog.Builder builder = new AlertDialog.Builder(Docket.CURRENT_CONTEXT);
builder.setTitle(R.string.resume_offer_title)
        .setAdapter(itemVals, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Log.i(TAG, "onClick: Chosen idx is "+ which);
            }
        }).setCancelable(true);

AlertDialog alert = builder.create();
alert.show();

And the Layout file R.layout.adapter_simple_list_item:

<?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="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <com.rey.material.widget.TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="false"
        style="@style/Material.Drawable.Ripple.Touch.MatchView"
        app:rd_enable="true"
        android:id="@+id/list_item"
        android:text="@string/test_string"
        android:paddingTop="20dp"
        android:paddingBottom="20dp"
        android:paddingLeft="24dp"
        android:paddingRight="24dp"
        android:textSize="18sp" />

</LinearLayout>

The only thing that happens on clicking is this is printed in the logcat:

D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN

Any glaring mistakes that I'm not aware of? Thanks in advance for any answers.

Upvotes: 0

Views: 203

Answers (2)

MathankumarK
MathankumarK

Reputation: 2905

Editted:

This code is working

Java code:

final ArrayAdapter<String> itemVals = new ArrayAdapter<String>(_A,
                    R.layout.check, R.id.list_item);

            String optionStartBeginning = "No resaume";
            itemVals.add(optionStartBeginning);

        /*    if (CastingBridge.getHasResume()) {
                String optionResume = "Resume playback from Here";
                itemVals.add(optionResume);
            }*/

            itemVals.add("Cancel");

            AlertDialog.Builder builder = new AlertDialog.Builder(_A);
            builder.setTitle("Simple")
                    .setAdapter(itemVals, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Log.i("", "onClick: Chosen idx is "+ which);
                        }
                    }).setCancelable(true);

            AlertDialog alert = builder.create();
            alert.show();

Layout File :

<?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="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/list_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:paddingBottom="20dp"
        android:paddingLeft="24dp"
        android:paddingRight="24dp"
        android:paddingTop="20dp"
        android:text="Text"
        android:textSize="18sp" />

</LinearLayout>

I hvae checked with this above code it's working

Upvotes: 1

Lino
Lino

Reputation: 6160

I would try to remove the focusable attribute

android:focusable="false"

or eventually set it to "true"

Upvotes: 0

Related Questions