0

I try to developp a little android application and show a dialog alert with just a edittext to fill custom as the next link

Creating a custom layout I have this code but it doesn't work

public class TraceDialogFragment extends DialogFragment {


TraceDialogListener mListener;

public interface TraceDialogListener {
    public void onDialogPositiveClick(DialogFragment dialog);
    public void onDialogNegativeClick(DialogFragment dialog);
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();


    builder.setView(inflater.inflate(R.layout.activity_trace, null))
            .setIcon(R.drawable.ic_action_locate)
            .setTitle(R.string.trace_title)
            // Add action buttons
            .setPositiveButton(R.string.btn_ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    mListener.onDialogPositiveClick(TraceDialogFragment.this);
                }
            })
            .setNegativeButton(R.string.btn_cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    mListener.onDialogNegativeClick(TraceDialogFragment.this);
                }
            });
    return builder.create();
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);

    try{
        mListener = (TraceDialogListener) context;
    }catch (ClassCastException e){
        throw new ClassCastException(context.toString() + " must implement TraceDialogListener");
    }
}

}

but the code run in a loop on

LayoutInflater inflater = getActivity().getLayoutInflater();

I don't understand

In my class main activity

I have this code

class MainActivity extends AppCompatActivity implements TraceDialogFragment.TraceDialogListener

DialogFragment traceDialog = new TraceDialogFragment();
traceDialog.show(getSupportFragmentManager(),"TraceDialogFragment");

Here my styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

and my logcat is the next

11-07 11:10:50.015 23570-23570/E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                            Process: , PID: 23570
                                                                                            java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
                                                                                                at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:359)

Thanks for your help,

lg0173
  • 208
  • 2
  • 8
  • 20

2 Answers2

2

Try this one for AlertDialogue with editText

  • Just call this method in your Activity or Fragment in Oncreate or OnCreateView

     public void inflateDialogue() {
        LayoutInflater layoutInflater=SelectUsers.this.getLayoutInflater();
        final View view = layoutInflater.inflate(R.layout.dialog_group_name, null);
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Group_name");
    
        alertDialog.setCancelable(false);
    
        edt_groupName = (EditText) view.findViewById(R.id.edt_groupName);
    
        alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, getResources().getString(R.string.ok), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                str_getTextFrom=edt_groupName.getText().toString();
                //here we have to call Database firebase
    
                dialog.dismiss();
    
            }
        });
        alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, getResources().getString(R.string.cancel), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        alertDialog.setView(view);
        alertDialog.show();
    }
    

  • create dialog_group_name in your xml

     <?xml version="1.0" encoding="utf-8"?>
     <LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:id="@+id/edt_groupName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:paddingLeft="@dimen/_10dp"
        android:paddingRight="@dimen/_10dp"
        android:textSize="@dimen/textSize"
        android:textColor="@color/colorPrimaryDark"
        android:textColorHint="@color/colorText"
        android:hint="@string/enter_groupName"
        android:inputType="textCapWords"
        android:singleLine="true"
        android:layout_marginTop="@dimen/_10dp"
        android:background="@null"
        android:maxLines="1"
        android:scrollbars="vertical" />
          </LinearLayout>
    
Vishal Yadav
  • 1,020
  • 4
  • 15
  • 30
0

I resolve my problem with change my namespace

import android.support.v7.app.AlertDialog; to import android.app.AlertDialog;

I don't understand everything .... but it works

lg0173
  • 208
  • 2
  • 8
  • 20