Can't show Spinner DropDown list inside a dialog - java

I have created a button "settings", when i click in, a dialog dispaly for authentication.
I just make a test, if the edit text is empty the dialog dismiss else another dialog box display which contains a spinner.
Here's the code:
case R.id.bsettings:
// Create Object of Dialog class
final Dialog login = new Dialog(MainActivity.this);
// Set GUI of login screen
login.setContentView(R.layout.login_dialog);
login.setTitle("Settings connection");
// Init button of login GUI
Button btnLogin = (Button) login.findViewById(R.id.btn_set_Login);
Button btnCancel = (Button) login.findViewById(R.id.btn_set_Cancel);
final EditText Id = (EditText)login.findViewById(R.id.id_setting);
final EditText txtPassword = (EditText)login.findViewById(R.id.Password_setting);
// Attached listener for login GUI button
btnLogin.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(Id.getText().toString().trim().length() > 0 && txtPassword.getText().toString().trim().length() > 0)
{
// Validate Your login credential here than display message
Toast.makeText(MainActivity.this,
"Login Sucessfull", Toast.LENGTH_LONG).show();
// Redirect to dashboard / home screen.
login.dismiss();
final Dialog settingdialog = new Dialog(MainActivity.this);
settingdialog.setContentView(R.layout.setting_dialog);
settingdialog.setTitle("Settings Menu");
spinner = (Spinner)findViewById(R.id.languagespinner);
ArrayAdapter<String>adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item,language);
spinner.setAdapter(adapter);
settingdialog.show();
}
else
{
Toast.makeText(MainActivity.this,
"Please enter Username and Password", Toast.LENGTH_LONG).show();
}
}
});
btnCancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
login.dismiss();
}
});
// Make dialog box visible.
login.show();
login.getWindow().setGravity(Gravity.TOP);
break;
The problem is when i click in a login button the second dialog didn't display.
Should you tell me what's wrong in my code ?

You find the spinner directly.
Chanage
spinner = (Spinner)findViewById(R.id.languagespinner);
to
spinner = (Spinner)settingdialog.findViewById(R.id.languagespinner);

Related

How to Make alert dialog close with buttons only in android

I want to make a alert dialog close when the user choose one of the available options in the box only, and doesn't close when he click the faded area around the alert dialog.
So how can i prevent the alert dialog from close in that way ?
if (totalCount == 10){
AlertDialog.Builder rateDialog = new AlertDialog.Builder(MainActivity.this);
LayoutInflater layoutInflater = getLayoutInflater();
View view = layoutInflater.inflate(R.layout.rating_deign, null);
rateDialog.setView(view);
final AlertDialog alert = rateDialog.create();
alert.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
alert.show();
btn_rate = view.findViewById(R.id.btn_rate);
close = view.findViewById(R.id.close);
btn_rate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
totalCount = 12;
editor.commit();
alert.cancel();
}
});
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
totalCount = 0;
editor.commit();
alert.cancel();
}
});
}
alert.setCancelable(false) you need to add.
Sets whether the dialog is cancelable or not. Default is true.

<Android> Full screen mode is broken and a transparent navigation bar appears when alert is made

I'm developing application for android tablet and wants it to be always keeping full screen mode.
I set flags for that, but when the alert dialog appears it breaks.
When it first appears it keeps full screen, however, when alert dialog is made again, the full screen mode is broken and a transparent navigation bar appears.
Why does the full screen mode fluctuate? Below is my code.
AlertDialog.Builder warning = new AlertDialog.Builder(my_context);
View view = ActivityTeetimeDetail.this.getLayoutInflater().inflate(my_alert_layout,null);
warning.setView(view);
AlertDialog dialog = warning.create();
TextView alertTitle = (TextView) view.findViewById(R.id.setAlertTitle);
alertTitle.setText("Text");
TextView alertContent = (TextView) view.findViewById(R.id.setAlertContent);
alertContent.setText("Text");
Button confirm = (Button) view.findViewById(R.id.dialog_button_confirm);
confirm.setText(R.string.Alert_Confirm);
confirm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
Button cancel = (Button) view.findViewById(R.id.dialog_button_cancel);
cancel.setVisibility(View.INVISIBLE);
dialog.setCancelable(true);
dialog.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_FULLSCREEN |
View.SYSTEM_UI_FLAG_LAYOUT_STABLE|
View.SYSTEM_UI_FLAG_IMMERSIVE|
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION|
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
dialog.show();
you are almost there, you just need to clear the focus from alertdialog that is navigationbar stays hidden.
AlertDialog.Builder warning = new AlertDialog.Builder(my_context);
View view = ActivityTeetimeDetail.this.getLayoutInflater().inflate(my_alert_layout,null);
warning.setView(view);
AlertDialog dialog = warning.create();
TextView alertTitle = (TextView) view.findViewById(R.id.setAlertTitle);
alertTitle.setText("Text");
TextView alertContent = (TextView) view.findViewById(R.id.setAlertContent);
alertContent.setText("Text");
Button confirm = (Button) view.findViewById(R.id.dialog_button_confirm);
confirm.setText(R.string.Alert_Confirm);
confirm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
Button cancel = (Button) view.findViewById(R.id.dialog_button_cancel);
cancel.setVisibility(View.INVISIBLE);
dialog.setCancelable(true);
// add this to your code.
dialog.getWindow().
setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
dialog.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_FULLSCREEN |
View.SYSTEM_UI_FLAG_LAYOUT_STABLE|
View.SYSTEM_UI_FLAG_IMMERSIVE|
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION|
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
dialog.show();

