Last active
December 18, 2015 22:58
-
-
Save chrisjenx/5857717 to your computer and use it in GitHub Desktop.
EditTextDialogFragment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bizzby.ui.fragments.dialogs; | |
import android.app.AlertDialog; | |
import android.app.Dialog; | |
import android.content.Context; | |
import android.content.DialogInterface; | |
import android.os.Bundle; | |
import android.support.v4.app.FragmentManager; | |
import android.text.Editable; | |
import android.text.Selection; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewTreeObserver; | |
import android.view.inputmethod.InputMethodManager; | |
import android.widget.EditText; | |
import com.bizzby.R; | |
/** | |
* Created with Intellij with Android, BIZZBY product. | |
* See licencing for usage of this code. | |
* <p/> | |
* User: chris | |
* Date: 21/02/2013 | |
* Time: 16:42 | |
*/ | |
public class EditTextDialogFragment extends BaseDialogFragment implements Dialog.OnClickListener | |
{ | |
private static final String TAG = EditTextDialogFragment.class.getSimpleName(); | |
private static final String EXTRA_EDIT_TEXT_VALUE = "extra_edit_text_value"; | |
public static final EditTextDialogFragment newInstance(final int titleRes, final String text) | |
{ | |
final EditTextDialogFragment fragment = new EditTextDialogFragment(); | |
Bundle b = new Bundle(2); | |
createArgs(b, titleRes, null); | |
if (text != null) | |
b.putString(EXTRA_EDIT_TEXT_VALUE, text); | |
fragment.setArguments(b); | |
return fragment; | |
} | |
private String mText; | |
private EditText mTextEdit; | |
private boolean mCanceled = false; | |
@Override | |
public void onCreate(final Bundle savedInstanceState) | |
{ | |
super.onCreate(savedInstanceState); | |
mText = getArguments().getString(EXTRA_EDIT_TEXT_VALUE); | |
} | |
@Override | |
public Dialog onCreateDialog(final Bundle savedInstanceState) | |
{ | |
final AlertDialog.Builder b = new AlertDialog.Builder(getActivity()); | |
b.setTitle(mTitle); | |
View v = LayoutInflater.from(getActivity()).inflate(R.layout.frag_diag_edit_text, null, false); | |
mTextEdit = (EditText) v.findViewById(R.id.frag_diag_edit_text); | |
b.setView(v); | |
b.setPositiveButton(android.R.string.ok, this); | |
b.setNegativeButton(android.R.string.cancel, this); | |
return b.create(); | |
} | |
@Override | |
public void onStart() | |
{ | |
super.onStart(); | |
mTextEdit.setText(mText); | |
//Try and show the keyboard after we show. | |
if (mTextEdit.getViewTreeObserver().isAlive()) | |
mTextEdit.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() | |
{ | |
@Override | |
public boolean onPreDraw() | |
{ | |
if (mTextEdit.getViewTreeObserver().isAlive()) | |
mTextEdit.getViewTreeObserver().removeOnPreDrawListener(this); | |
if (getActivity() != null) | |
{ | |
// final InputMethodManager keyboard = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); | |
// keyboard.showSoftInput(mTextEdit, 0); | |
mTextEdit.requestFocus(); | |
final Editable editable = mTextEdit.getText(); | |
Selection.setSelection(editable, mTextEdit.length()); | |
} | |
return true; | |
} | |
}); | |
} | |
@Override | |
public void onResume() | |
{ | |
super.onResume(); | |
} | |
@Override | |
public void onStop() | |
{ | |
super.onStop(); | |
mText = mTextEdit.getText().toString(); | |
final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService( | |
Context.INPUT_METHOD_SERVICE); | |
final View view = getActivity().getCurrentFocus(); | |
if (view != null) | |
imm.hideSoftInputFromWindow(view.getWindowToken(), 0); | |
} | |
public void show(final FragmentManager fragmentManager) | |
{ | |
if (fragmentManager != null) | |
{ | |
dismiss(fragmentManager, TAG); | |
super.show(fragmentManager, TAG); | |
} | |
} | |
@Override | |
public void onClick(final DialogInterface dialog, final int which) | |
{ | |
switch (which){ | |
case AlertDialog.BUTTON_NEGATIVE: | |
onCancel(dialog); | |
break; | |
case AlertDialog.BUTTON_POSITIVE: | |
onDismiss(dialog); | |
break; | |
} | |
} | |
@Override | |
public void onCancel(final DialogInterface dialog) | |
{ | |
mCanceled = true; | |
super.onCancel(dialog); | |
} | |
@Override | |
public void onDismiss(final DialogInterface dialog) | |
{ | |
if (!mCanceled && getTargetFragment() instanceof EditTextFragmentCallbacks) | |
((EditTextFragmentCallbacks) getTargetFragment()).onEditTextFinished(mTextEdit.getText().toString()); | |
super.onDismiss(dialog); | |
} | |
public static interface EditTextFragmentCallbacks | |
{ | |
public void onEditTextFinished(String text); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content"> | |
<EditText | |
android:id="@+id/frag_diag_edit_text" | |
style="@style/EditText.DescriptionDiag" | |
android:layout_margin="@dimen/margin_medium" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:minHeight="96dp" | |
android:inputType="textMultiLine|textCapSentences|textAutoComplete" | |
android:imeOptions="actionDone" | |
android:layout_gravity="top" | |
android:focusableInTouchMode="true" | |
android:gravity="bottom"> | |
<requestFocus/> | |
</EditText> | |
</FrameLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment