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?
Related
I have this dialog, with just a textView and no buttons. It displays some info and I need to change info on key press left and right. Unfortunately the dialog closes on any key.
This code in MainActivity invokes the dialog (redundant code omitted)
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_MENU) {
if (event.getAction() == KeyEvent.ACTION_UP) {
InfoDialog infoDialog = new InfoDialog();
infoDialog.showDialog(this,currentDateAndTime,chn);
return true;
}
}
return super.dispatchKeyEvent(event);
}
and this is the dialog code
public class InfoDialog {
private int counter = 0;
private Dialog dialog;
public void showDialog(final Activity activity, final String DateTime, final ChannelList.Channel chn){
dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.info_dialog);
counter = 0;
setDialogText(chn,DateTime,counter);
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
if (keyEvent.getAction() == KeyEvent.ACTION_UP) {
if (counter>0) counter--;
setDialogText(chn,DateTime,counter);
return true;
}
}
if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
if (keyEvent.getAction() == KeyEvent.ACTION_UP) {
if (counter<chn.infoText.size()-1) counter++;
setDialogText(chn,DateTime,counter);
return true;
}
}
dialog.dismiss();
return false;
}
});
dialog.show();
}
}
when I press any button, the dialog closes, even if the onKey has been invoked.
Did I miss something? how do I handle keypresses for my dialog only? (other dialogs may use same keys for different operation)
well, inside InfoDialog you are setting DialogInterface.OnKeyListener and on the bottom of onKey method you are calling dialog.dismiss(), try to remove this line... (and also return true for any case)
I am making an app that sends data over bluetooth to a HC-06. When I type something in the EditText and press send, the EditText loses focus. I found out this happens because I call a backgound thread with write().
How can I keep focus?
messageEdTxt = findViewById(R.id.message_edtxt);
messageEdTxt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEND) {
String messageEdtxt = messageEdTxt.getText().toString();
messageEdTxt.setText("");
write(messageEdtxt);
return true;
} else {
return false;
}
}
});
sendBtn = findViewById(R.id.send_btn);
sendBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String messageEdtxt = messageEdTxt.getText().toString();
write(messageEdtxt);
messageEdTxt.setText("");
}
});
The mConnectedThread is a custom thread to send messages.
private void write(String message) {
if (mSocket.isConnected()) {
if (mConnectedThread != null) {
mConnectedThread.write(message.getBytes(StandardCharsets.UTF_8));
} else {
finish();
}
} else {
finish();
}
}
Did you try
messageEdTxt.requestFocus();
on clicking sendBtn
I'm trying to set the focus on an EditText (inputOne) but my code is showing odd behaviour. When I press the button, inputOne gets focus and everything is nice. If I press "Enter" on the SoftKeyboard, firing the onKeyListener, inputTwo remains focussed. In both cases "inputOne.requestFocus" returns true.
This is my Code:
public class EditSeriesActivity extends Activity {
private FlashcardSeries series;
private EditText inputOne;
private EditText inputTwo;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editseries);
Intent intent = getIntent();
series = intent.getParcelableExtra("EXTRA_MASSAGE");
TextView textView = (TextView) findViewById(R.id.nameseries);
textView.setText(series.getName());
inputOne = (EditText) findViewById(R.id.side1);
inputTwo = (EditText) findViewById(R.id.side2);
inputTwo.setOnKeyListener(new View.OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
addCardToSeries(v);
break;
default:
break;
}
}
return false;
}
});
}
public void addCardToSeries(View view){
series.addFlashcard(new Flashcard(inputOne.getText().toString(), inputTwo.getText().toString()), this);
inputTwo.setText("");
inputOne.setText("");
inputOne.requestFocus();
}
}
try this
inputOne.requestFocus();
InputMethodManager inpuMethod = (InputMethodManager) getSystemService(G.context.INPUT_METHOD_SERVICE);
inpuMethod.showSoftInput(inputOne, InputMethodManager.SHOW_IMPLICIT);
I saw many posts on this subject but couldnt find the right answer for me.
I have an activity that dims when i pop the popup window.
the back button working but only the second time i press it, the first press dismiss the popup but its not un-dim the activity because i cant catch the event from the popupwindows, the second press is catched by the activity and only then i can un-dim it.
here are my tries to accomplish this:
m_PopupWindow.setBackgroundDrawable(new BitmapDrawable());
m_PopupWindow.setOutsideTouchable(true);
View popUpWindowLaout = m_PopupWindow.getContentView();
popUpWindowLaout.setFocusableInTouchMode(true);
//first press doesnt get caught here
popUpWindowLaout.setOnKeyListener(new View.OnKeyListener()
{
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
m_ActionBar.show();
unShadeTheActivity();
m_PopupWindow.dismiss();
return true;
}
}
});
//this func will catch the second press and will work, but i want the first press will do it.
#Override
public void onBackPressed() {
if (m_PopupWindow != null)
{
m_ActionBar.show();
unShadeTheActivity();
m_PopupWindow.dismiss();
}
else
{
super.onBackPressed();
}
}
change
public void onBackPressed() {
if (m_PopupWindow != null)
{
m_ActionBar.show();
unShadeTheActivity();
m_PopupWindow.dismiss();
}
else
{
super.onBackPressed();
}
}
to
public void onBackPressed() {
super.onBackPressed();
if (m_PopupWindow != null)
{
m_ActionBar.show();
unShadeTheActivity();
m_PopupWindow.dismiss();
}
else
{
// rest of the code
// you can use finish,dismiss or call startActivity
// finish();
}
}
popupWindow.setOnShowListener(HandlePopupShowLister);
popupWindow.setOnDismissListener(HandlePopUpDismissListerner);
public static OnDismissListener HandlePopUpDismissListerner = new OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub
Log.i("HandlePopUpDismissListerner", "HandlePopUpDismissListerner");
CommonVariable.IsPopupOpen = false;
}
};
public static OnShowListener HandlePopupShowLister = new OnShowListener() {
// onShowListener interface.
#Override
public void onShow(DialogInterface dialog) {
Log.i("HandlePopupShowLister", "HandlePopupShowLister");
// TODO Auto-generated method stub
CommonVariable.IsPopupOpen = true;
}
};
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);
}
}