Populating a Spinner in AlertDialog - android

I am trying to populate a spinner in an AlertDialog using the following code:
ArrayAdapter<String> dataAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item, providersList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
AlertDialog.Builder checkInDialog1 = new AlertDialog.Builder(this);
checkInDialog1.setView(R.layout.layout_checkin_items);
checkInDialog1.show();
AlertDialog builder = checkInDialog1.create();
Spinner checkInProviders = (Spinner) builder.findViewById(R.id.providers);
checkInProviders.setAdapter(dataAdapter);
But builder.findViewById is returning null for checkInProviders.
In my XML:
<Spinner
android:id="#+id/providers"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Please help. Thanks.

Try this
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
final View myView = inflater.inflate(R.layout.layout_checkin_items, null);
dialogBuilder.setView(myView);
Spinner checkInProviders = (Spinner) myView .findViewById(R.id.providers);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item, providersList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
checkInProviders.setAdapter(dataAdapter);
Try this
Dialog dialog=new Dialog(this);
dialog.setTitle("Title");
dialog.setContentView(R.layout.layout_checkin_items);
Spinner checkInProviders = (Spinner) dialog.findViewById(R.id.providers);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item, providersList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
checkInProviders.setAdapter(dataAdapter);
dialog.show();

I think you are not inflating the view
View dialogView = inflater.inflate(R.layout.layout_checkin_items, null);
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.layout_checkin_items, null);
dialogBuilder.setView(dialogView);
Spinner checkInProviders = (Spinner) dialogView .findViewById(R.id.providers);
In the bottom only add
AlertDialog b = dialogBuilder.create();
b.show();

Related

Android AlertDialog Builder with Edittext setView shows at bottom?

Hi I am using AlertDialog with no custom views, setting array adapter of lists by using:
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
final EditText input = new EditText(MainActivity.this);
builder.setView(input);
builder.setTitle("Please Select");
String[] listStrings = {"string1", "string2"};
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_single_choice, listStrings);
builder.setSingleChoiceItems(arrayAdapter, 0, new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
}
});
builder.show();
and I want to attach EditText for searching list, But EditText shows at the end of list. I want EditText top of list, so please suggest me without using custom layouts.
final EditText input = new EditText(MainActivity.this);
builder.setView(input);
I tried by changing sequence of builder.setView(input);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
final EditText input = new EditText(MainActivity.this);
LinearLayout layout=new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(input);
builder.setTitle("Please Select");
String[] listStrings = {"string1", "string2"};
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_single_choice, listStrings);
ListView list=new ListView(this);
list.setAdapter(arrayAdapter);
layout.addView(list);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
builder.setView(layout);
builder.show();
Try this
boolean checked = false;
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
LinearLayout linear = new LinearLayout(MainActivity.this);
LinearLayout.LayoutParams parm = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
linear.setOrientation(LinearLayout.VERTICAL);
linear.setLayoutParams(parm);
final EditText input = new EditText(MainActivity.this);
builder.setTitle("Please Select");
linear.addView(input);
String[] listStrings = {"string1", "string2"};
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_single_choice, listStrings);
ListView list=new ListView(this);
list.setAdapter(arrayAdapter);
linear.addView(list);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
CheckedTextView chk = (CheckedTextView) view;
Log.d("", "Checked = " + checked);
checked = checked ? false:true;
chk.setChecked(checked);
}
});
builder.setView(linear);
builder.show();

Android - spinner within dialog won't populate

I'm trying to add a two spinners inside a dialog (popup). The problem I'm having is populating the spinners. I get no error I can see, and basically the same code works if It's in a tab-fragment, and not the dialog.
This is the code that does not popluate the spinners inside the dialog.
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
League league;
league = ((LeagueMainActivity)getActivity()).getLeague();
View v = inflater.inflate(R.layout.diaglog_add_match, null);
Spinner spinner1 = (Spinner) v.findViewById(R.id.spinner_dialog_player1);
Spinner spinner2 = (Spinner) v.findViewById(R.id.spinner_dialog_player2);
String [] items = {"test 1", "test 2"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Log.d("Spinner: ", "" + spinner1);
spinner1.setAdapter(adapter);
spinner2.setAdapter(adapter);
builder.setView(inflater.inflate(R.layout.diaglog_add_match, null))
.setTitle("Add match")
.setPositiveButton("Create", 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) {
//LoginDialogFragment.this.getDialog().cancel();
/* do I really need to do anything??? */
}
});
AlertDialog dialog = builder.create();
return dialog;
}
This is the code that works within a (tabbed) fragment:
public class UnnamedFragment extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_unnamed, container, false);
Spinner spinner1 = (Spinner) rootView.findViewById(R.id.spinner);
String [] items = {"test 1", "test 2"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Log.d("Spinner: ", "" + spinner1);
spinner1.setAdapter(adapter);
return rootView;
}
}
Okay so what I did wrong was inflating/creating two independent views with the same context.
First I did:
View v = inflater.inflate(R.layout.diaglog_add_match, null);
And then:
builder.setView(inflater.inflate(R.layout.diaglog_add_match, null))
So I set the view for the builder as a new view, and not the same ones I used for the spinner.
So instead if I do:
View v = inflater.inflate(R.layout.diaglog_add_match, null);
builder.setView(v)
That does the trick.

AutoCompleteTextView in a AlertDialog Builder

