2

This problem may seem trivial but I wasn't able to find any nice and simple solution.

I've got an activity with a EditText and a software 'back' Button which simply calls finish() method of activity. When I click on the EditText, there is a soft keyboard shown to input the text.

I want to achieve the following functionality when clicking the 'back' button (exactly the same as it is with the hardware back button):

  • - when the Keyboard is hidden, the onClick method should call finish() to end the activity
  • - when the Keyboard is shown, the onClick methond should hide the keyboard.

    Is there any simple way to do that?

  • kmalmur
    • 2,609
    • 2
    • 26
    • 37
    • 1
      you could check http://stackoverflow.com/questions/2150078/android-is-software-keyboard-shown – nandeesh Jan 12 '12 at 17:31
    • 1
      Why would you want to do exactly what the hardware back button does? There's a reason its there, and users will most probably find it more intuitive also. – blindstuff Jan 12 '12 at 17:47
    • the app will run on low cost tablets. Some of them do not have a back key easily accessed (they look like iPad so they have only one button in front). – kmalmur Jan 13 '12 at 08:56

    1 Answers1

    0

    Keyboard Pasition finding if keyboard is hidden or not? getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

    Selection of Back Button 1)First you have to detect the back key in functionality : here is code: start changing the ‘Back’ button, behavior, you need to override the onKeyDown() method and than check if the desired button has been pressed:

      //Override the onKeyDown method  
        @Override  
        public boolean onKeyDown(int keyCode, KeyEvent event)  
        {  
            //replaces the default 'Back' button action  
            if(keyCode==KeyEvent.KEYCODE_BACK)  
            {  
                //do whatever you want the 'Back' button to do  
                //as an example the 'Back' button is set to start a new Activity named 'NewActivity'  
                this.startActivity(new Intent(YourActivity.this,NewActivity.class));  
            }  
            return true;  
        }  
    

    at least for Android 2.0.

    @Override  
    public void onBackPressed()  
    {  
        //do whatever you want the 'Back' button to do  
        //as an example the 'Back' button is set to start a new Activity named 'NewActivity'  
        this.startActivity(new Intent(YourActivity.this,NewActivity.class));  
    
        return;  
    }  
    
    NovusMobile
    • 2,210
    • 2
    • 18
    • 45