user2522227
user2522227

Reputation: 15

2 buttons in my android layout, only 1 will work

In the android sdk, I have programmed 2 buttons to bring me to 2 different layouts. However, only the first one I have programmed will work, while the second one will do completely nothing. I will provide the code if you can catch any things I missed, please tell me what you think. This is one of my first applications to run on android, so try to explain your suggestion as simple as you could.

Code:

  public class MainActivity extends Activity {
        private final String T = "Settings";
        @Override 
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            settingsButton();
        }
        private final String T2 = "ManualAdd";
        protected void onCreate1(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            feederButton();
        }


        private void settingsButton() {
            Button messageButton = (Button) findViewById(R.id.Settings);
            View.OnClickListener myListener = new View.OnClickListener() {@
                Override
                public void onClick(View v) {
                    Log.i(T, "You clicked the settings button!");
                    startActivity(new Intent(MainActivity.this, SettingsActivity.class));
                }
            };
            messageButton.setOnClickListener(myListener);
        }
        private void feederButton() {
            Button feedButton = (Button) findViewById(R.id.AddFeeder);
            View.OnClickListener my2ndListener = new View.OnClickListener() {
                public void onClick(View v) {
                    Log.i(T2, "You clicked the add button!");
                    startActivity(new Intent(MainActivity.this, ManualAdd.class));
                }
            };
            feedButton.setOnClickListener(my2ndListener);
        }
    }

Upvotes: 1

Views: 80

Answers (2)

Kodi
Kodi

Reputation: 109

Try this code

public class MainActivity extends Activity {

    Button Your_Button_Name;
    Button Your_Button_Name;
    Activity activity;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.Your_Layout_Name);

        //In (R.id.Your_Btn_Name) that's the name that you gave your button in the XML
        activity = this;
        Your_Button_Name = (Button) findViewById(R.id.Your_Btn_Name);
        Your_Button_Name = (Button) findViewById(R.id.Your_Btn_Name);
        Your_Button_Name.setOnClickListener(listener);
        Your_Button_Name.setOnClickListener(listener);

    }

    private View.OnClickListener listener = new View.OnClickListener() {
        public void onClick(View v) {

            switch (v.getId()) {
                case (R.id.Your_Btn_Name):
                    startActivity(new Intent(MainActivity.this, Your_Layout_Name.class));

                    break;

                case (R.id.Your_Btn_Name):
                    startActivity(new Intent(MainActivity.this, Your_Layout_Name.class));

                    break;
            }
        }
    };
}

Upvotes: 0

A--C
A--C

Reputation: 36449

You cannot make a different onCreate() method for every button you have. The reason why only one of your Buttons work is because only the onCreate() method is only called. The system has no idea about onCreate1(). To get both of your buttons working change

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

to

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

You can completely delete onCreate1() from your source code, it is now obsolete.

It would also be a good idea to follow the official Android "First App" tutorial instead.

Upvotes: 1

Related Questions