Morton
Morton

Reputation: 5760

How to get EditText value from this Dialogplus library?

I use this library dialogplus https://github.com/orhanobut/dialogplus to show custom view on the top.

Now I want to get EditText value, but I get an error null object.

I see they talk about how to get value:

For customview holder, there is a specific method to get your view back. Then you can use findViewById and access to the edittext or other inputs.

View view = dialogPlus.getHolderView();

But I don't know how to use this code...

My global

private Button searchButton;

Here is my code:

public void openSearchLayout() {
    DialogPlus dialog = DialogPlus.newDialog(getActivity())
            .setContentHolder(new ViewHolder(R.layout.search_layout))
            .setGravity(Gravity.TOP)
            .setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(DialogPlus dialog, View view) {

 presenterEdit = (EditText) view.findViewById(R.id.searchButton);

                    switch (view.getId()) {
                        case R.id.searchButton:

                            String testString = presenterEdit.getText().toString();

                            Toast.makeText(getActivity(), testString, Toast.LENGTH_SHORT).show();

                            break;
                    }
                }
            })
            .setExpanded(true, 900)  // This will enable the expand feature, (similar to android L share dialog)
            .create();
    dialog.show();
}

I try it like this, its no working:

@Override
                public void onClick(DialogPlus dialog, View view) {

                    view = dialog.getHolderView();
                    presenterEdit = (EditText) view.findViewById(R.id.searchButton);

                    switch (view.getId()) {
                        case R.id.searchButton:

                            String testString = presenterEdit.getText().toString();

                            Toast.makeText(getActivity(), testString, Toast.LENGTH_SHORT).show();

                            break;
                    }
                }

Here is my search_layout:

<?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">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingLeft="16dp"
    android:paddingTop="16dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/circlePresenter"
        android:textColor="@android:color/black"
        android:textSize="18dp" />

    <EditText
        android:id="@+id/presenterEdit"
        android:layout_width="match_parent"
        android:layout_height="38dp"
        android:layout_gravity="center"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="16dp"
        android:background="@drawable/search_edit"
        android:spinnerMode="dropdown" />

</LinearLayout>

    <Button
        android:id="@+id/searchButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="16dp"
        android:background="@drawable/corner_search"
        android:text="@string/integralDateSearch"
        android:textColor="@android:color/white"
        android:textSize="20dp" />

</LinearLayout>

Upvotes: 3

Views: 666

Answers (3)

Fawzy
Fawzy

Reputation: 51

You can use this library Click here by then you can do this

 CustomLayoutDialog dialog = new DialogPlusBuilder().buildCustomLayoutDialog(R.layout.search_layout);
                dialog.showNow(getChildFragmentManager(), "search_layout");
 SearchLayoutBinding=(SearchLayoutBinding) dialog.getCustomLayoutBinding();
 languageDialogBinding.presenterEdit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                          //TDOD implement here 
                    }
                }
            });

Upvotes: 1

Skizo-ozᴉʞS ツ
Skizo-ozᴉʞS ツ

Reputation: 20616

You should create the EditText and then assign it to your id that you've assigned in your xml something like this :

public void openSearchLayout() {
    DialogPlus dialog = DialogPlus.newDialog(getActivity())
            .setContentHolder(new ViewHolder(R.layout.search_layout))
            .setGravity(Gravity.TOP)
            .setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(DialogPlus dialog, View view) {
                //dialog.getHolderView().findViewById(...)
                EditText presenterEdit = (EditText)dialog.getHolderView().findViewById(R.id.presenterEdit);


                    switch (view.getId()) {
                        case R.id.searchButton:

                            String testString = presenterEdit.getText().toString();

                            Toast.makeText(getActivity(), testString, Toast.LENGTH_SHORT).show();

                            break;
                    }
                }
            })
            .setExpanded(true, 900)  // This will enable the expand feature, (similar to android L share dialog)
            .create();
    dialog.show();
}

Upvotes: 3

Nouman Ch
Nouman Ch

Reputation: 4121

try this

    View root = dialog.getHolderView();
    EditText presenterEdit  =  root.findViewById(R.id.presenterEdit);
    String text = presenterEdit.getText().toString();

hope this will work.

Upvotes: 2

Related Questions