I have tried to work this out but nothing pops into my head, the following code is a part of an application that has movies and if i click a search button it creates a dialog builder with a AutoCompleteTextView for typing a name of a movie.
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(ThisScreen.search_context, android.R.style.Theme_Black));
///// autocomplete thingie
String[] name_mov = new String[movie_categ.size()];
name_mov = movie_categ.toArray(name_mov);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, name_mov);
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View search_View = inflater.inflate(R.layout.this_movie_search, null);
myAutoComplete = (AutoCompleteTextView)
search_View.findViewById(R.id.movie_name);
myAutoComplete.setAdapter(adapter);
myAutoComplete.setThreshold(2);
/////
alertDialogBuilder.setView(searchView);
alertDialogBuilder.setOnCancelListener(new OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
isFirstDialog = 0;
}
});
alertDialog = alertDialogBuilder.create();
if (movie_categ.isEmpty() )
{
Toast.makeText(ThisScreen.search_context, "Verify net conn.", Toast.LENGTH_LONG).show();
isFirstDialog = 0;
}
else alertDialog.show();
and this_movie_search.xml has
<AutoCompleteTextView
android:id="#+id/movie_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/button_search"
android:textColorHint="#android:color/white"
android:hint="#string/search_hint"
android:ems="10"
android:maxLength="50"
/>
The list with the suggestions doesnt appear, any ideas why?
I searched for the answer for too long and didn't saw the obvious, i should have declared my myAutoComplete from the beginning as 'AutoCompleteTextView' type, and it works.
final AutoCompleteTextView myAutoComplete = (AutoCompleteTextView) searchView.findViewById(R.id.AutoCompleteTextView_declared_in_the_xml);
// autocomplete thingie
String[] name_mov = new String[movie_categ.size()];
name_mov = movie_categ.toArray(name_mov);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, name_mov);
myAutoComplete.setAdapter(adapter);
myAutoComplete.setThreshold(2);
//

Fatal error Spinner and ArrayAdapter

I have this part of code for spinner. But I get fatal error. What is wrong in here? Thanks! My Activity extends FragmentActivity.
Spinner spinner;
String[] layers = {getString(R.string.a), getString(R.string.b), getString(R.string.c)};
LayoutInflater li = LayoutInflater.from(this);
View v;
v = li.inflate(R.layout.nearest, null);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MyActivity.this, android.R.layout.simple_spinner_item, layers);
spinner = (Spinner) findViewById(R.id.spinner_nearest);
spinner.setAdapter(adapter);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(v);
builder.setCancelable(true);
AlertDialog alert = builder.create();
alert.show();}
Is your spinner in nearest.xml?
Then change to
spinner = (Spinner) v.findViewById(R.id.spinner_nearest);

Null Pointer Exception when using a spinner in alertDialog

This is the code I wrote:
// create a view in dialog box
final View fileView = getLayoutInflater().inflate(R.layout.filename, null);
String[] array_spinner = new String[5];
array_spinner[0]="1";
array_spinner[1]="2";
array_spinner[2]="3";
array_spinner[3]="4";
array_spinner[4]="5";
Spinner s = (Spinner) findViewById(R.id.sp_CAT);
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, array_spinner);
s.setAdapter(adapter);
s.setSelection(2);
//Spinner mCAT = (Spinner) findViewById(R.id.sp_CAT);
//ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
//fileView.getContext(), R.array.a_CAT, android.R.layout.simple_spinner_item);
//adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//mCAT.setAdapter(adapter);
EditText mSID = (EditText) fileView.findViewById(R.id.et_SID);
EditText mSCD = (EditText) fileView.findViewById(R.id.et_SCD);
EditText mNUM = (EditText) fileView.findViewById(R.id.et_NUM);
final String tSID=mSID.getText().toString();
final String tSCD=mSCD.getText().toString();
final String tNUM=mNUM.getText().toString();
// show dialog box
new AlertDialog.Builder(CameraActivity3.this).setTitle("Set FileName")
.setView(fileView)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Toast.makeText(CameraActivity3.this, tSID+tSCD+tNUM, Toast.LENGTH_SHORT).show();
}// public void onClick
}) // setPositiveButton
.setNegativeButton("Cancel", null).show();
However, I am receiving a null pointer exception. After some testing (commenting out some lines), it seems to be related to the spinner adapter. Did I get something wrong?
Use this:
Spinner s = (Spinner) fileView.findViewById(R.id.sp_CAT);
write below code line
Spinner s = (Spinner) fileView.findViewById(R.id.sp_CAT);
instead of
Spinner s = (Spinner) findViewById(R.id.sp_CAT);
For anyone looking at this in the future using DialogFragment to build the AlertDialog, you can't use just this in the ArrayAdapter. My solution is below:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View myView = inflater.inflate(R.layout.program_num_spinner_popup, null);
builder.setView(myView);
programNumbers = (Spinner) myView.findViewById(R.id.prog_num_spinner_list);
ArrayAdapter<CharSequence> numbersAdapter = ArrayAdapter.createFromResource(this.getActivity(), R.array.program_numbers, android.R.layout.simple_spinner_item);
numbersAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
programNumbers.setAdapter(numbersAdapter);
I replaced this with this.getActivity() combined with Zaz Gmy's answer and Dipak Keshariya's answer (which are the same).

Categories

Resources