PopupWindow not triggering sytem context dialog on EditText long-press - android

Sorry if the title was a bit vague.
I'm developing an app on Freelancer and I almost have it finished except for a complaint from the customer after some testing.
I use a PopupWindow in place of a dialog to edit contextual settings, if that makes any sense. I don't want to be too specific and risk giving the app concept away, which I'm sure the customer wouldn't be too pleased about.
The PopupWindow is given a Content View of a layout inflated from XML. In that layout are several EditText widgets. The issue is that those EditTexts will not trigger the default contextual dialog on long press that presents options for text/IME selection, and cut/copy/paste.
I saw a similar question trying to get the TouchTrigger or something and it not working without setBackgroundDrawable(), which I've tried with a simple new ColorDrawable(). It still doesn't work.
Is there any easy way to trigger the system-default long-press dialog in an OnLongPressListener, or will I have to move Heaven and Earth to implement it myself? Because if that's the case, I'll just write a Fragment for it and swap it out in a transaction. I know that'll work.
The relevant code:
Inside the initiating fragment:
RulesDialog dialog;
PopupWindow window;
public void showAddRuleDialog(){
dialog = new RulesDialog();
View view = getView();
window = new PopupWindow(dialog.initViews(this, null), view.getWidth(), view.getHeight(), true);
window.setBackgroundDrawable(new ColorDrawable());
dialog.setRulesDialogListener(new rulesDialogListener(){
#Override
public void onSave(ViewHolder holder) {
addRule(holder);
window.dismiss();
}
#Override
public void onCancel() {
window.dismiss();
}});
int[] location = {0,0};
view.getLocationOnScreen(location);
window.showAtLocation(view, 0, location[0], location[1]);
In RulesDialog:
public class ViewHolder{
public ViewHolder(View dialogView){
name = (TextView) dialogView.findViewById(R.id.name);
response = (TextView) dialogView.findViewById(R.id.response);
senders = (TextView) dialogView.findViewById(R.id.senders);
sendersAdd = (Button) dialogView.findViewById(R.id.sendersAdd);
sendersEdit = (Button) dialogView.findViewById(R.id.sendersEdit);
timeFrom = (TextView) dialogView.findViewById(R.id.from);
timeFromEdit = (Button) dialogView.findViewById(R.id.timeBeforeEdit);
timeTo = (TextView) dialogView.findViewById(R.id.to);
timeToEdit = (Button) dialogView.findViewById(R.id.timeAfterEdit);
keywords = (TextView) dialogView.findViewById(R.id.keywords);
matchCase = (CheckBox) dialogView.findViewById(R.id.matchCase);
matchAlone = (CheckBox) dialogView.findViewById(R.id.matchAlone);
matchPlural = (CheckBox) dialogView.findViewById(R.id.matchPlural);
cancel = (Button) dialogView.findViewById(R.id.cancel);
save = (Button) dialogView.findViewById(R.id.save);
}
TextView name;
TextView response;
TextView senders;
Button sendersAdd;
Button sendersEdit;
TextView timeFrom;
Button timeFromEdit;
TextView timeTo;
Button timeToEdit;
TextView keywords;
CheckBox matchCase;
CheckBox matchAlone;
CheckBox matchPlural;
Button cancel;
Button save;
}
Activity activity;
ViewHolder holder;
Fragment fragment;
public View initViews(Fragment mFragment, Rule rule){
fragment = mFragment;
activity = fragment.getActivity();
View dialogView = LayoutInflater.from(activity).inflate(R.layout.rules_dialog, null);
holder = new ViewHolder(dialogView);
final TextView senders = holder.senders;
holder.sendersAdd.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
showContacts();
}});
holder.sendersEdit.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
editSenders(senders);
}
});
final TextView timeFrom = holder.timeFrom;
holder.timeFromEdit.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
showTimePickerDialog(timeFrom);
}
});
final TextView timeTo = holder.timeTo;
holder.timeToEdit.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
showTimePickerDialog(timeTo);
}
});
holder.cancel.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
mListener.onCancel();
}});
holder.save.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
mListener.onSave(holder);
}});
if(rule == null)
rule = new Rule();
holder.name.setText(rule.name);
holder.response.setText(rule.response);
holder.senders.setText(rule.senders.toString());
holder.senders.setTag(rule.senders);
holder.keywords.setText(rule.keywords);
holder.matchCase.setChecked(rule.matchCase);
holder.matchAlone.setChecked(rule.matchAlone);
holder.matchPlural.setChecked(rule.matchPlural);
holder.timeFrom.setTag(rule.timeFrom);
holder.timeFrom.setText(Rules.formatTime(rule.timeFrom));
holder.timeTo.setTag(rule.timeTo);
holder.timeTo.setText(Rules.formatTime(rule.timeTo));
return dialogView;
}

