• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps multiple button click navagation

bumpn

Lurker
Question 1: I have 2 activities. I was wondering how to optimize it. I can either create 2 activities with multiple listeners. Or create multiple java files for each button(onclick listener)
Question 2: I have tried to create multiple listeners in one java but can only get one button to work. What is the syntax for multiple listeners in one java file? Here is my *updated code: now the issue is no matter what button is clicked on it leads to the same page.

activity1.java
PHP:
package install.fineline;

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

public class Activity1 extends Activity2 {

Button Button1;
Button Button2;
Button Button3;
Button Button4;
Button Button5;
Button Button6;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fineline);
    addListenerOnButton();
}


public void addListenerOnButton() {

    final Context context = this;

    Button1 = (Button) findViewById(R.id.autobody);

    Button1.setOnClickListener(new OnClickListener() {


        public void onClick(View arg0) {

            Intent intent = new Intent(context, Activity1.class);
            startActivity(intent);   

        }

    });
    
    Button2 = (Button) findViewById(R.id.glass);

    Button2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(context, Activity1.class);
            startActivity(intent);   

        }

    });
    
    Button3 = (Button) findViewById(R.id.wheels);

    Button3.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(context, Activity1.class);
            startActivity(intent);   

        }

    });
    
    Button4 = (Button) findViewById(R.id.speedy);

    Button4.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(context, Activity1.class);
            startActivity(intent);   

        }

    });
    
    Button5 = (Button) findViewById(R.id.sevan);

    Button5.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(context, Activity1.class);
            startActivity(intent);   

        }

    });

   Button6 = (Button) findViewById(R.id.towing);

   Button6.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        Intent intent = new Intent(context, Activity1.class);
        startActivity(intent);   

    }

});

}}
and activity2.java
PHP:
package install.fineline;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;

public class Activity2 extends Activity {

Button Button1;

public void onCreate1(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.autobody);
}
Button Button2;

public void onCreate2(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.glass);
}
Button Button3;

public void onCreate3(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.wheels);
}
Button button4;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.speedy);
}
Button Button5;

public void onCreate5(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sevan);
}

Button Button6;

public void onCreate6(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.towing);
}}
 
Why do you need multiple listeners? Couldn't you just use a flag that determines what state the app is in and then in one listener, check this flag and react accordingly?
 
main.xml
--------
|button1| (button press)>page1.xml
|button2| (button press)>page2.xml
|button3| (button press)>page3.xml
|button4| (button press)>page4.xml
|button5| (button press)>page5.xml
|button6| (button press)>page6.xml
-------------------------------------------
when a button is clicked it goes to a page
 
ok, well you could easily do this with one listener like so:

Code:
public void onClick(View v) {
    if (v == button1) {
        //Load page 1
    else if (v == button2) {
        //Load page 2
     } 
     //And so on...
}
 
Back
Top Bottom