0

I want my toolbar to hide while scrolling . But it is not happening . Please help me where I am wrong . I am sharing my code

.enter image description here

below in my Activitymainxml

<?xml version="1.0" encoding="utf-8"?>
<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:background="#DCDCDC"
android:padding="0dp"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="myresolver.faisal.home.com.myresolver.MainActivity">


<include
    android:id="@+id/Coordinate"
    layout="@layout/my_toolbar"></include>



</RelativeLayout>

my my_toolbarxml

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

<android.support.design.widget.CoordinatorLayout  
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/appBarLayout">

    <android.support.v7.widget.Toolbar  
    xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/orange"
        app:layout_scrollFlags="scroll|enterAlways"
        app:titleTextColor="@color/white">         
 </android.support.v7.widget.Toolbar>

 </android.support.design.widget.AppBarLayout>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/Coordinate"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="1dp"
        android:weightSum="4"

        >


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:weightSum="3">

            <ImageView
                style="@style/icon"
                android:background="@drawable/fear_96" />

            <ImageView
                style="@style/icon"
                android:background="@drawable/fear_96"></ImageView>

            <ImageView
                style="@style/icon"
                android:background="@drawable/fear_96" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:weightSum="3">

            <ImageView
                style="@style/icon"
                android:background="@drawable/fear_96" />

            <ImageView
                style="@style/icon"
                android:background="@drawable/fear_96" />

            <ImageView
                style="@style/icon"
                android:background="@drawable/fear_96" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:weightSum="3">

            <ImageView
                style="@style/icon"
                android:background="@drawable/fear_96" />

            <ImageView
                style="@style/icon"
                android:background="@drawable/fear_96" />

            <ImageView
                style="@style/icon"
                android:background="@drawable/fear_96" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:weightSum="3">

            <ImageView
                style="@style/icon"
                android:background="@drawable/fear_96" />

            <ImageView
                style="@style/icon"
                android:background="@drawable/fear_96" />

            <ImageView
                style="@style/icon"
                android:background="@drawable/fear_96" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:weightSum="3">

            <ImageView
                style="@style/icon"
                android:background="@drawable/fear_96" />

            <ImageView
                style="@style/icon"
                android:background="@drawable/fear_96" />

            <ImageView
                style="@style/icon"
                android:background="@drawable/fear_96" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:weightSum="3">

            <ImageView
                style="@style/icon"
                android:background="@drawable/fear_96" />

            <ImageView
                style="@style/icon"
                android:background="@drawable/fear_96" />

            <ImageView
                style="@style/icon"
                android:background="@drawable/fear_96" />
        </LinearLayout>


    </LinearLayout>
</ScrollView>

</android.support.design.widget.CoordinatorLayout> 

My mainactivity

package myresolver.faisal.home.com.myresolver;


import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;



public class MainActivity extends AppCompatActivity {


EditText searchEditText;
boolean isSearchBoxOpen = false;
Toolbar toolbar;

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


    CoordinatorLayout coordinatorLayout=(CoordinatorLayout) 
findViewById(R.id.Coordinate);
   toolbar=(Toolbar) coordinatorLayout.findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

}

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


    return super.onPrepareOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case R.id.search:
            onSearchedHandler();
            break;

        case R.id.setting:
            Toast.makeText(this, "Setting is UnderConstruction",  
Toast.LENGTH_LONG).show();
            break;
    }


    return super.onOptionsItemSelected(item);
}

public void onSearchedHandler() {
    if (isSearchBoxOpen) {

        String word = searchEditText.getText().toString();

        if (word != null) {
            Toast.makeText(this, "Searching for ..." + word,  
Toast.LENGTH_LONG).show();
        }
    } else {

        openSearchBox();

    }

}

@Override
public void onBackPressed() {

    if (isSearchBoxOpen) {

        ActionBar actionBar = getSupportActionBar();

        actionBar.setDisplayShowCustomEnabled(false);
        actionBar.setDisplayShowTitleEnabled(true);
        isSearchBoxOpen = false;

        return;

    }

    super.onBackPressed();
}


@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    if(isSearchBoxOpen)
    {
        outState.putString("EditText",searchEditText.getText().toString());
    }
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    if(savedInstanceState!=null)
    {
       String value=(String) savedInstanceState.get("EditText");
        if(value!=null)
        {
            openSearchBox();
            searchEditText.setText(value);
        }
    }
}

public void openSearchBox()
{
    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowCustomEnabled(true);

    View  
view=LayoutInflater.from(this).inflate(R.layout.search_layout,null);
    ActionBar.LayoutParams actionBarLayoutParams=new  


  ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParam 
s.MATCH_PARENT);

    actionBar.setCustomView(view,actionBarLayoutParams);
    actionBar.setDisplayShowTitleEnabled(false);

    searchEditText = (EditText)  
actionBar.getCustomView().findViewById(R.id.searchEditText);


    searchEditText.requestFocus();
    InputMethodManager keyboard = (InputMethodManager)  
getSystemService(INPUT_METHOD_SERVICE);
    keyboard.showSoftInput(searchEditText,   
InputMethodManager.HIDE_IMPLICIT_ONLY);


    isSearchBoxOpen = true;
 }
}

please help where i am wrong

FaisalAhmed
  • 3,469
  • 7
  • 46
  • 76

0 Answers0