ladougol ladougol - 12 days ago 8
Android Question

how to insert search view in toolbar?

Bonsoir à tous.J'ai bésoin d'aide,j'ai commencé à développer une application android de consultation d'actualités.J'en suis à l'interface je n'arrive pas insérer une icone search dans le toolbar .
Voilà mes fichiers 1.activity_main.xml, 2.menu_main.xml, 3.MainACtivity, style.xml

1-activity_main.xml

enter code here

<?xml version="1.0``" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">

<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/orange"
android:id="@+id/toolbar"
app:theme="@style/ThemeOverlay.AppCompat.Dark"
app:title="Midi Soleil">

</android.support.v7.widget.Toolbar>

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/drawerLayout">



<FrameLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/containerView">
</FrameLayout>



<android.support.design.widget.NavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="@+id/shitstuff"
app:itemTextColor="@color/black"
app:menu="@menu/drawermenu"
android:layout_marginTop="-24dp"/>



</android.support.v4.widget.DrawerLayout>

</LinearLayout>
enter code here


2-menu_main.xml

enter code here

<?xml version="1.0" encoding="utf-8"?>
<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_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />

<item android:id="@+id/search"
android:title="Actualités"
android:icon="@drawable/search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>

</menu>

enter code here


3-MainActivity

enter code here

package com.fabrice.drawerwithswipetabs;

import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {
DrawerLayout mDrawerLayout;
NavigationView mNavigationView;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;

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

/**
* Setup the DrawerLayout and NavigationView
*/

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mNavigationView = (NavigationView) findViewById(R.id.shitstuff);

/**
* Lets inflate the very first fragment
* Here , we are inflating the TabFragment as the first Fragment
*/

mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.containerView, new TabFragment()).commit();
/**
* Setup click events on the Navigation View Items.
*/

mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
mDrawerLayout.closeDrawers();


if (menuItem.getItemId() == R.id.nav_item_sent) {
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.containerView, new SentFragment()).commit();

}
if (menuItem.getItemId() == R.id.nav_item_alaune) {
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.containerView, new AlauneFragment()).commit();

}

if (menuItem.getItemId() == R.id.nav_item_inbox) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new TabFragment()).commit();
}

return false;
}

});

/**
* Setup Drawer Toggle of the Toolbar
*/

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.app_name,
R.string.app_name);

mDrawerLayout.setDrawerListener(mDrawerToggle);

mDrawerToggle.syncState();
}

/**
* Setup search view in the Toolbar
*/

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



// Associate searchable configuration with the SearchView

/*SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView1 = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()) );

return false;*/

return super.onCreateOptionsMenu(menu);
}


}

enter code here


4-styles.xml

enter code here

<?xml version="1.0" encoding="utf-8"?>
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/orange</item>
<item name="colorPrimaryDark">@android:color/holo_orange_dark</item>
</style>
</resources>

Answer

I - Create a searchable configuration in a searchable.xml saved in res/xml directory.

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

II - Declare your searchable activity in the AndroidManifest.xml file.

<activity android:name=".SearchActivity">  
   <intent-filter>
       <action android:name="android.intent.action.SEARCH" />
   </intent-filter>
   <meta-data android:name="android.app.searchable"
              android:resource="@xml/searchable"/>
</activity>

III- Declare your SearchView inside any layout.xml

<android.support.v7.widget.SearchView  
   android:id="@+id/search"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"/>

STYLING

I. Declare custom styles in your styles.xml file.

<style name="SearchViewTheme" >  
   <item name="colorControlActivated">@color/amber500</item>
   <item name="colorControlNormal">@color/green500</item>
</style>

II. Apply this style

<android.support.v7.widget.SearchView  
    android:id="@+id/search"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:searchIcon="@drawable/ic_library"
    app:theme="@style/SearchViewTheme"/>