Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android EditText enter key listener

Tags:

java

android

Please I have EditText field and I want on Enter key to start some my code, but I still got only new line in EditText field. Please can you help me? Here is my source code: In XML layout:

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hledej_text"
        android:id="@+id/hledej"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="217dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textHledani"
        android:layout_above="@+id/hledej"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:hint="Napis co chces hledat"
        android:imeOptions="actionDone"
        android:inputType="text|textMultiLine"/>
</RelativeLayout>

And MainActivity.java:

public class MainActivity extends ActionBarActivity {

    private EditText textHledani;
    private Button hledej;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textHledani = (EditText)findViewById(R.id.textHledani);
        hledej = (Button)findViewById(R.id.hledej);
        hledej.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(textHledani.getText().toString().isEmpty()){
                    Toast.makeText(v.getContext(),"Nevim co mam hledat!",Toast.LENGTH_LONG).show();
                }
                else{
                    provedHledani();
                }
            }
        });
        textHledani.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if ( (actionId == EditorInfo.IME_ACTION_DONE) || ((event.getKeyCode() == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN ))){
                    provedHledani();
                    return true;
                }
                else{
                    return false;
                }
            }
        });
    }

    private void provedHledani(){
        Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
        intent.putExtra(SearchManager.QUERY,textHledani.getText().toString());
        startActivity(intent);
    }


    @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;
    }

    @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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
like image 661
Pavel Matras Avatar asked Feb 18 '15 20:02

Pavel Matras


1 Answers

Try change EditText in layout to code below:

<EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textHledani"
        android:layout_above="@+id/hledej"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:hint="Napis co chces hledat"
        android:imeOptions="actionDone"
        android:singleLine="true"
        android:lines="1"
        android:inputType="text"/>
like image 93
Konrad Krakowiak Avatar answered Oct 22 '22 13:10

Konrad Krakowiak