symphony888
symphony888

Reputation: 11

Button not doing what its programmed to do

I have 2 if statements where if the

modechoiceId == R.id.textmode && sizechoiceId == R.id.normal -- 1

OR

modechoiceId == R.id.animationmode && sizechoieId = R.id.normal -- 2

The (1) will make the nextButton go to a new Activity which is the textmode32by32.class and the (2) will make the nextButton go to a new Activity which is the animationmode32by32.class.

Right now the problem is, both the options will go to the same page which is textmode32by32.class.

Does the problem lies in the RadioGroup? Or is the code in the if statement?


startPage.class

package tp.mp2014.dotmatrix;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;

public class startPage extends Activity {

    Button backButton, nextButton;
    RadioGroup modeselection, sizeselection;

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

        nextButton = (Button) findViewById(R.id.nextselection);
        backButton = (Button) findViewById(R.id.backselection);
        modeselection = (RadioGroup) findViewById(R.id.modeselection);
        sizeselection = (RadioGroup) findViewById(R.id.sizeselection);

        int modechoiceId = modeselection.getCheckedRadioButtonId();
        int sizechoiceId = sizeselection.getCheckedRadioButtonId();

        if(modechoiceId == R.id.textmode && sizechoiceId == R.id.normal){

            nextButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent nextSelection = new Intent(startPage.this, textmode32by32.class);
                    startActivity(nextSelection);

                }
            });

        }

        else if(modechoiceId == R.id.animationmode && sizechoiceId == R.id.normal){

            nextButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent nextSelection1 = new Intent(startPage.this, animationmode32by32.class);
                    startActivity(nextSelection1);

                }
            });

        }

        backButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent backSelection = new Intent(startPage.this, MainActivity.class);
                startActivity(backSelection);

            }
        });




    }

}

animationmode32by32.class

package tp.mp2014.dotmatrix;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class animationmode32by32 extends Activity {

    Button nextBtn, backBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.animationmode32by32);

        backBtn = (Button) findViewById(R.id.backBtn2);

        backBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent backSelection = new Intent(animationmode32by32.this, startPage.class);
                startActivity(backSelection);

            }
        });

    }

}

textmode32by32.class

package tp.mp2014.dotmatrix;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.SeekBar;

public class textmode32by32 extends Activity {

    Button nextBtn, backBtn;
    RadioButton left, stagnant, right;
    SeekBar scrollspeed;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    setContentView(R.layout.textmode32by32);

    backBtn = (Button) findViewById(R.id.backBtn1);

    backBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent backBtn = new Intent(textmode32by32.this, startPage.class);
            startActivity(backBtn);

        }
    });



    }



}

selectionmode.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/LinearLayout1H"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textmodeselection"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/textmodeselection"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/textsizeselection"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="50dp"
            android:text="@string/textsizeselection"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </LinearLayout>

    <RadioGroup
        android:id="@+id/sizeselection"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/modeselection"
        android:layout_toRightOf="@+id/modeselection" >

        <RadioButton
            android:id="@+id/normal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/sizechoice1" />

        <RadioButton
            android:id="@+id/extended"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/sizechoice2" />
    </RadioGroup>

    <RadioGroup
        android:id="@+id/modeselection"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/LinearLayout1H" >

        <RadioButton
            android:id="@+id/textmode"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/modechoice1" />

        <RadioButton
            android:id="@+id/animationmode"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/modechoice2" />

        <RadioButton
            android:id="@+id/imagemode"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/modechoice3" />
    </RadioGroup>

    <Button
        android:id="@+id/backselection"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:text="@string/back" />

    <Button
        android:id="@+id/nextselection"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="@string/next" />

</RelativeLayout>

Upvotes: 0

Views: 57

Answers (2)

Rod_Algonquin
Rod_Algonquin

Reputation: 26198

The problem is that when the startPage activity start it will set the button listener onclick directly to the if(modechoiceId == R.id.textmode && sizechoiceId == R.id.normal)

Why???

Because you are not changing it each time when another radio button is checked..

solution:

put the checking of radio button inside the onclick listener

change this:

if(modechoiceId == R.id.textmode && sizechoiceId == R.id.normal){

        nextButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent nextSelection = new Intent(startPage.this, textmode32by32.class);
                startActivity(nextSelection);

            }
        });

    }

    else if(modechoiceId == R.id.animationmode && sizechoiceId == R.id.normal){

        nextButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent nextSelection1 = new Intent(startPage.this, animationmode32by32.class);
                startActivity(nextSelection1);

            }
        });

    }

to:

 nextButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                int modechoiceId = modeselection.getCheckedRadioButtonId();
                int sizechoiceId = sizeselection.getCheckedRadioButtonId();

                if(modechoiceId == R.id.textmode && sizechoiceId == R.id.normal){

                   Intent nextSelection1 = new Intent(startPage.this, animationmode32by32.class);
                   startActivity(nextSelection1);

        }

                else if(modechoiceId == R.id.animationmode && sizechoiceId == R.id.normal){

                    Intent backSelection = new Intent(startPage.this, MainActivity.class);
            startActivity(backSelection);
}
                }
            });

Upvotes: 0

Sreedhu Madhu
Sreedhu Madhu

Reputation: 2468

Try like this,

nextButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            int modechoiceId = modeselection.getCheckedRadioButtonId();
            int sizechoiceId = sizeselection.getCheckedRadioButtonId();
            if (modechoiceId == R.id.textmode
                    && sizechoiceId == R.id.normal) {

                //first
            } else if (modechoiceId == R.id.animationmode
                    && sizechoiceId == R.id.normal) {

                //second activity
            }

        }
    });

Upvotes: 1

Related Questions