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());
}
});
Related
I have created and been able to label items that have been added to a scroll view. I want to find a way to save and store the items created so that when the user clicks off the page they still remain there.
I wanted to do this through the use of sharedprefs however I am not sure how and where the best place would be to add code. Would appreciate any suggestions.
public class Editscreen extends AppCompatActivity {
// https://codevedanam.blogspot.com/2021/04/dynamic-views-in-android.html
Button add;
AlertDialog dialog;
LinearLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_editscreen);
add = findViewById(R.id.button7);
layout = findViewById(R.id.container);
buildDialog();
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.show();
}
});
}
private void buildDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
View view = getLayoutInflater().inflate(R.layout.dialog,null);
EditText name = view.findViewById(R.id.nameEdit);
name.getText().clear();
builder.setView(view);
builder.setTitle("Enter Quiz name");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
addCard(name.getText().toString());
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog = builder.create();
}
private void addCard(String name) {
View view = getLayoutInflater().inflate(R.layout.cardlayout,null);
TextView nameView = view.findViewById(R.id.name);
Button delete = view.findViewById(R.id.deletequiz);
nameView.setText(name);
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
layout.removeView(view);
}
});
layout.addView(view);
}
I believe you can add this file to your project and you can call this static functions to save your data to pref. This is the compact way.
public class PrefUtil {
private static SharedPreferences getPrefs(Context context) {
return context.getSharedPreferences(context.getPackageName() + "_preferences",Context.MODE_PRIVATE);
}
public static void setStringPref(Context context,String key,String deger){
getPrefs(context).edit().putString(key,deger).apply();
}
public static String getStringPref(Context context,String key){
return getPrefs(context).getString(key,"");
}
public static void setIntPref(Context context,String key,int deger){
getPrefs(context).edit().putInt(key,deger).apply();
}
public static int getIntPref(Context context,String key){
return getPrefs(context).getInt(key, -1);
}
}
I am trying to display a dialog box when a particular item is clicked, but I can only see a little box in place of the layout I have set. here is the onbindview holder.
#Override
public void onBindViewHolder(ListEventAdapter.ListEventViewHolder holder, int position) {
EventResponse event = eventList.get(position);
view to be clicked
holder.home_view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showAlertDialog(view);
}
});
private void showAlertDialog(View view) {
Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.alert_dialog);
home_t = dialog.findViewById(R.id.home_t);
place_btn = (CircularProgressButton) dialog.findViewById(R.id.place_bet_btn);
close = dialog.findViewById(R.id.close);
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.cancel();
}
});
place_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
on R.layout.alert_dialog just use match_parent in layout_width
I have a Dialog class where I have kept my dialogs. Now the problem is that I want to get the View click listeners of my dialog back in my activity. I know this can be done by writing an interface but is there any other OOP way of doing it?
My Dialog class:
public class Dialogs{
public void testCompletionDialog() {
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(true);
dialog.setContentView(R.layout.test_complete_dialog);
dialog.setTitle("Ratta provet?");
dialog.findViewById(R.id.lesson_btn_marker).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//I want my activity to know that this view is clicked.
dialog.dismiss();
}
});
dialog.findViewById(R.id.lesson_btn_ratta).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//I want my activity to know that this view is clicked.
}
});
dialog.show();
}
}
My Activity:
if (areQueOver) {
Dialogs dialogs=new Dialogs(TestActivity.this);
dialogs.testCompletionDialog();
}
You may use it using EventBus
Inside your onClick in your Dialog class post an event telling that a dialog has been clicked. The event may contain a string variable telling which dialog is clicked.
Inside your Activity subscribe to and handle the event. You may check the String variable value to know which dialog was clicked.
Modify your Dialogs class as below:
public class Dialogs{
public void testCompletionDialog() {
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(true);
dialog.setContentView(R.layout.test_complete_dialog);
dialog.setTitle("Ratta provet?");
dialog.findViewById(R.id.lesson_btn_marker).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EventBus.getDefault().post("btn_marker");
dialog.dismiss();
}
});
dialog.findViewById(R.id.lesson_btn_ratta).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EventBus.getDefault().post("btn_ratta");
}
});
dialog.show();
Inside your Activity:
#Subscribe(threadMode = ThreadMode.MAIN) public void onEvent(String action){
if(action.equals("btn_ratta")){
} else if(action.equals("btn_marker")) {
}
}
inside onCreate add this-
EventBus.getDefault().register(this);
inside onDestroy add this-
EventBus.getDefault().unregister(this);
Alternative method:
Well, other than interface and EventBus, you may add a public method to your Activity say,
onDialogClicked(String dialogName){//TODO handle the click as per dialogName}
and then call this method from your onClick in your Dialogs class.
Create an interface.
public interface OnDialogConfirmClickListener {
void onDialogConfirmClick(Class parameter//or empty);
}
Implement this interface to your activity.
public class MainActivity extends Activity implements OnDialogConfirmClickListener {
...
}
Send interface as parameter to Dialogs or testCompletionDialog method.
public void testCompletionDialog(OnDialogConfirmClickListener listener) {
...
dialog.findViewById(R.id.lesson_btn_marker).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onDialogConfirmClick(parameter//or empty);
dialog.dismiss();
}
});
...
}
use listner for call buttons like this
Simpledialoginterface listner = new Simpledialoginterface() {
#Override
public void ok_button() {
//ok button click
}
#Override
public void cancel_button() {
//cancel button click
}
};
use this dialog
public static void popupnew(String tittle, String message, String Button, String Cancel,
final Activity context, final Simpledialoginterface listner) {
if (!((Activity) context).isFinishing()) {
android.app.AlertDialog.Builder alertDialog;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
alertDialog = new android.app.AlertDialog.Builder(context, android.R.style.Theme_Material_Light_Dialog_Alert);
} else {
alertDialog = new android.app.AlertDialog.Builder(context);
}
alertDialog.setTitle(tittle);
alertDialog.setMessage(message);
alertDialog.setPositiveButton(Button,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
listner.ok_button();
}
});
alertDialog.setNegativeButton(Cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
listner.cancel_button();
}
});
alertDialog.show();
}
}
//interface class
public interface Simpledialoginterface {
public void ok_button();
public void cancel_button();
}
popupnew("title","message","OK","Cancel",this,listner);//call dialog
Yes if you want to call any method of Actvity then you can call through context of Activity :
suppose method1() is under Activity and you want to call from Dailog then you can call through .
((MyActivity)((Activity)context)).method1();
Hello guys need some help i created a custom alertdialog which takes user input like username and password. i followed the android developers site What i want to do is once the user enters the username and password and press the sign in button in alertdialog i want to show these values to the activity which created the dialog. i am stuck on this wasted 3 hours on this. Any help would be much appreciated. This is my code.
MainActivity.java
public class MainActivity extends FragmentActivity implements NoticeDialogFragment.NoticeDialogListener{
private Button dialogButton;
private Button customDialogButton;
private TextView customDialogTextview;
private Button dialogWithInterface;
private EditText dialogUsername;
private EditText dialogPassword;
String username;
String password;
public void showNoticeDialog() {
// Create an instance of the dialog fragment and show it
DialogFragment dialog = new NoticeDialogFragment();
dialog.show(getFragmentManager(), "NoticeDialogFragment");
}
// The dialog fragment receives a reference to this Activity through the
// Fragment.onAttach() callback, which it uses to call the following methods
// defined by the NoticeDialogFragment.NoticeDialogListener interface
#Override
public void onDialogPositiveClick(DialogFragment dialog) {
// User touched the dialog's positive button
}
#Override
public void onDialogNegativeClick(DialogFragment dialog) {
// User touched the dialog's negative button
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
customDialogTextview = (TextView)findViewById(R.id.customdialogtext);
customDialogTextview.setText("Email and Password: ");
dialogButton = (Button)findViewById(R.id.dialogbutton);
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// 1. Instantiate an AlertDialog.Builder with its constructor
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
// Add the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// User clicked OK button
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// User cancelled the dialog
}
});
// 2. Chain together various setter methods to set the dialog characteristics
builder.setMessage("hello")
.setTitle("Dialog");
// 3. Get the AlertDialog from create()
AlertDialog dialog = builder.create();
dialog.show();
}
});
customDialogButton = (Button)findViewById(R.id.customdialogbutton);
customDialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
// Get the layout inflater
LayoutInflater inflater = MainActivity.this.getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialog, null));
builder.setPositiveButton("Sign In", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//sign in the user
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
dialogWithInterface = (Button)findViewById(R.id.dialogwithinterface);
dialogWithInterface.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showNoticeDialog();
}
});
}
NoticeDialogFragment.java
public class NoticeDialogFragment extends DialogFragment {
/* The activity that creates an instance of this dialog fragment must
* implement this interface in order to receive event callbacks.
* Each method passes the DialogFragment in case the host needs to query it. */
public interface NoticeDialogListener {
public void onDialogPositiveClick(DialogFragment dialog);
public void onDialogNegativeClick(DialogFragment dialog);
}
// Use this instance of the interface to deliver action events
NoticeDialogListener mListener;
// Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Verify that the host activity implements the callback interface
try {
// Instantiate the NoticeDialogListener so we can send events to the host
mListener = (NoticeDialogListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString()
+ " must implement NoticeDialogListener");
}
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Build the dialog and set up the button click handlers
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.dialog, null));
builder.setPositiveButton("Sign In", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Send the positive button event back to the host activity
mListener.onDialogPositiveClick(NoticeDialogFragment.this);
Toast.makeText(getActivity(), "positive", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Send the negative button event back to the host activity
mListener.onDialogNegativeClick(NoticeDialogFragment.this);
}
});
return builder.create();
}
}
Look at this method your activity implements:
#Override
public void onDialogPositiveClick(DialogFragment dialog) {
// User touched the dialog's positive button
}
It is apart of the custom interface you created in the dialog called NoticeDialogListener and is the way you want the dialog to communicate with the activity that called it.
public interface NoticeDialogListener {
public void onDialogPositiveClick(DialogFragment dialog);
public void onDialogNegativeClick(DialogFragment dialog);
}
Change this so that onDialogPostiveClick looks something like:
public void onDialogPositiveClick(String name, String password);
and pass the values from your EditText into the call when the button is clicked like so:
builder.setPositiveButton("Sign In", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Send the positive button event back to the host activity
mListener.onDialogPositiveClick(mNameEdit.getText().toString(), mPasswordEdit.getText().toString());
}
});
The next step would be to do whatever you wanted to do with the name and password values in your method you override in your activity for the onDialogPositiveClick() method.
#Override
public void onDialogPositiveClick(String name, String password) {
//do something with name and password here?
}
This seems like the easiest way to do what it is you want to do with your already existing code.
Whenever I debug the application it crashes when I click the button that pops up the dialog.
What should I do to make that dialog work?
public class Actionbar_BtnHandler extends Activity {
Context context;
public Actionbar_BtnHandler (Context context)
{
this.context=context;
}
public void btn_handler (Button btn_mic,Button btn_post)
{
btn_mic.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context,"MICROPHONE",Toast.LENGTH_LONG).show();
}
});
btn_post.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
// Get the layout inflater
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialog_signin, null))
// Add action buttons
.setPositiveButton("Post", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// sign in the user ...
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
builder.create();
}
});
}
}
Thanks in advance...
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
is inaccurate... The cast seems to fail
Try :
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
// Get the layout inflater
LayoutInflater inflater = Actionbar_BtnHandler.this.getLayoutInflater();
Try this write your AlertDialog code using Handler..
btn_post.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Handler handler = new Handler();
handler.post(MessagealertDisplay);
}
});
}
Runnable MessagealertDisplay = new Runnable() {
#Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
// Get the layout inflater
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialog_signin, null))
// Add action buttons
.setPositiveButton("Post", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// sign in the user ...
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
builder.create();
}
};
i used
Actionbar_BtnHandler btns = new Actionbar_BtnHandle(Class.this);
btns.btn_handler(btn_post, btn_mic);
instead of
Actionbar_BtnHandler btns = new Actionbar_BtnHandler(getApplicationContext);
btns.btn_handler(btn_post, btn_mic);
and it worked