Definition of OnClickListener in AndoridManifest.xml

I used OnClickListener to bind all my buttons in my menu.java file and I set it in the manifest but the button didn't work. I think that maybe I was wrong about the Manifesto.

Menu.java

package com.invoice;

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

public class menu extends Activity implements OnClickListener {
    /** called the activity is first created. */
    Button button1, button2, button3, button4, button5;


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

        button1 = (Button)findViewById(R.id.button1);
        button2 = (Button)findViewById(R.id.button2);
        button3 = (Button)findViewById(R.id.button3);
        button4 = (Button)findViewById(R.id.button4);
        button5 = (Button)findViewById(R.id.button5);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        button5.setOnClickListener(this);
    }   
    public void onClick(View v) {
        //TODO Auto-generate method stub
        //figure out which button was pressed
        switch (v.getId()) {
        case R.id.button1:
            //do button1 action
            break;
        case R.id.button2:
            //do button2 action
            break;
        case R.id.button3:
            //do button3 action
            break;
        case R.id.button4:
            //do button4 action
            break;
        case R.id.button5:
            //do button5 action
            break;
        }


        }
    }

      

Corresponding Java file (each button has a separate java file)

package com.invoice;


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

public class Job extends Activity
{

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

}

      

What I added for each button in the manifest:

<activity android:name=".Help"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="com.invoice.help" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            </activity> 

      

+3


source to share


1 answer


you will need to call the method startActivity

to start the Activity on each button, click like:

public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button1:
            //do button1 action

            // start next activity here
            Intent intent=new Intent(menu.this,Job.class);
            startActivity(intent);
            break;
         ///.... same for all buttons 

        }
   }

      



and you have to register all activity in AndroidManifest.xml

before using it like:

<activity android:name=".menu"
   android:label="@string/app_name">
   <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
   <intent-filter>
    </activity>
<activity android:name=".Help" />
<activity android:name=".Job" />
//.....same for others

      

+1


source







All Articles