Error inflating class androidx.appcompat.widget.Toolbar android - java

I try to use the toolbar in my app and I get this message: "Error inflating class androidx.appcompat.widget.Toolbar".
i glad if someone will find my problem (using the latest version of the android studio)
XML:
<androidx.appcompat.widget.Toolbar
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#00008577"
android:minHeight="?actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navigationIcon="#drawable/main"
android:id="#+id/toolbar"/>
JAVA:
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
setSupportActionBar(toolbar);
}
private void findViews() {
drawer_layout = findViewById(R.id.drawer_layout);
nav_view = findViewById(R.id.nav_view);
toolbar = findViewById(R.id.toolbar);
}
STYLE:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
Exception received
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.or.ourshoppinglist/com.or.ourshoppinglist.MainActivity}: android.view.InflateException: Binary XML file line #28: Binary XML file line #28: Error inflating class <unknown>

Hi noa shen and welcome to Stackoverflow.
Firstly, there can be an issue with your import for Toolbar. Since androix version came out now you got two versions of Toolbar:
androidx.appcompat.widget.Toolbar
and
android.support.v7.widget.Toolbar
So first check your imports in your JAVA.class and see if you imported the right version which would be:
androidx.appcompat.widget.Toolbar
Besides that, why is your toolbar width equals to 0?
<androidx.appcompat.widget.Toolbar
android:layout_width="0dp"
Also to use toolbar you need to paste this in your build Gradle file in dependencies:
implementation 'androidx.appcompat:appcompat:1.0.0'
If this doesn't solve anything please provide your whole XML, JAVA.class, and full stack trace of your exception.

In build.gradle (module app) file, implementation of The upgraded version : implementation 'androidx.appcompat:appcompat:1.4.1' caused me the error. When I used implementation 'androidx.appcompat:appcompat:1.0.0' , The error has gone.

added my whole XML
<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:context=".MainActivity"
tools:openDrawer="start">
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_gravity="start"
android:layout_height="match_parent"
android:layout_width="wrap_content"
app:headerLayout="#layout/header"
app:menu="#menu/main_menu" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">
<androidx.appcompat.widget.Toolbar
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="#00008577"
android:minHeight="?actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navigationIcon="#drawable/main"
android:id="#+id/toolbar"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.drawerlayout.widget.DrawerLayout>
JAVA:
package com.or.ourshoppinglist;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import android.os.Bundle;
import com.google.android.material.navigation.NavigationView;
public class MainActivity extends AppCompatActivity {
private DrawerLayout drawer_layout;
private NavigationView nav_view;
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
setSupportActionBar(toolbar);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer_layout,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
drawer_layout.addDrawerListener(toggle);
toggle.syncState();
}
private void findViews() {
drawer_layout = findViewById(R.id.drawer_layout);
nav_view = findViewById(R.id.nav_view);
toolbar = findViewById(R.id.toolbar);
}
}

I fixed this by changing android:background to app:srcCompat when I am using
?android:attr.

Related

How can I access a TextView in the onCreate function?

Hi have been using fragments and have a drawer header which is going to display the currently logged in user.
activity_main.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:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="false"
android:background="?android:attr/colorBackground"
tools:context=".MainActivity">
<include
layout="#layout/drawer_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<include
layout="#layout/content_main"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigationView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/drawer_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
This is the code that creates the layout and the adds the header and menu to the navigation view. I am trying to access the text stored in the drawer header with an id.
The problem i get is that when it tries to set the variable usernameHeader to the id "user" it throws an error saying that there is a null object reference. My understanding is that the drawer is create during the onCreate when it says "actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close);" i could be wrong and any help is greatly appreciated. Thanks in advance
UPDATE:
I have rewritten and minimal example to show the exact part where it breaks.
I have added the drawer_header.xml which contains the TextView with the id 'user'. I have also added the minimal version of the MainActivity.
drawer_header.xml
<?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:layout_width="match_parent"
android:layout_height="150dp"
android:background="#color/teal_700">
<TextView
android:id="#+id/user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:text="#string/user"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.secureit.drawerfragment;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import com.google.android.material.navigation.NavigationView;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
Toolbar toolbar;
NavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawerLayout = findViewById(R.id.drawer);
navigationView = findViewById(R.id.navigationView);
navigationView.setNavigationItemSelectedListener(this);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.setDrawerIndicatorEnabled(true);
actionBarDrawerToggle.syncState();
TextView textView = findViewById(R.id.user); // This is where the error shows "Null object reference"
textView.setText("user");
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
drawerLayout.closeDrawer(GravityCompat.START);
return false;
}
}
The problem is, you are accessing a TextView in different view from your activity. You should inflate your header view first and then use that to access your TextView. Add this to your onCreate method.
View headerView = navigationView.getHeaderView(0);
TextView txtUser= headerView.findViewById(R.id.user);
txtUser.setText("user");

Application crash when I try to open the activity