So I tried rewriting RulesDialog as a fragment, and it didn't work out too well. Had issues with making Fragment Transactions work right when called from the Fragments they're operating on.
(I know this isn't the point to fragments. I'm not really aiming to write a completely modular app right now. I just want to come out with a product the customer will be happy with.)
I ended up rewriting RulesDialog as an Activity instead, and using startActivityForResult() from the calling fragment. Then passing the edited data back with setResult(). It all works nicely in concert.

Related

Undo Button for a WordSearch app

I'm new to android and I'm having a hard time finding solutions to my problem on my app.
My app is a wordsearch game that uses tapping on the tiles as an input. This is the code for the onClick() of the dynamic textviews on the tablelayout:
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
w.setVisibility(View.VISIBLE);
//change the color of tapped textview
text.setTextColor(Color.WHITE);
text.setBackgroundColor(Color.parseColor(mColors[randomNum]));
String b = text.getText().toString();
uTxt.setText(""+uTxt.getText().toString() + b);
//check if answer is in the word grid
if(checkAns(uTxt, list))
{
w.setVisibility(View.GONE);
wC.setText(String.valueOf(Integer.parseInt(wC.getText()+"")-1));
if(Integer.parseInt(wC.getText()+"") == 0){
int newM = minutes*60 + seconds;
dataHelper.insertData(pNameC.getText().toString(), newM, currentDateandTime, Category.leve);
t.cancel();
Context mContext = getApplicationContext();
Activity mActivity = GameScreen.this;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
// Inflate the custom layout/view
View customView = inflater.inflate(R.layout.gameover,null);
// Initialize a new instance of popup window
PopupWindow mPopupWindow = new PopupWindow(
customView,
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
Typeface font = Typeface.createFromAsset(getAssets(), "raw2.ttf");
TextView cattxt = (TextView)customView.findViewById(R.id.catTxt);
String ctg = ti.getText().toString();
cattxt.setTypeface(font);
cattxt.setText(ctg);
Button yesB = (Button) customView.findViewById(R.id.maglaro2);
yesB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(GameScreen.this, Category.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
GameScreen.this.finish();
}
});
Button noB = (Button) customView.findViewById(R.id.hindi);
noB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(GameScreen.this, MainActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
GameScreen.this.finish();
}
});
mPopupWindow.showAtLocation(table, Gravity.CENTER,0,0);
}
uTxt.setText("");
}
}
});
Now my problem is I want an UNDO Button that will delete the last character on the uTxt and will change back the color of the last touched textView
Does anyone have any ideas on how to do that?
If yes leave a comment, answer, or suggestion below. TIA!
Typical solution for this problem is the usage of the command pattern (excellent for undo redo functionality).
See https://en.wikipedia.org/wiki/Command_pattern

Non responding app in "Recent Apps" when PopUpMenu and Edit Text Present

