Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9200029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:40:29+00:00 2026-06-17T22:40:29+00:00

First things first, I’m a novice in Android and apologize for anything unreasonable. What

  • 0

First things first, I’m a novice in Android and apologize for anything unreasonable.

What I’m trying to do here is to display Main activity and show an AlertDialog, asking for short password (full username and password will be saved in Preferences). I need to find a way to exit the application if the passcode doesn’t match, otherwise, load main activity.

Here is my code so far:

public class MainActivity extends Activity implements OnClickListener
{
Button  screening;
Button  screeningLog;

@Override
protected void onCreate (Bundle savedInstanceState)
{
    resetPreferences ();
    // Set theme
    setTheme (App.getTheme ());
    setContentView (R.layout.main);
    super.onCreate (savedInstanceState);
    createControlsAndListeners ();

    if (!App.isLoggedIn ())
    {
        final LinearLayout view = new LinearLayout (this);
        final TextView passcodeText = new TextView (this);
        final EditText passcode = new EditText (this);

        passcodeText.setText (R.string.passcode);
        passcode.setHint (R.string.passcode_hint);
        passcode.setInputType (InputType.TYPE_TEXT_VARIATION_PASSWORD);

        view.setOrientation (LinearLayout.VERTICAL);
        view.addView (passcodeText);
        view.addView (passcode);

        AlertDialog.Builder builder = new AlertDialog.Builder (this);
        builder.setTitle ("Enter Passcode.");
        builder.setView (view);
        builder.setPositiveButton (R.string.login, new DialogInterface.OnClickListener ()
        {
            public void onClick (DialogInterface dialog, int whichButton)
            {
                if (App.get (passcode).equals (App.getPassword ().substring (0, 4)))
                {
                    App.setLoggedIn (true);
                    dialog.dismiss ();
                }
                else
                {
                    Toast toast = Toast.makeText (MainActivity.this, Error.get (Error.AUTHENTICATION),
                            App.getDelay ());
                    toast.show ();
                }
            }
        });
        builder.show ();
    }
}

public void resetPreferences ()
{
    PreferenceManager.setDefaultValues (this, R.xml.preferences, false);
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences (this);
    App.setServer (preferences.getString ("server", ""));
    App.setUsername (preferences.getString ("username", ""));
    App.setPassword (preferences.getString ("password", ""));
    App.setNightMode (preferences.getBoolean ("night_mode", false));
    App.setDelay (Integer.parseInt (preferences.getString ("delay", "30000")));
}

private void createControlsAndListeners ()
{
    screening = (Button) findViewById (R.main_id.screeningButton);
    screening.setOnClickListener (this);
    screeningLog = (Button) findViewById (R.main_id.screeningLogButton);
    screening.setOnClickListener (this);
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:40:30+00:00Added an answer on June 17, 2026 at 10:40 pm

    Try this it might help you. You can use the intent for this.

    AlertDialog.Builder builder = new AlertDialog.Builder (this);
    builder.setTitle ("Enter Passcode.");
    builder.setView (view);
    builder.setPositiveButton (R.string.login, new DialogInterface.OnClickListener ()
    {
        public void onClick (DialogInterface dialog, int whichButton)
        {
            if (App.get (passcode).equals (App.getPassword ().substring (0, 4)))
            {
                App.setLoggedIn (true);
                dialog.dismiss ();
            }
            else
            {
                Toast toast = Toast.makeText (MainActivity.this, Error.get (Error.AUTHENTICATION),App.getDelay ());
                toast.show ();
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_HOME);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        }
    });
    builder.show ();
    

    Or
    If this is your first activity in app then call finish() method in else condition.

        AlertDialog.Builder builder = new AlertDialog.Builder (this);
        builder.setTitle ("Enter Passcode.");
        builder.setView (view);
        builder.setPositiveButton (R.string.login, new DialogInterface.OnClickListener ()
        {
            public void onClick (DialogInterface dialog, int whichButton)
            {
                if (App.get (passcode).equals (App.getPassword ().substring (0, 4)))
                {
                    App.setLoggedIn (true);
                    dialog.dismiss ();
                }
                else
                {
                    Toast toast = Toast.makeText (MainActivity.this, Error.get (Error.AUTHENTICATION),App.getDelay ());
                    toast.show ();
                    finish();
                }
            }
        });
        builder.show ();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First things first, I'm using the compatibility library and here's the error I'm getting:
I have a problem here. First things first, the code: Contents of test.bat: @echo
First things first, here is a little snippet code to help explain my problem:
First things, first, I'm just starting with Android Application Development and would like to
First things, first. Here are my data: lat <- c(12, 12, 58, 58, 58,
I'm trying to write a program in assembly and one of the first things
First things first, here is the assignment of @market_0_home outside of the iteration below,
newbie here. This is an iPhone utility project. First things first. I have a
First things first, let me explain my situation: I'm working on a client and
One of the first things I learned in C++ was that #include <iostream> int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.