2

I have a custom navigation bar that contains a button, I would dispatch the click event so that the activity that contains my navigation bar can respond to click

    public class BarrePersonnalisee extends LinearLayout implements OnClickListener {
        Context mycontext;
        View convertview;
        ImageButton searchadresse;

        public BarrePersonnalisee(Context context, AttributeSet attrs) {
            super(context, attrs);
            mycontext=context;
            convertview=LayoutInflater.from(mycontext).inflate(R.layout.barre, this);
            searchadresse=(ImageButton)convertview.findViewById(R.id.searchadresse);
            searchadresse.setOnClickListener(this);
        }

        public void onClick(View arg0) {
            switch (arg0.getId()) {
            case R.id.searchadresse:
                //I want to dispatch this event
                    break;
                   }
            }
    ....
    }

public class TaxiMapActivity extends MapActivity{

    BarrePersonnalisee barre;

    ...
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        barre=(BarrePersonnalisee)this.findViewById(R.id.barre1);
        //task to do here


}

can anyone help?

Android
  • 8,995
  • 9
  • 67
  • 108
alikyo
  • 56
  • 1
  • 5
  • Not getting exactly, can you please elaborate it? – Paresh Mayani Jul 25 '11 at 10:19
  • use setOnClickListener() method – AndroidDev Jul 25 '11 at 10:26
  • The navigation bar is a custom class that extends the LinearLayout, so I can integrate anywhere in my application as a simple view. I have an activity that contains the bar, and I am looking for a way to dispatch the click on the button to execute some tasks on my activity as if it was the listener of this event. (sorry I'm not a good as that in English) ^^ – alikyo Jul 25 '11 at 10:31

4 Answers4

3

I believe what you are looking for is button.performClick()

and if you don't want the click sound, you can do:

button.setSoundEffectsEnabled(false)
Ovi
  • 362
  • 3
  • 16
hitch45
  • 472
  • 4
  • 12
0

You can actually use EventBus here: http://greenrobot.org/eventbus/

  1. Load the EventBus

    dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:24.1.1' compile 'com.android.support:support-v4:24.1.1' compile 'org.greenrobot:eventbus:3.0.0' }

  2. Create new Event type Class. Let's call it BarrePersonnaliseeEvent.

public class BarrePersonnaliseeEvent {

private boolean _touchDown;
public BarrePersonnaliseeEvent(boolean touchDown) {
    _touchDown = touchDown;
}

public boolean isTouchDown(){
    return _touchDown;
} }
  1. Now pass the event to the EventBus

public class BarrePersonnalisee extends LinearLayout implements OnClickListener { Context mycontext; View convertview; ImageButton searchadresse;

    public BarrePersonnalisee(Context context, AttributeSet attrs) {
        super(context, attrs);
        mycontext=context;
        convertview=LayoutInflater.from(mycontext).inflate(R.layout.barre, this);
        searchadresse=(ImageButton)convertview.findViewById(R.id.searchadresse);
        searchadresse.setOnClickListener(this);
    }

    public void onClick(View arg0) {
        switch (arg0.getId()) {
        case R.id.searchadresse:
            //I want to dispatch this event
                EventBus.getDefault().post(new BarrePersonnaliseeEvent(true));
                break;
               }
        }
....
}
  1. Update Activity to start receiving events:

public class TaxiMapActivity extends MapActivity{

BarrePersonnalisee barre;

...
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    barre=(BarrePersonnalisee)this.findViewById(R.id.barre1);

}

@Subscribe(threadMode = ThreadMode.MAIN) public void onBarrePersonnaliseeEvent(BarrePersonnaliseeEvent event) { Toast.makeText(getActivity(), "Button touched: " + event.isTouchDown(), Toast.LENGTH_SHORT).show(); }

}

  1. Make sure to register and unregister for EventBus in your Activity

    @Override public void onStart(Context context) { super.onStart(context); EventBus.getDefault().register(this); }

    @Override public void onStop() { super.onStop(); EventBus.getDefault().unregister(this); }

codesnooker
  • 1,191
  • 10
  • 19
-1

Use this code

            Button b=new Button(this);
            b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                //what you want to do
            }
        })
AndroidDev
  • 4,521
  • 24
  • 78
  • 126
  • The navigation bar is a custom class that extends the LinearLayout, so I can integrate anywhere in my application as a simple view. I have an activity that contains the bar, and I am looking for a way to dispatch the click on the button to execute some tasks on my activity as if it was the listener of this event. (sorry I'm not a good as that in English) ^^ – alikyo Jul 25 '11 at 10:39
-1

Assuming that you need to attach click event to a button inside your navigation bar, you can use setOnClickListener() method, and implements the onClick() method, for example:

    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            //define what you want to do here
            Toast.makeText(getApplicationContext(), "Works", Toast.LENGTH_SHORT).show();
        }
    });

that will show a toast saying "Works" when you click the button.

ayublin
  • 1,855
  • 17
  • 25