Custom dialog disappears when clicking next to it - java

I want to show a custom dialog and force the user to click on whether button one or two.
The problem is that users can use the back button AND if they click on the view that is shown in the background my dialog also disappears.
Why? And how could I prevent this?
final Main t = this;
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.prompt_input_access);
dialog.setTitle("Title");
Button cmdLoginAccount = (Button) dialog.findViewById(R.id.cmdLoginAccount);
Button cmdLoginBank = (Button) dialog.findViewById(R.id.cmdLoginBank);
cmdLoginAccount.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
loginToBank = true;
dialog.dismiss();
Intent intent = new Intent(t, UserMenu.class);
startActivity(intent);
}
});
cmdLoginBank.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
loginToBank = false;
dialog.dismiss();
Intent intent = new Intent(t, UserMenu.class);
startActivity(intent);
}
});
dialog.show();

You just need to use the setCanceledOnTouchOutside method :
dialog.setCanceledOnTouchOutside(false);

Related

How to create a button on a different activity programmatically and save it on this activity?

I wanted to create a button on Homepage activity by clicking the button on GroupSettings activity. However, when I do so, and go back to Homepage, the button is not saved on the page.
Here is the java code that i have:
public void btnpressedme(View caller){
ConstraintLayout constraintLayout = findViewById(R.id.homepageLayout);
Button btnShow = new Button(this);
btnShow.setText("SIUUUUUUU");
btnShow.setLayoutParams(new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
btnShow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View caller) {
Toast.makeText(GroupSettings.this, "HELLOOOO", Toast.LENGTH_LONG).show();
//Intent intent = new Intent(this, Group.class);
//startActivity(intent);
}
});
if (constraintLayout != null) {
constraintLayout.addView(btnShow);
}
}

Java Android String Intent displaying null when moving to previous screen

So I have a screen where a user enters their name, on button click they are redirected to a menu where it displays their name.
In the menu screen I have a button that takes me to another screen (About Me), on that screen I have a button with an Intent to go back to the Menu Activity.
The issue is that when I click the button with the Intent to go back, the name displays as null instead of what the User entered.
This does not happen when I use the actual android navigation buttons to go to the previous screen, only the Button i created to go back to the Menu Activity.
Launcher Activity where user enters their name
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher_screen);
Button launcherNextBtn = (Button) findViewById(R.id.launcherNextBtn);
EditText launcherVisitorNameEditText = (EditText) findViewById(R.id.launcherVisitorNameEditText);
TextView launcherVisitorNameErrorTextView = (TextView) findViewById(R.id.launcherVisitorNameErrorTextView);
launcherVisitorNameErrorTextView.setText("");
launcherNextBtn.setOnClickListener((View v) -> {
String visitorName = launcherVisitorNameEditText.getText().toString();
if (visitorName.equals("")) {
launcherVisitorNameErrorTextView.setText("Please enter a value");
} else {
launcherVisitorNameErrorTextView.setText("");
Intent goToMenuActivity = new Intent(LauncherScreen.this, MenuScreen.class);
goToMenuActivity.putExtra("visitorName", visitorName);
startActivity(goToMenuActivity);
}
});
Menu activity where it displays the users name
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_screen);
Button menuHomeBtn = (Button) findViewById(R.id.menuHomeBtn);
Button menuAboutMeBtn = (Button) findViewById(R.id.menuAboutMeBtn);
TextView menuNameTextView = (TextView) findViewById(R.id.menuNameTextView);
String visitorName = getIntent().getStringExtra("visitorName");
menuNameTextView.setText("Dear " + visitorName);
menuHomeBtn.setOnClickListener((View v) -> {
Intent goToLauncherActivity = new Intent(MenuScreen.this, LauncherScreen.class);
goToLauncherActivity.putExtra("visitorName", visitorName);
startActivity(goToLauncherActivity);
});
menuAboutMeBtn.setOnClickListener((View v) -> {
Intent goToAboutMeActivity = new Intent(MenuScreen.this, AboutMeScreen.class);
startActivity(goToAboutMeActivity);
});
}
About me activity, when I click the button to back to Menu, i get "Dear null"
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about_me_screen);
Button aboutMeBackBtn = (Button) findViewById(R.id.aboutMeBackBtn);
String visitorName = getIntent().getStringExtra("visitorName");
aboutMeBackBtn.setOnClickListener((View v) -> {
Intent goToMenuActivity = new Intent(getApplicationContext(), MenuScreen.class);
startActivity(goToMenuActivity);
});
}
Your problem is in About me activity screen
aboutMeBackBtn.setOnClickListener((View v) -> {
Intent goToMenuActivity = new Intent(getApplicationContext(), MenuScreen.class);
startActivity(goToMenuActivity);
});
This time you are not passing an extra field, so you are getting null
Answer 1
aboutMeBackBtn.setOnClickListener((View v) -> {
onBackPressed();
});
Answer 2
aboutMeBackBtn.setOnClickListener((View v) -> {
Intent goToMenuActivity = new Intent(getApplicationContext(), MenuScreen.class);
goToMenuActivity.putExtra("visitorName", visitorName)
startActivity(goToMenuActivity);
});

How to make a button permanently unclickable

