I have a tyny, but annoying problem with the AlertDialog Builder.
I want to handle an item select in a custom made AlertDialog, but OnItemSelectedListener doesn't seem to pick my clicks.
Custom Dialog:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="120dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:background="#AAFFFFFF"
android:orientation="vertical">
<TextView
android:text="English"
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="#+id/lang_english"
android:gravity="center_vertical"
android:background="#color/borders"
android:paddingLeft="10dp"
android:onClick="onLanguageButtonClicked" />
<View
android:layout_height="1dp"
android:layout_width="match_parent"
android:background="#000000" />
<TextView
android:text="عربى"
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="#+id/lang_arabic"
android:gravity="center_vertical"
android:background="#color/borders"
android:paddingRight="10dp"
android:onClick="onLanguageButtonClicked" />
<View
android:layout_height="1dp"
android:layout_width="match_parent"
android:background="#2e2e2e"/>
<TextView
android:text="کوردی "
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="#+id/lang_kurdish"
android:gravity="center_vertical"
android:background="#color/borders"
android:paddingRight="10dp"
android:onClick="onLanguageButtonClicked" />
</LinearLayout>
On button click I open the dialog and handle it:
public void onClickDrawer(View view) {
switch (view.getId()) {
case R.id.button_account_language:
handleLanguageDialog();
break;
case R.id.button_account_currency:
handleCurrencyDialog();
break;
default:
break;
}
}
And the handler:
private void handleLanguageDialog() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(R.layout.dialog_language);
builder.setOnItemSelectedListener(new AdapterView.
OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
Log.i("info", "pressed" + Integer.toString(position));
restartInLocale(new Locale(preferences.getString("locale","")));
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
builder.setNegativeButton("CANCEL", new DialogInterface
.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.create().show();
}
Is this the correct approach or am I missing something?
Thank you in advance!
I don't this is correct Approach, you are making it very much complex. If I am not wrong then you are trying to show Language selection dialog for that you should follow the below given steps.
Create string-array inside strings.xml resource file
<string-array name="lang">
<item>English</item>
<item>عربى</item>
</string-array>
When you want to show alert dialog write down below code
private void handleLanguageDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.pick_color)
.setItems(R.array.lang, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
switch(which)
{
case 0:// English
break;
case 1:// عربى
break;
}
}
});
return builder.create();
}
This is the simplest way to show list of languages in alert dialog and without any positive or negative button .
It looks a bit off. It should look like this:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().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(R.string.signin, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// sign in the user ...
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
LoginDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}
https://developer.android.com/guide/topics/ui/dialogs.html
private void handleLanguageDialog() {
ArrayList<String> listItems = new ArrayList<>();
listItems.add("English");
listItems.add("Arabic");
new AlertDialog.Builder(this)
.setTitle("title")
.setCancelable(false)
.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems),
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
}
}).setPositiveButton("OK", 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) {
}
}).show();
}
Try to use setOnItemClick listener on the list/spinner view whichever you are using to display the list instead of builder itself. Like if you are using spinner to show the list use-
final Spinner spinner = (Spinner) layout.findViewById(R.id.spinner); spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3){
int item = spinner.getSelectedItemPosition();
commandWriter(item);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
Try this: add click listener to the language item in your Dialog
onItemSelected() is applied for listview or spinner items your Dialog layout dialog_language does not have those.
Try this code:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Get the layout inflater
LayoutInflater inflater =getLayoutInflater();
View myview = inflater.inflate(R.layout.dialog_language, null);
// Inflate and set the layout for the dialog
builder.setView(myview);
TextView english = (TextView) myview.findViewById (R.id.lang_english);
english.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//do some stuff
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Log.i("info", "pressed" + Integer.toString(position));
restartInLocale(new Locale(preferences.getString("locale","")));
}
});
TextView arabic = (TextView) myview.findViewById (R.id.lang_arabic);
arabic.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//do other stuff
}
});
// Add action buttons
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.show();
Related
My goal is to access a seekbar. I tried many things, even pulling the Seekbar seekingbar before the override, but I get always the null toast. R.layout.firstLayout, R.layout.secondLayout, R.id.seekingbar, they all exist.
This is my abbreviated code
public class MainActivity extends AppCompatActivity {
SeekBar seekingbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekingbar = (SeekBar) findViewById(R.id.seekingbar);
LayoutInflater inflater = getLayoutInflater();
final View firstLayout = inflater.inflate(R.layout.firstLayout, null);
final View secondLayout = inflater.inflate(R.layout.secondLayout, null);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
.setPositiveButton("Forward", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
AlertDialog dialog2 = new AlertDialog.Builder(MainActivity.this)
.setView(secondLayout)
.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
if (seekingbar == null)
{
Toast.makeText(MainActivity.this,"Null",Toast.LENGTH_LONG).show();
}
}
}).create();
dialog2.show();
}
}).create();
dialog.show();
}
});
}
}
firstLayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/seekbarll"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
tools:context=".MainActivity">
<SeekBar
android:id="#+id/seekingbar"
style="#style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="10"
android:min="0"
android:progress="3" />
</LinearLayout>
Try to find the seekBar in the firstLayout after declaring it:
"firstLayout.findViewById(R.id.seekbar)"
I tried this method and inflate home.xml in my HomeFragment.java:
public View onCreateView(#NonNull LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(R.layout.home, container, false);
return root;
}
public void OnClickOnTransit(View v){
final Button n = (Button) v;
final String id = n.getTag().toString();
AlertDialog.Builder builderSingle = new AlertDialog.Builder(getContext());
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1);
arrayAdapter.add("Tag as On Transit");
arrayAdapter.add("Cancel");
builderSingle.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
String strOption = arrayAdapter.getItem(which);
if(strOption.equalsIgnoreCase("Tag as On Transit"))
{
Tag_as_on_transit(id);
}
else if(strOption.equalsIgnoreCase("Cancel"))
{
//confirmViewLeveling(emp_tag[1],emp_name.getText().toString());
}
else
{
}
}
});
builderSingle.show();
}
Here is my home.xml where i put my listview.
<ListView
android:id="#+id/lv_customer"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
And this is my nested_listview.xml where my button was placed.
<Button
android:id="#+id/btn_ontransit"
android:layout_width="40dip"
android:layout_height="40dip"
android:background = "#drawable/ic_local_shipping_orange_24dp"
android:textColor="#ff4500"
android:textStyle="bold"
android:text=""
android:textSize="25sp"
android:onClick="OnClickOnTransit"/>
What i want is to call OnClickOnTransit from nested_listview.xml. Thanks in advance!
In that case your parentActivity must have this method
public void OnClickOnTransit(View v){
}
However if you don't want that, You can implement like this in your fragment's onViewcreated method.
Button btn_conferma = (Button) view.findViewById(R.id.btn_conferma);
btn_conferma.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// do something
}
});
I'm using AlertDialog Box with EditText where user input his name. I would like to prevent the alertDialog from closing when EditText is Empty.
Below is my code
private void request_user_name() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Enter name:");
final EditText input_field = new EditText(this);
input_field.setText(sharedpreferences.getString("username",""));
final SharedPreferences.Editor editor = sharedpreferences.edit();
builder.setCancelable(false);
builder.setView(input_field);
String savedName = sharedpreferences.getString(username,"");
input_field.setText(savedName);
input_field.setSelection(input_field.getText().length());
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
name = input_field.getText().toString();
editor.putString(username, name);
editor.apply();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
request_user_name();
}
});
builder.show();
}
I tried to insert
if(TextUtils.isEmpty(name)) {
input_field.setError("Your message");
return;
}
Inside the AlertDialog Box like this, but it didn't work
private void request_user_name() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Enter name:");
final EditText input_field = new EditText(this);
input_field.setText(sharedpreferences.getString("username",""));
final SharedPreferences.Editor editor = sharedpreferences.edit();
builder.setCancelable(false);
builder.setView(input_field);
String savedName = sharedpreferences.getString(username,"");
input_field.setText(savedName);
input_field.setSelection(input_field.getText().length());
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
name = input_field.getText().toString();
if(TextUtils.isEmpty(name)) {
input_field.setError("Your message");
return;
}
editor.putString(username, name);
editor.apply();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
request_user_name();
}
});
builder.show();
}
Below is the image file, I have
and the image what I would like to get
Thanks in advance.
Use setCancelable(false).
Just set this
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Enter name:");
builder.setCancelable(false);
You see the AlertDialog use a default click listener which after forwarding the click call dismiss by default.
In your case you have to override the default click listener from dialog itself to define your custom behavior for dialog:
//Create the AlertDialog with a reference to edit it later
final AlertDialog dialog = new AlertDialog.Builder(context)
.setView(v)
.setTitle(R.string.my_title)
.setCancelable(false)
.setPositiveButton(android.R.string.ok, null) //Set to null. We override the onclick
.setNegativeButton(android.R.string.cancel, null)
.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialogInterface) {
Button button = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Check if your condition is met, Dismiss once everything is OK.
dialog.dismiss();
}
});
}
});
dialog.show();
Try this sample i made. First you have to create a layout for your dialog, just customize it according to your needs.
//R.layout.new_message_dialog
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/checker_txt_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/pin_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send message to?"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<EditText
android:id="#+id/pin_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="Name"
android:inputType="text"
android:padding="10dp"
android:layout_marginTop="10dp"
android:cursorVisible="false"
android:singleLine="true" />
<TextView
android:id="#+id/pin_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:padding="10dp"
android:text="Confirm"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/colorAccent" />
</LinearLayout>
Then inflate the view and set it to your builder.
View view = getLayoutInflater().inflate(R.layout.new_message_dialog, null, false);
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setView(view);
TextView tvConfirmBtn = view.findViewById(R.id.pin_btn);
final EditText txtName = view.findViewById(R.id.pin_txt);
final AlertDialog dialog = builder.create();
dialog.show();
tvConfirmBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String name = txtName.getText().toString().trim();
if (name.isEmpty()) {
txtName.setError("Please provide a name.");
txtName.requestFocus();
} else {
//do something here
dialog.dismiss();
}
}
});
I have got this Alert Dialog which has these two buttons (Ok and Cancel). I want to know how I go about implementing it.
So When you click on the cancel button it should close the alert dialog and return back to the fragment I am currently on. And if I click on the Ok button it should replace the current alert dialog and place it with another one.
this is my code below for the confimration. java file;
public class confirmation extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inf = getActivity().getLayoutInflater();
final View theDIalog = inf.inflate(R.layout.example_xml, null);
builder.setView(theDIalog);
AlertDialog dialog = builder.create();
theDIalog.findViewById(R.id.makeaTransferOk).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Okay button is clicked", Toast.LENGTH_SHORT).show();
}
});
theDIalog.findViewById(R.id.makeaTransferCancel).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
return dialog;
}
}
this is my code for the example_xml;
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffc0c0c0">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="#+id/makeaTransferCancel"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:id="#+id/makeaTransferOk"/>
</RelativeLayout>
Please could someone help me
Try this code for the functionality you have mentioned above:
AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
builder1.setMessage("Write your message here.");
builder1.setCancelable(true);
builder1.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//put your code that needed to be executed when okay is clicked
dialog.cancel();
}
});
builder1.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage("your message ");
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss(); //<-- change it with ur code
}
} );
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
} );
alertDialog.show();
builder.setPositiveButton(text, new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}).setNegativeButton(text, new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}).create();
I've looked at a lot of similar questions and can't seem to get anything to work. I have a main class with a function like this that edits shows a dialog box then edits a List when a button is pressed.
public class EditPlayers extends SherlockFragmentActivity {
listPlayerNames.setAdapter(new EditPlayerAdapter(ctx,
R.layout.score_row_edit_player, listScoreEdit));
public void deletePlayer(final int position) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
EditPlayers.this);
// Setting Dialog Title
alertDialog.setTitle("Delete Player");
// Setting Dialog Message
alertDialog.setMessage("Are you sure?");
// Setting Delete Button
alertDialog.setPositiveButton("Delete",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
listScoreEdit.remove(position);
updateListView();
}
});
// Setting Cancel Button
alertDialog.setNeutralButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
}
How do I access that function from the getView() in the adapter? Here's the XML for the row
<TextView
android:id="#+id/nameEdit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:layout_weight="70"
android:text="Name"
android:textColor="#666666"
android:textSize="22sp"
android:textStyle="bold" >
</TextView>
<Button
android:id="#+id/deletePlayer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="30"
android:text="Delete"
android:focusable="false" />
Here's the getView()
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
convertView = (LinearLayout) inflater.inflate(resource, null);
Score score = getItem(position);
TextView txtName = (TextView) convertView.findViewById(R.id.nameEdit);
txtName.setText(score.getName());
Button b = (Button)convertView.findViewById(R.id.deletePlayer);
b.setTag(position);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//call function here
}
});
return convertView;
}
I'm totally lost at this point so any help would be appreciated. Thanks!
I would recommend providing an interface back to your activity that lets it know when that button is pressed. I would not recommend calling an activity's method from an ArrayAdapter. It is too tightly coupled.
Try something like this:
Your Activity
public class EditPlayers extends SherlockFragmentActivity implements EditPlayerAdapterCallback {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EditPlayerAdapter adapter = new EditPlayerAdapter(this,
R.layout.score_row_edit_player, listScoreEdit);
adapter.setCallback(this);
listPlayerNames.setAdapter(adapter);
}
private void deletePlayer(final int position) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
EditPlayers.this);
// Setting Dialog Title
alertDialog.setTitle("Delete Player");
// Setting Dialog Message
alertDialog.setMessage("Are you sure?");
// Setting Delete Button
alertDialog.setPositiveButton("Delete",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
listScoreEdit.remove(position);
updateListView();
}
});
// Setting Cancel Button
alertDialog.setNeutralButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
#Override
public void deletePressed(int position) {
deletePlayer(position);
}
}
Adapter:
public class EditPlayerAdapter extends ArrayAdapter {
private EditPlayerAdapterCallback callback;
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
convertView = (LinearLayout) inflater.inflate(resource, null);
Score score = getItem(position);
TextView txtName = (TextView) convertView.findViewById(R.id.nameEdit);
txtName.setText(score.getName());
Button b = (Button)convertView.findViewById(R.id.deletePlayer);
b.setTag(position);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(callback != null) {
callback.deletePressed(position);
}
}
});
return convertView;
}
public void setCallback(EditPlayerAdapterCallback callback){
this.callback = callback;
}
public interface EditPlayerAdapterCallback {
public void deletePressed(int position);
}
}
Your EditPlayerAdapter gets a Context passed to it. Activity extends Context
If the Context passed is your EditPlayers and you store a class-scoped reference to that Context in your Adapter, you can then do:
((EditPlayers) yourContextVar).function();
Better yet, make an interface of some sort. It will help clarify and organise your code and it applies the same principle.