When I press login on my app it should take me to activity.targa, but instead it crashes and I can't understand why (My phone is Redmi Note 7, but it also crashes on Mi A2)
activity.targa:
<?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:background="#FFFFFF"
android:fitsSystemWindows="true"
tools:context=".TargaActivity">
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<com.google.android.material.circularreveal.CircularRevealFrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView.../>
<com.google.android.material.textfield.TextInputLayout.../>
<TextView.../>
<TextView.../>
</LinearLayout>
<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"
app:headerLayout="#layout/nav_header"
app:menu="#menu/drawer_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
and TargaActivity:
package com.efficientparking.efficientparking;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import java.util.HashMap;
public class TargaActivity extends AppCompatActivity {
private DrawerLayout drawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_targa);
androidx.appcompat.widget.Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = 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();
}
#Override
public void onBackPressed() {
if(drawer.isDrawerOpen(GravityCompat.START)){
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
}
androidx.appcompat.widget.Toolbar toolbar = findViewById(R.id.toolbar);
I had to change here since Android Studio gave me troubles because he was saying that the following method (setSupportActionBar(toolbar)) didn't exist.
But now as soon as I try to get into activity_targa, app crashes.
Android studio actually load the xml and it seems nice, but then I compile the apk and it crashes.
Log file:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.efficientparking/com.efficientparking.efficientparking.TargaActivity}: java.lang.ClassCastException: android.widget.Toolbar cannot be cast to androidx.appcompat.widget.Toolbar
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3311)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3460)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2047)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7590)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
Caused by: java.lang.ClassCastException: android.widget.Toolbar cannot be cast to androidx.appcompat.widget.Toolbar
at com.efficientparking.efficientparking.TargaActivity.onCreate(TargaActivity.java:22)
at android.app.Activity.performCreate(Activity.java:7893)
at android.app.Activity.performCreate(Activity.java:7880)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3286)
... 11 more
You should use android.widget.Toolbar in both xml and java, as setSupportActionBar takes android.widget.Toolbar

How to remove the shadow under Action bar

I have been trying to get the shadow under the app Action bar to disappear but it wont which is very annoying. iI have set the theme to NoActionBar and opted to using toolbar instead.
And have i have also set the elevation to 0dp in my activity.java
package com.example.drawerlayout;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import android.os.Bundle;
import android.view.MenuItem;
import androidx.appcompat.widget.Toolbar;
import com.google.android.material.appbar.AppBarLayout;
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
private Toolbar mToolbar;
private AppBarLayout actionLayout;
private Toolbar t;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
mToolbar = (Toolbar) findViewById(R.id.nav_action);
setSupportActionBar(mToolbar);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setElevation(0);
}
}
The Toolbar looks thus:
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="#+id/nav_action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/mm_blue" />
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Simply add app:elevation="0dp"
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp">
<!-- Toolbar -->
</com.google.android.material.appbar.AppBarLayout>
Set this property toolbar:elevation="0dp" to AppBarLayout to remove shadow from Toolbar.
eg-
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appbar"
toolbar:elevation="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="#+id/nav_action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/mm_blue" />
</com.google.android.material.appbar.AppBarLayout>
Check below code
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="0dp"
app:elevation="0dp">
<androidx.appcompat.widget.Toolbar
android:id="#+id/nav_action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/mm_blue" />
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Just remove the AppBar layout and directly put your toolbar in the coordinate 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"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="#+id/nav_action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/mm_blue" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Try this may it should help you out.

Missing Hamburger Icon Android

I tried searching for different answers regarding the hamburger icon, but haven't found anything similar. I'm trying to create a hamburger icon and have been following a tutorial. The code runs fine, but the icon is missing when I open it in the virtual device. Any suggestions are appreciated.
activity_main xml
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.android.navigationapp.navigationapp.MainActivity"
android:id="#+id/drawerLayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Main Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:layout_gravity="center_vertical"
android:textAlignment="center"
android:textSize="24dp" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="#menu/navigation_menu"
android:layout_gravity="start">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
MainActivity Java
package com.android.navigationapp.navigationapp;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

Toolbar not showing app name and back button

So because of my problems with toolbar shadow, which I posted here earlier, I came up with idea how to remove it. Now there's no shadow at least, but there's no activity name or back button. See image below
Here's the content.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="0dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="hr.app.liftme.liftmehr.Vjezbe"
tools:showIn="#layout/activity_vjezbe">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Vježbe"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<include
android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="#layout/toolbar_layout"
>
</include>
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
app:tabMode="scrollable"
app:tabGravity="fill"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/viewPager"
android:background="#ffffff">
</android.support.v4.view.ViewPager>
</android.support.design.widget.AppBarLayout>
</LinearLayout>
and here's the toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:fitsSystemWindows="true"
android:id="#+id/toolBar"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.v7.widget.Toolbar>
java file
package hr.app.liftme.liftmehr;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
public class Vjezbe extends AppCompatActivity {
private Toolbar toolbar;
TabLayout tabLayout;
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vjezbe);
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar == null){
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Vježbe");
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setElevation(0);
}
tabLayout = (TabLayout)findViewById(R.id.tabLayout);
viewPager = (ViewPager)findViewById(R.id.viewPager);
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
//here are the fragments, not going to copy all of that
As you can see I tried something with if statement in java file, but that didn't work.
What am I missing?
Problem
if (toolbar == null)
Solutions
Use !null checking instead of null
if (toolbar != null){
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Vježbe");
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setElevation(0);
}
Edit
// toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null){
//Your code
getSupportActionBar().setTitle("Vježbe");
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setElevation(0);
}
Worked
By adding android:elevation="0dp" .
remove
if(toolbar==null) beacuse this condition is true only if toolbar is null
and make it
if (toolbar != null){
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Vježbe");
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setElevation(0);
}
Just Add this code to you onCreate event like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.example_activity);//set view
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);// initial your tool bar
setSupportActionBar(toolbar);//set tool bar
getSupportActionBar().setDisplayHomeAsUpEnabled(true);//allow back button
getSupportActionBar().setDisplayShowHomeEnabled(true);//Show back arrow
getSupportActionBar().setLogo(R.mipmap.yourlogo);//set logo
getSupportActionBar().setDisplayUseLogoEnabled(true);//enable log
}

Categories