I'm tring to make a Navigation Drawer using Design Support Library. In my activity_main i wrote this code.
<?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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar"/>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/header_layout"
app:menu="#menu/menu_drawer"/>
</android.support.v4.widget.DrawerLayout>
When i run my application it works and show the Navigation Drawer but it's below the Status Bar and not above. How can i move it to stay above the StatusBar?
Simply adding android:fitsSystemWindows="false" to DrawerLayout will solve the problem.
Make your activity as full screen to overcome status bar as below or any other method as available on WWW: but it's not good to have drawer over status bar.
public class ActivityName extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// remove title
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
}
}
use this on navigation view xml :
app:insetForeground="#ffffff"
Related
I just included an options menu in my app, but in one of my activities it seems to create a white empty space- can anyone tell me why??
It work in other activities in the same app, but not this single activity.
I Hope anyone can help :-)
public class GifsActivity extends OptionsMenu {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gifs);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ImageView gifImageView = (ImageView) findViewById(R.id.iv_gif);
Glide.with(this)
.load("http://i.imgur.com/Vth6CBz.gif")
.asBitmap()
.placeholder(R.drawable.ic_cloud_off_red)
.error(R.drawable.ic_cloud_off_red)
.into(gifImageView);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:gravity="center_vertical"
android:orientation="vertical"
tools:context="com.example.examand1.GifsActivity">
<include layout="#layout/toolbar_layout"
/>
<ImageView
android:id="#+id/iv_gif"
android:layout_width="match_parent"
android:layout_height="220dp" />
</RelativeLayout>
Picture of the activity
Remove android:gravity="center_vertical" from your layout. Then your layout will be as below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/include"
layout="#layout/toolbar_layout"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
<ImageView
android:layout_below="#id/include"
android:id="#+id/iv_gif"
android:layout_width="match_parent"
android:layout_height="220dp" />
</RelativeLayout>
I have a bitmap which I use to display an image from firebase in an imageview. I want both the status bar and the action bar's colors to be adjusted by the vibrant color of the pictures from the database. I searched for how to do this, and have the following code below
Palette.from(myBitmap).generate(new Palette.PaletteAsyncListener() {
#Override
public void onGenerated(Palette palette) {
android.app.ActionBar bar=setBackgrounddrawable(palette.getVibrantColor(getResources().getColor(R.color.colorPrimary)));
getWindow().setStatusBarColor(palette.getVibrantColor(getResources().getColor(R.colorPrimary)));
}
});
But I get an error on the line of code for get action bar saying 'set background drawable cannot be applied to int' When I remove the line of code for setting color of the actionbar only the status bar color is set and not the action bar, how can I set the color of the action bar as well?
As You see, setBackgroundDrawable() receive a param what type is Drawable,
but you pass an Int Color value in, then you got the error.
I do not test it, but You can give it a try with:
setBackgroundDrawable(new ColorDrawable(palette.getVibrantColor(getResources().getColor(R.color.colorPrimary))));
Maybe, it will be easy to use Toolbar?
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.viewpager.widget.ViewPager
android:id="#+id/mainPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="4dp"
/>
</LinearLayout>
app_bar_main.xml:
<?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=".activities.MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="150dp"
android:layout_height="match_parent"
android:src="#drawable/logo_vector_dark"
/>
</LinearLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Setup toolbar in onCreate of Activity:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Change color:
Palette.from(myBitmap).generate(new Palette.PaletteAsyncListener() {
#Override
public void onGenerated(Palette palette) {
toolbar.setBackgroundColor(palette.getVibrantColor(getResources().getColor(R.color.colorAccent)));
}
});
I tried to include the navigation bar in an existing app. but the contens of the activity is also not shown on inclusion
this is my code --
<?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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
tools:context=".common.components.main.HomeActivity"
tools:openDrawer="start">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<include layout="#layout/nav_toolbar" />
<include layout="#layout/activity_home_contents" />
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:itemTextAppearance="#style/TextAppearance.AppCompat.Body1"
app:itemTextColor="#FFFFFF"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
This is the contents of my nav_toolbar layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbar1"
android:background="#color/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
>
</android.support.v7.widget.Toolbar>
Similarly I have activity_home_contents in scroll view as
<ScrollView 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:fillViewport="true"
android:background="#color/white"
android:scrollbars="none"
>
<RelativeLayout android:orientation="vertical"
android:id="#+id/home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="30dp"
tools:context=".common.components.main.HomeActivity">
......
And code snipplet from HomeActivity.java is
.....
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
floodEvacApplication = (FloodEvacApplication)
getApplicationContext();
lastAddress = floodEvacApplication.getLatestAddress();
if (lastAddress == null) {
initGoogleAPI();
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar1);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout)
findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
NavigationView navigationView = (NavigationView)
findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
......
I tried various ways avaliable online and searched online but couldn't able to display the activity_home_contents also but if I remove the navigation drawer and display the activiy_home then I am able to display it.
Try to add the following line under android.support.design.widget.NavigationView in Drawer's XML:
android:layout_gravity="start"
So I'm done with my app and doing final tests. On Android 5.0+ it runs fine, but on pre Lollipop devices i get strange bug.
At first, action bar was not visible on activities with ScrollView and I fixed it by putting ScrollView inside of RelativeLayout. But that solves only half of the problem. Action bar color is transparent and I can tell that because background color in my activities is #f2f2f2 and text on Action bar is white.
I see action bar title, back button and all images I put in action bar, but just can't set the color of it to ColorPrimary which is material light blue.
Here's the ACTIVITY code (not content)
<?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="hr.app.liftme.liftmehr.VjezbeTricepsTricepsEkstenzijaKablovima">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay"
android:elevation="10dp"
app:elevation="10dp">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_vjezbe_triceps_triceps_ekstenzija_kablovima" />
</android.support.design.widget.CoordinatorLayout>
And here's java file where i declare
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vjezbe_triceps_triceps_ekstenzija_kablovima);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Can anyone please help me with this?
/** In your xml,color/background ,change according to your Requirement **/
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="55dp"
android:padding="#dimen/appbar_padding"
android:background="#color/appbarColor">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/place_order"
android:layout_gravity="center"
android:textSize="#dimen/appbar_txt_size"
android:textStyle="bold"
android:textColor="#color/white"
android:layout_marginTop="15dp"
android:id="#+id/toolbar_title"/>
</android.support.v7.widget.Toolbar>
/** In your java file **/
toolbar.setBackgroundColor(Color.parseColor("#617191"));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getActivity().getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(getActivity().getResources().getColor(R.color.toolbar));
}
I am trying to implement a toolbar in my activity but it doesn't seem to work. My MainActivity is a ViewPager, which contains Fragments 1 and 2. I tried to implement my toolbar directly into the Viewpager, this didn't work so instead I tried to wrap my Viewpager in a RelativeLayout and include the toolbar in that RelativeLayout, but this also doesn't work.
The layout for my toolbar (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:layout_width="match_parent"
android:id="#+id/app_bar"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:theme="#style/CustomToolBarStyle"
android:title="Something"
android:logo="#drawable/ic_launcher">
</android.support.v7.widget.Toolbar>
this is how I've implemented the toolbar in my activity (activity_main.xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<include android:id="#+id/app_bar" layout="#layout/app_bar"/> <-----------
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
</android.support.v4.view.ViewPager>
</RelativeLayout>
and this is what I've done in my activity's Java file to set the toolbar as the actionbar (MainActivity.java):
public class MainActivity extends AppCompatActivity {
ViewPager pager;
android.support.v4.app.FragmentManager fragmentManager;
FloatingActionButton fab;
Toolbar tb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager = (ViewPager) findViewById(R.id.pager);
fab = (FloatingActionButton) findViewById(R.id.fab_main);
tb = (Toolbar) findViewById(R.id.app_bar);
fragmentManager = getSupportFragmentManager();
pager.setAdapter(new myAdapter(fragmentManager));
AdView mAdView = (AdView) findViewById(R.id.adView);
//AdRequest adRequest = new AdRequest.Builder()
//.addTestDevice("YOUR_DEVICE_HASH")
//.build();
//do not make visible while testing!!
//mAdView.loadAd(adRequest);
setSupportActionBar(tb); <------------------
it would really appreciate it if someone is able to help me out. Have a nice day!
Vidal
Try to remove id from include tag and add android:layout_below="#+id/app_bar" to your ViewPager.
Change your toolbar height from wrap_content to you ?attr/actionBarSize as shown below:
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:layout_width="match_parent"
android:id="#+id/app_bar"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:theme="#style/CustomToolBarStyle"
android:title="Something"
android:logo="#drawable/ic_launcher">
</android.support.v7.widget.Toolbar>
and position your viewpager below Toolbar as shown below
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<include android:id="#+id/app_bar" layout="#layout/app_bar"/>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/pager"
android:layout_below="#id/app_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF" />
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
Hope this helps!