Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9251347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:40:33+00:00 2026-06-18T10:40:33+00:00

I’ve recently had a problem with Android Keyboard views. What I wanted to achieve

  • 0

I’ve recently had a problem with Android Keyboard views.

What I wanted to achieve was a decent keyboard, where I could add key spacing in pixels and the rest of the space would be shared by the keys depending on how I weighted them.

I also found percentage widths were inaccurate (rows ends varied by +-6px).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T10:40:34+00:00Added an answer on June 18, 2026 at 10:40 am

    Each key is given a width, usually 64 pixels. For a half-normal width key I would assign 32px. For double I gave it 128px. Etc.

    The keyboard is then run through a function called “fixKeyboard” which decides upon the width for each key. You tell it how many 64px width keys per row, and it will scale the keys to a size that works. (i.e keysPerStandardRow == how many pixels wide the keyboard is / 64px)

    private void fixKeyboard(Keyboard k, int keysPerStandardRow)
    {
        List<Key> keys = k.getKeys();
        int dw = GlobalHelperFunctions.getDisplay(this).getWidth();
        int ttly = 0;
        int divisor = 64 * keysPerStandardRow;
        int ttl_weights = 0;
        for (Key key : keys)
        {
            //See below for the deal with 424242
            int weight = key.width + (key.gap == 424242 ? 0 : key.gap);
            if (key.gap == 424242)
                key.gap = 0;
            else
                key.gap = (ttl_weights + key.gap) * dw / divisor - ttl_weights * dw / divisor;
            key.width = (ttl_weights + key.width) * dw / divisor - ttl_weights * dw / divisor;
            if (key.y != ttly)
            {
                ttl_weights = 0;
                ttly = key.y;
            }
            key.x = ttl_weights * dw / divisor;
            ttl_weights += weight;
        }
    }
    
    ...
    
    static public Display GlobalHelperFunctions.getDisplay(Context c)
    {
        if (c != null)
        {
            WindowManager wm = (WindowManager)c.getSystemService(Context.WINDOW_SERVICE);
            if (wm != null)
            {
                return wm.getDefaultDisplay();
            }
        }
        return null;
    }
    

    Alas, this was not the end of the story. It seems that the keyboard view/keyboard decides upon the width of the keyboard the moment you hand it the XML file. If for some reason you’ve changed the keys like I have, android scales them back into its pre-calculated box. I didn’t want this, obviously. So here’s what I did: I simply forced it to have the maximum possible width by defining a huge horizontal gap in between each key (424242px), which I then reset to 0 when running fixKeyboard.

    Since I was using this approach, you can define gaps easily by simply not using the trigger number! Here’s my QWERTY keyboard you can use if you’d like as an example:

    <?xml version="1.0" encoding="utf-8"?>
     <Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
             android:keyHeight="80px"
             android:horizontalGap="424242px"
             android:verticalGap="2px" >
         <Row android:keyWidth="64px">
             <Key android:keyLabel="1" android:keycode="KEYCODE_1"/>
             <Key android:keyLabel="2" android:keycode="KEYCODE_2"/>
             <Key android:keyLabel="3" android:keycode="KEYCODE_3"/>
             <Key android:keyLabel="4" android:keycode="KEYCODE_4"/>
    
             <Key android:keyLabel="5" android:keycode="KEYCODE_5"/>
             <Key android:keyLabel="6" android:keycode="KEYCODE_6"/>
             <Key android:keyLabel="7" android:keycode="KEYCODE_7"/>
             <Key android:keyLabel="8" android:keycode="KEYCODE_8"/>
    
             <Key android:keyLabel="9" android:keycode="KEYCODE_9" />
             <Key android:keyLabel="0" android:keycode="KEYCODE_0"/>
             <Key android:keyIcon="@android:drawable/ic_input_delete" android:keyOutputText="◁" android:keyWidth="128px" android:codes="0x25C1" android:isRepeatable="true"/>
         </Row>
         <Row android:keyWidth="64px">
             <Key android:keyLabel="q" android:keycode="KEYCODE_Q" />
             <Key android:keyLabel="w" android:keycode="KEYCODE_W"/>
             <Key android:keyLabel="e" android:keycode="KEYCODE_E" />
             <Key android:keyLabel="r" android:keycode="KEYCODE_R" />
    
             <Key android:keyLabel="t" android:keycode="KEYCODE_T"/>
             <Key android:keyLabel="y" android:keycode="KEYCODE_Y"  />
             <Key android:keyLabel="u" android:keycode="KEYCODE_U"/>
             <Key android:keyLabel="i" android:keycode="KEYCODE_I" />
    
             <Key android:keyLabel="o" android:keycode="KEYCODE_O" />
             <Key android:keyLabel="p" android:keycode="KEYCODE_P" />
             <Key android:keyLabel="・"/>
             <Key android:keyIcon="@android:drawable/ic_menu_preferences" android:keyOutputText="●" android:codes="0x25CF"/>
         </Row>
         <Row android:keyWidth="64px">
             <Key android:keyLabel=" " android:keyHeight="0px" android:keyWidth="0px" android:horizontalGap="32px"/>
             <Key android:keyLabel="a" android:keycode="KEYCODE_A"/>
             <Key android:keyLabel="s" android:keycode="KEYCODE_S"/>
             <Key android:keyLabel="d" android:keycode="KEYCODE_D"/>
    
             <Key android:keyLabel="f" android:keycode="KEYCODE_F" />
             <Key android:keyLabel="g" android:keycode="KEYCODE_G"/>
             <Key android:keyLabel="h" android:keycode="KEYCODE_H"/>
             <Key android:keyLabel="j" android:keycode="KEYCODE_J" />
    
             <Key android:keyLabel="k" android:keycode="KEYCODE_K" />
             <Key android:keyLabel="l" android:keycode="KEYCODE_L" />
             <Key android:keyLabel="ENTER" android:keyOutputText="◒" android:keyWidth="96px"/>
             <Key android:keyLabel="カタ"  android:keyOutputText="◎" android:codes="0x25CE"/>
         </Row>
         <Row android:keyWidth="64px">
             <Key android:keyLabel="「" />
             <Key android:keyLabel="z" android:keycode="KEYCODE_Z"/>
             <Key android:keyLabel="x" android:keycode="KEYCODE_X"/>
             <Key android:keyLabel="c" android:keycode="KEYCODE_C"/>
    
             <Key android:keyLabel="v" android:keycode="KEYCODE_V" />
             <Key android:keyLabel="b" android:keycode="KEYCODE_B"/>
             <Key android:keyLabel="n" android:keycode="KEYCODE_N"/>
             <Key android:keyLabel="m" android:keycode="KEYCODE_M"/>
    
             <Key android:keyLabel="、"/>
             <Key android:keyLabel="。"/>
             <Key android:keyLabel="⇧" android:keycode="KEYCODE_SHIFT_RIGHT" android:isModifier="true"/>
             <Key android:keyLabel="HW" android:keyOutputText="◍" android:codes="0x25CD"/>
         </Row>
         <Row android:keyWidth="64px">
             <Key android:keyLabel=" " android:keyWidth="704px"/>
             <Key android:keyLabel="ひら"  android:keyOutputText="◐" android:codes="0x25D0"/>
         </Row>
     </Keyboard>
    

    A few important things: to make a gap BEFORE a key, I just added a 0 width key. <Key android:keyLabel=" " android:keyHeight="0px" android:keyWidth="0px" android:horizontalGap="32px"/>

    Another thing: For the control keys (Like the one that opens the IME menu, I’ve used special characters to alert the code: android:keyOutputText="◐" etc.

    It took me a while to search for the way to open the IME menu, so here’s how:
    How to set/call an new input method in Android

    HTH, all code is public domain

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I've tracked down a weird MySQL problem to the two different ways I was
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have been unable to fix a problem with Java Unicode and encoding. The
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.