Context
I'm working with popupwindows to allow a user to quickly rename a cardview in an activity.
I do this by using a ViewSwitcher to swap the TextView (original name) for an EditText(new name).
Problem
When the EditText and PopUpWindow to confirm are present an the user presses "RECENT APPS", you cannot for some reason get back into the app. ie. when you click it, it won't respond.
Diagnosis
I think it's an issue with Window Focus. I've tried EditText.clearFocus() from ET and dismissing all PopUps onPause, no luck.
Is there a way to use onFocusChangeListener to remove this issue?
Code (I've tried to remove as much superfluous items as possible)
TheHubActivity.java
public class TheHubActivity extends AppCompatActivity implements RecyclerViewAdapter.onCardClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
// KEYBOARD
imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
//... Set up recycle view
rvContent = new ArrayList<>();
}
#Override
public void onCardLongClick(Flow longClickedFlow, int cardPosition, View cardViewClicked) {
showLongClickPopUpMenu(longClickedFlow,cardPosition, cardViewClicked);
}
private void showLongClickPopUpMenu(final Flow longClickedFlow, final int cardPosition, final View cardViewClicked) {
LayoutInflater layoutInflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup_window_longclick, null);
LinearLayout viewGroup = (LinearLayout) layout.findViewById(R.id.popup_longclick);
// Creating the PopupWindow
final PopupWindow popup = new PopupWindow(layout, RecyclerView.LayoutParams.WRAP_CONTENT,
RecyclerView.LayoutParams.WRAP_CONTENT);
popup.setFocusable(true);
// Getting a reference to Close button, and close the popup when clicked.
ImageView delete = (ImageView) layout.findViewById(R.id.popup_delete_item);
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/*.... Delete current Flow from internal file and UI */
popup.dismiss();
}
});
ImageView edit = (ImageView) layout.findViewById(R.id.popup_edit_item);
edit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
popup.dismiss();
renameFlow(cardPosition, cardViewClicked);
}
});
// Displaying the popup at the specified location, + offsets.
popup.showAsDropDown(cardViewClicked, cardViewClicked.getMeasuredWidth(),popupDisplayHeight, Gravity.TOP);
longClickPopup = popup;
}
private void renameFlow(final int cardPosition, final View cardViewClicked) {
final ViewSwitcher switcher = (ViewSwitcher) cardViewClicked.findViewById(R.id.rename_switcher);
final EditText rename = (EditText) switcher.findViewById(R.id.item_flow_rename);
rename.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (rename.hasFocus()) {
showEditPopupWindow(rename, cardViewClicked, switcher, cardPosition);
} else {
imm.hideSoftInputFromWindow(rename.getWindowToken(), 0);
}
}
});
switcher.showNext();
rename.requestFocus();
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
/* Forces keyboard */
}
private void showEditPopupWindow(final EditText newName, View cardViewClicked, final ViewSwitcher switcher, final int cardPosition) {
LayoutInflater layoutInflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup_window_editing, null);
LinearLayout viewGroup = (LinearLayout) layout.findViewById(R.id.popup_editing);
// Creating the PopupWindow
final PopupWindow popup = new PopupWindow(layout, RecyclerView.LayoutParams.WRAP_CONTENT,
RecyclerView.LayoutParams.WRAP_CONTENT);
popup.setFocusable(false); // So that user can edit text
// Getting a reference to Close button, and close the popup when clicked.
ImageView confirmEdit = (ImageView) layout.findViewById(R.id.popup_confirm_item_changes);
confirmEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/* .. Changes name of cardview through edit text */
switcher.showNext();
popup.dismiss();
newName.clearFocus();
}
}
});
ImageView cancelEdit = (ImageView) layout.findViewById(R.id.popup_cancel_item_changes);
cancelEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switcher.showNext();
popup.dismiss();
}
});
popup.showAsDropDown(cardViewClicked, cardViewClicked.getMeasuredWidth(),popupDisplayHeight, Gravity.TOP);
editingPopup = popup;
}
#Override
protected void onPause() {
dismissPopups();
super.onPause();
}
private void dismissPopups() {
if (longClickPopup!=null && longClickPopup.isShowing()) {
longClickPopup.dismiss();
}
if (editingPopup!=null && editingPopup.isShowing()) {
editingPopup.dismiss();
}
}
}
For Visual People
I solved the issue... and it was surprisingly larger and completely unrelated to the Focus/PopUps (tunnel vision does that I guess).
In my Manifest I was using android:launchMode="singleTop" which was creating weird behaviour when TheHubActivity was sent to recent apps because this was my entrance activity. From the Developer Docs singleTop functions like so:
Similarly, a new instance of a "singleTop" activity may also be created to handle a new intent. However, if the target task already has an existing instance of the activity at the top of its stack, that instance will receive the new intent (in an onNewIntent() call); a new instance is not created. In other circumstances — for example, if an existing instance of the "singleTop" activity is in the target task, but not at the top of the stack, or if it's at the top of a stack, but not in the target task — a new instance would be created and pushed on the stack.
<activity
android:name=".TheHubActivity"
android:label="#string/app_name"
~~~~~~android:launchMode="singleTop"~~~~~~~~
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

