Centering the 3 dots of the setting or the search - java

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!

Related

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 - CollapsingToolbarLayout setHomeButtonEnabled

I'm having trouble implementing HomeUpEnabled when using CollapsingToolbarLayout, I can see the icon but noting happens when I press it.
Here is the CollapsingToolbarLayout
avivity.xml
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
>
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed">
<ImageView
android:id="#+id/header_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#80000000" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
Activity.java
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
I have declared the activity in my manifest. Any help would be appreciated.
----------------------------EDIT-----------------------------------
THIS IS WHAT FIXED IT:
I don't know if it is correct, but it works.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onBackPressed();
}
});
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
And in my xml I changed the background of the toolbar to:
android:background="?android:attr/selectableItemBackground"
to get the ripple effect on the icon.
Hope this helps someone.
Overrited Following method and specify your action in method block with menu id as home :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
drawerLayout.openDrawer(GravityCompat.START); // OPEN DRAWER
return super.onOptionsItemSelected(item);
}
return super.onOptionsItemSelected(item);
}
You have to set the parent activity name in your manifest.xml
Your code is correct but only displays the back arrow on your toolbar, you have to tell your activity which is the activity to come back to.
<activity android:name="YourActivity" android:parentActivityName="YourParentActivity" />
Ok, Create 3 different xml file for activity_main, app_bar and content_main
app_bar
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="#dimen/app_bar_height"
android:fitsSystemWindows="true"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="#color/colorPrimaryDark"
app:contentScrim="?attr/colorPrimaryDark"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/AppTheme.PopupOverlay" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="bottom"
app:tabIndicatorHeight="5dp"
android:background="#color/material_blue_grey_800"
app:tabIndicatorColor="#color/colorPrimaryDark"
app:tabSelectedTextColor="#color/colorPrimaryDark"
app:tabGravity="fill"
app:tabMode="fixed"/>
</android.support.design.widget.CollapsingToolbarLayout>
content_main
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="MainActivity"
tools:showIn="#layout/activity_main">
<android.support.v4.view.ViewPager
android:id="#+id/tabs_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
activity_main
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">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="top"
android:background="#color/colorPrimaryDark"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:minHeight="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:title="" />
<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">
<include
layout="#layout/app_bar"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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/mystuff"
app:menu="#menu/drawermenu" />
</android.support.v4.widget.DrawerLayout>

Android: FloatingActionButton Click Event not working

I have been implementing a FloatingActionButton but somehow hasn't been responding at all. There's no error, nothing happens.
Here is respective Java and xml code:
I have replaced the code in Click event with a Log Event
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.e("ABCD","ABCD");
}
});
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:fitsSystemWindows="true"
tools:context="edu.ahduni.seas.gyapak.MainActivity">
<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:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:clickable="true"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/textlayout"
tools:context="edu.ahduni.seas.gyapak.MainActivity">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:padding="10dp" />
</LinearLayout>
Lets put the Toolbar and AppbarLayout below the fab, and see what happens:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<!-- include your layout here -->
</LinearLayout>
<!-- when the screen is loading, in your case optional -->
<LinearLayout
android:id="#+id/progress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone">
<include layout="#layout/layout_loading"></include>
</LinearLayout>
<!-- when the screen is empty no data, in your case optional -->
<LinearLayout
android:id="#+id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone">
<include layout="#layout/layout_empty"></include>
</LinearLayout>
<!-- a fab -->
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
app:backgroundTint="#009688"
style="#style/FabStyle"
android:layout_gravity="bottom|end"
android:layout_marginBottom="20dp"
android:visibility="invisible" />
<!-- the toolbar layout -->
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:title="#string/app_name"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Dark" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
First check the fab click without the content, then try it with the content in content linear layout.
I modified your layout to this (removed all the content) and the click is
working
<?xml version="1.0" encoding="utf-8"?>
<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:fitsSystemWindows="true">
<!--<include layout="#layout/content_main" />-->
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_gravity="bottom|end"
android:clickable="true"
android:src="#android:drawable/ic_dialog_email"
app:backgroundTint="#FF0000"
app:rippleColor="#FFF" />
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
So your grid view must be taking all the click passthroughs, let me check and get back again.
[Edit]
It is not GridView after all. There was one unnecessary line in the code.
Voila found your problem, remove this line on your onPostExecute() of ParseActivity
MainActivity.this.setContentView(R.layout.activity_main);
near Line number 286 probably.
this sets the layout again and doesn't register the on click listener, thats why when it was in the layout file (DownloadZip), it was working.
If it is necessary for you then reinit the layout i.e. find the fab and put another setOnClickListener. you could put those code in a method and call them on the places you need to reinit layout.
Replace the code of activity_main with these :
<?xml version="1.0" encoding="utf-8"?>
<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:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"></android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="20dp"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
The code is right. Are you sure that you are override
protected void onCreate(#Nullable Bundle savedInstanceState)
instead of
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState)
?
Example:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_incoming_call);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fabCall);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("mylog" , "it happens...");
}
});
}

How to add views under collapsing toolbar layout?

