Bring up keyboard when clicking on EditText

user2361174

As the title says, I want to bring up the keyboard when I click/tap on the EditText. I've looked at other questions asking for the same thing but the answers just aren't working for me. I don't think the other questions are trying to do this in the onCreate() method and I don't think that they're using this answer for not focusing on the EditText when the app begins. I've added a list of code segments of what I tried and didn't work to avoid redundant answers.

MainActivity.java:

public class MainActivity extends ActionBarActivity {

    private boolean mIsPlaying;
    private int primesLE;
    private GridView mGridView;
    private ImageAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mIsPlaying = false;

        ViewGroup.LayoutParams layoutParams;

        primesLE = 0;
        int numOfColumns = (int)Math.round(Math.sqrt((double) primesLE));
        int numOfRows = (int)Math.ceil((double)primesLE/(double)numOfColumns);

        View relativeLayout = findViewById(R.id.relativeLayout);
        View title_horizontalScrollView = relativeLayout.findViewById(R.id.title_horizontalScrollView);
        View dataLayout = title_horizontalScrollView.findViewById(R.id.dataLayout);
        mGridView = (GridView) dataLayout.findViewById(R.id.mGridView);
        layoutParams = mGridView.getLayoutParams();
        layoutParams.width = 150*numOfColumns; //this is in pixels
        mGridView.setLayoutParams(layoutParams);
        mGridView.setNumColumns(numOfColumns);
        mAdapter = new ImageAdapter(this, android.R.layout.simple_list_item_1, primesLE);
        mGridView.setAdapter(mAdapter);

        View inputLayout = relativeLayout.findViewById(R.id.inputLayout);
        EditText inputEditText = (EditText)inputLayout.findViewById(R.id.inputEditText);

        //list of what doesn't work

        /*
        inputEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                }
            }
        });
        */
        /*
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(inputEditText, InputMethodManager.SHOW_IMPLICIT);
        */

