Hide toolbar and tablayout with searchview - java

I have the following layout:
<LinearLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activities.MainActivity">
<include
android:id="#+id/app_toolbar"
layout="#layout/toolbar" />
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:tabGravity="fill"
app:tabMaxWidth="0dp"
app:tabMode="fixed" />
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="#id/tab_layout" />
The menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
android:icon="#drawable/ic_settings_white_24dp"
app:showAsAction="never" />
<item
android:id="#+id/search"
android:icon="#drawable/ic_search_white_24dp"
android:title="#string/search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView"/>
and the code for the searchView:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.options_menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
this.searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
//searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String query) {
search(query);
return true;
}
});
return true;
}
The tablayout contains 2 tabs for different purposes.
I also have a searchview as option menu in my toolbar.
I would like the searchview to hide the toolbar as well as the tablayout when the searchview is clicked. You can have a look at the Whatsapp or Youtube search feature to understand what I mean.
Any ideas?
Thanks

For that you can create custom search view like this.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/topLayout"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:layout_alignParentTop="true"
android:orientation="vertical">
<LinearLayout
android:id="#+id/searchViewLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/shadow"
android:orientation="horizontal"
android:visibility="gone">
<ImageView
android:id="#+id/ic_back"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center|start"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:contentDescription="#string/app_name"
android:gravity="center|start"
android:padding="5dp"
android:src="#drawable/backspase" />
<EditText
android:id="#+id/search_view"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:focusable="true"
android:gravity="center|start"
android:hint="#string/search_hint"
android:imeOptions="actionDone"
android:padding="5dp"
android:singleLine="true" />
<ImageView
android:id="#+id/clearSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:contentDescription="#string/app_name"
android:padding="5dp"
android:src="#drawable/ic_cross" />
</LinearLayout>
<include layout="#layout/toolbar" />
</LinearLayout>
<View
android:id="#+id/shadow"
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_below="#+id/topLayout"
android:background="#color/colorPrimary"
android:visibility="visible" />
<android.support.design.widget.TabLayout
android:id="#+id/tabHost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/shadow"
android:background="#color/colorPrimary"
app:tabGravity="fill"
app:tabIndicatorColor="#color/ColorWhite"
app:tabMode="fixed"
app:tabSelectedTextColor="#color/ColorWhite"
app:tabTextColor="#85A3B1" />
<peacocktech.in.mybulletin.utils.NonSwipeableViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/tabHost" />
</RelativeLayout>
After That use this code for activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, 1, Menu.NONE, getResources().getString(R.string.action_update)).setIcon(R.drawable.ic_search).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case 1:
// Visible Search layout (R.id.searchViewLayout)
preferencesUtility.setString("search_open", "true");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
onbackpress check searchview open or not
#Override
public void onBackPressed() {
if (preferencesUtility.getString("search_open").equals("true")) {
// Hide/Visibility gone Search layout (R.id.searchViewLayout)
} else {
finish
}
}
Do not Show/Hide toolbar
only set searchViewLayout visibility hide/shown.

Related

When click searchbar it the value below come up

