1

This is just a small test app/program I am working on that i plan to put into a larger app.

I Have an editText that is not clickable or focusable. I think this is the only way to make it so it wont open the keyboard via touch. The soft keyboard pops up with a button click and enables editing of the edit text.I want editText to become focused (the underline become blue) as soon as the button is clicked so the user knows they are in the field. As of now, only when they begin typing does the underline become blue. I feel like i am missing something obvious.

Also , depending on how I exit out of the app , sometimes when I come back to it , when I click the button, the button itself gets focus , and any typing does not appear in the editText.

<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"
    tools:context="${relativePackage}.${activityClass}" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="110dp"
        android:onClick="showKeyboard"
        android:text="Button" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="37dp"
        android:ems="10" 

        android:clickable="FALSE">

        <requestFocus />
    </EditText>

</RelativeLayout>

JAVA

package com.example.textfield;

    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.inputmethod.EditorInfo;
    import android.view.inputmethod.InputMethodManager;
    import android.widget.Button;
    import android.widget.TextView;

    public class MainActivity extends Activity {

    Button btn;
    TextView txt;

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

        btn = (Button) findViewById(R.id.button1);
        txt = (TextView) findViewById(R.id.editText1);
        txt.setFocusable(false);
        txt.setClickable(false);
        //txt.setImeOptions(EditorInfo.IME_ACTION_DONE);


    }

    public void showKeyboard(View v) {

        txt.setFocusable(true);
        txt.requestFocus();
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);



      }
    }
spacemonkey
  • 133
  • 1
  • 11

1 Answers1

0

Similiar question here,please read

https://stackoverflow.com/a/14327901/4555911

So your code should look like this

Main_activity.xml

...
android:onClick="Clicked"
/>
...

and the MainActivity.java

    public class MainActivity extends Activity{

                    Button btn;
                    Edittext txt;


                    @Override
                    protected void onCreate(Bundle savedInstanceState) {

                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.activity_main);

                        btn = (Button) findViewById(R.id.button1);
                        txt = (EditText) findViewById(R.id.editText1);

                        //txt.setImeOptions(EditorInfo.IME_ACTION_DONE);


          }

    public void Clicked(View view)
        {

                            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
//txt.requestfocus();

        }
     }
Community
  • 1
  • 1
ii7scw
  • 351
  • 1
  • 3
  • 17
  • I want the editText to not be clickable , I only want the keyboard to show up when the button is pushed. My code works as such , Im just focusing on one tiny little thing; when the button is clicked and the keyboard shows up , I would like that little underline part in the editText to become blue , instead of staying grey... I suppose I could set the color of it to holo blue upon button click.. – spacemonkey Mar 05 '15 at 00:38
  • I updated my answer and now,when you click on the button your keyboard will pop up but you won't get focus on the edit text use http://stackoverflow.com/a/17673648/4555911 to apply different colors to text underline – ii7scw Mar 05 '15 at 10:52