0

I am trying to take control of the back button of my phone and set a layout visible invisible when pressed back key but nothing appear. I wonder if its possible to do so.

public boolean onKeyDown(int keyCode, KeyEvent ev) {

    switch(keyCode) {

    case KeyEvent.KEYCODE_BACK:

        if (main_layout.getVisibility() == View.VISIBLE) {

           finish();

        } else if (sub_layout.getVisibility() == View.VISIBLE){

            boutton_radio.setVisibility(View.VISIBLE);

            boutton_radio.setEnabled(true);

            sub_layout.setEnabled(false);

            sub_layout.setVisibility(View.GONE);

            main_layout.setVisibility(View.VISIBLE);

            main_layout.setEnabled(true);
        }


    return true;

    }
    return false;

    }

Using on backey pressed too give the same result

@Override

public void onBackPressed() {

    // TODO Auto-generated method stub

}
SubbaReddy PolamReddy
  • 2,083
  • 2
  • 17
  • 23
Dimitri
  • 1,924
  • 9
  • 43
  • 65

3 Answers3

0

yah I it should work. I used it to do another function and it will work for you too. This is how I called a alert box to be called on back key pressed.

@Override
    public void onBackPressed() {
        final MediaPlayer areusure = MediaPlayer.create(TalkingWebBrowserActivity.this, R.raw.areusure);
        areusure.start();
        AlertDialog alertbox = new AlertDialog.Builder(this)
        .setMessage("Do you want to exit the Browser?")
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {

            // do something when the button is clicked
            public void onClick(DialogInterface arg0, int arg1) {

                finish();

            }
        })
        .setNegativeButton("No", new DialogInterface.OnClickListener() {

            // do something when the button is clicked
            public void onClick(DialogInterface arg0, int arg1) {
                           }
        })
          .show();

    }
Amalan Dhananjayan
  • 2,277
  • 1
  • 34
  • 47
  • He is not asking about how to add dialog in backpressed please read question first !!! – Kosh Feb 13 '13 at 09:03
0

You're on the right path. :)

public void onBackPressed() {

if (main_layout.getVisibility() == View.VISIBLE) {

       finish();

    } else if (sub_layout.getVisibility() == View.VISIBLE){

        boutton_radio.setVisibility(View.VISIBLE);

        boutton_radio.setEnabled(true);

        sub_layout.setEnabled(false);

        sub_layout.setVisibility(View.GONE);

        main_layout.setVisibility(View.VISIBLE);

        main_layout.setEnabled(true);
    }

}

JanithaR
  • 1,094
  • 2
  • 11
  • 23
0

Why u checking two different things in ur if else. If u put in ur else if(main_layout.getVisability instead of sub main. Try it and tell me if that solved or no

Kosh
  • 6,140
  • 3
  • 36
  • 67