Java code optimalization - Two functions into one - java

Hello I have this code:
this.firstBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final CharSequence[] items = {"1", "2", "3"};
AlertDialog.Builder builder = new AlertDialog.Builder(SlovnikoidActivity.this);
builder.setTitle("test");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
langFrom.setText(items[item]);
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
this.secondBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final CharSequence[] items = {"1", "2", "3"};
AlertDialog.Builder builder = new AlertDialog.Builder(SlovnikoidActivity.this);
builder.setTitle("test");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
langFrom.setText(items[item]);
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
There are just two different parts: this.firstBtn and this.secondBtn
Is there a way how to merge it? For example when I click on firstBtn it calls a function foo(firstBtn) and secondBtn calls foo(secondBtn) and the rest would work the same?
I'm not sure about syntax etc. because I'm new to Java and Android development today.
Thanks

What you want can be done relatively easily. You can define a new class that implements OnClickListener, and use a new instance of this class in both locations.
class MyOnClickListener implements OnClickListener {
public void onClick(View v) {
final CharSequence[] items = {"1", "2", "3"};
AlertDialog.Builder builder = new AlertDialog.Builder(SlovnikoidActivity.this);
builder.setTitle("test");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
langFrom.setText(items[item]);
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
Your code can then look like this:
this.firstBtn.setOnClickListener(new MyOnClickListener());
this.secondBtn.setOnClickListener(new MyOnClickListener());
Obviously this solution can be taken further, if your two MyOnClickListener's need to be just slightly different. This can be done by creating a constructor that takes in the parameters that you want to change and store them as member variables, which are then used on the call to onClick(...):
class MyOnClickListener implements OnClickListener {
private CharSequence[] items;
public MyOnClickListener(CharSequence[] _items) {
items = _items;
}
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(SlovnikoidActivity.this);
builder.setTitle("test");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
langFrom.setText(items[item]);
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
And this can then be used like so:
final CharSequence[] items1 = {"1", "2", "3"};
final CharSequence[] items2 = {"2", "3", "4"};
this.firstBtn.setOnClickListener(new MyOnClickListener(items1));
this.secondBtn.setOnClickListener(new MyOnClickListener(items2));
UPDATE
public class SlovnikoidActivity extends Activity {
//slovnikoidActivity definition
//inner class definition for MyOnClickListener
class MyOnClickListener implements OnClickListener {
private CharSequence[] items;
public MyOnClickListener(CharSequence[] _items) {
items = _items;
}
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(SlovnikoidActivity.this);
builder.setTitle("test");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
langFrom.setText(items[item]);
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
}

As an alternative to nicholas.hauschild's answer, if you don't want to make a separate class, just make your activity implement View.OnClickListener.
Then, move your onClick(View v) method to the same class your button code is already in. If you need to distinguish between different buttons ever, then just do switch (v.getId()).

Related

Android: What is the best practice to organize methods to show alert dialog

I am beginner in Android development. Suppose I have some methods to show AlertDialog within an Activity. But each AlertDialog behavior is slightly different. What is the best practice to organize the methods to show AlertDiaolog?
code is like this.
private void showNumberPickerDialog() {
LayoutInflater inflater = this.getLayoutInflater();
View numberPickerDialogView = inflater.inflate(R.layout.number_picker, null);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Title for number picker here");
alertDialog.setCancelable(false);
alertDialog.setView(numberPickerDialogView);
final NumberPicker numberPicker = roomSizeNumberDialogView.findViewById(R.id.number_picker);
numberPicker.setMaxValue(10);
numberPicker.setMinValue(0);
numberPicker.setWrapSelectorWheel(false);
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Something here
}
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}
private void showMessageDialog(final boolean isA) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Title here");
alertDialog.setCancelable(false);
alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (isA) {
doA();
} else {
doB();
}
}
});
alertDialog.show();
}
private void showAlertDialogC() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
final EditText inputEditText = new EditText(this);
inputEditText.setInputType(InputType.TYPE_CLASS_TEXT);
innputEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(10)});
alertDialog.setTitle("Title here");
alertDialog.setCancelable(false);
alertDialog.setView(nameEditText);
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do something here
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}
Is there a good way to organize the parts like this?
You can also use a customizable dialog if that's what you are looking for.
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog_options);
dialog.show();
TextView tvDelete = dialog.findViewById(R.id.tvDelete);
tvDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
Dialog deleteDialog = new Dialog(context);
deleteDialog.setContentView(R.layout.dialog_delete);
deleteDialog.show();
}
});
Treat it just like you would treat an activity. Set onClickListeners on the views for which you want some particular actions. I believe this custom dialog is much more flexible than the AlertDialog
I like to handle my dialog in a separate class, that way you have more control over everything - clickListners, layout design, etc... and you don't have tons of code lines in your activity.
For example, create dialogClass:
public class ProgressDialog extends Dialog {
public ProgressDialog(#NonNull Context context) {
super(context);
setContentView(R.layout.progress_dialog); //this is your layout for the dialog
}
}
And all you need to do is to create dialog instant and call it like this:
ProgressDialog progressDialog = new ProgressDialog(getContext());
progressDialog.show(); // this line shows your dialog

