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 6896209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:00:25+00:00 2026-05-27T07:00:25+00:00

Quite new to coding for android but this issue has me tearing my hair

  • 0

Quite new to coding for android but this issue has me tearing my hair out because it seems to make no sense at all…

I have an activity with four form elements in the layout: a CheckBox, two EditTexts and a Button.

When the user presses the button, it saves the content of the EditTexts as two preference values.

When the user presses the checkbox, it does the following:
If the checkbox is checked, load the preferences and store them into two variables.
Check if either of those variables contain empty strings after trimming them.
If so, show an error message, otherwise show a success message.

Essentially, the two text fields are used to set a pair of preferences which must not be empty when the checkbox is clicked.

It seems to work fine if I click the checkbox before pressing the button – error message or success message shown as appropriate.

If I press the save button and then click the checkbox, it always shows the success message regardless of the preferences.

Code follows (trimmed from the program as a whole)…

layout.xml

    <CheckBox android:id="@+id/cboxActive" android:text="Click me!" android:onClick="toggleActive" android:layout_width="wrap_content" android:layout_height="wrap_content" />

    <EditText android:id="@+id/editFrom" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="phone"><requestFocus /></EditText>

    <EditText android:id="@+id/editTo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="phone"></EditText>

    <Button android:id="@+id/btnSave" android:onClick="savePrefs" android:text="Save" android:layout_width="120dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" />

</LinearLayout>

main class:

public class AutoMessengerActivity extends Activity 
{
SharedPreferences settings;
    CheckBox cboxActive;
    EditText editFrom, editTo;

    boolean active;
    String from, to;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        editFrom = (EditText) findViewById(R.id.editFrom);
        editTo = (EditText) findViewById(R.id.editTo);

        showPrefsInUI();
    }

    private void loadPrefs()
    {
        //Load preferences
        settings = getPreferences(MODE_PRIVATE);   
        from = settings.getString("from", "");
        to = settings.getString("to", "");

    }

    private void showPrefsInUI()
    {
        loadPrefs();

        //Set UI elements to preference values
        editFrom.setText(from);
        editTo.setText(to);   
    }


    public void savePrefs(View view) 
    {
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("from", editFrom.getText().toString());
        editor.putString("to", editTo.getText().toString());

        editor.commit();

        Toast.makeText(this, "Prefs saved!", Toast.LENGTH_SHORT).show();
    }


    public void toggleActive(View view) 
    { 

        if (cboxActive.isChecked())
        {
        loadPrefs();

            //This toast is for debugging
            //It shows the correct data in all circumstances...
        Toast.makeText(this, "F: " + from + "   T: " + to, Toast.LENGTH_SHORT).show();

            //This is the part that seems to fail if you save then click checkbox
        if (from.trim() == "" || to.trim() == "")
        {           
            Toast.makeText(this, "Error - Prefs not saved", Toast.LENGTH_LONG).show();
            cboxActive.setChecked(false);
        }
        else
        {
            Toast.makeText(this, "Success!", Toast.LENGTH_SHORT).show();
        }

        }
        else
        {
        Toast.makeText(this, "Unchecked!", Toast.LENGTH_SHORT).show();
        }

    }
}

Hopefully that code gives an idea of the problem and allows it to be replicated…

  • 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-05-27T07:00:25+00:00Added an answer on May 27, 2026 at 7:00 am

    Oh god, how silly of me – Urban and jcxavier hit the nail on the head… I forgot about that damn annoying quirk of Java! Changed the line to

    from.trim().equals("") || to.trim().equals("")
    

    And it works fine!

    For what it’s worth, that HAD actually crossed my mind briefly, but it was 2am when I tried equals and I got confused about requiring an Object as the parameter and ended up specifying null rather than “” – which didn’t work…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am quite new to Java and am currently coding an Android application. In
i'm quite new here but this site helped me a lot already, so I
This is in regards to C# coding, im quite new to this programming language,
I am quite new to spring coding.. I asked a question earlier but no
It seems that this problem has already been encountered by quite a few people:
First of all, I'm quite new to the Android and JAVA world (coming from
Scenario: Quite new to DI and Ninject but would love to master it so
New to stackoverflow, been very helpful searching, but alas the time has come to
I'm quite new with PHP and need help coding an add script for my
This time I wont be saying I'm new to razor mainly because I'm coming

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.