SurfaceView as backGround - java

I want to use my surfaceView as the background of my activity
but there is my problem:
if I set setZOrderOnTop(true); it will cover the entire screen on top of everything and nothing is visible on it and if I set it to false surfaceView will be invisible.
here is my activity layout:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".views.activities.IntroActivity">
<!--this is my custom surface view-->
<ir.example.app.views.custom.noise3
android:id="#+id/intro_noise"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0.8"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.v4.view.ViewPager
android:id="#+id/introPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="#+id/nextIntro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="8dp"
android:src="#drawable/next_round_btn_shape"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/lastIntro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:src="#drawable/back_round_btn_shape"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
EDIT: This is my viewPager fragment's layout
<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:layout_width="match_parent"
android:layout_height="match_parent">
<com.hosseinkud.toolbox.utiles.CTV
android:id="#+id/introTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="88dp"
android:layout_marginEnd="8dp"
android:gravity="center"
android:text="some nice text"
android:textColor="#color/white"
android:textSize="#dimen/textVeryLarge"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/introPic"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:src="#drawable/tvtest"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/introTitle"
app:layout_constraintVertical_bias="0.0" />
and this is screenshot when I set setZOrderOnTop(true);:

At the end, I found the answer myself and you should handle it like following by adding setZorderOnTop(false); to your activity and below code to your appTheme :
<style name="Theme.Transparent" parent="AppTheme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowIsFloating">false</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
But I suggest that you simply use textureView instead of surfaceView
it prevents tons of issues you might have and you don't even need to use a transparent background too.

Related

How to make TextView appear when app is run?

My text views aren't appearing when running the app. I've tried adjusting the visibility and elevation. I've looked online and none of the answers worked.
How do I fix this? (Java)
This is the render in android studio
This is the emulator, my physical phone shows the same thing
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="#color/cardview_light_background"
android:elevation="-1dp"
android:theme="#style/ThemeOverlay.GrubhubWheel.FullscreenContainer">
<!-- The primary full-screen view. This can be replaced with whatever view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc. -->
<!-- This FrameLayout insets its children based on system windows using
android:fitsSystemWindows. -->
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<ImageButton
android:id="#+id/gear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#android:color/transparent"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_settings_5672"
tools:ignore="SpeakableTextPresentCheck" />
<TextView
android:id="#+id/Blind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/yaro_st"
android:textAlignment="center"
android:textAppearance="#style/TextAppearance.AppCompat.Display3"
android:textColor="#010101"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Blind" />
<TextView
android:id="#+id/Ate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:fontFamily="#font/galano_grotesque_alt_semi_bold"
android:textAlignment="center"
android:textAppearance="#style/TextAppearance.AppCompat.Display4"
android:textColor="#4ca66c"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/Blind"
tools:text="Ate" />
<ImageButton
android:id="#+id/wheel"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginVertical="10dp"
android:backgroundTint="#android:color/transparent"
android:elevation="-1dp"
android:forceHasOverlappingRendering="true"
android:rotation="15"
android:scaleType="fitCenter"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Ate"
app:layout_constraintVertical_bias="0.85"
app:srcCompat="#drawable/random_wheel"
tools:ignore="SpeakableTextPresentCheck,ImageContrastCheck" />
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>

How to Show/Hide Action Bar on Swipe using Coordinator Layout?

