Search code examples
androidswitch-statementonclicklistener

I set up a switch-statement to handle OnClick events in my Activity, but only the first case works. Why?


I created an empty activity in my Android project, and added one TextView and one Button to it, like so:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:onClick="onClick"
    tools:context="com.radical.pillbox.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/CloseActivityCross"
    android:text="@string/cross"
    android:textSize="50sp"
    android:padding="10dp"
    android:fontFamily="@font/montserrat_bold"
    android:textColor="@color/pillbox_green_primary_dark" />

<TextView
    android:id="@+id/IntroString"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/CloseActivityCross"
    android:fontFamily="@font/montserrat_light"
    android:padding="10dp"
    android:text="Welcome to Pillbox."
    android:textColor="#000000"
    android:textSize="30sp"
    android:gravity="left"
    android:textAlignment="gravity"/>

<EditText
    android:id="@+id/EmailEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/IntroString"
    android:layout_marginLeft="10dp"
    android:alpha="0.64"
    android:fontFamily="@font/montserrat_light"
    android:hint="[email protected]"
    android:inputType="textEmailAddress"
    android:textColor="@color/pillbox_background_dark"
    android:textColorHint="@color/pillbox_accent_green" />

<EditText
    android:id="@+id/PasswordEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/EmailEditText"
    android:layout_marginStart="10dp"
    android:alpha="0.64"
    android:fontFamily="@font/montserrat_light"
    android:hint="password"
    android:inputType="textPassword"
    android:textColor="@color/pillbox_background_dark"
    android:textColorHint="@color/pillbox_accent_green" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/FooterString"
    android:textColor="#000000"
    android:textSize="12sp"
    android:text="2018."
    android:layout_centerHorizontal="true"
    android:textAlignment="center"
    android:fontFamily="@font/montserrat_light"
    android:layout_alignParentBottom="true"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:id="@+id/SignInButton"
    android:background="@drawable/round_button"
    android:text="Sign In"
    android:textSize="15sp"
    android:textColor="@color/pillbox_green_primary_dark"
    android:layout_below="@id/PasswordEditText"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:id="@+id/SignUpButton"
    android:background="@drawable/round_button"
    android:text="Sign Up"
    android:textSize="15sp"
    android:textColor="@color/pillbox_green_primary_dark"
    android:layout_below="@id/PasswordEditText"
    android:layout_toRightOf="@id/SignInButton"/>

</RelativeLayout>

My MainActivity.java file is:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EditText email = (EditText) findViewById(R.id.EmailEditText);
        EditText password = (EditText) findViewById(R.id.PasswordEditText);
        TextView close = (TextView) findViewById(R.id.CloseActivityCross);
        Button sign_up = (Button) findViewById(R.id.SignUpButton);
        Button sign_in = (Button) findViewById(R.id.SignInButton);

        email.setOnClickListener(MainActivity.this);
        password.setOnClickListener(MainActivity.this);
        sign_in.setOnClickListener(MainActivity.this);
        close.setOnClickListener(MainActivity.this);
    }

    @Override
    public void onClick(View view) {
        EditText email = (EditText) findViewById(R.id.EmailEditText);
        EditText password = (EditText) findViewById(R.id.PasswordEditText);
        TextView close = (TextView) findViewById(R.id.CloseActivityCross);
        Button sign_up = (Button) findViewById(R.id.SignUpButton);
        Button sign_in = (Button) findViewById(R.id.SignInButton);

        switch (view.getId()) {
            case (R.id.SignInButton):
                    Log.d("Event:", "Signed in with correct credentials.");
                break;

            case (R.id.CloseActivityCross):
                Toast.makeText(MainActivity.this, "Closing.", Toast.LENGTH_SHORT).show();
                MainActivity.this.finish();
                Log.d("Cross:", "Activity closed");
                break;
        }
    }
}

In my main activity, I created cases in a switch-statement to handle the OnClick Events, but only the first case defined works. The remaining cases never trigger for some reason or trigger after an extended delay. To diagnose where the problem occurs, I have:
1. Tried to change the order in which I add the listeners
2. Changed cases with the first being CloseActivityCross once and SignInButton the next. Each time, only the first case works properly.
Why does this happen? I should mention that in one trial, things began working perfectly for a while, and now the same issue is cropping up.


Solution

  • Listeners are OK in java code . The problem is in xml . Your parent layout is RelativeLayout and you have use rule android:layout_below .

    In some places you have used "@id instead of "@+id . So this will make overlap the views . Make it write as "@+id in all android:layout_below tags and remove android:onClick="onClick" from root layout . see if it works .

    Suggestion :- If all of your widgets aligned vertically then Use LinearLayout with vertical orientation .