its image showing value and search bar
it is the image showing when clicking the search bar it will come up the values in list-view
I want to know why this happen and how to avoid the situation please help me thanks in advance
public class ListItem extends AppCompatActivity
{
ListView listView1;
ArrayList<String> listproduct;
ArrayAdapter<String > adapternew;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listitem);
ActionBar actionBar=getSupportActionBar();
actionBar.setTitle("Please Select the Item");
SearchView search=(SearchView)findViewById(R.id.Search);
ScrollView scroll=(ScrollView)findViewById(R.id.Scroll);
ListView listView1=(ListView)findViewById(R.id.Listview);
listproduct=new ArrayList<>();
listproduct.add("Stove Top Stuffing");
listproduct.add("Campell's Soup");
listproduct.add("Tide");
listproduct.add("Pampers");
listproduct.add("Pepsi Products");
listproduct.add("Tang");
listproduct.add("Top Ramen");
listproduct.add("Knorr");
listproduct.add("Palmolive");
listproduct.add("Scotch-Brite");
listproduct.add("Bounty Paper Towls");
listproduct.add("Oreo Cookies");
listproduct.add("Quaker Oats");
listproduct.add("Lays Potato Chips");
adapternew = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,listproduct);
listView1.setAdapter(adapternew);
SearchManager manager=(SearchManager)getSystemService(Context.SEARCH_SERVICE);
search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
search.clearFocus();
if(listproduct.contains(query))
{
adapternew.getFilter().filter(query);
}
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
adapternew.getFilter().filter(newText);
return false;
}
});
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int newposition, long id) {
Toast.makeText(ListItem.this,listproduct.get(newposition)+"",Toast.LENGTH_SHORT).show();
Intent i=new Intent(ListItem.this,AddOrder.class);
i.putExtra("position",listproduct.get(newposition));
//adapternew.notifyDataSetChanged();
startActivity(i);
}
});
}
}
here is the code that is to show the list view item and i want to know whether the value in list view come up when clicking the search bar
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SearchView
android:id="#+id/Search"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentTop="true"
android:iconifiedByDefault="false"
android:queryHint="Search Here"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.00" />
<ScrollView
android:id="#+id/Scroll"
android:layout_width="100dp"
android:layout_height="150dp"
tools:layout_editor_absoluteX="-8dp"
tools:layout_editor_absoluteY="396dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar2"
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
tools:layout_editor_absoluteX="9dp"
tools:layout_editor_absoluteY="2dp" />
<ListView
android:id="#+id/Listview"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_below="#id/Search"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.100"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.200">
</ListView>
</androidx.constraintlayout.widget.ConstraintLayout>
and this is the xml file of the listview
I think this is the code you are wanting.
No need to use scrollview here because ListView itself is scrollable. LinearLayout with Vertical Orientation will solve your issue
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar2"
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
tools:layout_editor_absoluteX="9dp"
tools:layout_editor_absoluteY="2dp" />
<SearchView
android:id="#+id/Search"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentTop="true"
android:iconifiedByDefault="false"
android:queryHint="Search Here"/>
<ListView
android:id="#+id/Listview"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="10dp">
</ListView>
</LinearLayout>
I think with ConstraintLayout , you can use app:layout_constraintTop_toBottomOf to assign it below the desired view instead of layout_below
<ListView
android:id="#+id/Listview"
android:layout_width="match_parent"
android:layout_height="300dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.100"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/search" <!-- change here -->
app:layout_constraintVertical_bias="0.200">

The Toolbar shows only the Title from the Item in menu_main for toolbar

The Toolbar shows only the Title from the Item in menu_main for toolbar
My MainActivity Toolbar
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
style="#style/MyToolbar"
app:theme="#style/Theme.Mytheme"
android:elevation="4dp"
/>
menu_main.xml
<?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/coins_value_item"
android:orderInCategory="4"
android:actionLayout="#layout/coin_bar"
android:title="OnlyThisTitleShownOnPhone"
app:showAsAction="always"/>
</menu>
coin_bar
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
style="?attr/actionButtonStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="50dp"
android:paddingVertical="5dp"
android:background="#drawable/bg_round_corner"
android:layout_gravity="center_vertical">
<ImageView
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:src="#drawable/coin_icon"/>
<TextView
android:id="#+id/coins_value_text_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:layout_marginLeft="25dp"
android:textSize="25sp"
android:textColor="#color/white"
android:fontFamily="#font/main_font"
android:text="1000" />
</FrameLayout>
MainActivity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
Correctly shown in Android Studio:
What's wrong in that situation?
Incorrectly displayed only on the device, namely, only the title property of the item itself is displayed
I just changing this:
android:actionLayout="#layout/coin_bar"
to this:
app:actionLayout="#layout/coin_bar"

Android Studio have different toolbar items on different activity's

