com.loloof64.android.chess_position_manager.file_explorer.NewPositionDialogFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.loloof64.android.chess_position_manager.file_explorer.NewPositionDialogFragment.java

Source

/**
 * This file is part of ChessPositionManagerAndroid.
    
 ChessPositionManagerAndroid is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
    
 ChessPositionManagerAndroid is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
    
 You should have received a copy of the GNU General Public License
 along with ChessPositionManagerAndroid.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.loloof64.android.chess_position_manager.file_explorer;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;

import com.loloof64.android.chess_position_manager.R;

/**
 * Created by laurent-bernabe on 24/12/14.
 */
public class NewPositionDialogFragment extends DialogFragment {

    public interface NewPositionDialogListener {
        void onNewPositionDialogPositiveClick(String positionName);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        final View view = inflater.inflate(R.layout.new_element_dialog_layout, null);

        builder.setTitle(R.string.title_new_position).setView(view)
                .setPositiveButton(R.string.action_validate, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        EditText nameEditText = (EditText) view.findViewById(R.id.new_directory_name);
                        listener.onNewPositionDialogPositiveClick(nameEditText.getText().toString());
                    }
                }).setNegativeButton(R.string.action_cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        NewPositionDialogFragment.this.getDialog().cancel();
                    }
                });
        Dialog dialog = builder.create();
        // Very important for the edit view.
        dialog.getWindow().clearFlags(
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        return dialog;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // Verify that the host activity implements the callback interface
        try {
            // Instantiate the dialogListener so we can send events to the host
            listener = (NewPositionDialogListener) activity;
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(activity.toString() + " must implement NewPositionDialogListener");
        }
    }

    private NewPositionDialogListener listener;

}