Placement of Dialog box in android studio main activity of Android - java

I want to delete the recycler view item by long press methode and on long press , a dialog box should appear with yes and no. As i click on yes, the item should be delete.. My code for dialog box...
public static class DialogBox extends DialogFragment {
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
builder.setTitle("Do you want to delete it?").setMessage("Confirmation Dialog")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
return builder.create();
}
}
}
My code for delete button of dialog box
public void onDeleteItem(int position) {
DialogBox dialogBox=new DialogBox();
dialogBox.show(getSupportFragmentManager(),"Example Dialog");
removeItem(position);
}
});
Here what should I do that I can easily remove the item by placing the code at right position

You can use this Alternative.
In this scenario You can long press the item and you will get the delete option if you press delete thn it will delete the item else you can press out side of that item to cancel.
#Override
public void onBindViewHolder(EmployeeListAdapter.ViewHolder holder, final int position) {
holder.cardClick.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
#Override
public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
MenuItem delete = contextMenu.add(0, position, 0, "Delete");
delete.setOnMenuItemClickListener(onDeleteMenu);
}
});
}
/*
* Delete Menu
* */
private final MenuItem.OnMenuItemClickListener onDeleteMenu = new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
deleteTaskApi(IdList.get(item.getItemId() ));
return true;
}
};

Related

How do I make my AlertDialog appear as soon as EditText has focus?

I want my AlertDialog to pop up as soon as my EditText has focus. Right now, I have to click on my EditText twice.
The first time I click my EditText, the soft keypad appears (for typing directly in my Edittext). Then, the second time I click the EditText, the AlertDialog appears.
How can I have it so only one click on the EditText is needed for the AlertDialog to pop up?
Here's my code:
In my onCreate...
//when user clicks on "commentName" EditText we want a new AlertDialog to open
commentName.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(NewContact.this);
builder.setTitle("Ur Comment:");
//start the following xml file/ layout
View viewInflated = LayoutInflater.from(NewContact.this).inflate(R.layout.comment_text_pop_up, null, false);
builder.setView(viewInflated);
// Set up the buttons
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
});
I got it to work according to your description with an OnFocusChangeListener.
Just replace the entire snippet from your question with the following. And paste in the AlertDialog code below the comment.
// commentName is your EditText
commentName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
return; // Prevents alert from being shown when losing focus.
}
// Your AlertDialog code goes here
}
});
The if (!hasFocus) prevents the alert from being shown when the user clicks something else that makes the EditText lose focus.
This did it for me, OnTouchListener:
//when user clicks on "commentname" edittext we want a new textbox to open
commentname.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
AlertDialog.Builder builder = new AlertDialog.Builder(NewContact.this);
builder.setTitle("Ur Comment:");
//start the following xml file/ layout
View viewInflated = LayoutInflater.from(NewContact.this).inflate(R.layout.comment_text_pop_up, null, false);
builder.setView(viewInflated);
// Set up the buttons
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
return true;
}
return false;
}
});
Another way you could do this with least code is by simply disabling focus on the EditText. You can do that by adding below attribute to your EditText...
<EditText
...
android:clickable="true"
android:focusable="false"/>
Then set OnClickListener on your EditText, and show the dialog in onClick() method.

Open two dialog together

I have two dialog.i open dialog one and click one button in adapter listview in dialog, and i want open another dialog to ask question for are you sure delet this item or no.
But just i can open one of them at a time.
What can i do?
viewHolder.btnDelet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new DialogDelet(context).setOnListener(new ListenerDelet() {
#Override
public void onSuccess() {
listenerAdapterUserAndRole.unSelect(item);
}
#Override
public void onCancel() {
}
}).Show();
}
});
You can cancel first dialog when user is confirm or you want to open next dialog
like:
public void open(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("Are you sure,
You wanted to make decision");
alertDialogBuilder.setPositiveButton("yes",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this,"You clicked yes
button",Toast.LENGTH_LONG).show();
//cancel first dialog here
firstDialogObj.cancel;
// do your stuff contnious what you want
}
});
alertDialogBuilder.setNegativeButton("No",new DialogInterface.OnClickListener() {
Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}

How to call listview with confirmation dialog?

I want to show confirmation dialog box to user that when user click on yes button open listview to select.
Use this link..............................
http://rajeshvijayakumar.blogspot.in/2013/04/alert-dialog-dialog-with-item-list.html
You can go like this
Listview.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
build = new AlertDialog.Builder(YourActivity.this);
build.setTitle("Title name");
build.setMessage("?");
build.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
//IF yes button pressed then write here
}
dialog.cancel();
}
});
build.setNegativeButton("No", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
dialog.cancel();
}
});
AlertDialog alert = build.create();
alert.show();
return true;
}
});

How to open a DialogFragment by clicking on a TextView