How to get the text value of a button from another layout?

First of all, I have searched in forum for "How to get text from edittext in other layout", and tried many other ways, then I decided to use LayoutInflater but it did not help.
The implementation of LayoutInflater (I follow this guide)
LayoutInflater factory = getLayoutInflater();
View regisText = factory.inflate(R.layout.regis, null);
EditText user = (EditText) regisText.findViewById(R.id.edt1);
String usr = user.getText().toString();
My parent layout is named activity_input.xml having 3 buttons (I do not set text button). I put only button Id for easy view.
<Button
android:id="#+id/btn1">
</Button>
<Button
android:id="#+id/btn2">
</Button>
<Button
android:id="#+id/btn3">
</Button>
After I click a button, one dialog appears to select value (1, 2 or 3) to set the button display text (1, 2 or 3) in activity_input.xml.
final Dialog dialog1 = new Dialog(user_input.this);
dialog1.setTitle("Select a Number");
dialog1.setContentView(R.layout.game_dialog);
dialog1.show();
button1 = (Button) dialog1.findViewById(R.id.btn1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Button p1_btn = (Button) findViewById(x);
p1_btn.setText("1");
dialog1.dismiss();
}
});
The onClick works perfect.
But my problem is that I want to get the text of the button in parent layout again, I try to implement Layoutinflater but it does not work, here is the code.
LayoutInflater f1 = getLayoutInflater();
View v1 = f1.inflate(R.layout.activity_input, null);
Button btn1 = (Button) v1.findViewById(R.id.b1);
String t1 = btn1.getText().toString();
I check to make sure that the String t1 is retrieve successfully by print the string but it shows nothing
Hope you guide help me as soon as possible!
Thanks you very much.
You can make a static variable and store the result there, then access it from your dialog. So in your ActivityInput.java
public class ActivityInput extends Activity {
public static String input = "";
//code
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Button p1_btn = (Button) findViewById(x);
input = "1";
p1_btn.setText(input);
dialog1.dismiss();
}
});
}
Now in your other java class ActivityOutput.java:
public class ActivityOutput extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
//code
String result = ActivityInput.input;
//use the result string somewhere to display it in your app
}
}

When creating a button from a Dialog Box

