Ive got a Layout where I have viewpager with sliding tabs.Now I want to make the upper part of my layout transparent so I get the effect that the activity before it is kinda visible.
It should look like this :
My Layout :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Main">
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar"
android:layout_marginTop="200dp" //THIS part should make the upper piece transparent
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<com.kas.SlidingTabs.SlidingTabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="2dp"
android:background="#color/colorPrimary"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_height="match_parent"
android:layout_width="match_parent"
></android.support.v4.view.ViewPager>
My Apptheme I used in this Activity :
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
I allready tried #android:color/transparent as background in LinearLayout but its not helping.I also tried this solution https://stackoverflow.com/a/8831226/4035864
But then my logcat says :You need to use a Theme.AppCompat theme (or descendant) with this activity.
EDIT:
ToolbarLayout :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="#color/colorPrimary"
android:elevation="2dp"
android:theme="#style/Base.ThemeOverlay.AppCompat.Dark"
xmlns:android="http://schemas.android.com/apk/res/android" />
Related
I am using a prominent app bar with an image in a fragment.
My style and xml files look like this:
theme.xml:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.MainTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">#color/DeepSkyBlue</item>
<item name="colorPrimaryVariant">#color/DeepSkyBlueVariant</item>
<item name="colorOnPrimary">#color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">#color/teal_200</item>
<item name="colorSecondaryVariant">#color/teal_700</item>
<item name="colorOnSecondary">#color/purple_500</item>
<!-- Status bar color. -->
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
</resources>
main_activity.xml (which opens automatically the fragment):
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragment_container_view_tag"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.tempapp.pkgFragments.PersonAddFragment" />
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_add_person.xml (which uses a layout to prevent redudancy):
<androidx.coordinatorlayout.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"
>
<include
android:id="#+id/layoutAppBar"
layout="#layout/layout_app_bar_prominent" />
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
last but not least, my layout_app_bar_prominent.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- Layout tag not necessary, as we don't use any binding....-->
<com.google.android.material.appbar.AppBarLayout android:id="#+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="#dimen/app_bar_height_image_view"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!--- contentScrim defines what should "appear" when the bar is not collapsed.
if not set in this case, the image will remain when bar is not collapsed-->
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:contentScrim="?attr/colorPrimary"
app:collapsedTitleTextColor="?attr/colorOnPrimary"
app:expandedTitleTextColor="?attr/colorOnPrimary"
>
<ImageView
android:id="#+id/expandedImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7"
android:contentDescription="#string/app_bar_expanded_image"
/>
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:navigationIcon="?attr/homeAsUpIndicator"
>
</com.google.android.material.appbar.MaterialToolbar>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
So here is my situation:
My aim is that the toolbar image (expandedImage) should appear in the statusbar also.
After researching, some suggested adding the following code in my activity in order to accomplish this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
);
}
This works perfect and my image appears in my status bar:
HOWEVER, the issue I am having now, is that (when i collapse my toolbar), the title and menu icons appear behind the status bar due to the code I add above:
So there is basically no margin between the statusbar and my toolbar, which by default always exists.
If I remove the flag option, I don't have that issue anymore but then the image wouldn't appear in the status bar.
Any ideas how I could solve my issue? I've tried many solutions like:
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:windowTranslucentStatus">true</item>
android:fitsSystemWindows="true"
but none of the situations worked for me...
Using CoordinatorLayout instead of ConstraintLayout for your activity will probably solve your problem.
NOTE: android:fitsSystemWindows=true is (in every subview) necessary, otherwise the app bar will never "fill" the status bar or you will experience some unexpected behaviour.
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
>
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragment_container_view_tag"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"ยด
android:name="com.example.tempapp.pkgFragments.PersonAddFragment" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
I have troubles with making it work.
It only works with default fonts like sans-seriff. Any imported fonts work with all textviews, however not with the collapsing toolbar. Please help.
Styles.xml
<style name="AppBarTitleStyle" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:fontFamily">#font/amatica_sc_reg</item>
<item name="android:titleTextColor">#android:color/white</item>
</style>
CollapsingToolbar.xml
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="308dp"
android:background="#color/color_secondary"
app:expanded="false">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="#+id/collapsingtoolbarid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/colliding"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="exitUntilCollapsed|scroll|snap"
app:title="Learn Miwok"
app:expandedTitleTextAppearance="#style/AppBarTitleStyle"
app:collapsedTitleTextAppearance="#style/AppBarTitleStyle">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbarid"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin">
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
you can add font with java by using this below code
final Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/amatica_sc_reg");
collapsingToolbar.setCollapsedTitleTypeface(tf);
collapsingToolbar.setExpandedTitleTypeface(tf);
I have created a persistent bottom sheet in android with the intent of displaying a ListView containing additional information about locations. I want the sheet to have rounded corners. I got a ton of results for modal dialog but none for persistent. Is it possible or should I use the modal version?
As suggested in an answer here, I tried wrapping it in a card view but the app crashes when I try to open the fragment.
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".fragments.MapFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
app:cardCornerRadius="20dp">
<fragment
android:id="#+id/autocomplete_fragment"
android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout="#layout/places_autocomplete_item_powered_by_google" />
</androidx.cardview.widget.CardView>
<com.google.android.gms.maps.MapView
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="450dp"
app:cardCornerRadius="24dp"
app:cardElevation="8dp"
app:layout_behavior="#string/bottom_sheet_behavior"
app:behavior_hideable="true"
app:behavior_peekHeight="55dp">
<androidx.core.widget.NestedScrollView
android:id="#+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tvBottomSheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/placeholder_text"
android:textColor="#color/colorCommon_BLACK"
android:textSize="37sp" />
</androidx.core.widget.NestedScrollView>
</androidx.cardview.widget.CardView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
With the new Material Component library you can customize the shape of your component using the shapeAppearanceOverlay attribute.
You can use something like:
<!-- BottomSheet -->
<style name="CustomBottomSheet" parent="Widget.MaterialComponents.BottomSheet">
<item name="shapeAppearanceOverlay">#style/CustomShapeAppearanceOverlay.MaterialComponents.BottomSheet</item>
....
</style>
<style name="CustomShapeAppearanceOverlay.MaterialComponents.BottomSheet" parent="">
<item name="cornerSizeTopRight">16dp</item>
<item name="cornerSizeTopLeft">16dp</item>
<item name="cornerSizeBottomRight">0dp</item>
<item name="cornerSizeBottomLeft">0dp</item>
</style>
Then you can apply this style to your single component or your theme app.
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
...
<item name="bottomSheetStyle">#style/CustomBottomSheet</item>
</style>
If you are using a non-modal bottom sheet just apply the style. For example:
<LinearLayout
android:id="#+id/standardBottomSheet"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
style="?attr/bottomSheetStyle"
...>
U can add shape for you bottom sheet background background
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/you_color"/>
<corners
android:bottomRightRadius="2dp"
android:bottomLeftRadius="2dp"
android:topLeftRadius="2dp"
android:topRightRadius="2dp"/>
</shape>
You could also use the app:shapeAppearance attribute:
Create a ShapeAppearance in styles.xml:
<resources>
...
<style name="BottomSheetShapeAppearance">
<item name="cornerFamily">rounded</item>
<item name="cornerSizeTopLeft">16dp</item>
<item name="cornerSizeTopRight">16dp</item>
</style>
</resources>
Set the app:shapeAppearance attribute on the BottomSheet component:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
style="#style/Widget.MaterialComponents.BottomSheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
app:shapeAppearance="#style/BottomSheetShapeAppearance">
...
</LinearLayout>
Here's how it looks:
More info: https://material.io/develop/android/theming/shape
You can wrap your sheet layout with a card view to have rounded corners.
<?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">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="450dp"
app:layout_behavior="#string/bottom_sheet_behavior"
app:cardCornerRadius="24dp"
app:cardElevation="8dp"
app:behavior_hideable="true"
app:behavior_peekHeight="55dp">
<androidx.core.widget.NestedScrollView
android:id="#+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tvBottomSheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ABC"
android:textSize="37sp" />
</androidx.core.widget.NestedScrollView>
</androidx.cardview.widget.CardView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
I want to create a Android ActionBar which looks like the following.
How can I build such a ActionBar. Is it possible to build it with Android material Design?
I would use the new ToolBar from Android Design Support Library
http://android-developers.blogspot.it/2015/05/android-design-support-library.html
Using ToolBar instead of ActionBar will give you more control on the elements themselves.
You can find a good article here:
http://www.android4devs.com/2014/12/how-to-make-material-design-app.html
app_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#3F51B5"
android:theme="#style/ThemeOverlay.AppCompat.Dark" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:id="#+id/tabLayout"/>
</FrameLayout>
</android.support.v7.widget.Toolbar>
main_activity_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<include layout="#layout/app_bar" />
</RelativeLayout>
main_activity_onCreate()
toolBar = (Toolbar) findViewById(R.id.appBar);
toolBar.setTitle("Titolo");
setSupportActionBar(toolBar);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.user));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.user_group_1));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.user_group_2));
styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
Technically you could stitch together such a layout with multiple elements. For example you have a header layout containing a TabLayout on the left and a Toolbar on the right side, instead of using the standard ActionBar.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:orientation="horizontal">
<android.support.design.widget.TabLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<android.support.v7.widget.Toolbar
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
This example uses the AppCompat v7 and the Design Support library.
You may style the Views in a way (e.g. same background color) such that it looks as if its one single UI element.
I am trying to make a transparant toolbar for my app. I now use windowTranslucentStatus to achieve this. My problem is now that my statusbar is over my toolbar. I guess I need to give my toolbar a margin top with the height of the statusbar. How can do I know what the height of the statusbar is?
My layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activites.DealersActivity_">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/toolbar" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</RelativeLayout>
</LinearLayout>
<include layout="#layout/drawer"
android:id="#+id/drawer"
></include>
</android.support.v4.widget.DrawerLayout>
My Toolbar:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="#drawable/toolbar_background">
<TextView
android:visibility="gone"
android:typeface="monospace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toolbar Title"
android:textSize="20sp"
android:textColor="#color/toolbar_text"
android:layout_gravity="center"
android:id="#+id/toolbar_title" />
</android.support.v7.widget.Toolbar>
After your explanations, it also seems to me that you need to set margin in your toolbar.
Regretfully, I am not aware of a way to determine the height of a status bar in the layout xml (e.g. like "?attr/actionBarSize"). However, you can determine it in the code: see here.
When you get the height programatically, you would like to change the padding of your toolbar programatically too. Use this method to do it.
I wanted to do the same thing. What I ended up doing is this:
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=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:background="#drawable/gradient_actionbar"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="#color/colorTransparent"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab_main_openCameraIntent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:fabSize="normal"
android:tint="#color/colorIcons"
android:elevation="2dp"
app:srcCompat="#drawable/ic_camera_kindawhite_24dp" />
</android.support.design.widget.CoordinatorLayout>
And in styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#color/colorTransparent</item>
<item name="android:windowTranslucentStatus">false</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
I hope this helps!