madara09
madara09

Reputation: 81

Android OnclickListener multiple functions in one button

hey guys Im creating a map with button + for the zoom. I want is when i click the button it zoom 20% and when i click it again the button it zoom 50% and click it again zoom 100%. How can i do that?

  Button zooomin = (Button) findViewById(R.id.plus);
    zooomin.init(this);
    zooomin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            MapView.zoomin(20%);
            MapView.zoomin(50%);
            MapView.zoomin(100%);
        }
    });

I hope you can help me about my problem, Im newbie in android developing.

Upvotes: 0

Views: 1071

Answers (8)

Bhargav Ghodasara
Bhargav Ghodasara

Reputation: 378

you have to write like this,

  int count = 0;
  Button zooomin = (Button) findViewById(R.id.plus);
      zooomin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (count == 0) {
                MapView.zoomin(20%);
            } else if (count == 1) {
                MapView.zoomin(50%);
            } else {
                MapView.zoomin(100%);
            }
            count++;
        }
  });

Upvotes: 0

Filipe G. Coelho
Filipe G. Coelho

Reputation: 114

you can insert this variable:

private static int button_state=0;
private static int[] available_zooms = new int[]{20,50,100}

now on your method onClick you insert:

MapView.zoomin(available_zooms[button_state++]);
if(button_state==available_zooms.length)
  button_state=0;

looking like this:

private static int button_state=0;
private static int[] available_zooms = new int[]{20,50,100}
Button zooomin = (Button) findViewById(R.id.plus);

zooomin.init(this);
zooomin.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        MapView.zoomin(available_zooms[button_state++]);
        if(button_state==available_zooms.length)
          button_state=0;
    }
});

Upvotes: 2

Badr
Badr

Reputation: 294

int zoomLevel = 0;
Button zooomin = (Button) findViewById(R.id.plus);
    zooomin.init(this);
    zooomin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(zoomLevel ==0){
                MapView.zoomin(20%);
                zoomLevel = 20;
             }else if(zoomLevel == 20){
                MapView.zoomin(50%);
                zoomLevel = 50;
             }else if(zoomLevel == 50){
                MapView.zoomin(100%);
                zoomLevel = 100;
             }
        }
    });

Upvotes: 0

Bluemarble
Bluemarble

Reputation: 2059

Create a List of Integers storing the zoom values, then increment the index of the list by 1 in each click. When zoom level reaches 100, the next click will reset the zoom level to 20 %.

Button zooomin = (Button) findViewById(R.id.plus);
zooomin.init(this);

ArrayList<Integer> zoomlevels = new ArrayList<Integer>();
zoomlevels.add(20);
zoomlevels.add(50);
zoomlevels.add(100);

int counter = 0;

zooomin.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        MapView.zoomin(zoomlevels[counter]);
        counter++;
        if(counter == 2) counter = 0;
    }
});

Upvotes: 0

Arbaz Ali Rizvi
Arbaz Ali Rizvi

Reputation: 253

Try this

    int mycount = 0;
    Button zooomin = (Button) findViewById(R.id.plus);
    zooomin.init(this);
    zooomin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        ++mycount
            if(mycount == 1){
            String zoom = "20%"}
            else if(mycount == 2){
            String zoom = "50%"}
            else if(mycount == 3){
            String zoom = "100%"}

            MapView.zoomin(zoom);
        }
    });

Upvotes: 1

Aldrin Joe Mathew
Aldrin Joe Mathew

Reputation: 482

You can use MapView.getZoomLevel() method to get the current zoom level and then use a switch or something to find the next zoom level depending on the current zoom level and use MapView.zoomIn() to zoom in.

Upvotes: 0

Fathima km
Fathima km

Reputation: 2699

private int click_count = 0;

k = (Button)findViewById(R.id.button1);
k.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        int count = ++click_count;

        if(count == 1) {

//zoom to 20%
}
        if(count == 2) {

// zoom to 50%
}        
if(count == 3){

/zoom to 100%

}
    }
});

Upvotes: 0

shervinox
shervinox

Reputation: 166

This can be done with different approachs; one might be this:

float zoom = map.getCameraPosition().zoom;

if (zoom<20) {MapView.zoomin(20%);}
else if (zoom>=20 && zoom<50) {MapView.zoomin(50%);}
else if (zoom>=50) {MapView.zoomin(100%);}

Upvotes: 1

Related Questions