I'm trying to get a shadow to appear beneath my action bar, but for some reason the shadow continually appears and begins at the very top of the screen, instead of beneath the action bar. I'm using a CustomView android.support.v7.widget.Toolbar action bar widget in each of my xml layout files. How can I get the shadow to properly appear beneath this action bar instead of at the top? Can I set some kind of divider equal to the height of this custom view before the shadow appears? Can we do something like set the height of the gradient to 50dp but only show the gradient in the last 5-10% of that height?
My theme:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">#color/white</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">#000</item>
<!-- colorAccent is used as the default value for colorControlActivated
which is used to tint widgets -->
<item name="colorAccent">#color/teal</item>
<!-- You can also set colorControlNormal, colorControlActivated
colorControlHighlight & colorSwitchThumbNormal. -->
<item name="android:textColorSecondary">#d7d7d7</item>
<item name="android:windowContentOverlay">#drawable/actionbar_shadow</item>
</style>
Shadow drawable:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size android:height="4dp" />
<gradient
android:angle="270"
android:endColor="#android:color/transparent"
android:startColor="#color/grey" />
</shape>
Toolbar:
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_height="50dp"
android:minHeight="30dp"
android:layout_width="match_parent"
android:paddingTop="10dp"
android:paddingBottom="10dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:background="#color/white">
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="#drawable/actionbar_title_r"
android:id="#+id/ActionBarTitle" />
</android.support.v7.widget.Toolbar>
Toolbar will have a shadow only in devices running on API 21 or higher. In order to achieve this effect you can add the property android:elevation="4dp" And it will work fine. Hope this helps
You can try several dp for elevation on the material appBar layout and see the degree of effect it gives. Then you decide how much do you give it.
in the appBar,
android:elevation = "1dp"
play with 2dp, 3dp,4dp and choose whats right for you.
Related
How can I style the slider, for example change colors for its track and thumb etc using xml?
slider.xml:
<com.google.android.material.slider.Slider
android:id="#+id/settingsMission_changeShakeDif_slider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:stepSize="20.0"
android:theme="#style/SliderTheme"
android:valueFrom="0.0"
android:valueTo="40.0"
app:labelBehavior="gone" />
themes.xml
<style name="SliderTheme" parent="Widget.MaterialComponents.Slider">
<!-- Add attributes here -->
<item name="trackColorActive">#238ae6</item>
<item name="trackColorInactive">#a7bada</item>
<item name="tickColor">#13161d</item>
<item name="thumbColor">#238ae6</item>
</style>
When I do it like this, the color of my slider did not change (it remained the default purple color)
And when I try to run the app and open the bottom sheet dialog that has the slider, the app crashes. I am getting this runtime error too:
Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.AppCompat (or a descendant).
Found this useful article that solved my problem:
https://github.com/material-components/material-components-android/issues/1145
changes made to code:
slider.xml
<com.google.android.material.slider.Slider
android:id="#+id/settingsMission_changeShakeDif_slider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:stepSize="20.0"
android:theme="#style/AppTheme"
android:valueFrom="0.0"
android:valueTo="40.0"
app:labelBehavior="gone"
app:thumbColor="#238ae6"
app:tickColor="#13161d"
app:trackColorActive="#238ae6"
app:trackColorInactive="#a7bada" />
themes.xml
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
i am using this library to make the same bottom navigation but when i click on item the icon will disappear, here is my code
activity_main:
<com.github.kwasow.bottomnavigationcircles.BottomNavigationCircles
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:forceDarkAllowed="true"
app:labelVisibilityMode="selected"
app:menu="#menu/navigation" />
navigation items:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/destination_ejournal"
android:icon="#drawable/ejournal_selector"
android:title="#string/bottomnav_ejournal" />
<item
android:id="#+id/destination_solah"
android:icon="#drawable/solah_selector"
android:title="#string/bottomnav_solah" />
<item
android:id="#+id/destination_home"
android:icon="#drawable/home_selector"
android:title="#string/bottomnav_home" />
<item
android:id="#+id/destination_dua"
android:icon="#drawable/dua_selector"
android:title="#string/bottomnav_dua" />
<item
android:id="#+id/menu_more"
android:icon="#drawable/more_selector"
android:title="#string/bottomnav_more" />
MainActivity:
val bottomNavigation = findViewById<BottomNavigationCircles>(R.id.navigation)
bottomNavigation.color = Color.WHITE
so what am i doing wrong here?
is it in the activity or XML?
Images of the problem:
If i click Home
If i navigate to another
If i change color from white to red
home selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/home_selected" android:state_checked="true" />
<item android:drawable="#drawable/ic_home" android:state_checked="false" />
</selector>
ic_home: is verctor
home_selected: is png
SOLUTION:
i was using a selector to display a different drawable if the "state_checked" and one of them was PNG format so it didn't appear, i used 2 vectors for the selector and it works fine now
I tried your example with that library & the problem is that you're setting the color to white at runtime & that kind of blends into the bottomnav color.
You can try different color e.g red.
val bottomNavigation = findViewById<BottomNavigationCircles>(R.id.navigation)
bottomNavigation.color = Color.RED
It gives me following result:
I have implemented the following code for getting the transparent status bar but not able to
Kindly help me.
If fitSystemWindows is true then status bar not getting transparent and if fitSystemWindows is false to rootlayout then content is going below navigation bar.
I have applied following code before setting content view on my activty.
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static void setFullTheme(Activity activity){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(Color.TRANSPARENT);
}
}
This is the theme I have applied to the activity
<style name="ThemeFullScreen" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:statusBarColor" tools:targetApi="lollipop">#android:color/transparent
</item>
<item name="android:windowEnableSplitTouch">false</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:windowTranslucentStatus" tools:targetApi="kitkat">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
By implementing these the status bar got transparent but navigation bar too got transluscent and hence the content was going behind the navigation bar.
So I applied android:fitSystemWindows="true" to the root view but after that status bar got milky color.
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_container_home"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/main_frag_home"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/content_member_profile"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="?attr/actionBarSize">
</FrameLayout>
<com.aurelhubert.ahbottomnavigation.AHBottomNavigation
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
</FrameLayout>
Following picture,when I set true to fitSystemWindows in root layout. Content goes behind navigation bar.
Following picture,when I remove the fitSystemWindows property in root layout. Status bar color goes white.
put android:fitSystemWindows="true" again and change android:windowDrawsSystemBarBackgrounds attribute in your theme as false
nevigate to this https://developer.android.com/reference/android/R.attr documentation and you can find out
The window must be drawing the system bar backgrounds with
windowDrawsSystemBarBackgrounds and the navigation bar must not have
been requested to be translucent with windowTranslucentNavigation.
Corresponds to Window.setNavigationBarColor(int).
I have to add a left margin between the icons and the NavigationView, in arrow in the image bellow:
I know that according to google specs, this margin must have 16dp but I need to change it. I have tried:
<dimen tools:override="true" name="design_navigation_icon_padding">64dp</dimen>
<dimen tools:override="true" name="design_navigation_separator_vertical_padding">20dp</dimen>
But still not working. Any ideas?
The xml layout of that item is design_navigation_item.xml
<android.support.design.internal.NavigationMenuItemView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?attr/listPreferredItemHeightSmall"
android:paddingLeft="?attr/listPreferredItemPaddingLeft"
android:paddingRight="?attr/listPreferredItemPaddingRight"
android:foreground="?attr/selectableItemBackground"
android:focusable="true"/>
As you can see, paddings that are applied are taken from the activity's theme - listPreferredItemPaddingLeft and listPreferredItemPaddingRight. Thus, you have to apply your custom theme to NavigationView overriding those attributes with necessary values.
In styles.xml:
<style name="MyNavigationViewItemStyle" parent="AppTheme">
<item name="listPreferredItemPaddingLeft">0dp</item>
<item name="listPreferredItemPaddingRight">0dp</item>
</style>
We want to change only those two attributes from activity's theme, thus we are extending the theme, that is applied to the activity.
In layout xml:
<android.support.design.widget.NavigationView
...
app:theme="#style/MyNavigationViewItemStyle"/>
Result
For my case I added one line to xml: app:itemHorizontalPadding="#dimen/horizontal_padding_on_nav_drawer"
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"
android:clipToPadding="false"
android:paddingBottom="#dimen/space48"
app:headerLayout="#layout/layout_nav_header"
app:insetForeground="#color/black"
app:itemBackground="#drawable/background_selected_menu"
**app:itemHorizontalPadding="#dimen/horizontal_padding_on_nav_drawer"**
app:menu="#menu/drawer">
when i customize title bar in view by adding a image, i face an issue as the images is not fixed to the edges accurately. I have attached the image where you can see space between the window margin and image added. How to overcome this problem. Below are the code that i have used to customize the title bar. Can anyone help me to fix this issue.
java code :
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.home_view);
this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.upwindow);
layout code :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="32dp">
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/up_bar"
android:src="#drawable/up_bar" />
<TextView
android:id="#+id/pagename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Sify MyStorage"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
create style xml in values folder :
<resources>
<style name="CustomWindowTitleBackground" />
<style name="CustomTheme" parent="android:Theme">
<item name="android:windowTitleSize">32dp</item>
<item name="android:windowTitleBackgroundStyle">#style/CustomWindowTitleBackground
</item>
</style>
</resources>
then refer it in manifest as :
android:theme="#style/CustomTheme">
this will solve it .
hope this help .
Do you have different images for different resolutions? It might be that your image simply does not fit that space, and that you need a larger one. Be careful though, you might run into issues with your app working across different platforms with scaling.
You need to play a little with styling in order to override this behavior. Define a custom theme (style) as below:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="customTitle">
<item name="android:background">#drawable/up_bar</item>
</style>
<style name="myTheme" parent="android:Theme">
<item name="android:windowTitleBackgroundStyle">#style/customTitle</item>
</style>
</resources>
and then use this theme in your activity by applying android:theme="#style/myTheme" to your desired activity tag in AndroidManifest.xml
requestWindowFeature(Window.FEATURE_NO_TITLE);
and setBackground to your layout in layout file...
Like,,,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent" android:background="#drawable/up_bar" <---Add this
android:layout_height="32dp">
and Remove imageview from xml