I am very new when it comes to android app development and android studio.
I have been searching around for some couple days but still havent found the solution.
How can i have different toolbar Items on different activity's is their an way to do that ?
Do you have an example so i could take a look at that?
Do you know an tutorial that has that in it?
already much thanks for the help.
MainActivity.java
package com.example.stage.absa;
import android.Manifest;
import android.content.ClipData;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class MainActivity extends AbsRuntimePermission {
private ZXingScannerView scannerView;
private static final int REQUEST_PERMISSION = 10;
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
requestAppPermissions(new String[]{
Manifest.permission.CAMERA},
R.string.msg, REQUEST_PERMISSION);
}
#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) {
int id = item.getItemId();
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (id == R.id.action_settings) {
setContentView(R.layout.activity_settings);
return true;
}
return super.onOptionsItemSelected(item);
}
public void browser1(View view) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.suppliance.nl/"));
startActivity(browserIntent);
}
public void scanCode(View view) {
scannerView = new ZXingScannerView(this);
scannerView.setResultHandler(new ZxingScannerResultHandler());
setContentView(scannerView);
scannerView.startCamera();
}
class ZxingScannerResultHandler implements ZXingScannerView.ResultHandler {
#Override
public void handleResult(Result result) {
String resultCode = result.getText();
Toast.makeText(MainActivity.this, resultCode, Toast.LENGTH_SHORT).show();
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
scannerView.stopCamera();
}
}
#Override
public void onPermissionsGranted(int requestCode) {
//Do anything when permisson granted
Toast.makeText(getApplicationContext(), "Toegang geaccepteerd", Toast.LENGTH_LONG).show();
}
}
activity_main.xml
<?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
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleTextColor="#android:color/white"
android:background="?attr/colorPrimary">
</android.support.v7.widget.Toolbar>
<!-- Layout for content is here. This can be a RelativeLayout -->
<RelativeLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.stage.absa.MainActivity">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="browser1"
android:layout_margin="2dp"
android:src="#drawable/suppliance" />
<Button
android:id="#+id/S"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="scanCode"
android:layout_margin="5dp"
android:backgroundTint="#color/colorPrimary"
android:text="Scan" />
</RelativeLayout>
</LinearLayout>
SettingsActivity.java
package com.example.stage.absa;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
public class SettingsActivity extends AppCompatActivity {
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
#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_settings, menu);
return true;
}
}
activity_settings.xml
<?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
android:id="#+id/toolbar"
android:layout_width="match_parent"
app:titleTextColor="#android:color/white"
android:layout_height="?attr/actionBarSize">
<ImageView
android:id="#+id/tv_header_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginRight="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Name"
android:textColor="#ffffff"
android:gravity="center"
android:id="#+id/brand_name" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_marginLeft="5dp"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C Name :"
android:layout_gravity="right"
android:layout_marginRight="20dp"
android:textColor="#ffffff"
android:id="#+id/t1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User Name :"
android:layout_gravity="right"
android:layout_marginRight="20dp"
android:textColor="#ffffff"
android:id="#+id/t2" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
<!-- Layout for content is here. This can be a RelativeLayout -->
<RelativeLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.stage.absa.MainActivity">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Settings" />
</RelativeLayout>
</LinearLayout>
menu_settings.xml
<?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">
<item
android:id="#+id/action_settings"
android:icon="#drawable/ic_action_back"
android:title="Back"
app:showAsAction="ifRoom" />
</menu>
menu_main.xml
<?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">
<item
android:id="#+id/action_settings"
android:icon="#drawable/ic_action_settings"
android:title="Settings"
app:showAsAction="ifRoom" />
</menu>
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu1, menu);
return true;
}
Just pass different menu resources for the different activities ie R.id.menu1 or R.menu.menu2.
You can set custom toolbar layout for any activity like.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
app:titleTextColor="#android:color/white"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/AppTheme.PopupOverlay">
<ImageView
android:id="#+id/tv_header_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginRight="20dp"
android:src="#drawable/siyaram_logo"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Name"
android:textColor="#ffffff"
android:gravity="center"
android:id="#+id/brand_name" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_marginLeft="5dp"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C Name :"
android:layout_gravity="right"
android:layout_marginRight="20dp"
android:textColor="#ffffff"
android:id="#+id/t1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User Name :"
android:layout_gravity="right"
android:layout_marginRight="20dp"
android:textColor="#ffffff"
android:id="#+id/t2" />
</LinearLayout>
</android.support.v7.widget.Toolbar>

Centering the 3 dots of the setting or the search

As you can see, the 3 dots of the menu or the search button are in the top third of the tool bar. How can I center it?
If its relevant - some of my XML's:
tool_bar.xml:
<?xml version="1.0" encoding="utf-8"?>
<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/ColorPrimary"
android:elevation="4dp" >
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="start"
android:src="#drawable/app_icon" />
</android.support.v7.widget.Toolbar>
activity_main.xml:
<include
android:id="#+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
layout="#layout/tool_bar" >
</include>
<other views>
searchable.xml:
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="#string/hint_search"
android:icon="#drawable/magnifying_glass"
android:label="#string/app_name"
android:searchSuggestAuthority="com.app.recommendations.content_provider"
android:searchSuggestIntentAction="android.intent.action.VIEW"
android:searchSuggestIntentData=""
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer" />
main_menu.xml:
...
<item
android:id="#+id/search"
android:icon="#drawable/magnifying_glass"
android:title="#string/hint_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|ifRoom"/>
...
Change the code of your too_bar.xml
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="start"
android:src="#drawable/app_icon" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
You can create a custom ToolBar with ActionMenuView.
In your toolbar.xml
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" >
<android.support.v7.widget.ActionMenuView
android:id="#+id/action_menu"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="?attr/actionBarSize"/>
</android.support.v7.widget.Toolbar>
In your MainActivity,
public class MainActivity extends AppCompatActivity {
ActionMenuView actionMenuView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
actionMenuView = (ActionMenuView) toolbar.findViewById(R.id.action_menu);
actionMenuView.setOnMenuItemClickListener(new ActionMenuView.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
return onOptionsItemSelected(menuItem);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, actionMenuView.getMenu());
return true;
}
}
Set ActionViewMenu's gravity to 'center'. I hope it will help you.
Instead of using android:layout_weight="2" and android:layout_height="0dp", I just setted the height to wrap_content and that made the toolbar lean mean and centered!