The original code has been deleted, the new working code is shown. The idea behind the code is to create a new textView within a layout that has a custom name to it that the user provides. Previously, a NPE error was happening. This is a fix. Any questions, please feel free to ask.
EDIT: Found the solution
The fix needs to be as followed:
accountEdit = new EditText(this); // accountEdit needs to be a global variable
then within the builder.setPositiveButton
builder.setPositiveButton(R.string.btn_save, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dInterface, int whichButton)
{
LinearLayout lineLayout = (LinearLayout)findViewById(R.id.linear_layout);
String newAccountName = accountEdit.getText().toString();
newTextView = new TextView( getBaseContext() );
newTextView.setVisibility(View.VISIBLE);
newTextView.setText( newAccountName );
newTextView.setId(id);
newTextView.setTextSize(35);
newTextView.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
onClickNew(view);
}
});
newTextView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
Toast.makeText(getBaseContext(), "Testing" , Toast.LENGTH_LONG).show();
return true;
}
});
This will create the button, as well as set the name of the button to the information that is in the EditText within the Dialog Box. Previously, the EditText was from another activity, and was being called wrong, which caused the NPE. Thank you for all the help.
As Wenhui metioned, you call the finViewById inside the onclick listener of the button, so the wrong context is used. Do it like in the following example:
final EditText accountEdit = (EditText)findViewById(R.id.newAccountButton);
final String newAccountName = accountEdit.getText().toString();
final LinearLayout lineLayout = (LinearLayout)findViewById(R.id.linear_layout);
builder.setPositiveButton(R.string.btn_save, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dInterface, int whichButton)
{
newTextView = new TextView(getBaseContext());
newTextView.setVisibility(View.VISIBLE);
newTextView.setText("Test");
newTextView.setId(id);
newTextView.setTextSize(35);
newTextView.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
onClickNew(view);
}
});
lineLayout.addView(newTextView);
id++;
}
});

OnclickListener for individual elements in a row from a ListActivity using SimpleCursorAdapter to Bind to DB not working properly

Please help.
As I have stated in the title I am trying to make that individual elements of a row of a List adapter launch different actions depending on what the user click.
It "kind of" works but it takes LONG for it to react to user clicks. What is it that I am doing wrong?
Thanks in advance,
So I tried the following code in
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Cursor c = (Cursor) this.getListAdapter().getItem(position);
// c.moveToNext();
prescription_id = c.getString(0);
TextView pName = (TextView) v.findViewById(R.id.text2);
TextView paName = (TextView) v.findViewById(R.id.text3);
TextView rDateLabel = (TextView) v.findViewById(R.id.textView1);
TextView rDate = (TextView) v.findViewById(R.id.text4);
TextView rLeftLabel = (TextView) v.findViewById(R.id.text5);
TextView rLeft = (TextView) v.findViewById(R.id.text6);
ImageView callPhone = (ImageView) v.findViewById(R.id.Call_Pharmacy);
pName.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
goToPDetails();
}
});
pa.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
goToPDetails();
}
});
rDateLabel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
goToPDetails();
}
});
rDate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
goToPDetails();
}
});
rLeftLabel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
goToPDetails();
}
});
rLeft.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
goToPDetails();
}
});
callPhone.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Some Code
}
});
}
All those onClick listeners (those on single sub-views of one ListView element) probably shouldn't be here in the onListItemClick method, but in the getView method of your Adapter instead (with proper use of the convertView argument).
The way you do it seems quite wrong, maybe your onListItemClick method isn't even needed if you correctly implement the various onClick listeners at the right place.
Using an xml based layout for your list item is key here. Set each individually clickable View with two attributes android:clickable="true" and android:onClick="<your click handler>" the method will need to be implemented with this signature: public void <your click handler> (View v) {...} in your Activity. A side note is that you'll have to make a design decision to implement a click handler to overlap handling (one click hanlder for more than one View) or a single view handler per View, the former is best for when click are substantially similar in function and the latter is when they are different.
The next step is to implement the click handler, the key here is to use ListView.getPositionForView(View v) so you can associate the row, the data, and the View clicked.
Don't forget to implement ListActivity.onListItemClick() as a catch-all for clicking on the root layout of the list item and as a catch-all for Views that don't have their own onClick handler set.
The above technique will have good performance and makes use of several Android API's to speed your development.
If you decide to implement the listeners in code, please study getView() closely (as darma mentioned) and for the sake of performance (if you have several items in your list) reuse the click listeners with the above discussion about how to associate the data and row.

Categories

Resources