Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how i can close the keyboard when the user press the back icon in my toolbar?

i have try to show by my code the keyboard because the closeListener and the onExpandListener doesn't work.Where i have fail? There is a method that can be invoke when the users press the back button of the toolbar?

image of back toolbar button --> http://imgur.com/WhzT1tp

this is my java code; the class that have the searchview:

public class MainActivity extends ActionBarActivity {

SearchView searchView = null;


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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

    //Title and subtitle
    toolbar.setTitle("Magazzino Chimica");

    setSupportActionBar(toolbar);

    Intent intent = getIntent();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main,menu);

    // Associate searchable configuration with the SearchView
    final SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    searchView = (SearchView) menu.findItem(R.id.action_search)
            .getActionView();
    searchView.setSearchableInfo(searchManager
            .getSearchableInfo(getComponentName()));

    searchView.setIconifiedByDefault(false);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Toast.makeText(getApplicationContext(),"premuto",Toast.LENGTH_LONG).show();
    if (R.id.action_search == item.getItemId()){
        searchView.requestFocus();
        InputMethodManager tastiera = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        tastiera.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    }
    return true;
}

my searchable.xml file:

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/label_ricerca"
    android:hint="@string/suggerimento_ricerca" >
</searchable>

my manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="it.developer.eliax1996.databasegestionariochimica" >

    <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <meta-data
            android:name="android.app.default_searchable"
            android:value=".SearchResultsActivity" />

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />

        </intent-filter>
    </activity>

    <!-- Search results activity -->
    <activity android:name=".SearchResultsActivity"
        android:parentActivityName=".MainActivity" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>

        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>

    </application>

</manifest>

and my menu:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">

    <item
    android:id="@+id/action_search"
    android:title="cerca"
    android:icon="@drawable/ic_search_white"
    app:showAsAction="collapseActionView|ifRoom"
    app:actionViewClass="android.widget.SearchView" />

</menu>

how i can close the keyboard when the users press the back button in the toolbar?

Edit: I tried but when the app is running, if I press the button indicated by the image, the program doesn't pass in this method:

@Override
public boolean onOptionsItemSelected(MenuItem item) {}

Edit x2: I tried but it passes to the method only after 2 clicks, i put the project file online :), sorry for the xml because i am italian and all the writes in the xml resource are italian, but there are only 4 writes :)

project files --> https://drive.google.com/file/d/0B907WWO7eA5bZ1BTUmlIekxDaGs/view?usp=sharing

Edit x3: I followed your advice, but doesn't jet work, can you try my code? :)

like image 853
eliax1996 Avatar asked Oct 19 '22 19:10

eliax1996


1 Answers

In onCreateOptionsMenu(Menu menu), add this line:

// This will add the back arrow to your action bar.
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

In your onOptionsItemSelected(), add this:

// Respond to the action bar's Up/Home button
if (item.getItemId() == android.R.id.home) {
    hideSoftKeyboard(this);
    // finish(), NavUtils.navigateUpFromSameTask(activity) or something like that
}

with the method to hide the keyboard:

public static void hideSoftKeyboard(Activity activity) {
    InputMethodManager inputMethodManager = (InputMethodManager)  activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
like image 168
Robbe Avatar answered Oct 31 '22 20:10

Robbe