In my app I have one button for changing theme. And I have 3 different themes. I found a code in github perfect to me but in that code using radio button for changing theme. And i dont wanna use radio button, the user should see different theme evertime he clicked theme button. So i checked internet if there is a sample for converting radio button to clickListener but i couldn't find any.
public void showDialog(View view) {
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.dialog);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
RadioGroup radioGroup = dialog.findViewById(R.id.radioGroup);
Button submitBtn = dialog.findViewById(R.id.submit);
Button cancelBtn = dialog.findViewById(R.id.cancel);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int itemChecked) {
switch (itemChecked) {
case R.id.themeBlue:
editor.putInt("theme", R.style.BlueTheme);
editor.apply();
break;
case R.id.themeOrange:
editor.putInt("theme", R.style.OrangeTheme);
editor.apply();
break;
case R.id.themeGreen:
editor.putInt("theme", R.style.DefaultTheme);
editor.apply();
break;
}
}
});
submitBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
recreate();
}
});
cancelBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.cancel();
}
});
dialog.show();
}
This is the code on github
themeButton = findViewById(R.id.themeButton);
themeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
and this is mine.Can you help me please?
Related
as follows:
Button btnSubmit = findViewById(R.id.btn_login_submit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_login_submit:
Toast.makeText(LoginActivity.this, "click", Toast.LENGTH_SHORT).show();
break;
}
}
});
Button btnSubmit = findViewById(R.id.btn_login_submit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(LoginActivity.this, "click", Toast.LENGTH_SHORT).show();
}
});
They both worked equally well. Can the switch be omitted?
The btnSubmit has been findViewById. Is switch getId repeated?
Yes you can remove switch case it work same because R.id.btn_login_submit is same
But my suggestion is use view binding instead of findViewById
i am in a pickle at the moment to how to approach making 3 buttons within a single dialog box make the option selected change the text of the button used to show the dialog box, so far only managed to get option3 to work as i suppose its the last intent so the only button that works would be the button for option3
here is the code im trying to pass to the main button from the dialog
public class LabelDialog extends AppCompatDialogFragment {
private Button option1, option2, option3;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.label_dialog, null);
builder.setView(view);
//option 1 and 2
option3 = view.findViewById(R.id.label_lecturebtn);
option3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String option3 = "Lecture";
Intent intent3 = new Intent(getContext(), AddTask.class);
intent3.putExtra("lecture", option3);
startActivity(intent3);
dismiss();
}
});
and the main code
protected void onCreate(Bundle savedInstanceState) {
//...
label_button = findViewById(R.id.button_labels);
label_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openDialog();
}
});
checkLabelResult();
}
public void openDialog() { //Labels dialog
//code to open dialog
}
public void checkLabelResult() {
//intents 1 and 2, same format as 3 but keys changed accordingly
Intent intent3 = getIntent();
String option3 = intent3.getStringExtra("lecture");
label_button.setText(option3);
}
Code for Option 1
option1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String option1 = "Important";
Intent intent = new Intent(getContext(), AddTask.class);
intent.putExtra("important", option1);
startActivity(intent);
dismiss();
}
Code for Option 2
option2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String option2 = "To Do";
Intent intent2 = new Intent(getContext(), AddTask.class);
intent2.putExtra("to do", option2);
startActivity(intent2);
dismiss();
}
I'd rather not use Alert Dialog, but I will if I can set the positive Button to be the button I already have. If I can't do that, is there a way to set positive and negative Buttons in a custom dialog?
You can use the following custom alert dialog.
public class CustomAlertDialog {
public void showDialog(Context activity, String msg, String buttonText, final CustomDialogListener customDialogListener){ //one button with callback
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.custom_alert);
TextView text = (TextView) dialog.findViewById(R.id.text_alertdialog);
text.setText(msg);
Button dialogButton = (Button) dialog.findViewById(R.id.btn_alert_dialog);
dialogButton.setText(buttonText);
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
customDialogListener.onPositiveButtonClick();
}
});
dialog.show();
}
public void showDialog(Context activity, String msg, String positiveButtonText, String negativeButtonText, final CustomDialogListener customDialogListener){//two button with callback
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.custom_alert_two_button);
TextView text = (TextView) dialog.findViewById(R.id.text_alert_two_dialog);
text.setText(msg);
Button positiveDialogButton = (Button) dialog.findViewById(R.id.btn_alert_two_dialog_YES);
Button negativeDialogButton = (Button) dialog.findViewById(R.id.btn_alert_two_dialog_NO);
positiveDialogButton.setText(positiveButtonText);
negativeDialogButton.setText(negativeButtonText);
positiveDialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
customDialogListener.onPositiveButtonClick();
}
});
negativeDialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
customDialogListener.onNegativeButtonClick();
}
});
dialog.show();
}
public void showDialog(Context activity, String msg, String buttonText){ //simple alert without callback
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.custom_alert);
TextView text = (TextView) dialog.findViewById(R.id.text_alertdialog);
text.setText(msg);
Button dialogButton = (Button) dialog.findViewById(R.id.btn_alert_dialog);
dialogButton.setText(buttonText);
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
The custom_alert.xml contains one button and one textView to display the message.
The custom_alert_two_button.xml contains two buttons and one textView to display the message.
Last one contains only one textview to display message.
i have a pop up which contains edit text..how can i prevent the softkeyboard from showing up on displaying of pop up's because my pop up is moving up as soon as it is displayed.
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
i used this line in the activity in which a pop up is displayed on button click. please help...!
android:windowSoftInputMode="stateHidden"
i also used this line in manifest. but of no use.
public class MainActivity extends Activity implements OnClickListener {
AlertDialog dialog;
View checkBoxView ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.des_button).setOnClickListener(this);
//checkBoxView = View.inflate(this, R.layout.popup_description, null);
}
#Override
public void onClick(View v) {
final Dialog dialog = new Dialog(this);
dialog.getWindow().getAttributes().windowAnimations =
R.style.dialog_animation;
// dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.setContentView(R.layout.popup_description);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Button btn_Start = (Button) dialog.findViewById(R.id.ok);
btn_Start.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
Button cancel = (Button) dialog.findViewById(R.id.cancel);
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
}
#Override
public void onDestroy() {
if (dialog!=null) {
if (dialog.isShowing()) {
dialog.dismiss();
}
Try
android:windowSoftInputMode="stateAlwaysHidden"
in your manifest in that activity. one more thing you can do call the following method after opening that dialog popup.
public static void hideKeyboard()
{
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(editTextInPopup.getWindowToken(), 0);
}
Im trying to make a Dialog close when button is pressed.But Every time i press the button the application crashes.
public class CanvasPaint extends Activity implements OnClickListener {
final Button widthbtn = (Button) findViewById(R.id.widthbtn);
final Button widthpopBtn = (Button) findViewById(R.id.widthpopBtn);
final Context context = this;
final Dialog widthDialog = new Dialog(context);
widthbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
widthpopBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
widthDialog.dismiss();
}
});
widthDialog.show();
}
});
}
That's some inception code. Why are you setting a click listener inside a click listener? It should be more like
widthbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
widthDialog.show();
}
});
widthpopBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
widthDialog.dismiss();
}
});
Your dialog layout is not properly inflated, and therefore, your widthpopBtn will be null. Try inflating the layout like this:
LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.your_dialog_layout, null);
Button widthpopBtn = (Button) dialogView.findViewById(R.id.widthpopBtn);