I am making a log-in system on Android. And I want the register Button to be unclickable when it has been clicked. I am using this code:
final Button register = (Button) findViewById(R.id.register);
register.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
register.setEnabled(false);
Intent register = new Intent(getApplicationContext(), register.class);
startActivity(register);
}
});
This is working great, but I want the Button to remain unclickable even when the application or phone has been restarted. Does anyone know a way to make the Button unclickable permanently even when the application has been shut down?
As I already said in the comments section something like this may work:
public class MyActivity extends Activity {
private static final String KEY_IS_BUTTON_CLICKABLE = "key_clickable";
#Override
public void onCreate(Bundle savedInstanceState) {
...
final Button register = (Button) findViewById(R.id.register);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean isClickable = sharedPreferences.getBoolean(KEY_IS_BUTTON_CLICKABLE, true);
register.setEnabled(isClickable);
if(isClickable) {
register.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
register.setEnabled(false);
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit()
.putBoolean(KEY_IS_BUTTON_CLICKABLE, false);
Intent register = new Intent(getApplicationContext(), register.class);
startActivity(register);
}
});
}
}
...
}
In this case you could take a pessimistic approach and disable the button in the layout (by default) with android:clickable="false" and enable it in the condition where registration is required.

find the button onclick function in android activity

This is my login form is valid means :
if(isUserValidated && isPasswordValidated)
{
if(DetailProductDescription.product_id==null){
Intent intent = new Intent(LoginForm.this,HomePage.class);
startActivity(intent);
}
else
{
Intent intent = new Intent(LoginForm.this,WatchList.class);
startActivity(intent);
}
}
EDIT:
Here i have to check another condition in else part:
in else part having to check another condition:
if am getting the product_id on DetailProductDescription page without login :
DetailProductDescription class only having these 2 buttons.They are watchlist and wishlist.
if i have to click watchlist button on DetailProductDescription.class means its go to WatchList class.
if i have to click wishlist button on DetailProductDescription.class means its go to
new AddToWishListAsync().execute(); class.
How can i identify the these DetailProductDescription.watchlist and DetailProductDescription.wishlist button on LoginForm and how can i write the condition for these ??
please provide me solution for tehse.
So your Problem is,
Button watchlist -> Login class -> WatchList class
Button wishlist -> Login class-> AddToWishListAsync class
'Login' have to identify which button.
isn't it?
I'll suggest put an extra with intent call. ie,
in DetailProductDescription
watchListBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(DetailProductDescription.this, LoginForm.class);
intent.putExtra("from","watch");
startActivity(intent);
}
});
wishListBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(DetailProductDescription.this, LoginForm.class);
intent.putExtra("from","wish");
startActivity(intent);
}
});
Now, in LoginForm
String from=getIntent.getStringExtra("from");// you got it as 'watch' or 'wish', or null.
Now check the string and proceed.
It will be better to have WatchList button and WishList button inside your login xml layout itself. The visibility of these buttons should be View.INVISIBLE or View.GONE by default. Either you can hide these buttons from layout or you can do so in onCreate method of your Login Activity.
Button watchListBtn = null;
Button wishListBtn = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_login_screen);
watchListBtn = (Button) findViewById(R.id.loginBtn);
watchListBtn.setVisibility(View.GONE);
watchListBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(LoginForm.this, WatchList.class);
startActivity(intent);
}
});
wishListBtn = (Button) findViewById(R.id.wishlistBtn);
wishListBtn.setVisibility(View.GONE);
wishListBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// your code to launch wishlist activity
}
});
// your other onCreate stuff........
}
Then your login validation code
if(isUserValidated && isPasswordValidated) {
if(DetailProductDescription.product_id==null) {
Intent intent = new Intent(LoginForm.this,HomePage.class);
startActivity(intent);
} else {
Intent intent = new Intent(LoginForm.this,WatchList.class);
startActivity(intent);
}
} else {
watchListBtn.setVisibility(View.VISIBLE);
wishListBtn.setVisibility(View.VISIBLE);
}

Closing an AlertDialog box after a button has been clicked

I have the following code which displays a dialog box to the user if no network connection is detected.
private void createNoNetworkDialog() {
LayoutInflater inflater = LayoutInflater.from(this);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
View view = inflater.inflate(R.layout.offline_mode_dialog,null);
builder.setView(view);
builder.show();
}
There are two buttons in this dialog which have methods defined for their onClick actions. I would like to close the dialog pop-up after either of these button is pressed. Any ideas??
Yes,call dismiss() from the Listener's onClick since the DialogInterface reference is passed, which allows for dismissal.
Eg
builder.setPositiveButton ("Yes", new DialogInterface.OnClickListener()
{
public void onClick (DialogInterface dialog, int which)
{
//do stuff beforehand
dialog.dismiss();
}
});
Or if your buttons are inside the layout, show the dialog and keep a reference to it (final AlertDialog dialog = builder.show()). Then use dialog.findViewById() to find the respective buttons. Assign a normal View.OnClickListener and in it call dismiss() using the dialog reference you're holding.
Try this, Am using custom layout its working for me. inilitized Button custon_dialog.findViewById() and then write OncliclListner(). It will work
final Dialog custon_dialog = new Dialog(Login.this);
custon_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
custon_dialog.setContentView(R.layout.forget_custom_dialog);
custon_dialog.setCancelable(true);
Button submit_Btn = (Button) custon_dialog
.findViewById(R.id.submit);
Button cancel_Btn = (Button) custon_dialog
.findViewById(R.id.cancel);
submit_Btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//do your stuf
}
});
cancel_Btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
custon_dialog.dismiss();
}
});
custon_dialog.show();
}

Categories

Resources