How to not close AlertDialog android

Hi my problem is when I select an item my AlertDialog dismiss
alertDialog = new AlertDialog.Builder(getActivity());
alertDialog
.setSingleChoiceItems(ageArr, 1, btnSelectItem)
.setPositiveButton(R.string.dialog_ok, btnPositiveAgeDialog)
.setNegativeButton(R.string.dialog_cancel, null)
.show();
what my dialog click positive looks is.
private DialogInterface.OnClickListener btnSelectItem = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
selectedIndexAge = which;
}
};
I tried setting the listener to null and it does not close but still
I needed it because I wanted to know which item is selected
Just put it
itemView.setOnClickListener(null);
or
You can use the implementation of hasOnClickListeners() for knowing the status of listener taken from android.view.View class for
public boolean hasOnClickListeners() {
ListenerInfo li = mListenerInfo;
return (li == null && li.mOnClickListener == null);
}
Use the following link for further modifications
Set listener instance in fragment on application restore
try this
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Boolean wantToCloseDialog = false;
//Do stuff, possibly set wantToCloseDialog to true then...
if(wantToCloseDialog)
dialog.dismiss();
//else dialog stays open. Make sure you have an obvious way to close the dialog especially if you set cancellable to false.
}
});
Something else must be closing your AlertDialog. Below is a program that I believe duplicates the minimum requirements you have posted, and selecting one of the items does not close the dialog.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] values = new String[]{ "one", "two", "three", "four" };
DialogInterface.OnClickListener choiceListener = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "selected index: " + which, Toast.LENGTH_SHORT).show();
}
};
DialogInterface.OnClickListener positiveListener = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "positive button", Toast.LENGTH_SHORT).show();
}
};
new AlertDialog.Builder(this)
.setSingleChoiceItems(values, 1, choiceListener)
.setPositiveButton("ok", positiveListener)
.setNegativeButton("cancel", null)
.show();
}
}

Creating ids to implement select listener in dialog

Iam creating a dialog with following code, who creates multiple choice check box.. But I don't know how to create their id's to add click event , I m new to android please help me..:
private void showDailog() {
final String[] items = {" Blue", " Red", " Black", " White", " Pink"};
final ArrayList itemsSelected = new ArrayList();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select any theme you want : ");
builder.setMultiChoiceItems(items, null,
new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int selectedItemId,
boolean isSelected) {
if (isSelected) {
itemsSelected.add(selectedItemId);
} else if (itemsSelected.contains(selectedItemId)) {
itemsSelected.remove(Integer.valueOf(selectedItemId));
}
}
})
.setPositiveButton("Done!", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
//Your logic when OK button is clicked
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id)
}
});
dialog = builder.create();
dialog.show();
}
Instead of Alert Dialog create a simple dialog with your custom layout like this
Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.dialog_lauout);
dialog.show();
Button button = (CheckBox) dialog.findViewById(R.id.button);
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});

Adding and removing textviews from a dynamic list in Android Studio?

