-1

I've already used the hidesoftkeymethod to hide my keyboard when pressing a button, however I want the same thing to work for when enter is pressed.

Below is the code for which I want to implement this:

package nl.dut.ide.software.myapplication;

import android.app.LauncherActivity;
import android.content.Context;
import android.content.Intent;
import android.hardware.input.InputManager;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.view.KeyEvent;

import java.util.ArrayList;
import java.util.jar.Attributes;

public class LoginPage extends AppCompatActivity {

EditText Name;
Button Add;
ListView Lv;
Button Reset;
Button Sgame;

private ImageView information;
private PopupWindow popupWindow;
private LayoutInflater layoutInflater;
private RelativeLayout relativeLayout;

//creating arraylist
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;

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

    //Definition of variables
    Name = (EditText) findViewById(R.id.Playername);
    Add = (Button) findViewById(R.id.Addbutton);
    Lv = (ListView) findViewById(R.id.list);
    Reset = (Button) findViewById(R.id.Resetbutton);
    Sgame = (Button) findViewById(R.id.Startgamebutton);

    information = (ImageView) findViewById(R.id.info);
    relativeLayout = (RelativeLayout) findViewById(R.id.constrain);

    adapter = new ArrayAdapter<String>(this, R.layout.listviewlayout, R.id.list_content, list);
    Lv.setAdapter(adapter);

    //Enter to add player name to list + keyboard hide upon input + enter
    final EditText Name = (EditText) findViewById(R.id.Playername);
    class MyKeyListener implements View.OnKeyListener {
        @Override
        public boolean onKey (View v, int keyCode, KeyEvent event) {
            if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                    (keyCode == KeyEvent.KEYCODE_ENTER)) {

                String name = Name.getText().toString();
                if (Name.length() > 0) {
                    list.add(name);
                    adapter.notifyDataSetChanged();
                    Name.getText().clear();
                }
                hideSoftKeyboard(LoginPage.this);
                return true;
            }
            return false;
        }

    }

    Name.setOnKeyListener(new MyKeyListener());
    Add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            String name = Name.getText().toString();
            if (Name.length() > 0) {
                list.add(name);
                Name.getText().clear();
                adapter.notifyDataSetChanged();
                hideSoftKeyboard(LoginPage.this);
            }
        }
        public void hideSoftKeyboard(LoginPage theActivity) {
            final InputMethodManager inputMethodManager = (InputMethodManager) theActivity.getSystemService(LoginPage.INPUT_METHOD_SERVICE);
            if (inputMethodManager.isActive()) {
                if (theActivity.getCurrentFocus() != null) {
                    inputMethodManager.hideSoftInputFromWindow(theActivity.getCurrentFocus().getWindowToken(), 0);
                }
            }
        }
    });

    Reset.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            list.clear();

        }
    });

    Sgame.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(!list.isEmpty())
                openActivity2();
        }
    });

    information.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
            ViewGroup container = (ViewGroup) layoutInflater.inflate(R.layout.informationpopup, null);

            popupWindow = new PopupWindow(container, 1150, 2000, true);
            popupWindow.showAtLocation(relativeLayout, Gravity.NO_GRAVITY, 140, 300);

            container.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    popupWindow.dismiss();
                    return true;
                }
            });

        }
    });
}

public void openActivity2() {
    Intent intent = new Intent(this, Gamescreen.class);
    String s= list.get(0);
    intent.putExtra("Name1", s);
    startActivity(intent);
}}
  • @Adhi here is the full code. Hope this helps to see where the problem lies. - @Adhi here is the full code. Hope this helps to see where the problem lies

1 Answers1

0

Add the following lines in your EditText XML:

android:imeOptions="actionDone"
android:inputType="textImeMultiLine"

EDIT

final EditText Name = (EditText) findViewById(R.id.Playername);
class MyKeyListener implements View.OnKeyListener {
    @Override
    public boolean onKey (View v, int keyCode, KeyEvent event) {
        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                (keyCode == KeyEvent.KEYCODE_ENTER)) {

            String name = Name.getText().toString();
            if (Name.length() > 0) {
                list.add(name);
                adapter.notifyDataSetChanged();
                Name.getText().clear();
            }
            InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(textInput.getWindowToken(), 0);  

            return true;
        }
        return false;
    }          

}

Name.setOnKeyListener(new MyKeyListener());
Add.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view) {
        String name = Name.getText().toString();
        if (Name.length() > 0) {
            list.add(name);
            Name.getText().clear();
            adapter.notifyDataSetChanged();
            hideSoftKeyboard(LoginPage.this);
        }
    }
    public void hideSoftKeyboard(LoginPage theActivity) {
        final InputMethodManager inputMethodManager = (InputMethodManager) theActivity.getSystemService(LoginPage.INPUT_METHOD_SERVICE);
        if (inputMethodManager.isActive()) {
            if (theActivity.getCurrentFocus() != null) {
                inputMethodManager.hideSoftInputFromWindow(theActivity.getCurrentFocus().getWindowToken(), 0);
            }
        }
    }
});

Reset.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        list.clear();

    }
});

Sgame.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if(!list.isEmpty())
            openActivity2();
    }
});

information.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
        ViewGroup container = (ViewGroup) layoutInflater.inflate(R.layout.informationpopup, null);

        popupWindow = new PopupWindow(container, 1150, 2000, true);
        popupWindow.showAtLocation(relativeLayout, Gravity.NO_GRAVITY, 140, 300);

        container.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                popupWindow.dismiss();
                return true;
            }
        });

        }
    });
}

public void openActivity2() {
    Intent intent = new Intent(this, Gamescreen.class);
    String s= list.get(0);
    intent.putExtra("Name1", s);
    startActivity(intent);
}}
Abhi
  • 3,013
  • 1
  • 14
  • 32