I have a strange issue that i cannot solve, i have a little spacing between my settings bar and the toolbar, watch here
my xml for the toolbar:
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
on activity i call it like this:
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
All Acitivity XML (put all my code in external file and include it)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:fitsSystemWindows="true"
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.afcosta.inesctec.pt.android.FamilyLibrary"
android:paddingTop="6dp">
<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"
app:srcCompat="#android:drawable/ic_menu_camera"
app:backgroundTint="#f1c40f"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginEnd="37dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginBottom="33dp" />
<android.support.v7.widget.RecyclerView
android:id="#+id/gallery"
android:layout_width="0dp"
android:layout_height="562dp"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="97dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<include
layout="#layout/custom_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/toolbar"/>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Familias"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="#+id/gallery"
android:layout_marginStart="8dp"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="15dp"
app:layout_constraintLeft_toLeftOf="parent"
android:textSize="24dp"/>
</android.support.constraint.ConstraintLayout>
it appears, but that strange white space is annoying :/
You have to remove padding from ConstraintLayout:
android:paddingTop="6dp"
Related
I need help in my XML layout, since I need to put Toolbar, FrameLayout, BottomNavigationView in one activity, Below is my code, the problem that the toolbar not appeared only the FrameLayout and BottomNavigationView
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true"
xmlns:android="http://schemas.android.com/apk/res/android">
<include
android:id="#+id/app_bar"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
layout="#layout/app_bar_layout" />
<FrameLayout
android:id="#+id/activity_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="#+id/bottom_navigation"
app:layout_constraintTop_toBottomOf="#+id/app_bar"
/>
<View
android:id="#+id/horizontalDivider"
android:layout_width="match_parent"
android:layout_height="0.3dp"
android:layout_above="#+id/bottom_navigation"
android:layout_gravity="center_vertical"
android:background="#android:color/black" />
<include
android:id="#+id/bottom_navigation"
layout="#layout/bottom_navigation"
android:layout_alignParentBottom="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
</android.support.constraint.ConstraintLayout>
Try this
<android.support.constraint.ConstraintLayout 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">
<include
android:id="#+id/app_bar"
layout="#layout/aa"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<FrameLayout
android:id="#+id/activity_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#color/colorAccent"
app:layout_constraintBottom_toTopOf="#+id/horizontalDivider"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/app_bar" />
<View
android:id="#+id/horizontalDivider"
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_gravity="center_vertical"
android:background="#android:color/black"
app:layout_constraintBottom_toTopOf="#+id/bottom_navigation"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/activity_content" />
<include
android:id="#+id/bottom_navigation"
layout="#layout/aa"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/horizontalDivider" />
</android.support.constraint.ConstraintLayout>
OUTPUT
I am new in Android and working on a App. I am trying to develop an App having drawer layout. Everything is fine but I can't change the Title of the drawer menu as marked on the screenshot.
Here is the layout file for your convenience. I have taken assistance from this link:
<?xml version="1.0" encoding="utf-8"?>
<!-- Use DrawerLayout as root container for activity -->
<android.support.v4.widget.DrawerLayout 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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- Layout to contain contents of main body of screen (drawer will slide over this) -->
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/user_home_main_view">
<Button
android:id="#+id/button_mutual_funds_icl"
style="#style/Widget.AppCompat.Button"
android:layout_width="252dp"
android:layout_height="49dp"
android:layout_marginTop="120dp"
android:layout_marginEnd="16dp"
android:text="Mutual Funds of ICL"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/icon_mutual_funds_of_icl"
android:layout_width="51dp"
android:layout_height="49dp"
android:layout_marginStart="28dp"
android:layout_marginTop="120dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toStartOf="#+id/button_mutual_funds_icl"
app:layout_constraintHorizontal_bias="0.384"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/mutual_funds_of_icl" />
<Button
android:id="#+id/button_current_nav"
android:layout_width="252dp"
android:layout_height="49dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:text="Current NAVs"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button_mutual_funds_icl" />
<ImageView
android:id="#+id/icon_current_nav"
android:layout_width="51dp"
android:layout_height="49dp"
android:layout_marginStart="28dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toStartOf="#+id/button_current_nav"
app:layout_constraintHorizontal_bias="0.458"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/icon_mutual_funds_of_icl"
app:srcCompat="#drawable/net_asset_value" />
<Button
android:id="#+id/button_fund_performance"
android:layout_width="250dp"
android:layout_height="49dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:text="Fund Performance"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button_current_nav" />
<ImageView
android:id="#+id/icon_fund_performance"
android:layout_width="51dp"
android:layout_height="49dp"
android:layout_marginStart="28dp"
android:layout_marginTop="26dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toStartOf="#+id/button_fund_performance"
app:layout_constraintHorizontal_bias="0.448"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/icon_current_nav"
app:srcCompat="#drawable/fund_performance" />
<Button
android:id="#+id/button_how_to_invest"
android:layout_width="249dp"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:layout_marginEnd="16dp"
android:text="How to Invest"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button_fund_performance" />
<ImageView
android:id="#+id/icon_how_to_invest"
android:layout_width="53dp"
android:layout_height="49dp"
android:layout_marginStart="28dp"
android:layout_marginTop="36dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toStartOf="#+id/button_how_to_invest"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/icon_fund_performance"
app:srcCompat="#drawable/how_to_invest" />
<Button
android:id="#+id/user_home_button_invest_now"
android:layout_width="214dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="141dp"
android:layout_marginEnd="8dp"
android:background="#color/colorPrimary"
android:text="Invest Now"
android:textColor="#color/colorAccent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.513"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button_how_to_invest" />
</android.support.constraint.ConstraintLayout>
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar" />
</FrameLayout>
<!-- Container for contents of drawer - use NavigationView to make configuration easier -->
<android.support.design.widget.NavigationView
android:id="#+id/user_nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="#menu/drawer_view"
app:headerLayout="#layout/nav_header"/>
</android.support.v4.widget.DrawerLayout>
You can add custom header layout.
https://developer.android.com/training/implementing-navigation/nav-drawer#HeaderLayout
More Customization
If you want more customization for Your Drawer. You can create a Layout for Drawer and include it in android.support.design.widget.NavigationView
Like this
</android.support.v4.widget.DrawerLayout>
...........
<!- Drawer >
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="150dp"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:layout_gravity="left"
>
<!--- Your custom layout >
<include layout="#layout/custom_drawer_layout"
android:id="#+id/filterDrawer"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_gravity="end"
/>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
Then you can create a Layout for custom_drawer_layout
custom_drawer_layout.xml
<RelativeLayout>
<spinner/>
<button/>
<whatever/>
</RelativeLayout>
Hope you get the idea.
You can see a related project here
In my ProfileFragment.java I have a collapsing toolbar.
There is a problem when I scroll the toolbar; my profile image doesn't scroll entirely.
Does the collapsing toolbar need to be inside a Coordinator Layout?
I've tested with relative and I couldn't scroll.
This is a gif showing its behavior:
.
This is my XML code:
<android.support.design.widget.AppBarLayout
android:id="#+id/testeparabackground"
android:layout_width="match_parent"
android:layout_height="203dp"
android:background="?attr/actionBarDivider">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/backgroundcollapsedtoolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="exitUntilCollapsed|scroll">
<ImageView
android:id="#+id/imgFotoCapa"
android:layout_width="446dp"
android:layout_height="203dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:paddingLeft="-32dp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="#drawable/banner_profile"
tools:layout_editor_absoluteY="1dp" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbarid"
android:layout_width="match_parent"
android:layout_height="110dp"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<ImageView
android:id="#+id/picFotoPerfil3"
android:layout_width="117dp"
android:layout_height="103dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="109dp"
android:elevation="50dp"
app:layout_anchor="#+id/testeparabackground"
app:layout_anchorGravity="bottom|center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#android:color/black" />
</android.support.design.widget.CoordinatorLayout>
Add app:layout_behavior="#string/appbar_scrolling_view_behavior" inside your ImageView
TabLayout call and load Fragments. Fragments preview no problem, everythink ok. Problem start after load fragments. BottomNavigationView
android:layout_alignParentBottom="true"
not show in bottom. But change position to top there is no problem, it is show.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:text="#string/yazarlar"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/navigation" />
</android.support.constraint.ConstraintLayout>
Actually, I don't see in your code anything like
android:layout_alignParentBottom="true"
So that means, that in your previous code, you used RelativeLayout as parent, so please post your original code. I would recommend you using something like this:
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/navigation" />
Insert this code android:layout_alignParentBottom="true" not shown in fragment. I try some layout. Orginal layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:text="#string/yazarlar"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
android:layout_alignParentBottom="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/navigation" />
</RelativeLayout>
I am working with a simple application on android where at the moment i net to create a toolbar so i created a custom toolbar like this:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:layout_gravity="center"
android:id="#+id/toolbar_title"
/>
</android.support.v7.widget.Toolbar>
then i included this custom layout on my activity:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:fitsSystemWindows="true"
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.afcosta.inesctec.pt.android.FamilyLibrary">
<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"
app:srcCompat="#android:drawable/ic_menu_camera"
app:backgroundTint="#f1c40f"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginEnd="37dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginBottom="33dp" />
<android.support.v7.widget.RecyclerView
android:id="#+id/gallery"
android:layout_width="0dp"
android:layout_height="562dp"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="97dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<include
layout="#layout/custom_toolbar"
android:id="#+id/toolbar"
tools:layout_constraintTop_creator="1"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="#+id/textView3"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Familias"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="#+id/gallery"
android:layout_marginStart="8dp"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="15dp"
app:layout_constraintLeft_toLeftOf="parent"
android:textSize="24dp"/>
when i try to setText on my activity for the TextView, i fails and i get this error: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.support.v7.widget.Toolbar.findViewById(int)' on a null object reference
here is my code on the activity:
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);;
setSupportActionBar(myToolbar);
TextView toolbarText = (TextView) myToolbar.findViewById(R.id.toolbar_title);
toolbarText.setText("FamÃlias");
Any tip?
Thank you very much!
If I'm not mistaken, you don't need to call findViewById() on your toolbar, you should just be able to use this: findViewById(R.id.toolbar_title)
Your code should end up looking like this:
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
TextView toolbarText = (TextView) findViewById(R.id.toolbar_title);
toolbarText.setText("FamÃlias");
You are giving two different id to toolbar layout
android:id="#+id/my_toolbar"
and in main layout while including you are giving
<include
layout="#layout/custom_toolbar"
android:id="#+id/toolbar" />
make these ids same