I show/hide the bottom navigation bar on swiping with CoordinatorLayout but am unable to show/hide the action bar using CoordinatorLayout. What mistake I did in my layout?
On swiping, the bottom navigation bar in my layout is auto-hide and show but not on the top action bar?
This is My XML Layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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:orientation="vertical"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/noInternet_layout"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/no_wifi_image"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_wifi_off_24" />
<TextView
android:id="#+id/no_wifi_text"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="#string/noWifi"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="#+id/no_wifi_image"
app:layout_constraintStart_toStartOf="#+id/no_wifi_image"
app:layout_constraintTop_toBottomOf="#+id/no_wifi_image" />
<Button
android:id="#+id/no_wifi_button"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="TRY AGAIN"
app:layout_constraintEnd_toEndOf="#+id/no_wifi_text"
app:layout_constraintStart_toStartOf="#+id/no_wifi_text"
app:layout_constraintTop_toBottomOf="#+id/no_wifi_text" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintLyt"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/toolbar"
layout="#layout/main_toolbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_scrollFlags="scroll|enterAlways" />
<androidx.fragment.app.FragmentContainerView
android:id="#+id/navHostFrag"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="#+id/constraintLyt"
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:layout_constraintTop_toBottomOf="#id/toolbar"
app:layout_constraintVertical_bias="1.0"
app:navGraph="#navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/navView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:itemTextColor="#drawable/bottom_text_selector"
app:itemIconTint="#drawable/bottom_navigation_selector"
app:layout_behavior="#string/hide_bottom_view_on_scroll_behavior"
app:itemTextAppearanceActive="#color/white"
app:labelVisibilityMode="labeled"
app:itemRippleColor="#color/menucolor"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_gravity="bottom"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/bottom_nav_menu" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Please Help me to solve this issue?
Finally I found.
This is very simple to get swipeable (Show-Hide) both side of bottom Navigation and Top ActionBar without any backend code.
Follow Below code
<!--This is your parent layout-->
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
app:elevation="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways|snap"
android:layout_height="?attr/actionBarSize">
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<!--Move your main layout of the screen to a different layout and call it with include. Without including that, you cannot hide one of them Top or Bottom. So move that layout and include it here. (example- #layout/content_main)-->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<include layout="#layout/content_main"/>
</FrameLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_view"
android:layout_width="match_parent"
android:layout_height="#dimen/custom_bottomBar_size"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior"
style="#style/Widget.Custom.BottomNavigationView"
app:itemRippleColor="?android:attr/windowBackground"
app:itemTextAppearanceActive="#color/white"
app:labelVisibilityMode="labeled"
app:menu="#menu/bottom_navigation" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
By using ablow proper method, you'll easily swipe up-down both BottomNavigation and Top ActionBar
Output is like below
Hope It'll help someone.

Bottom navigation view disappears on pixel 4

I am getting this issue when only using Pixel 4 devices. I have a bottomnavigationview on my home screen and it just disappears.
Here is the code for my home screen
<?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:background="#color/colorWhite"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/nav_fragment_home_container"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#id/bottom_navigation"
app:defaultNavHost="true"
app:navGraph="#navigation/home_nav_graph" />
<!--<FrameLayout
android:id="#+id/nav_fragment_container_list"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#id/bottom_navigation" />-->
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorWhite"
app:itemBackground="#color/white_transparent"
app:itemTextColor="#drawable/bottom_nav_color"
app:labelVisibilityMode="labeled"
app:itemIconSize="#dimen/_18sdp"
android:isScrollContainer="false"
app:itemTextAppearanceActive="#style/BottomNavigationView.Active"
app:itemTextAppearanceInactive="#style/BottomNavigationView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="#menu/bottom_navigation" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/scan_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
app:fabCustomSize="#dimen/_35sdp"
app:layout_anchorGravity="center_horizontal"
app:layout_anchor="#id/bottom_navigation"
android:backgroundTint="#color/colorPrimaryDark"
app:tint="#null"
app:rippleColor="#color/colorPrimaryDark"
android:background="#color/colorPrimaryDark"
app:srcCompat="#drawable/ic_scan_nav"
app:elevation="#dimen/_10sdp"
android:translationZ="#dimen/_10sdp"
app:borderWidth="0dp"
android:outlineProvider="none"
tools:ignore="ContentDescription"
app:layout_constraintTop_toTopOf="#id/bottom_navigation"
app:layout_constraintBottom_toTopOf="#id/bottom_navigation"
app:layout_constraintLeft_toLeftOf="#id/bottom_navigation"
app:layout_constraintRight_toRightOf="#id/bottom_navigation"/>
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scan Receipt"
style="#style/SfProDisplay10Pt"
android:layout_marginTop="3dp"
app:elevation="#dimen/_20sdp"
android:translationZ="#dimen/_20sdp"
app:layout_constraintTop_toBottomOf="#id/scan_button"
app:layout_constraintLeft_toLeftOf="#id/scan_button"
app:layout_constraintRight_toRightOf="#id/scan_button"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:elevation="#dimen/_20sdp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:id="#+id/spendleeid_layout"/>
</androidx.constraintlayout.widget.ConstraintLayout>
As you can see there is a FragmentContainerView, which shows fine but on the Pixel 4, the bottomnavigationview disappears. From checking the layout inspector while debugging it seems that the bottomnavigation becomes invisible or hidden behind the view. On other devices I see it works fine. One thing that I see that fixes this issue is changing the style of the activity to include this
<item name="android:windowFullscreen">true</item>
I would just like to know if there is any way I can show the bottomnavigationview without using the fullscreen style. Or is there any plausible explanation for this? Any help would be appreciated
try another layout like LinearLayout
it maybe help

How to change the drawer title?

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

How to change the button colors when button is clicked?

I am creating an app the has choices. I want to know what button was clicked so I want to change the color of the clicked button here is my XML selector code.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#182f57"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#4e8fcf"/> <!-- focused -->
<item android:color="#4e8fcf"/> <!-- default -->
</selector>
Here is my XML activity
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D6EAF8"
tools:context=".AssessmentActivity">
<TextView
android:id="#+id/question"
android:layout_width="373dp"
android:layout_height="132dp"
android:layout_marginTop="16dp"
android:gravity="center"
android:textColor="#000000"
android:textSize="22sp"
android:typeface="normal"
app:fontFamily="sans"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.421"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/none"
android:layout_width="189dp"
android:layout_height="51dp"
android:background="#drawable/clicked_button"
android:text=""
app:layout_constraintBottom_toTopOf="#+id/mild"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/results" />
<Button
android:id="#+id/mild"
android:layout_width="189dp"
android:layout_height="51dp"
android:background="#4e8fcf"
android:text=""
app:layout_constraintBottom_toTopOf="#+id/moderate"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/none" />
<Button
android:id="#+id/moderate"
android:layout_width="189dp"
android:layout_height="51dp"
android:background="#4e8fcf"
android:text=""
app:layout_constraintBottom_toTopOf="#+id/severe"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/mild" />
<Button
android:id="#+id/severe"
android:layout_width="189dp"
android:layout_height="51dp"
android:background="#4e8fcf"
android:text=""
app:layout_constraintBottom_toTopOf="#+id/very"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/moderate" />
<Button
android:id="#+id/very"
android:layout_width="189dp"
android:layout_height="51dp"
android:background="#4e8fcf"
android:text=""
app:layout_constraintBottom_toTopOf="#+id/btnresult"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/severe" />
<TextView
android:id="#+id/results"
android:layout_width="375dp"
android:layout_height="142dp"
android:gravity="center"
android:text=""
android:textColor="#000"
android:textSize="22sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/question"
app:layout_constraintVertical_bias="0.04" />
<Button
android:id="#+id/btnresult"
android:layout_width="88dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="#4e8fcf"
android:text="Result"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.071"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.973" />
<Button
android:id="#+id/back"
android:layout_width="88dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="#4e8fcf"
android:text="Menu"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.95"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.973" />
<TextView
android:id="#+id/usersname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="#D6EAF8"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/moderate"
app:layout_constraintHorizontal_bias="0.296"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/results"
app:layout_constraintVertical_bias="0.959" />
</android.support.constraint.ConstraintLayout>
As you can see I tried to change one button. This line
android:background="#drawable/clicked_button"
and this is what happened
The button color was not set.
I made like this one.
<Button
android:id="#+id/button_sign_in"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#drawable/button_selector"
android:text="#string/button_sign_in"
android:textAllCaps="false"
android:textColor="#color/colorButtonText" />
Make a new drawable resource file.
for example: button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/colorPrimary" android:state_pressed="true" />
<item android:drawable="#color/colorAccent" android:state_focused="true" />
<item android:drawable="#color/colorPrimaryDark" />
</selector>
In onclick method set button color in following ways
Button_ID.setBackgroundColor(Color.RED); // From android.graphics.Color
or
Button_ID.setBackgroundColor(0xFFFF0000); // 0xAARRGGBB

Categories