Android NavigationView drawer not closing

Implementing a NavigationView drawer - I am able to generate the drawer and see it, but cannot close it with swiping. Additionally, the NavigationItemSelectedListener does not appear to be set up correctly, as I cannot detect click events on the items.
MainActivity.java
private NavigationView navigationView;
private DrawerLayout navDrawerLayout;
private ListView navDrawerList;
private ArrayList<NavDrawerItem> navDrawerItemList;
private ActionBarDrawerToggle navDrawerToggle;
private void setupNavDrawer() {
this.navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
Utility.showDebugToast(String.valueOf(item.getItemId()));
if(item.isChecked()) {
item.setChecked(false);
} else {
item.setChecked(true);
}
navDrawerLayout.closeDrawers();
return true;
}
});
this.navDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
this.navDrawerList = (ListView) findViewById(R.id.left_drawer);
this.navDrawerItemList = new ArrayList<>();
this.navDrawerItemList.add(new NavDrawerItem("NAME", NavDrawerItemType.NAME));
this.navDrawerItemList.add(new NavDrawerItem("Create Mesh", NavDrawerItemType.CREATE_MESH));
this.navDrawerList.setAdapter(new NavDrawerAdapter(this, R.layout.drawer_item, R.id.drawer_tab_text, this.navDrawerItemList));
this.navDrawerToggle = new ActionBarDrawerToggle(this, navDrawerLayout, R.string.accept, R.string.accept) {
#Override
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}
#Override
public void onDrawerOpened(View view) {
super.onDrawerOpened(view);
}
};
this.navDrawerLayout.addDrawerListener(navDrawerToggle);
navDrawerToggle.syncState();
}
activity.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/purpose_background" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/appbar_grey"
>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
/>
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
style="#style/LobbyTabLayout"
android:layout_width="match_parent"
android:layout_height="44dp"
app:layout_scrollFlags="scroll|enterAlways"
app:tabMode="scrollable"/>
<LinearLayout
android:id="#+id/search_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_scrollFlags="scroll|enterAlways" >
<EditText
android:id="#+id/search_box"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="3"
android:lines="1"
android:textSize="16sp"
android:hint="#string/search_editText_placeholder"/>
<Button
android:id="#+id/action_search_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:text="#string/search_button"/>
</LinearLayout>
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_lobby_container">
<LinearLayout
android:id="#+id/lobby_search_area"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Lobby"
android:focusableInTouchMode="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#+id/grid_fragment"/>
</LinearLayout>
<com.nhaarman.supertooltips.ToolTipRelativeLayout
android:id="#+id/mesh_list_tooltip"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
</FrameLayout>
<FrameLayout
android:id="#+id/dialog_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"/>
<include android:layout_width="match_parent"
android:layout_height="match_parent"
layout="#layout/spinner_overlay"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/toggle_fragment_button"
android:background="#color/background_white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="20dp"
android:alpha=".9"/>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start">
<ListView android:id="#+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#000"
android:alpha="0.7"
/>
</android.support.design.widget.NavigationView>
</android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.DrawerLayout>
drawer_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_tab"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/profile_image"
android:layout_width="76dp"
android:layout_height="76dp"
android:src="#drawable/logo_icon_statusbar"
app:border_color="#FF000000"
android:layout_marginLeft="24dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginStart="24dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingBottom="4dp"
android:id="#+id/drawer_tab_text"
android:layout_alignLeft="#+id/profile_image"
android:layout_alignStart="#+id/profile_image" />
</LinearLayout>
Why can't I slide the Drawer in and out?
With regards to the drawer closing behaviour, I expect this is to do with the structure of your activity.xml.
As per the guidance here:
To use a DrawerLayout, position your primary content view as the first child with a width and height of match_parent. Add drawers as child views after the main content view and set the layout_gravity appropriately.
So this is saying that in your case where you have a content section and one drawer, the DrawerLayout should have two children; first the content, then the drawer. In your xml currently, the DrawerLayout only has one child, everything is in the CoordinatorLayout.
Try moving your NavigationView outside of the CoordinatorLayout so your activity looks something like this:
<DrawerLayout>
<CoordinatorLayout>
content
</CoordinatorLayout>
<NavigationView>
drawer stuff
</NavigationView>
</DrawerLayout>

Categories

Resources