0

I'm making an alternative keyboard app for Android and I want to force this keyboard into permanent landscape mode (it looks terrible otherwise).

I know how to do this if it were an activity

android:screenOrientation="landscape"

but of course my main class extends InputMethodService rather than Activity so I can't use that. The user interface is drawn using getLayoutInflater().inflate() on an XML file.

Thanks

tmdst
  • 1
  • 1
  • What do you mean by landscape mode? Do you mean always come up in extract mode (the keyboard with the big rectangular edit box on top that comes up full screen over the app)? Or do you mean force the entire activity into landscape mode as well (in which case the answer is you can't, the activity controls that not the keyboard service). – Gabe Sechan Dec 04 '15 at 02:01
  • Why make it a service? A service is basically an activity without UI contracts (A Service is an application component that can perform long-running operations in the background and does not provide a user interface). Have you look here: http://developer.android.com/guide/topics/text/creating-input-method.html – Frank B. Dec 04 '15 at 02:03
  • You can also take a look at another post that has a lot of activity. Only helpful if you havent gotten far tho... http://stackoverflow.com/questions/9577304/how-to-make-a-android-custom-keyboard – Frank B. Dec 04 '15 at 02:04
  • I'm not actually using the extract mode at all and so I've not written the `onCreateExtractTextView` method. You're right, I meant forcing the (I guess there's an underlying) activity into landscape mode. – tmdst Dec 04 '15 at 02:10
  • Frank, when I first created the idea I drew it up as an Activity. However, in order to make it work as a keyboard and send text to other applications, I had to make it extend `InputMethodService`. This allowed it to be selected from available keyboards in settings, as well as be used to enter text as a standard keyboard. How can I go back to doing this as an activity? – tmdst Dec 04 '15 at 02:17

1 Answers1

0

As Gabe said, you can't control the orientation of the app.

What you can do is, if the app is in portrait mode, don't show the keyboard but display a message saying 'the keyboard works only in landscape mode'.

This can be done using the onConfigurationChanged() method.

@Override
public void onConfigurationChanged(Configuration newConfig) {
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        IBinder token = getWindow().getWindow().getAttributes().token;

        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            imm.showSoftInputFromInputMethod(token, InputMethodManager.SHOW_FORCED);
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            imm.hideSoftInputFromInputMethod(token, InputMethodManager.RESULT_HIDDEN);
            Toast.makeText(getApplicationContext(),"Keyboard works only in landscape mode!",Toast.LENGTH_LONG).show();
        }
}

Ans also in onStartInputView() method put a check:

@Override
    public void onStartInputView(EditorInfo info, boolean restarting) {
        super.onStartInputView(info, restarting);
        if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
            //hide the keyboard & display a message (same as onConfigurationChanged())
        } else{
            //dont do anything...we are good to go!
        }
    }
Prasad Pawar
  • 1,606
  • 1
  • 15
  • 30