I want to add collapsing toolbar in my app. I want to add a graph layout also image view and text views inside the collapsing toolbar layout. Like this image.
This graph is in collapsing toolbar layout. When we do scroll up graph goes up and remains as a toolbar.
Now I am trying to add views under collapsing toolbar layout. But I am not able to move them and align them. How to do?
Layout:
<android.support.design.widget.CoordinatorLayout 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:id="#+id/parentPanel"
xmlns:fab="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:id="#+id/appbar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_reorder_black_48dp"
android:id="#+id/navigationMenu" />
</android.support.v7.widget.Toolbar>
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_check_box_white_24dp"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffe5e5e5"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:id="#+id/scrollView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="10dp">
<include layout="#layout/card_layout" />
<include layout="#layout/card_layout" />
<include layout="#layout/card_layout" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
app:layout_anchor="#id/appbar"
app:layout_anchorGravity="bottom|right|end"
style="#style/FabStyle"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_anchor = "#id/parentPanel"
app:layout_anchorGravity = "bottom|right|end"
android:padding="20dp"
android:weightSum="1">
<com.github.clans.fab.FloatingActionMenu
android:id="#+id/menu1"
android:layout_width="match_parent"
android:layout_height="219dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
fab:menu_labels_ellipsize="end"
fab:menu_labels_singleLine="true"
fab:menu_backgroundColor="#ccffffff"
fab:menu_fab_label="Menu label"
android:layout_gravity="bottom">
<com.github.clans.fab.FloatingActionButton
android:id="#+id/fab1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_add_white_24dp"
fab:fab_size="mini"
fab:fab_label="addList" />
<com.github.clans.fab.FloatingActionButton
android:id="#+id/fab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_list_white_24dp"
fab:fab_size="mini"
fab:fab_label="list1" />
<com.github.clans.fab.FloatingActionButton
android:id="#+id/fab3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_list_white_24dp"
fab:fab_size="mini"
fab:fab_label="list2" />
<com.github.clans.fab.FloatingActionButton
android:id="#+id/fab4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_list_white_24dp"
fab:fab_size="mini"
fab:fab_label="list3" />
</com.github.clans.fab.FloatingActionMenu>
</LinearLayout>
Activity:
public class MainActivity extends AppCompatActivity {
private CollapsingToolbarLayout collapsingToolbarLayout = null;
private FloatingActionButton fab1;
private FloatingActionButton fab2;
private FloatingActionButton fab3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final FloatingActionMenu menu1 = (FloatingActionMenu) findViewById(R.id.menu1);
collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbarLayout.setTitle("List Title");
ImageView navigationMenu = (ImageView)findViewById(R.id.navigationMenu);
fab1 = (FloatingActionButton) findViewById(R.id.fab1);
fab2 = (FloatingActionButton) findViewById(R.id.fab2);
fab3 = (FloatingActionButton) findViewById(R.id.fab3);
fab1.setOnClickListener(clickListener);
fab2.setOnClickListener(clickListener);
fab3.setOnClickListener(clickListener);
navigationMenu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,NavigationMenuActivity.class));
overridePendingTransition(R.anim.left, R.anim.enter);
}
});
menu1.setOnMenuButtonClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (menu1.isOpened()) {
Toast.makeText(MainActivity.this, menu1.getMenuButtonLabelText(), Toast.LENGTH_SHORT).show();
}
menu1.toggle(true);
}
});
menu1.setClosedOnTouchOutside(true);
dynamicToolbarColor();
toolbarTextAppernce();
}
private void dynamicToolbarColor() {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.b);
Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
#Override
public void onGenerated(Palette palette) {
collapsingToolbarLayout.setContentScrimColor(palette.getMutedColor(getResources().getColor(R.color.colorPrimary)));
collapsingToolbarLayout.setStatusBarScrimColor(palette.getMutedColor(getResources().getColor(R.color.colorPrimaryDark)));
}
});
}
private void toolbarTextAppernce() {
collapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.collapsedappbar);
collapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.expandedappbar);
}
private View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
String text = "";
switch (v.getId()) {
case R.id.fab1:
text = fab1.getLabelText();
break;
case R.id.fab2:
text = fab2.getLabelText();
fab2.setVisibility(View.GONE);
break;
case R.id.fab3:
text = fab3.getLabelText();
fab2.setVisibility(View.VISIBLE);
break;
}
}
};
}
Now I have a single image view inside collapsing toolbar layout and that I can't move or align. How to align view and add views under collapsing toolbar?
Thank you..
CollapsingToolbarLayout is a subclass of FrameLayout.
For maximum flexibility, put all your widgets inside a LinearLayout or RelativeLayout and make it the only child of the CollapsingToolbarLayout. Also for best results with vertical sizing, set the height to a specific dimension.
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="#dimen/graph_toolbar_height"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_collapseMode="parallax">
<!-- graph UI here -->
</RelativeLayout>
</android.support.design.widget.CollapsingToolbarLayout>
Create a layout inside collapsingtoolbarlayout and add your views in there.
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="#color/colorPrimary"
app:expandedTitleGravity="bottom|center_horizontal"
app:expandedTitleMarginBottom="64dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="230dp"
android:scaleType="centerCrop"
android:src="#drawable/ic_about"
app:layout_collapseMode="parallax" />
<TextView
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#color/colorAccent"
android:text="#string/about_title"
android:textSize="#dimen/text_size_large" />
</LinearLayout>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:layout_marginTop="#dimen/status_bar_height"
app:layout_collapseMode="pin"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

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