I build an AlertDialog box with
public class ConstantDialogFragment extends DialogFragment {
private AlertDialog.Builder builder;
private AlertDialog alertDialog;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final int[] constantProtocol = {0};
builder = new AlertDialog.Builder(getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
builder.setTitle(getResources().getString(R.string.some_message))
.setMultiChoiceItems(R.array.some_choice, null,
new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
// some method
}
})
.setPositiveButton(getResources().getString(R.string.ok_dialog), new DialogInterface.OnClickListener() {
class LoadConfigTask {
private ProgressDialog dialog;
private Activity activity;
public LoadConfigTask(Activity activity) {
this.activity = activity;
dialog = new ProgressDialog(activity, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
}
public void execute() {
try {
// some method
} catch (Exception e) {
// some method
}
}
}
#Override
public void onClick(DialogInterface dialog, int id) {
// some method
}
})
.setNegativeButton(getResources().getString(R.string.cancel_dialog), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// some thing
}
});
builder.create();
alertDialog = builder.show();
return alertDialog;
}
public AlertDialog getAlertDialog(){
return alertDialog;
}
Then I tried this espresso implementation:
onView(withText("my first choice")).
perform(click());
and I get
NoMatchingViewException: No views in hierarchy found matching: with text: is my string"
How can I fix that ?
Fetching in the Android source code, I found useful methods. So to get control on the items of the AlertDialog, I had to do that
ListView listView = alertDialog.getListView();
View child = listView.getChildAt(0);
child.performClick();
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've seen other posts but can't make heads nor tails of the following error.
Trying to have a dialog pop up on Recycler View item. I do have a similar dialog that works. I must use a different dialog because of different options in the second instance.
public class EditItemDialog extends AppCompatDialogFragment{
private EditText projectAmountEditText;
private EditDialogListener listener;
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View v = inflater.inflate(R.layout.activity_inventory_item, null);
builder.setView(v)
.setTitle("Edit Amount")
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
int amountChanged = Integer.parseInt(projectAmountEditText.getText().toString());
listener.editAmount(amountChanged);
}
});
projectAmountEditText = v.findViewById(R.id.amount_edit_text_item_activity);
if(projectAmountEditText == null){
//TODO
}
builder.show();
return builder.create();
}
#Override
public void onAttach(#NonNull Context context) {
super.onAttach(context);
try {
listener = (EditDialogListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() +
"must implement EditDialogLister");
}
}
public interface EditDialogListener{
void editAmount(int amountChanged);
}
}
Here is the logcat
Process: com.x.x, PID: 8485
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:5235)
at android.view.ViewGroup.addView(ViewGroup.java:5064)
at android.view.ViewGroup.addView(ViewGroup.java:5036)
I had to add a onDestroy override on the dialog to free the View.
I have made a custom alertdialog that has 2 Edittexts in it. I want to pass information to it from an existing database when clicking something.
this is the Custom alert dialog's class
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.add_job_dialouge, null);
builder.setView(view).setTitle("Add Job")
.setPositiveButton("add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
if(isDouble(wageValue.getText().toString())){
String nameOjob = jobName.getText().toString();
Double valueOwage = Double.parseDouble(wageValue.getText().toString());
listener.applyTexts(nameOjob, valueOwage);
builder.create();
}else{
wageValue.setError("Wrong Format Error");
}
}
})
.setNeutralButton("cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
jobName = view.findViewById(R.id.job_name_ETD);
wageValue = view.findViewById(R.id.wage_ETD);
//I want to pass information to these
//jobName.setText();
//wageValue.setText();
return builder.create();
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
try {
listener =(ExampleDialogueListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + "must implement Example Dialogue Listener");
}
}
public interface ExampleDialogueListener{
void applyTexts(String jobname, Double wage);
}
boolean isDouble(String str) {
try {
Double.parseDouble(str);
return true;
} catch (NumberFormatException e) {
return false;
}
}
}
I want to pass info to the edittext through parameters, so that there's info on the edit text when I create the alertdialog
It looks like you're extending a DialogFragment.
You can pass data in the same as you would a regular Fragment.
Bundle args = new Bundle();
args.putString("arg_key","something");
f.setArguments(args);
Where f is the instance of your DialogFragment.
Inside your DialogFragment you can call getArguments() to get your bundle.
public class QuotesFragment extends Fragment implements QuotesDataHelper.QuotesListener, QuotesAdapter.OnQuoteActionListener {
public static final String TAG = QuotesFragment.class.getSimpleName();
private Views mViews;
private QuotesDataHelper mDataHelper;
private Quote mSelectedQuote;
public QuotesFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment
*
* #return A new instance of fragment QuotesFragment.
*/
public static QuotesFragment newInstance() {
QuotesFragment fragment = new QuotesFragment();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
/**
* Retrieve the request for quotes data from service
*/
private void getQuotesData() {
showProgress(true);
mDataHelper.getQuotesDetails(LobbSharedPreferences.getFromSharedPreferences(getContext(), StringUtils.LOBB_SHARED_PREFERENCE_TRUCK_OWNER_ID));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_quotes_page, container, false);
mViews = new Views(rootView);
mDataHelper = new QuotesDataHelper(this);
getQuotesData();
return rootView;
}
#Override
public void onResume() {
super.onResume();
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onDetach() {
super.onDetach();
}
/**
* Shows the progress UI and hides the dashboard.
*/
private void showProgress(final boolean show) {
mViews.mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
mViews.mQuotesView.setVisibility(show ? View.GONE : View.VISIBLE);
}
private void showNoDataView(String message) {
mViews.mNoDataView.setVisibility(View.VISIBLE);
mViews.mQuotesView.setVisibility(View.GONE);
mViews.mNoDataView.setText(message);
}
private void showToast(String message) {
Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
}
#Override
public void showQuotes(ArrayList<Quote> quotesList) {
showProgress(false);
if (quotesList != null && quotesList.size() > 0) {
mViews.mQuotesView.setHasFixedSize(true);
mViews.mQuotesView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
final LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mViews.mQuotesView.setLayoutManager(layoutManager);
QuotesAdapter mAdapter = new QuotesAdapter(getContext(), quotesList);
mAdapter.setQuoteActionListener(this);
mViews.mQuotesView.setAdapter(mAdapter);
} else {
showNoDataView(getResources().getString(R.string.quotes_nodata));
}
}
#Override
public void showError(String title, String errorMessage) {
showProgress(false);
showNoDataView(errorMessage);
}
#Override
public void onQuoteIgnored() {
showProgress(false);
showToast(getResources().getString(R.string.quote_ignore_success));
//Quote ignored . So reload the data
getQuotesData();
}
#Override
public void onQuoteIgnoreFailed() {
showProgress(false);
if (!((Activity) getContext()).isFinishing()) {
new android.app.AlertDialog.Builder(getContext()).setTitle(getResources().getString(R.string.error_title)).setMessage(getResources().getString(R.string.error_ignore_quote)).setNeutralButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
}
}
#Override
public void onCreateQuoteSuccess() {
showProgress(false);
showToast(getResources().getString(R.string.quote_create_success));
//Quote created. So refresh
getQuotesData();
}
#Override
public void onCreateQuoteFailure() {
showProgress(false);
showToast(getResources().getString(R.string.quote_create_failure));
}
#Override
public void onQuote(Quote quote) {
mSelectedQuote = quote;
showConfirmationAlert();
}
private void showConfirmationAlert() {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setMessage(getResources().getString(R.string.quote_confirm));
builder.setCancelable(true);
builder.setPositiveButton(
getResources().getString(R.string.quote_confirm_yes),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
showProgress(true);
mDataHelper.createQuote(mSelectedQuote);
dialog.cancel();
}
});
builder.setNegativeButton(
getResources().getString(R.string.quote_confirm_no),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog confirmationAlert = builder.create();
confirmationAlert.show();
}
#Override
public void onIgnore(int requestID) {
showProgress(true);
mDataHelper.ignoreQuote(requestID);
}
private class Views {
RecyclerView mQuotesView;
View mProgressView;
TextView mNoDataView;
Views(View root) {
mQuotesView = (RecyclerView) root.findViewById(R.id.view_myquotes);
mProgressView = root.findViewById(R.id.progress_container_quotes);
mNoDataView = (TextView) root.findViewById(R.id.view_quotes_nodata);
}
}
}
[]
private void showConfirmationAlert() {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setMessage(getResources().getString(R.string.quote_confirm));
builder.setCancelable(true);
builder.setPositiveButton(
getResources().getString(R.string.quote_confirm_yes),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
showProgress(true);
mDataHelper.createQuote(mSelectedQuote);
dialog.cancel();
}
});
builder.setNegativeButton(
getResources().getString(R.string.quote_confirm_no),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog confirmationAlert = builder.create();
confirmationAlert.show();
}
you get the mSelectedQuote in onQuote() mth.
Now you can get the amount on mSelectedQuote
Pass it to showConfirmationAlert(amount);
set it to AlertDialog as below -
private void showConfirmationAlert(int amount) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setMessage(getResources().getString(R.string.quote_confirm) + amount);
In my Android application I created a custom DialogFragment with a custom View but I want the Positive and Negative Button with transparent background.
So I saw some SO answers and i did something like this:
private class PersonalInfoDialogFragment extends DialogFragment {
#Override
public void onStart(){
super.onStart();
Button pButton = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_POSITIVE);
Button nButton = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_NEGATIVE);
pButton.setBackgroundColor(getResources().getColor(Color.TRANSPARENT));
nButton.setBackgroundColor(getResources().getColor(Color.TRANSPARENT));
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view=inflater.inflate(R.layout.personalinformation_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.personalinformation)
.setView(view)
.setPositiveButton(R.string.edit, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Some code
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
final AlertDialog dialog = builder.create();
//I also tried this: dialog.getButton(DialogInterface.BUTTON_POSITIVE).setBackgroundColor(Color.TRANSPARENT);
return dialog;
}
}
I don't get why I am facing NullPointerException. Any idea?
public Button pButton ,nButton ;
#Override
public void onStart(){
super.onStart();
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view=inflater.inflate(R.layout.personalinformation_dialog, null);
pButton = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_POSITIVE);
nButton = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_NEGATIVE);
pButton.setBackgroundColor(getResources().getColor(Color.TRANSPARENT));
nButton.setBackgroundColor(getResources().getColor(Color.TRANSPARENT));
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.personalinformation)
.setView(view)
.setPositiveButton(R.string.edit, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Some code
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
final AlertDialog dialog = builder.create();
//I also tried this: dialog.getButton(DialogInterface.BUTTON_POSITIVE).setBackgroundColor(Color.TRANSPARENT);
return dialog;
}
}