        /*
        inputEditText.setFocusableInTouchMode(true);
        inputEditText.setFocusable(true);
        InputMethodManager imm = (InputMethodManager) MainActivity.this.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(inputEditText.getWindowToken(), 0);
        inputEditText.requestFocus();
        imm.showSoftInput(inputEditText, 0);
        */
        /*        
            inputEditText.setOnKeyListener(new View.OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(
                    inputEditText.getWindowToken(), 0);
                return true;
            }

            });
         */   

    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    private void playSoE(){

        for(int i = 0; i < primesLE; i++){
            mAdapter.setPositionColor(i, 0xffff0000 + 0x100 * i);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        Log.d("Menu","Button Pressed");
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        else if (id == R.id.action_status){
            if(mIsPlaying) {
                mIsPlaying = false;
                item.setIcon(R.drawable.ic_action_play);
                item.setTitle("Play");
                playSoE();
            }
            else {
                mIsPlaying = true;
                item.setIcon(R.drawable.ic_action_pause);
                item.setTitle("Pause");
            }
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/relativeLayout"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/inputLayout"
        android:layout_alignParentTop="true"
        android:background="#a9a9a9"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:id="@+id/textView"
            android:text="All primes lower this number:"
            />

        <!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
        <LinearLayout
            android:focusable="true" android:focusableInTouchMode="true"
            android:layout_width="0px" android:layout_height="0px"/>

        <!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
             to prevent the dummy from receiving focus again -->
        <AutoCompleteTextView android:id="@+id/autotext"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:nextFocusUp="@id/autotext" android:nextFocusLeft="@id/autotext"/>

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:inputType="numberSigned"
            android:ems="10"
            android:id="@+id/inputEditText"
            android:layout_weight="1" />
    </LinearLayout>


    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/inputLayout"

        android:id="@+id/title_horizontalScrollView"
        android:fillViewport="true">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity"
            android:id="@+id/dataLayout"
            >

            <GridView
                android:id="@+id/mGridView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:columnWidth="90dp"
                android:verticalSpacing="10dp"
                android:horizontalSpacing="10dp"
                android:stretchMode="columnWidth"
                android:gravity="center"
                android:layout_alignParentBottom="true" />

        </RelativeLayout>

    </HorizontalScrollView>

</RelativeLayout>

ImageAdapter.java:

public class ImageAdapter extends ArrayAdapter {
    private Context mContext;
    private int mCount;
    private boolean setBlueBackground = false;
    private int[] gridColors;

    public ImageAdapter(Context c, int resource, int count) {
        super(c, resource);
        mContext = c;
        mCount = count;
        gridColors = new int[mCount];
        Arrays.fill(gridColors,Color.LTGRAY);
    }


    public int getCount() {
        return mCount;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView textView;

        if (convertView == null) {
            // if it's not recycled, initialize some attributes
            textView = new TextView(mContext);
            textView.setGravity(Gravity.CENTER);
            textView.setLayoutParams(new GridView.LayoutParams(100, 100));

        } else {
            textView = (TextView) convertView;
        }


        textView.setBackgroundColor(gridColors[position]);

        textView.setText("" + position);
        return textView;
    }

    public void setPositionColor(int position, int color) {
        gridColors[position] = color;
        notifyDataSetChanged();
    }

}

menu_main.xml:

<?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/action_status"
        android:icon="@drawable/ic_action_play"
        android:title="Play"
        app:showAsAction="always"/>
    <item android:id="@+id/action_stop"
        android:icon="@drawable/ic_action_stop"
        android:title="Stop"
        app:showAsAction="always"/>
    <item android:id="@+id/action_settings"
            android:title="@string/action_settings"
            app:showAsAction="never"  />

    </menu>

P.S. I'm trying to bring up the keyboard with numbers only, and figure out how how to close it and execute it too.

Christian Abella

You are not seeing the inputEditText because the AutoCompleteTextView control is overlapping it. Try putting the inputEditText before the autotext and you will see the keyboard with numbers only. I also added android:orientation="vertical" to your XML.

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/inputLayout"
        android:layout_alignParentTop="true"
        android:background="#a9a9a9"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:id="@+id/textView"
            android:text="All primes lower this number:"
            />

        <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberSigned"
        android:ems="10"
        android:id="@+id/inputEditText"/>

        <!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
        <LinearLayout
            android:focusable="true" android:focusableInTouchMode="true"
            android:layout_width="0px" android:layout_height="0px"/>

        <!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
             to prevent the dummy from receiving focus again -->
        <AutoCompleteTextView android:id="@+id/autotext"
                              android:layout_width="fill_parent" android:layout_height="wrap_content"
                              android:nextFocusUp="@id/autotext" android:nextFocusLeft="@id/autotext"/>


    </LinearLayout>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Bring up keyboard when clicking on EditText

From Dev

Keyboard pushing edittext content when clicking

From Dev

How to bring up onboard only when physical keyboard is disabled

From Dev

Clicking button hides keyboard and doesnt hit the action when keyboard is up

From Dev

How to display custom keyboard when clicking on edittext in android

From Dev

Keyboard shortcut to bring up System Monitor

From Dev

Keyboard shortcut to bring up System Monitor

From Dev

Windows 10 tablet taskbar crashes when trying to bring up on-screen keyboard

From Dev

How to bring up GPS location only after clicking button?

From Dev

Keyboard hides EditText when selected

From Dev

How to create an EditText that goes up with the Keyboard on Android

From Dev

Bring up textfield when button is tapped

From Dev

Check blank space when clicking in a EditText

From Dev

How to clear the string in the editText when clicking on it

From Dev

Android: Show Listview when clicking on EditText

From Dev

How to clear the string in the editText when clicking on it

From Dev

Android keyboard not showing when clicking on input in webview

From Dev

How to disable keyboard when clicking a UITextField in iOS?

From Dev

in iOS 8 when clicking on uitextfield keyboard is not open

From Dev

Keyboard doesn't show when clicking into a MultiAutoCompleteTextView

From Dev

Android Disable Keyboard Input when Keyboard is Up

From Dev

Scrollup to textarea when keyboard is up

From Dev

Move layout when edittext show keyboard in fragment

From Dev

EditText hides under keyboard when clicked on

From Dev

retain EditText proportion when adjustResize and soft keyboard are on

From Dev

Android: Clear keyboard input when edittext is cleared

From Dev

Android EditText was not completely visible when keyboard appears

From Dev

Scrolling when keyboard opened Multiple edittext in RecyclerView

From Dev

EditText gets hidden when Keyboard appears

Related Related

  1. 1

    Bring up keyboard when clicking on EditText

  2. 2

    Keyboard pushing edittext content when clicking

  3. 3

    How to bring up onboard only when physical keyboard is disabled

  4. 4

    Clicking button hides keyboard and doesnt hit the action when keyboard is up

  5. 5

    How to display custom keyboard when clicking on edittext in android

  6. 6

    Keyboard shortcut to bring up System Monitor

  7. 7

    Keyboard shortcut to bring up System Monitor

  8. 8

    Windows 10 tablet taskbar crashes when trying to bring up on-screen keyboard

  9. 9

    How to bring up GPS location only after clicking button?

  10. 10

    Keyboard hides EditText when selected

  11. 11

    How to create an EditText that goes up with the Keyboard on Android

  12. 12

    Bring up textfield when button is tapped

  13. 13

    Check blank space when clicking in a EditText

  14. 14

    How to clear the string in the editText when clicking on it

  15. 15

    Android: Show Listview when clicking on EditText

  16. 16

    How to clear the string in the editText when clicking on it

  17. 17

    Android keyboard not showing when clicking on input in webview

  18. 18

    How to disable keyboard when clicking a UITextField in iOS?

  19. 19

    in iOS 8 when clicking on uitextfield keyboard is not open

  20. 20

    Keyboard doesn't show when clicking into a MultiAutoCompleteTextView

  21. 21

    Android Disable Keyboard Input when Keyboard is Up

  22. 22

    Scrollup to textarea when keyboard is up

  23. 23

    Move layout when edittext show keyboard in fragment

  24. 24

    EditText hides under keyboard when clicked on

  25. 25

    retain EditText proportion when adjustResize and soft keyboard are on

  26. 26

    Android: Clear keyboard input when edittext is cleared

  27. 27

    Android EditText was not completely visible when keyboard appears

  28. 28

    Scrolling when keyboard opened Multiple edittext in RecyclerView

  29. 29

    EditText gets hidden when Keyboard appears

HotTag

Archive