In Android i want to make a TextView That when we click on that open dialog box with edittext.
TextView mClientName;
mClientName = (TextView) findViewById(R.id.EnterName);
mClientName.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View mView, MotionEvent mMotionEvent) {
getClientNameDialog();
return false;
}});
private void getClientNameDialog() {
View mView = View.inflate(Aura.this, R.layout.getclientname, null);
mSavedClientName = ((EditText) mView.findViewById(R.id.GetClientName));
final InputMethodManager mInputMethodManager = (InputMethodManager) Aura.this
.getSystemService(Context.INPUT_METHOD_SERVICE);
mInputMethodManager.restartInput(mView);
AlertDialog.Builder mBuilder = new AlertDialog.Builder(Aura.this);
mBuilder.setTitle(getString(R.string.enterclientname));
mBuilder.setPositiveButton(getString(R.string.save), new Dialog.OnClickListener() {
public void onClick(DialogInterface mDialogInterface, int mWhich) {
mGetClientNameString = SavedClientName.getText().toString().trim();
if (mGetClientNameString.length() > 0) {
mClientName.setText(mGetClientNameString);
mInputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
mDialogInterface.dismiss();
}
}
});
mBuilder.setView(mView);
mBuilder.show();
if (mInputMethodManager != null) {
mInputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}
Related
private void openInput(View v, String nickname) {
if (popInputNickName == null) {
View view = LayoutInflater.from(UserInfoActivity.this).inflate(R.layout.pop_nickname_modify, null);
popInputNickName = new InputPopWindow(view, LinearLayout.LayoutParams.MATCH_PARENT
, LinearLayout.LayoutParams.WRAP_CONTENT, true);
popInputNickName.setAnimationStyle(R.style.NewContentAnim);
popInputNickName.setBackgroundDrawable(new BitmapDrawable());
popInputNickName.setFocusable(true);
popInputNickName.setTouchable(true);
popInputNickName.setOutsideTouchable(true);
edit_nickname = (EditText) view.findViewById(R.id.edit_pop_nickname);
text_commit = (TextView) view.findViewById(R.id.text_pop_commit);
text_cancel = (TextView) view.findViewById(R.id.text_pop_cancel);
if (!TextUtils.isEmpty(nickname)) {
edit_nickname.setText(nickname);
}
text_commit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String nickName = edit_nickname.getText().toString().trim();
if (!TextUtils.isEmpty(nickName)) {
modifyNickName(nickName);
} else {
ToastUtil.Infotoast(UserInfoActivity.this, getString(R.string.null_nickname));
}
}
});
text_cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
popInputNickName.dismiss();
}
});
} else {
if (!TextUtils.isEmpty(nickname)) {
edit_nickname.setText(nickname);
}
}
popInputNickName.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
popInputNickName.showAtLocation(v, Gravity.BOTTOM, 0, 0);
edit_nickname.requestFocus();
InputMethodManager imm = (InputMethodManager) edit_nickname.getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
openInputTime = System.currentTimeMillis();
}
the popwindow show
When I enter to an activity(call it A) & shows a popwindow to modify user's nickname. When I finish A, the screen will be black for a while. But when i enter A and not open the popwindow, then no black screen appears, it completes as normal. So i wonder what caused this problem and how to fix it.
I am so sorry that the question is not clear. In the end I solved the problem.I used a custom-view that caused this problem.The edit_nickname in the PopWindow is a custom-view that extends EditText.
public class TfEditView extends EditText {
private OnFinishComposingListener mFinishComposingListener;
public TfEditView(Context context) {
super(context);
}
public TfEditView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TfEditView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setOnFinishComposingListener(OnFinishComposingListener listener) {
this.mFinishComposingListener = listener;
}
#Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
return new MyInputConnection(super.onCreateInputConnection(outAttrs), false);
}
public class MyInputConnection extends InputConnectionWrapper {
public MyInputConnection(InputConnection target, boolean mutable) {
super(target, mutable);
}
#Override
public boolean finishComposingText() {
boolean finishComposing = super.finishComposingText();
if (mFinishComposingListener != null) {
mFinishComposingListener.finishComposing();
}
return finishComposing;
}
}
public interface OnFinishComposingListener {
void finishComposing();
}
}
private long openInputTime = 0;
private final long canHideTime = 500;
private void openInput(View v, String nickname) {
if (popInputNickName == null) {
View view = LayoutInflater.from(UserInfoActivity.this).inflate(R.layout.pop_nickname_modify, null);
popInputNickName = new InputPopWindow(view, LinearLayout.LayoutParams.MATCH_PARENT
, LinearLayout.LayoutParams.WRAP_CONTENT, true);
popInputNickName.setAnimationStyle(R.style.NewContentAnim);
popInputNickName.setBackgroundDrawable(new BitmapDrawable());
popInputNickName.setFocusable(true);
popInputNickName.setTouchable(true);
popInputNickName.setOutsideTouchable(true);
edit_nickname = (TfEditView) view.findViewById(R.id.edit_pop_nickname);
text_commit = (TextView) view.findViewById(R.id.text_pop_commit);
text_cancel = (TextView) view.findViewById(R.id.text_pop_cancel);
if (!TextUtils.isEmpty(nickname)) {
edit_nickname.setText(nickname);
}
edit_nickname.setOnFinishComposingListener(new TfEditView.OnFinishComposingListener() {
#Override
public void finishComposing() {
if (popInputNickName != null && popInputNickName.isShowing() && openInputTime != 0 && (System.currentTimeMillis() - openInputTime > canHideTime)) {
popInputNickName.dismiss();
}
}
});
text_commit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String nickName = edit_nickname.getText().toString().trim();
if (!TextUtils.isEmpty(nickName)) {
modifyNickName(nickName);
} else {
ToastUtil.Infotoast(UserInfoActivity.this, getString(R.string.null_nickname));
}
}
});
text_cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
popInputNickName.dismiss();
}
});
} else {
if (!TextUtils.isEmpty(nickname)) {
edit_nickname.setText(nickname);
}
}
popInputNickName.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
popInputNickName.showAtLocation(v, Gravity.BOTTOM, 0, 0);
edit_nickname.requestFocus();
InputMethodManager imm = (InputMethodManager) edit_nickname.getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
openInputTime = System.currentTimeMillis();
}
In the code above ,the popindow can dismissed longer than 500ms from the popwindow begin to show.
I've developed a keyboard and now i need to add emojis to it , from other questions i've realized the best way is with popupwindow,
Here's what i've done:
case -102:
LayoutInflater layoutInflater
= (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.emoji_view, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
popupWindow.showAsDropDown(getWindow().getOwnerActivity().getCurrentFocus(),50, -30);
Unfortunatly this doesn't work , showAsDropDown needs a view as its first var , and if the keyboard is in another app i don't have a view to give him...
Is there a way to fix that ?
or am i going about it all wrong and there is a better way...
all help will be appreciated!
Given that you are using the official SoftKeyBoard implementation as a template for your keyboard:
//Cut some pieces of the code for clarity
case -102:
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.emoji_view, null);
PopupWindow popupWindow = new PopupWindow(popupView, MATCH_PARENT, MATCH_PARENT);
popupWindow.showAsDropDown(mInputView);
Use the view that you have inflated for your keyboard, in the offical SoftKeyBoard and the snippet above it is called mInputView.
hi i have done same things. I have made one custom keyboard in android.
EmoticonsPagerAdapter emojiAdapter;
/**
* Defining all components of emoticons keyboard
*/
private void enablePopUpView() {
final ViewPager pager = (ViewPager) popUpView
.findViewById(R.id.emoticons_pager);
pager.setOffscreenPageLimit(3);
final ArrayList<EmojiItem> paths = EmojiUtil.getInstance(acitiviy)
.getAllEmojis();
final ArrayList<EmojiItem>[] groups = new ArrayList[5];
for (EmojiItem emoji : paths) {
if (groups[emoji.emojiGroup] == null) {
groups[emoji.emojiGroup] = new ArrayList<EmojiItem>();
}
groups[emoji.emojiGroup].add(emoji);
}
final ArrayList<EmojiItem> history = new ArrayList<EmojiItem>();
ArrayList<Integer> historyIds = SettingsUtil.getHistoryItems(acitiviy);
for (Integer his : historyIds) {
for (EmojiItem emoji : paths) {
if (emoji.id == his) {
history.add(emoji);
break;
}
}
}
history.add(paths.get(0));
final KeyClickListener onEmojiClick = new KeyClickListener() {
#Override
public void keyClickedIndex(EmojiItem index) {
int cursorPosition = editMessage.getSelectionStart();
editMessage.getText().insert(cursorPosition, index.emojiText);
try {
editMessage.getText().setSpan(
new ImageSpan(index.emojiDrawable), cursorPosition,
cursorPosition + 1,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} catch (Exception e) {
}
if (history.get(0) != index)
history.add(0, index);
SettingsUtil.setHistoryItems(acitiviy, history);
emojiAdapter.notifyDataSetChanged();
pager.setAdapter(emojiAdapter);
}
};
((ImageButton) popUpView.findViewById(R.id.emoji2))
.setImageDrawable(groups[0].get(0).emojiDrawable);
((ImageButton) popUpView.findViewById(R.id.emoji3))
.setImageDrawable(groups[1].get(0).emojiDrawable);
((ImageButton) popUpView.findViewById(R.id.emoji4))
.setImageDrawable(groups[2].get(0).emojiDrawable);
((ImageButton) popUpView.findViewById(R.id.emoji5))
.setImageDrawable(groups[3].get(0).emojiDrawable);
((ImageButton) popUpView.findViewById(R.id.emoji6))
.setImageDrawable(groups[4].get(0).emojiDrawable);
popUpView.findViewById(R.id.emoji1).setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
emojiAdapter.emojis = history;
emojiAdapter.notifyDataSetChanged();
pager.setAdapter(emojiAdapter);
}
});
popUpView.findViewById(R.id.emoji2).setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
emojiAdapter.emojis = groups[0];
emojiAdapter.notifyDataSetChanged();
pager.setAdapter(emojiAdapter);
}
});
popUpView.findViewById(R.id.emoji3).setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
emojiAdapter.emojis = groups[1];
emojiAdapter.notifyDataSetChanged();
pager.setAdapter(emojiAdapter);
}
});
popUpView.findViewById(R.id.emoji4).setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
emojiAdapter.emojis = groups[2];
emojiAdapter.notifyDataSetChanged();
pager.setAdapter(emojiAdapter);
}
});
popUpView.findViewById(R.id.emoji5).setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
emojiAdapter.emojis = groups[3];
emojiAdapter.notifyDataSetChanged();
pager.setAdapter(emojiAdapter);
}
});
popUpView.findViewById(R.id.emoji6).setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
emojiAdapter.emojis = groups[4];
emojiAdapter.notifyDataSetChanged();
pager.setAdapter(emojiAdapter);
}
});
emojiAdapter = new EmoticonsPagerAdapter(acitiviy, groups[0],
onEmojiClick);
pager.setAdapter(emojiAdapter);
// Creating a pop window for emoticons keyboard
popupWindow = new PopupWindow(popUpView, LayoutParams.MATCH_PARENT,
(int) keyboardHeight, false);
View backSpace = (View) popUpView.findViewById(R.id.imageBackspace);
backSpace.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
KeyEvent event = new KeyEvent(0, 0, 0, KeyEvent.KEYCODE_DEL, 0,
0, 0, 0, KeyEvent.KEYCODE_ENDCALL);
editMessage.dispatchKeyEvent(event);
}
});
popupWindow.setOnDismissListener(new OnDismissListener() {
#Override
public void onDismiss() {
emoticonsCover.setVisibility(LinearLayout.GONE);
}
});
ViewPager pagerStickers = (ViewPager) popUpView
.findViewById(R.id.stickers_pager);
pagerStickers.setOffscreenPageLimit(3);
}
private void showKeyboardPopup(View root, boolean attaches) {
if (!popupWindow.isShowing()) {
popupWindow.setHeight((int) (keyboardHeight));
if (isKeyBoardVisible) {
imageEmoji.setImageResource(R.drawable.emoji_kbd);
emoticonsCover.setVisibility(LinearLayout.GONE);
} else {
imageEmoji.setImageResource(R.drawable.ic_down);
emoticonsCover.setVisibility(LinearLayout.VISIBLE);
}
try {
popupWindow.showAtLocation(root, Gravity.BOTTOM, 0, 0);
} catch (Exception e) {
}
} else {
imageEmoji.setImageResource(R.drawable.emoji_btn_normal);
popupWindow.dismiss();
return;
}
imageAttaches.setBackgroundColor(attaches ? 0xFF808080 : 0x00000000);
imageEmojis.setBackgroundColor(attaches ? 0x00000000 : 0xFF808080);
imageStickers.setBackgroundColor(0x00000000);
layoutEmojis.setVisibility(attaches ? View.GONE : View.VISIBLE);
layoutStickers.setVisibility(View.GONE);
}
Please checkout for more details click here.
Thanks hope this will help you.It is bit old but you can try it.
you can refer this github link for complete code
EmojiIcon
I want to hide soft keyboard everywhere in my application permanently. So I can use my custom keyboard. I have checked many solutions but still soft keyboard is appearing. Here is my code to register searchview.
EditText mEditText;
SearchView mSearchView;
public void registerSearchView(final SearchView sview, final MenuItem searchItem ) {
mSearchView = sview;
SearchManager searchManager = (SearchManager) mHostActivity.getSystemService(Context.SEARCH_SERVICE);
mSearchView.setSearchableInfo(searchManager.getSearchableInfo(mHostActivity.getComponentName()));
mEditText = (EditText) mSearchView.findViewById(R.id.search_src_text);
ImageView closeButton = (ImageView) mSearchView.findViewById(R.id.search_close_btn);
closeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mEditText.getText() == null || mEditText.getText().length() < 1) {
mSearchView.onActionViewCollapsed();
MenuItemCompat.collapseActionView(searchItem);
hideCustomKeyboard();
} else {
mEditText.setText("");
mSearchView.setQuery("", false);
}
//Collapse the search widget
}
});
mEditText.setOnTouchListener(onTouch);
mSearchView.setOnTouchListener(onTouch);
mEditText.setOnFocusChangeListener(oFocusChange);
mSearchView.setOnFocusChangeListener(oFocusChange);
mEditText.setOnClickListener(onClickListener);
mSearchView.setOnClickListener(onClickListener);
mSearchView.setOnSearchClickListener(onClickListener);
private View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
((InputMethodManager) mHostActivity.getSystemService(ActionBarActivity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(), 0);
showCustomKeyboard(v);
v.clearFocus();
v.requestFocus();
}
};
private View.OnTouchListener onTouch = new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent motionEvent) {
if (mEditText.getText()==null || mEditText.getText().length() < 1){
v.clearFocus();
v.requestFocus();
}
((InputMethodManager) mHostActivity.getSystemService(ActionBarActivity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(), 0);
v.onTouchEvent(motionEvent);
return true;
}
};
private View.OnFocusChangeListener oFocusChange = new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean b) {
if (b == false) {
hideCustomKeyboard();
} else {
((InputMethodManager) mHostActivity.getSystemService(ActionBarActivity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(), 0);
showCustomKeyboard(v);
}
}
};
This will work as you can force android to hide the keyboard !!
public static void disableSoftInputFromAppearing(EditText editText) {
if (Build.VERSION.SDK_INT >= 11) {
editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
editText.setTextIsSelectable(true);
} else {
editText.setRawInputType(InputType.TYPE_NULL);
editText.setFocusable(true);
}
}
above code will work foresure !!
as well for search view
mSearchView.clearFocus();
will solve the issue
Add this to you manifest :
<activity android:name="com.your.package.ActivityName"
android:windowSoftInputMode="stateHidden" />
i've recently started developing in android and am currently stuck at a point i need to receive values from a dialog box. I have a mainActivity which extends fragmentActivity and an AlertDialog Class.
1)i created a static method showDefalutDialog in AlertDialog class and its being called from mainActivity button click listener with parameters being passed to alertDialog.
2)In showDefalutDialog static method i created .setPositivebutton and .setNegativeButton with a Yes/No DialogInterface respectively.
now here's what i want to do.
1)When yes button on interface is clicked it should return a value to mainActivity
so i can implement it in an if statement to perform a certain function.
moving from windows c# programming doing so isn't a problem but i just don't know how to implement that in android below is relevant code snip
private void sendSms()
{
SharedPreferences pref = getApplicationContext().getSharedPreferences("Sms_MyPref", 0);
mail = pref.getString("email", null); // getting String
tel = pref.getString("receiver_tel", null); // getting String
layout = (LinearLayout)findViewById(R.id.linearLayout1);
from_dateEdit = (EditText) findViewById(R.id.date_edit);
to_dateEdit = (EditText) findViewById(R.id.date_edit_to);
snButton = (Button)findViewById(R.id.form_send_button);
from = (Button)findViewById(R.id.from);
to = (Button)findViewById(R.id.to);
spn = (Spinner)findViewById(R.id.form_spinner);
spn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Object item = parent.getItemAtPosition(pos);
spinnerV = (String) item;
if(pos == 0)
{
layout.setVisibility( pos == 0 ? View.VISIBLE : View.VISIBLE);
from_dateEdit.setText(DatePickerFragment.getYesteesDate());
from.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDatePicker();
}
});
to_dateEdit.setText(DatePickerFragment.getTodaysDate());
to.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDatePicker2();
}
});
new1 = null;
new2 = null;
from_dateEdit.setText(new1);
to_dateEdit.setText(new2);
}
else if(pos == 1)
{
layout.setVisibility( pos == 1 ? View.GONE : View.VISIBLE);
new1 = null;
new2 = null;
new1 = "a";
new2 = "b";
}
else if(pos == 2)
{
layout.setVisibility( pos == 2 ? View.GONE : View.VISIBLE);
new1 = null;
new2 = null;
new1 = "a";
new2 = "b";
}
else if(pos == 3)
{
layout.setVisibility( pos == 3 ? View.GONE : View.VISIBLE);
new1 = null;
new2 = null;
new1 = "a";
new2 = "b";
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
snButton.setOnClickListener(new OnClickListener() {
public void onClick(View view)
{
if(new1 == null && new2 == null)
{
alert.showAlertDialog(MainActivity.this, "Error..", "Please specify a date range", false);
}
else if(new1 != null && new2 == null)
{
alert.showAlertDialog(MainActivity.this, "Error..", "Please specify a date TO", false);
}
else if(new1 == null && new2 != null)
{
alert.showAlertDialog(MainActivity.this, "Error..", "Please specify a date FROM", false);
}
else
{
gen = new1.toString()+","+new2.toString();
alert();
//i want to return a value from dialog yes/no click
if(/*dialog yes is clicked*/)
{
sms();
}
else if(/*dialog No is clicked*/)
{
return;
}
}
}
});
}
private void alert()
{
AlertDialogManager.showDefalutDialog(getApplicationContext(), spinnerV, mail, new1,new2);
}
public void sms()
{
String both = "{"+ spinnerV.toString() + ","+gen.toString()+","+ mail.toString()+"}";
sendSMS(tel,both);
}
and showDefaultDialog static method from AlertDialog class
#SuppressLint("InflateParams")
public static void showDefalutDialog(final Context context, String order, final String mail, String fromD, String toD) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
// set title
alertDialogBuilder.setTitle(R.string.finalmsg);
LayoutInflater li = LayoutInflater.from(context);
View view = li.inflate(R.layout.data_summary_view, null);
EditText EMAIL = (EditText)view.findViewById(R.id.Email);
EditText Selectedorder = (EditText)view.findViewById(R.id.order);
EditText Dfrom = (EditText)view.findViewById(R.id.edit_from);
EditText Dto= (EditText)view.findViewById(R.id.edit_to);
LinearLayout ll = (LinearLayout) view.findViewById(R.id.datelayout);
LinearLayout l2 = (LinearLayout) view.findViewById(R.id.datelayout2);
Selectedorder.setText(order);
EMAIL.setText(mail);
if(fromD.toString() != "a" && toD.toString() != "b")
{
ll.setVisibility(View.VISIBLE);
l2.setVisibility(View.VISIBLE);
Dfrom.setText(fromD);
Dto.setText(toD);
}
else if(fromD.toString() == "a" && toD.toString() == "b")
{
ll.setVisibility(View.GONE);
l2.setVisibility(View.GONE);
}
// set dialog message
alertDialogBuilder.setView(view);
//int msdt = data.toString().toCharArray().length;
//Toast.makeText(context, "MsData char count : " + msdt , Toast.LENGTH_SHORT).show();;
alertDialogBuilder
.setOnCancelListener(new OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
try {
Intent main = new Intent(context, MainActivity.class);
main.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(main);
} catch (Exception e) {
Log.d(TAG, "Error while starting Main activity from Dialog ! ");
}
}
})
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
Toast.makeText(context,"Your Order will be sent to "+ mail +" please check your inbox for comfirmation." , Toast.LENGTH_SHORT).show();
dialog.cancel();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.dismiss();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
You can define you custom interface simmilar to this one:
public interface MyDialogClickListener {
void onPositiveClicked(String value);
}
Then you create instance and pass to method, where you create dialog:
public static void showDeafultDialog(..., MyDialogClickListener listener) {
// ...
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
listener.onPositiveClicked("you can pass yout value here")
}
})
// ...
}
Handle result:
private void sendSms() {
AlertDialogManager.showDeafultDialog(..., new MyDialogClickListener() {
#Override
public void onPositiveClicked(String value) {
// do whatever you want with value
}
});
I'm having some trouble with a simple dialog i'm working on. I have three different TextView's that when they are clicked I have a dialog box pop up with only the Title and one EditText. I'm trying to make it so I use the same custom dialog for all three text views, but depending on which TextView is tapped the dialog set the new value to that TextView. Below is my code:
#Override
protected Dialog onCreateDialog(int id) {
super.onCreateDialog(id);
CustomDialogPercent dialog = null;
switch(id){
case 1:
dialog = new CustomDialogPercent(this, id);
dialog.setTitle("Shipping Percent");
dialog.show();
break;
case 2:
dialog = new CustomDialogPercent(this, id);
dialog.setTitle("Tax Percent");
dialog.show();
break;
case 3:
dialog = new CustomDialogPercent(this, id);
dialog.setTitle("Commission Percent");
dialog.show();
break;
default:
dialog = null;
}
return dialog;
}
private void registerListeners() {
shippingPercent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showDialog(1);
}
});
taxPercent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showDialog(2);
}
});
commissionPercent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showDialog(3);
}
});
}
public class CustomDialogPercent extends Dialog {
int id = 0;
public CustomDialogPercent(Context context, int id) {
super(context);
this.id = id;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.basic_dialog);
basicDialogEntry = (EditText) findViewById(R.id.basic_dialog_entry);
basicDialogEntry.setKeyListener(DigitsKeyListener.getInstance(true,true));
}
#Override
public void onStart() {
super.onStart();
switch(id) {
case 1:
basicDialogEntry.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(basicDialogEntry.getWindowToken(), 0);
shippingPercent.setText(basicDialogEntry.getText().toString());
try {
if ((Float.parseFloat(basicDialogEntry.getText().toString())) < 0) {}
}catch(Exception ex) {
shippingPercent.setText("0");
}
mathCalculations();
CustomDialogPercent.this.dismiss();
}
return false;
}
});
break;
case 2:
basicDialogEntry.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(basicDialogEntry.getWindowToken(), 0);
taxPercent.setText(basicDialogEntry.getText().toString());
try {
if ((Float.parseFloat(basicDialogEntry.getText().toString())) < 0) {}
}catch(Exception ex) {
taxPercent.setText("0");
}
mathCalculations();
CustomDialogPercent.this.dismiss();
}
return false;
}
});
break;
case 3:
basicDialogEntry.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(basicDialogEntry.getWindowToken(), 0);
commissionPercent.setText(basicDialogEntry.getText().toString());
try {
if ((Float.parseFloat(basicDialogEntry.getText().toString())) < 0) {}
}catch(Exception ex) {
commissionPercent.setText("0");
}
mathCalculations();
CustomDialogPercent.this.dismiss();
}
return false;
}
});
break;
}
}
}
OK. With this code what is happening is the first time I tap each TextView they work perfectly. But once I tap any of them again the dialog boxes pop up correctly with the correct title, but values that are set back to the TextView are incorrect.
Example; i enter 8 for the shippingPercent and them 25 for the commissionPercent. And then i go back to shippingPercent and enter 5. The 5 doesn't get set. Instead it will be 25. If i keep using the dialog boxes it seems like my mathCalculations() stops working aswell.
Thanks for all the help.
Maybe you could assign a variable
activeTextView = theTextViewYouWant;
...then update activeTextView with the value you want?