How to display a spinner when a radio button clicked

I want to display a spinner on the screen only if a radio button is clicked but I couldn't find a solution. Here is my current code.
RadioGroup radio_grp=(RadioGroup) rootView.findViewById(R.id.radio_grp);
RadioButton rb1 = (RadioButton) rootView.findViewById(R.id.radioButton1);
RadioButton rb2 = (RadioButton) rootView.findViewById(R.id.radioButton2);
View.OnClickListener button1Listener = new View.OnClickListener() {
public void onClick(View v) {
// Toast message etc.
}
};
View.OnClickListener button2Listener = new View.OnClickListener() {
public void onClick(View v) {
// I want to show a spinner if they click the second radio button
}
};
rb1.setOnClickListener(button1Listener);
rb2.setOnClickListener(button2Listener);
return rootView;
First initialize the spinner like:
Spinner mSpinner = (Spinner) rootView.findViewById(R.id.mSpinner);
Of course, before that, add a spinner in your layout somewhere, and set it's android:visibility="invisible" if the first selected radio button is 1, else set it to visible. Then add listeners:
View.OnClickListener button2Listener = new View.OnClickListener() {
public void onClick(View v) {
// I want to show a spinner if they click the second radio button
if (v.getId() == R.id.radioButton1) mSpinner.setVisibility(View.INVISIBLE);
if (v.getId() == R.id.radioButton2) mSpinner.setVisibility(View.VISIBLE);
}
};
If you don't want it to occupy any space once it's not on the screen, use View.GONEinstead of View.INVISIBLE. Refer here for the difference.
Put ((Spinner) findViewById(R.id.mySpinner)).performClick(); in second radiobutton click
View.OnClickListener button2Listener = new View.OnClickListener() {
public void onClick(View v) {
((Spinner) findViewById(R.id.mySpinner)).performClick();
}
};

android keep dialogbox open until user click on button [duplicate]

i am having an custom alert view which pop ups on a button click event.all the things are going fine.but the problem is:
if user clicks outside alert dialog it disappear.i want to restrict user for clicking out side.I am giving him the choice of cancel/cross button to close alert dialog.
so how to restrict user clicking outside the alert box?
code:
the code in onCreate for button click where i am calling show dialog:
final Button cdButton = (Button) findViewById(R.id.denonCdImage);
cdButton.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v)
{
showDialog(CD_CATG_ID);
}
});
protected Dialog onCreateDialog(int id) {
AlertDialog.Builder builder;
Context mContext = this;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.categorydialog,(ViewGroup) findViewById(R.id.layout_root));
GridView gridview = (GridView)layout.findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
/** Check the id for the device type for image tobe change */
switch(id) {
case 1 : // for the cd image
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,final int position, long id) {
Toast.makeText(view.getContext(), "Image selected for CD", 3000).show();
cdImageId = getImageId(position);
int elementId = getApplicationContext().getResources().getIdentifier(cdImageId, "drawable", getPackageName());
cdImageView.setImageResource(elementId);
Log.d("CdImageid", ""+cdImageId);
closeDialog(view);
}
});
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
dialog = builder.create();
break;
default:
dialog = null;
}
/** onclick listner for the close button */
ImageView close = (ImageView) layout.findViewById(R.id.close);
close.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
dialog.dismiss();
}
});
return dialog;
}
any suggestions?
thanks!
There are two methods concerning this behaviour: setCancelable() and setCanceledOnTouchOutside() as you can see in the reference.

Getting the selected radio button inside OnClick for a button

I am building a simple app
which allow the user to enter his name and select the radio button "with name"
and a toast shows , Hello Name
and the other radio for guest to show hello guest , so How I can get the selected radio inside the OnClick do I need to get them from FindViewByID ??
here my code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
}
public void onClick(View v) {
if (v.getId() == R.id.button1) {
//getting the selected radio if its radio 0
Toast.makeText(this, "Hello Guest", Toast.LENGTH_LONG).show();
}
//getting the selected radio if its radio 1
EditText txt = (EditText) findViewById(R.id.editText1);
Toast.makeText(this, "Hello " + txt.toString(), Toast.LENGTH_LONG).show();
}
Try this instead:
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
//YOUR CODE HERE
}
});
Put that in onCreate() and see how that works out for you. Have you also tried seeing if the Id of View v passed into onClick is actually the correct view? To see this, use log to check the Id.
better you directly check the radio button state on button click listener:
#Override
public void onClick(View v)
{
if(v == button)
{
if(rb1.isChecked() == true)
Toast.makeText(this, "Hello"+rb1.getText(), Toast.LENGTH_LONG).show();
if(rb2.isChecked() == true)
Toast.makeText(this, "Hello"+rb2.getText(), Toast.LENGTH_LONG).show();
}
}
where
rb1=(RadioButton)findViewById(R.id.optname);
rb2=(RadioButton)findViewById(R.id.optguest);
FOR MORE

Categories