I am trying to make toolbar gradient with transparent but nothing is working. Here is my code.
activity_main.xml (Updated)
<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"
tools:context="com.amrat.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/gradient"
android:theme="#style/AppTheme.AppBarOverlay"
app:elevation="0dp">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#drawable/gradient"
android:layout_alignParentBottom="true"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
gradient.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="270"
android:endColor="#00ffffff"
android:startColor="#ffffff"
android:type="linear" />
</shape>
Trying to do like this:
But got this:
Please help me if anyone knows how to do this.
UPDATE
AndroidManifest.xml
<activity
android:name=".activity.MainActivity"
android:theme="#style/AppTheme.Transparent.NoActionBar" />
style.xml
<item name="colorPrimary">#android:color/transparent</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="colorControlNormal">#color/light_grey_1</item>
<item name="colorControlActivated">#color/orange_1</item>
<item name="colorControlHighlight">#color/light_grey_2</item>
</style>
Use #android:color/transparent directly. Follow this one -
toolbar layout -
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
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/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#drawable/background_toolbar_transparent" />
background_toolbar_transparent.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="270"
android:endColor="#android:color/transparent"
android:startColor="#color/black_alpha"/>
</shape>
Add a color with alphaon colors.xml -
<color name="black_alpha">#66000000</color>
You need to change your start and end colors to transparent.
<gradient
android:angle="270"
android:startColor="#android:color/transparent"
android:endColor="#android:color/transparent"
android:type="linear" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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:background="#android:color/holo_red_dark"
android:orientation="vertical">
<!-- Your content comes here -->
<android.support.v7.widget.Toolbar
android:id="#+id/layout_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#android:color/transparent"
android:elevation="6dp"
tools:targetApi="lollipop"/>
</FrameLayout>
Frame Layout basically works as stack and the last added item comes to top.
Good luck
Emre
Related
How to access a switch in a menu using ViewBinding ?
menu/drawer_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu 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"
tools:showIn="navigation_view"
>
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_read"
android:title="#string/read"
android:icon="#drawable/ic_read"
/>
<item
android:id="#+id/nav_write"
android:icon="#drawable/ic_write"
android:title="#string/write"
/>
<item
android:id="#+id/nav_theme"
android:icon="#drawable/ic_theme"
android:title="Dark Theme"
app:actionLayout="#layout/theme_switch"
/>
</group>
<item android:title="#string/about">
<menu>
<item
android:id="#+id/nav_source_code"
android:icon="#drawable/ic_source_code"
android:title="#string/source_code"/>
</menu>
</item>
</menu>
theme_switch.xml:
<androidx.appcompat.widget.SwitchCompat xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toggleSwitch"
android:layout_width="fill_parent"
android:layout_height="match_parent" />
activity_home.xml :
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeActivity"
android:id="#+id/drawer_layout"
android:fitsSystemWindows="true"
tools:openDrawer="start"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:id="#+id/toolBar"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
/>
<FrameLayout
android:id="#+id/fl_home"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="#+id/nav_view"
app:headerLayout="#layout/nav_header"
app:menu="#menu/drawer_menu"
/>
</androidx.drawerlayout.widget.DrawerLayout>
These are my layout files.
In brief, I've created a DrawerLayout inside which I've implemented my menu using NavigationView. One of the menu item is a switch and I want to add a .setOnCheckedChangeListener to this switch.
I've tried accessing the switch like this inside the onCreate() function of my activity:
val switch = binding.navView.menu.findItem(R.id.nav_theme) as SwitchCompat
switch.setOnCheckedChangeListener { _, isChecked ->
}
However, it is throwing an exception that MenuItem cannot be cast into a SwitchCompat.
I don't see any otherway to access this switch.
If you want a switch on your item, use actionViewClass to define the component.
And if a not mistaken you can remove app:actionLayout="#layout/theme_switch"
app:actionViewClass="androidx.appcompat.widget.SwitchCompat"
I want to put this little green line in the menu item that is active ... already has to create a shape and a custom style but no success ...
My activity_main_drawer.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">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_ordem"
android:checked="true"
android:title="Home"/>
<item
android:id="#+id/nav_auto_infracao"
android:title="Payment Profile" />
<item
android:id="#+id/nav_apreensao_deposito"
android:title="Payment history" />
<item
android:id="#+id/nav_doacao"
android:title="My card" />
<item
android:id="#+id/nav_sincronia"
android:title="Frinds" />
</group>
</menu>
Try to use View to draw line.
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/black" />
One way to solve this is you can highlight the item by calling the following method to detect when to change the visibility and position the view:
onNavigationItemSelected(navigationView.getMenu().getItem(0));
Since you've mentioned you already have a shape and a custom style, put the view (here in this code below, selectedItemBottomView) inside the NavigationView in activity_main or the XML layout file where you've used the activity_main_drawer like this:
activity_main
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="#menu/activity_main_drawer" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:id="#+id/selectedItemBottomView"
android:layout_width="match_parent"
android:layout_height="10dp"
android:background="#color/green" />
</LinearLayout>
</RelativeLayout>
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
On selected, change the visibility or position as per your requirement:
selectedItemBottomView.setVisibility(View.VISIBLE);
Hope this helps.
Trying to add a menu to my toolbar, however the code supposedly required to do that is showing errors:
Here's my other code:
res/menu/home_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/settings"
android:title="#string/settings" />
<item
android:id="#+id/dark_theme"
android:title="#string/dark_theme" />
</menu>
Here's the layout with the Toolbar I want to add the menu to:
res/layout/activity_home.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" 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="#color/colorVeryDark"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar" />
</LinearLayout>
Any idea how I can add my menu to the toolbar?
I am trying to make an action bar (through toolbar) with a navigation drawer, however I only manage to get the navigation drawer part working. When the app loads in the emulator, it does seem that the toolbar shows up on the screen for a fraction of a second before it disappears, showing a screen without a toolbar. How do i get the toolbar to show up/not disappear on load?
This is how it looks like:
styles.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorPrimaryDark</item>
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</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>
styles.xml (v21)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- API 14 theme customizations can go here. -->
<item name="android:statusBarColor">#color/colorPrimaryDark</item>
<item name="colorPrimary">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorPrimaryDark</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
</style>
</resources>
MainActivity.java (trimmed away excess codes)
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
public class MainActivity extends AppCompatActivity implements OnMarkerClickListener {
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
...
}
main_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="#layout/toolbar_layout"
/>
</LinearLayout>
<android.support.design.widget.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/navigation_view"
android:layout_gravity="start"
app:menu="#menu/drawer_menu"
>
</android.support.design.widget.NavigationView>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mainfrag"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="#layout/main_layout" />
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
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="wrap_content"
android:id="#+id/toolbar"
android:background="?attr/colorPrimaryDark"
android:minHeight="?attr/actionBarSize"
android:fitsSystemWindows="true"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
>
</android.support.v7.widget.Toolbar>
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
...
As Ajay mentioned that DrawerLayout should only contain two layouts so I am editing some of your codes to fulfill the requirements and I think this would work just give it a try
main_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include
android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="#layout/other_layout"/>
<android.support.design.widget.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/navigation_view"
android:layout_gravity="start"
app:menu="#menu/drawer_menu"/>
</android.support.v4.widget.DrawerLayout>
Now add your other layouts to another layout file
other_layout.xml
<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">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbar"
android:background="?attr/colorPrimaryDark"
android:minHeight="?attr/actionBarSize"
android:fitsSystemWindows="true"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<FrameLayout
android:id="#+id/mainfrag"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="#layout/main_layout" />
</FrameLayout>
</LinearLayout>
main_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="#layout/toolbar_layout"
/>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mainfrag"
android:layout_width="match_parent"
android:layout_height="0dp" //Changed Value
android:layout_weight="1"> //Added
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="#layout/main_layout" />
</FrameLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/navigation_view"
android:layout_gravity="start"
app:menu="#menu/drawer_menu"
>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
DrawerLayout Should only contain two child Views, first the main content and then the drawer view.
Creating a Navigation Drawer
Hello friends i have tried many different ways to highlight my listview but nothing works for me. i want to highlight my customlist view when i click on that and than open a new activity.
here is my xml file list_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selector style for listrow -->
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="#drawable/gradient_bg" />
<item android:state_pressed="true"
android:drawable="#drawable/gradient_bg_hover" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="#drawable/gradient_bg_hover" />
</selector>
here is my gradient_bg_hover.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- Gradient BgColor for listrow Selected -->
<gradient
android:startColor="#18d7e5"
android:centerColor="#16cedb"
android:endColor="#09adb9"
android:angle="270" />
</shape>
And gradient_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- Gradient Bg for listrow -->
<gradient
android:startColor="#f1f1f2"
android:centerColor="#e7e7e8"
android:endColor="#cfcfcf"
android:angle="270" />
</shape>
and my _activity_setup.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
</LinearLayout>
help me
Just remove the list-selector from your listview and instead, set the background for your item view:
Your listview:
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="#null" />
Your item view (for example):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/list_selector" >
</LinearLayout>
Hope this helps.