CHarris
CHarris

Reputation: 2793

why isn't my editText.requestFocus(); working?

When my app starts the soft key board loads up by default. I do this with

final EditText editText = (EditText) findViewById(R.id.editText);
editText.requestFocus();

in my onCreate method. In my XML my editText has android:inputType="phone", so that's what makes the keyboard appear.

When the user scrolls my listview, the keyboard disappears, which I do with, also in onCreate :

if (scrollState !=0){
    InputMethodManager inputMethodManager = (InputMethodManager)
            getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(listview.getWindowToken(), 0);
}

Now I've put a imagebutton into my app. When the user clicks it, I want the keyboard to start up again, but it's not working. Just wondering why not, because it seems like a pretty basic instruction :

public void softkeyboardButton(View v)
{

    final EditText editText = (EditText) findViewById(R.id.editText);
    editText.requestFocus();
    Log.e("button", "Yep, it worked.");

}

Here's my entire code :

package com.example.chris.sunil_gupta;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.provider.CallLog;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.NumberPicker;

public class MainActivity extends Activity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {

//create a ListView object called listview
        final ListView listview;


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

        listview = (ListView) findViewById(R.id.ListView_2);

        String[] countries =  {"Ireland", "France","England","Ireland", "France","England","Ireland", "France","England","Ireland", "France","England"};

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, countries);

        listview.setAdapter(adapter);

        final EditText editText = (EditText) findViewById(R.id.editText);
        editText.requestFocus();


listview.setOnScrollListener(new AbsListView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {



        Log.e("scrollState", "Yep, it worked.");


        if (scrollState !=0){
            InputMethodManager inputMethodManager = (InputMethodManager)
                    getSystemService(Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(listview.getWindowToken(), 0);
        }


    }
//    this method looks for changes in the edittext box.
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

    }
});



        editText.addTextChangedListener(new TextWatcher() {


            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
//          If there are changes typed in the edittext, then change the height of the edittext from 0,
//            as described in the activity_main xml file
            public void afterTextChanged(Editable s) {

                float density=getResources().getDisplayMetrics().density;

                if (editText.length() > 0) {


//                    not sure what density and layoutparams do. I think we're
//                    converting pixels to dp
                    editText.getLayoutParams().height =(int)(50*density);
//                    editText.requestLayout();
                    Log.e("scrollState", "Yep, it worked.");

                }
//                if there is nothing in edittext, make it invisible
                else if (editText.length() == 0){

                    editText.getLayoutParams().height =(int)(0*density);

                }
                editText.requestLayout();

            }
        });





    }

    public void softkeyboardButton(View v)
    {

        final EditText editText = (EditText) findViewById(R.id.editText);
        editText.requestFocus();
        Log.e("button", "Yep, it worked.");

    }

    //This clears the edittext next time user starts the application, rather than
//    having the same numbers there, which the user probably doesn't want anymore
    protected void onResume() {
        final EditText editText = (EditText) findViewById(R.id.editText);
        super.onResume();
        editText.setText("");


    }

    }

Upvotes: 1

Views: 1096

Answers (0)

Related Questions