I've been struggling with this for a while now, looking for any tips how to proceed. I'm trying to create a fragment that has an add button, that will create a new textview and when clicking the textview , you would have the option to delete it.
Adding textviews works fine. The problem is in the foreach loop that sets up listeners for each textview and adds the option to delete them. It's forcing me to declare the current parameter final, or the deletion won't work. But if I do declare it final, then I can't add any new textviews to the list, so they wont have listeners.
So if anyone could give me some kind of a hint, that would be hugely appreciated.
Here's the list and the adding part.
//Variables
List<TextView> TextViews = new ArrayList<TextView>();
#Override
//Put button functionality here
public void onClick(View v) {
final LinearLayout myLayout = (LinearLayout) view.findViewById(R.id.linearlayout);
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Title");
// Set up the input/
final EditText input = new EditText(getContext());
input.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(input);
// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
notesIndex = new Integer(notesIndex + 1);
noteText = input.getText().toString();
TextView a = new TextView(view.getContext());
a.setText(noteText);
a.setHeight(150);
a.setGravity(Gravity.CENTER);
myLayout.addView(a);
TextViews.add(a);
noteTexts.add(noteText);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
And here is the foreach loop that sets up listeners for each TextView. They are both called in OnCreateView.
for (final TextView current : TextViews) {
current.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Delete?");
// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
myLayout.removeView(current);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
});
}
// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
addViewToLayout();
}
});
void addViewToLayout(){
notesIndex = new Integer(notesIndex + 1);
noteText = input.getText().toString();
TextView a = new TextView(view.getContext());
a.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showAlertToDelete(v)
}
});
a.setText(noteText);
a.setHeight(150);
a.setGravity(Gravity.CENTER);
myLayout.addView(a);
TextViews.add(a);
noteTexts.add(noteText);
}
void showAlertToDelete(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Delete?");
// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
myLayout.removeView(v);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}

DialogInterface.OnClickListener listener variable cannot be resolved [java]

I am building an alert dialog with a selection list. I can not figure out why my variable for the OnClickListener cannot resolve.
I have put the code in a separate activity by itself and it works but inside my main activity it does not.
public void categoryDialogShow(final Context context, String[] categoryOptions){
final AlertDialog actions;
AlertDialog.Builder categoryAlert = new AlertDialog.Builder(context);
categoryAlert.setTitle("Choose a Category");
categoryAlert.setItems(categoryOptions, actionListener);
//=============================================================
//==============actionListener cannot be resolved to a variable
//=============================================================
categoryAlert.setNegativeButton("Cancel", null);
actions = categoryAlert.create();
DialogInterface.OnClickListener actionListener = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//goto category list with which as the category
}
};
actions.show();
}
This is the activity that works with no problems:
public class MainActivity extends ActionBarActivity {
AlertDialog actions;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Activity");
Button button = new Button(this);
button.setText("Click for Options");
button.setOnClickListener(buttonListener);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose an Option");
String[] options = { "A", "B", "C" };
builder.setItems(options, actionListener);
builder.setNegativeButton("Cancel", null);
actions = builder.create();
setContentView(button);
}
DialogInterface.OnClickListener actionListener = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0: // Delete
break;
case 1: // Copy
break;
case 2: // Edit
break;
default:
break;
}
}
};
View.OnClickListener buttonListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
actions.show();
}
};
}
Just move the declaration above where you need to reference it:
public void categoryDialogShow(final Context context, String[] categoryOptions){
final AlertDialog actions;
DialogInterface.OnClickListener actionListener = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//goto category list with which as the category
}
};
AlertDialog.Builder categoryAlert = new AlertDialog.Builder(context);
categoryAlert.setTitle("Choose a Category");
categoryAlert.setItems(categoryOptions, actionListener);
categoryAlert.setNegativeButton("Cancel", null);
actions = categoryAlert.create();
actions.show();
}
The reason it works in the other class, is that the actionListener reference is a class member variable, just as I suspected.
Notice that the declaration is not inside any method. You could do the same with your new Activity:
DialogInterface.OnClickListener actionListener = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//goto category list with which as the category
}
};
public void categoryDialogShow(final Context context, String[] categoryOptions){
final AlertDialog actions;
AlertDialog.Builder categoryAlert = new AlertDialog.Builder(context);
categoryAlert.setTitle("Choose a Category");
categoryAlert.setItems(categoryOptions, actionListener);
categoryAlert.setNegativeButton("Cancel", null);
actions = categoryAlert.create();
actions.show();
}
Either declare
DialogInterface.OnClickListener actionListener = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//goto category list with which as the category
}
};
before
categoryAlert.setItems(categoryOptions, actionListener);
or declare it outside your method.

Categories