I have a MainActivity like this one below:
My question is how to open a DialogFragment clicking on the TextView "click HERE to give a name to the task" placed next to "play" button.
Here is the code of my TextView:
TextView buttonView = new TextView(this);
buttonView.setHint("click HERE to give a name to the task");
buttonView.setX(50);
buttonView.setY(50);
and the code of the DialogFragent:
public class ButtonNameDialogFragment extends DialogFragment {
private IFragment iButNamFrag;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder setButNameAlert = new AlertDialog.Builder(getActivity());
setButNameAlert.setTitle("Set Task name");
LayoutInflater inflater = getActivity().getLayoutInflater();
setButNameAlert.setView(inflater.inflate(R.layout.button_name_fragment, null))
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Implement dialogPositiveClick
}
})
.setNegativeButton(R.string.undo, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Implement dialogNegativeClick
}
});
return setButNameAlert.create();
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
iButNamFrag = (IFragment) activity;
}
}
and here is the interface:
public interface IFragment {
public void onDialogPositiveClick(DialogFragment dialog);
public void onDialogNegativeClick(DialogFragment dialog);
}
You can set an onClickListener to any view in Android and then perform any behavior you would like
buttonView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Create new DiaglogFragment and display it
}
};
This is the same method used for any kind of button pressing. There are plenty of other answers already out on StackOverflow with further examples of this. If you need more information on tap recognition or displaying fragments, a quick search will find it on Stack.
buttonView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
DialogFragment frag = new ButtonNameDialogFragment();
frag.show(*context*, ButtonNameDialogFragment.class.getCanonicalName());
}
});

Creating DialogFragment with EditText

I'm new to android, started it about a month ago, and now I'm trying to make a "Shopping List" app for the sake of practice. In this app I have a ListView, where user can insert items via EditText above that ListView. When user longClick on item, ContextMenu with "Edit", "Delete" and "Mark" fields appears. I have already made "Delete" button work, but I still have problems with "Edit" function. To make this function work I created DialogFragment class, so when user presses the "Edit" button, this DialogFragment appears. This DF has EditText field, where we enter data we want to change. Here is DialogFragment class code:
public class AlertEdit extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder bd = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
bd.setView(inflater.inflate(R.layout.alert, null))
.setTitle("Edit")
.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
((MyActivity)getActivity()).doPositiveClick();
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
((MyActivity)getActivity()).doNegativeClick();
}
});
return bd.create();
}
as you can see, we have positive button here, which calls doPositiveClick method from MyActivity, which appears to be the main activity.
.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
((MyActivity)getActivity()).doPositiveClick();
}
So, here is the MyActivity class code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
lw = (ListView) findViewById(R.id.listView);
edtxt = (EditText) findViewById(R.id.editText);
alertEd = (EditText) findViewById(R.id.alertEdit);
goods = new ArrayList<String>();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, goods);
lw.setAdapter(adapter);
lw.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View v,
int position, long id) {
}
});
registerForContextMenu(lw);
edtxt.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction()== KeyEvent.ACTION_DOWN) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
goods.add(0, edtxt.getText().toString());
adapter.notifyDataSetChanged();
edtxt.setText("");
return true;
}
}
return false;
}
});
}
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo info){
super.onCreateContextMenu(menu, v, info);
getMenuInflater().inflate(R.menu.actions, menu);
}
public boolean onContextItemSelected(MenuItem item) {
position = (int) info.id;
switch (item.getItemId()) {
case R.id.cnt_mnu_delete:
goods.remove(position);
adapter.notifyDataSetChanged();
return true;
case R.id.cnt_mnu_edit:
}
return super.onContextItemSelected(item);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void doPositiveClick()
{
}
public void doNegativeClick()
{
}
public void showDialog()
{
DialogFragment frag = new AlertEdit();
frag.show(getFragmentManager(), "edit");
}
}
My problem is that I have no idea how to create that Edit function. I tryied to use AdapterContextMenuInfo, but it works only in onContextItemSelected method, because it requires and Item to work with. Hope you help me and sorry for the possible lack of information, ask me any additional questions please.
P.S. I'm trying to make this dialog for almost two weeks already and I'm really frustrated because of that.
I'm using this method - it's simple and you may adapt it to your needs:
First of all make an interface to handle your result, for example:
public interface OnDialogResultListener {
public void onDialogResult(String result);
}
Then use your dialog with additional view, like this:
public void showDialogAndGetResult(final int title, final String message, final String initialText, final OnDialogResultListener listener) {
// additional View - use appropriate View to your needs:
final EditText editText = new EditText(this);
editText.setText(initialText);
new AlertDialog.Builder(MainActivity.this)//
.setTitle(title)//
.setMessage(message)//
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (listener != null) {
listener.onDialogResult(editText.getText().toString());
}
}
})//
.setNegativeButton(android.R.string.cancel, null)//
.setView(editText)//
.show();
}
At last implement this interface in your activity:
public class YourActivity Extends Activity implements OnDialogResultListener{
...
#Override
public void onDialogResult(String result) {
//do what you need
}
...
}
Edit:
You may replace EditText by any View, including Layouts.
Still you may use the same scheme to return result from your DialogFragment descendant - just pass OnDialogResultListener in constructor or initializing method. I would say AlertDialog is more lightweight and DialogFragment allows more control and you may use both according to your needs.

Categories

Resources