button BACK to a collapsingToolbar - Android - java

It happens that from my main activity I call an activity in that activity that I call tale with collapsingToolbar, well everything works fine only that I can not give BACK because the collapsinToolbar does not generate a button back.
Java code of the main activity
package com.herprogramacion.alquileres;
import android.database.Cursor;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.herprogramacion.alquileres.provider.Contrato.Alquileres;
public class ActividadListaAlquileres extends AppCompatActivity implements AdaptadorAlquileres.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor> {
private RecyclerView listaUI;
private LinearLayoutManager linearLayoutManager;
private AdaptadorAlquileres adaptador;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.actividad_lista_alquileres);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Filtro...", Snackbar.LENGTH_LONG)
.setAction("Acción", null).show();
}
});
// Preparar lista
listaUI = (RecyclerView) findViewById(R.id.lista);
listaUI.setHasFixedSize(true);
linearLayoutManager = new LinearLayoutManager(this);
listaUI.setLayoutManager(linearLayoutManager);
adaptador = new AdaptadorAlquileres(this, this);
listaUI.setAdapter(adaptador);
// Iniciar loader
getSupportLoaderManager().restartLoader(1, null, this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_actividad_lista_alquileres, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(AdaptadorAlquileres.ViewHolder holder, String idAlquiler) {
// Snackbar.make(findViewById(android.R.id.content), ":id = " + idAlquiler,
// Snackbar.LENGTH_LONG).show();
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(this, Alquileres.URI_CONTENIDO, null, null, null, null);
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
if (adaptador != null) {
adaptador.swapCursor(data);
}
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
}
}
Java code of the activity where I generate the collapsingToolbar
package com.herprogramacion.alquileres;
import android.content.Intent;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
public class biografia2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_biografia2);
CollapsingToolbarLayout collapsingToolbar =
(CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbar.setTitle("Titulo ");
}
}
Xml code of my main activity
<?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="com.herprogramacion.alquileres.ActividadListaAlquileres">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<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/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/contenido_actividad_lista_alquileres" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#drawable/icono_filtro" />
</android.support.design.widget.CoordinatorLayout>
Xml code of the activity where I generate the collapsingToolbar
<?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"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="#dimen/detail_backdrop_height"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp" >
<ImageView
android:id="#+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax"
android:src="#drawable/jack"
android:alpha="0.5"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="170dp"
android:layout_gravity="center">
</RelativeLayout>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="24dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/card_margin">
<LinearLayout
style="#style/Widget.CardContent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Info"
android:textAppearance="#style/TextAppearance.AppCompat.Title" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/lorem_ipsum" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/card_margin"
android:layout_marginLeft="#dimen/card_margin"
android:layout_marginRight="#dimen/card_margin">
<LinearLayout
style="#style/Widget.CardContent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Friends"
android:textAppearance="#style/TextAppearance.AppCompat.Title" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/lorem_ipsum" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/card_margin"
android:layout_marginLeft="#dimen/card_margin"
android:layout_marginRight="#dimen/card_margin">
<LinearLayout
style="#style/Widget.CardContent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Related"
android:textAppearance="#style/TextAppearance.AppCompat.Title" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/lorem_ipsum" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:layout_anchor="#id/appbar"
app:layout_anchorGravity="bottom|right|end"
android:src="#drawable/ic_discuss"
android:layout_margin="#dimen/fab_margin"
android:clickable="true"/>
</android.support.design.widget.CoordinatorLayout>

You can add back navigation icon in the xml for Toolbar component using app:navigationIcon attribute.
<android.support.v7.widget.Toolbar
android:id="#+id/materialup.toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:layout_scrollFlags="scroll"
app:navigationIcon="?homeAsUpIndicator" />

You should be able to show the default back button using setDisplayHomeAsUpEnabled:
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Related

setDrawerLockMode disable the drawerLayout

Hi guys I search on google about how to disable the swipe that open the drawerLayout i found this 2 codes.
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, GravityCompat.END);
but these 2 codes disable the opening of drawerLayout from the swipe and from the actionBarDrawerToggle like disabling it at all. Now the question is how can i disable the swipe that opens the drawerLayout without disabling the open by the ActionBarDrawerToggle. This is my application codes.
MainActivity.java
package co.pilot.assist;
import android.os.Bundle;
import android.widget.ImageView;
import androidx.appcompat.widget.Toolbar;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
public class MainActivity extends AppCompatActivity {
private DrawerLayout _drawer;
private Toolbar _toolbar;
private boolean isOpen;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
_drawer = findViewById(R.id._drawer);
_toolbar = findViewById(R.id._toolbar);
drawerSetUp();
}
private void drawerSetUp() {
ActionBarDrawerToggle _toggle = new ActionBarDrawerToggle(MainActivity.this, _drawer, _toolbar,
R.string.app_name, R.string.app_name);
_drawer.addDrawerListener(_toggle);
_toggle.syncState();
_drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
}
main.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start" >
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="#+id/_coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<androidx.appcompat.widget.Toolbar
android:id="#+id/_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/arsenic_92" />
</com.google.android.material.appbar.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:orientation="vertical" >
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="8dp" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/arsenic_90"
app:menu="#menu/main_bottom_nav_menu" />
</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<LinearLayout
android:id="#+id/_nav_view"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#EEEEEE" >
<include layout="#layout/_drawer_main" />
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>

Recycler View inside frame layout not scrolling vertically

I have a recycler view inside fragment "Home" and it is not scrolling vertically. Can someone please help me out !!
My fragment is inside home screen containing a toolbar, drawer navigation and bottom tab navigation . i got another recycler view inside home fragment and that recycler view is not scrolling
This recyclerView is not scrolling >>>
Here is my HomeScreen code
<?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="match_parent"
tools:context=".homeSc"
android:id="#+id/constraintLayout">
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</FrameLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="60dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/bottom_nav_menu" />
<androidx.drawerlayout.widget.DrawerLayout
android:id="#+id/drawerNavigationDrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
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:id="#+id/drawerNavigationToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#B3E3E1"
android:elevation="4dp"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:titleTextColor="#color/black"/>
<FrameLayout
android:id="#+id/drawerNavigationFragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:isScrollContainer="true"/>
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/drawerNavigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/drawer_nav_header"
app:menu="#menu/drawer_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Here is my Home Fragment Code
<?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:id="#+id/frameLayoutHomeSc"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerVFragHomeSc"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
Recycler View Adapter Code
package com.example.knowledgespaceapk;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class adapterRecVHomeFrag extends RecyclerView.Adapter<adapterRecVHomeFrag.myviewholder>{
ArrayList<dataModelRecVFragHome> dataHolder;
public adapterRecVHomeFrag(ArrayList<dataModelRecVFragHome> dataHolder) {
this.dataHolder = dataHolder;
}
#NonNull
#Override
public myviewholder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new myviewholder(LayoutInflater.from(parent.getContext()).inflate(R.layout.single_row_design_rec_homef
,parent,false));
}
#Override
public void onBindViewHolder(#NonNull myviewholder holder, int position) {
holder.img.setImageResource(dataHolder.get(position).getImage());
holder.title.setText(dataHolder.get(position).getTitle());
holder.desc.setText(dataHolder.get(position).getDescription());
}
#Override
public int getItemCount() {
return dataHolder.size();
}
class myviewholder extends RecyclerView.ViewHolder{
ImageView img;
TextView title,desc;
public myviewholder(#NonNull View itemView) {
super(itemView);
img = itemView.findViewById(R.id.imageVSingleRowDesRecHomeF);
title = itemView.findViewById(R.id.titleTxtVSingleRDesRecHomeF);
desc = itemView.findViewById(R.id.descriptionTxtVSingleRDesRecHomeF);
}
}
}
Recyler View Element layout Code
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="5dp"
android:elevation="4dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imageVSingleRowDesRecHomeF"
android:layout_width="match_parent"
android:layout_height="219dp"
android:visibility="visible"
app:srcCompat="#drawable/fest" />
<TextView
android:id="#+id/titleTxtVSingleRDesRecHomeF"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:text="TextView"
android:textColor="#000000"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="#+id/descriptionTxtVSingleRDesRecHomeF"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_marginLeft="8dp"
android:textColor="#000000"
android:textSize="24sp"
android:textStyle="bold"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
Home Screen Java Code
package com.example.knowledgespaceapk;
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 androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import com.example.knowledgespaceapk.databinding.ActivityHomeScBinding;
import com.google.android.material.bottomnavigation.BottomNavigationView;
public class homeSc extends AppCompatActivity {
private DrawerLayout drawerLayout;
private BottomNavigationView bottomNavigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_sc);
Toolbar toolbar = findViewById(R.id.drawerNavigationToolbar);
setSupportActionBar(toolbar);
drawerLayout = findViewById(R.id.drawerNavigationDrawerLayout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawerLayout,
toolbar,R.string.open,R.string.close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
replaceFrag(new HomeFragment());
bottomNavigationView = findViewById(R.id.bottomNavigationView);
bottomNavigationView.setOnItemSelectedListener(item -> {
switch (item.getItemId()){
case R.id.home : replaceFrag(new HomeFragment()); break;
case R.id.group: replaceFrag(new GroupFragment()); break;
case R.id.notification:replaceFrag(new NotificationFragment()); break;
}
return true;
});
}//End OnCreate
#Override
public void onBackPressed() {
if(drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
}else {
super.onBackPressed();
}
}
private void replaceFrag(Fragment fragment){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frameLayout,fragment);
fragmentTransaction.commit();
}
}//End Main
Try to set the DrawerLayout as parent view in your HomeScreen 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/drawerNavigationDrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".homeSc"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/constraintLayout">
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="60dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/bottom_nav_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="#+id/drawerNavigationToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#B3E3E1"
android:elevation="4dp"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:titleTextColor="#color/black"/>
<FrameLayout
android:id="#+id/drawerNavigationFragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:isScrollContainer="true"/>
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/drawerNavigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/drawer_nav_header"
app:menu="#menu/drawer_menu" />
</androidx.drawerlayout.widget.DrawerLayout>

Why does findViewById() returns "null"?

I have an important issue with java
I have this code :
package fr.christian.lbcde;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.tabs.TabLayout;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;
import fr.christian.lbcde.ui.main.PageViewModel;
import fr.christian.lbcde.ui.main.SectionsPagerAdapter;
public class MainActivity extends AppCompatActivity {
PageViewModel pageViewModel;
View activity_main, tab_create, tab_connect;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pageViewModel = new ViewModelProvider(this).get(PageViewModel.class);
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
FloatingActionButton fab = findViewById(R.id.fab);
tab_connect = findViewById(R.id.tab_connect);
tab_create = findViewById(R.id.tab_create);
System.out.print("Result of tab_connect : ");
System.out.print(findViewById(R.id.tab_connect));
System.out.print(" ");
System.out.println(tab_connect==null);
System.out.print("Result of tab_create : ");
System.out.print(findViewById(R.id.tab_create));
System.out.print(" ");
System.out.println(tab_create==null);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addNew();
}
});
// hideAll();
// tab_connect.setVisibility(View.VISIBLE);
pageViewModel.addTabListener(new PageViewModel.tabChangeListener() {
#Override
public void onChangeTab(View view, int tab) {
switch (tab) {
case 1:
hideAll();
tab_connect.setVisibility(View.VISIBLE);
break;
case 2:
hideAll();
tab_create.setVisibility(View.VISIBLE);
break;
default:
new Toast(getApplicationContext())
.makeText(getApplicationContext(), "Rien a afficher pour cet onglet", Toast.LENGTH_SHORT)
.show();
}
}
});
}
public void addNew() {
new Toast(getApplicationContext())
.makeText(getApplicationContext(), "Cette option n'est pas encore disponible", Toast.LENGTH_SHORT)
.show();
}
public void hideAll() {
View[] views = {
tab_create,
tab_connect
};
for (int i = 0; i < views.length; i++) {
views[i].setVisibility(View.INVISIBLE);
}
;
}
}
and four files for my layouts :
activity 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=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/Theme.LeBonCoinDeLÉvangile.AppBarOverlay">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:minHeight="?actionBarSize"
android:padding="#dimen/appbar_padding"
android:text="#string/app_name"
android:textAppearance="#style/TextAppearance.Widget.AppCompat.Toolbar.Title" />
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:tabSelectedTextColor="?attr/colorPrimaryVariant"/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:background="?attr/colorPrimary"
app:srcCompat="#android:drawable/ic_menu_info_details" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
fragment_main.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:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.main.PlaceholderFragment">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:layout_marginBottom="#dimen/activity_vertical_margin">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<include layout="#layout/login_main"/>
<include layout="#layout/create_main"/>
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
create_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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/tab_create"
android:orientation="vertical"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:visibility="invisible">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TAB_CREATE"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BONJOUR LES ÉLËPHANTS"/>
</LinearLayout>
and login_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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/tab_connect"
android:orientation="vertical"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:layout_marginBottom="#dimen/activity_vertical_margin">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TAB_CONNECT"/>
<TextView
style="#style/h2textStyle"
android:text="#string/titleConnect_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/loginConnect_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
</LinearLayout>
The problem is that at the lines :
System.out.print(findViewById(R.id.tab_connect));
and
System.out.print(findViewById(R.id.tab_create));
I have this result (for the both) :
null
So I can't remove the error "java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setVisibility(int)' on a null object reference" in hideAll(), tab_create.setVisibility(View.VISIBLE), etc... because when I initialize my layouts, they are initialized with a null object (and not my layouts).
I add some lines like
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TAB_CREATE"/>
in my file to see if those tabs are really those I see on my screen and its appear that I really see them (when I cancel the lines with the layouts visibility).
Thanks for your help!
Your tab_connect etc. are included in fragment_main layout and not in activity_main layout that is inflated when you call findViewById() for those ids. The code you posted does not show where you're using fragment_main but what is certain is that it is not in your activity's view hierarchy at the time when you try to look up those views.
Either move the findViewById() calls and whatever you're doing with the views to the fragment, or move the views from the fragment to your main activity.

Overlay element on toolbar

I want to impose a Cardview element on the toolbar. And at the stage of previewing everything is displayed correctly. However, after compiling, Cardview is under the toolbar. What could be the problem? Thanks you!
Screenshot.
It should be.:
Screenshot.
How come.:
API 22. Here and There.
My project structure: file "activity_documents" (there are left-navigation) -> file "app_bar_document" (there are toolbar) -> file "content_documents"
My code:
"activity_documents":
<?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"
tools:openDrawer="start">
<android.support.v7.widget.ContentFrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="#layout/app_bar_documents_type"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v7.widget.ContentFrameLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_documents_type"
app:menu="#menu/activity_documents_type_drawer" />
</android.support.v4.widget.DrawerLayout>
"app_bar_documents":
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".DocumentsTypeActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/back_toolbar_documents"
android:minHeight="?attr/actionBarSize"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:titleTextColor="#android:color/white">
<TextView
android:id="#+id/textview_documents"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|top"
android:text="Документы"
android:textColor="#android:color/background_light" />
</android.support.v7.widget.Toolbar>
<!--android:background="?attr/colorPrimary"-->
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_documents_type"/>
<!--<include layout="#layout/content_doc"/>-->
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin" />
</FrameLayout>
"content_documents"
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".DocumentsTypeActivity"
tools:showIn="#layout/app_bar_documents_type">
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<android.support.v7.widget.CardView
android:id="#+id/cardView_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="65dp"
android:layout_marginEnd="20dp"
app:cardBackgroundColor="#android:color/background_light"
app:cardCornerRadius="6dp"
app:cardElevation="4dp"
app:cardUseCompatPadding="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:orientation="horizontal">
<android.support.v7.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/ic_documents_multipass" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:orientation="horizontal">
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MultiPass.One"
android:textColor="#android:color/background_dark"
android:textSize="16sp"
app:fontFamily="sans-serif-black" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="20dp"
android:gravity="center_horizontal"
android:orientation="horizontal">
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ваш международный \n документ"
android:textAlignment="center"
android:textColor="#android:color/background_dark" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</LinearLayout>
</FrameLayout>
DocumentActivity:
package com.multipassone;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class DocumentsTypeActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_documents_type);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(null);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
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();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
}

Android ImageButton does not display image

I am new to java and I am developing a simple mediaplayer: I have problems with two image buttons: when i run the app the relative images are not displayed. This is my code:
JAVA:
package lukes.mediaplayer;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.app.Activity;
public class alpha extends Activity implements View.OnClickListener {
SeekBar seekBar_alpha;
ImageButton btPlay_alpha;
ImageButton btStop_alpha;
MediaPlayer mp;
Handler seekHandler = new Handler();
ImageView display;
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alpha);
display = (ImageView) findViewById(R.id.imagetop);
display.setOnClickListener(this);
getInit();
seekUpdation();
}
public void getInit() {
seekBar_alpha = (SeekBar) findViewById(R.id.seekBar_alpha);
btPlay_alpha = (ImageButton) findViewById(R.id.btPlay_alpha);
btStop_alpha = (ImageButton) findViewById(R.id.btStop_alpha);
btPlay_alpha.setOnClickListener(this);
btStop_alpha.setOnClickListener(this);
mp = MediaPlayer.create(this, R.raw.alpha_audio);
seekBar_alpha.setMax(mp.getDuration());
}
Runnable run = new Runnable() {
#Override
public void run() {
seekUpdation();
}
};
public void seekUpdation() {
seekBar_alpha.setProgress(mp.getCurrentPosition());
seekHandler.postDelayed(run, 1000);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.imagetop:
Intent intent = new Intent (alpha.this, alpha_img.class);
startActivity(intent);
case R.id.btPlay_alpha:
mp.start();
break;
case R.id.btStop_alpha:
mp.pause();
}
}
#Override
protected void onPause(){
super.onPause();
if (mp!=null && mp.isPlaying()){
mp.pause();
}
}
}
The "btPlay_alpha" and "btStop_alpha" image buttons does not display their image. Can you help me to fix that? Thank you!
Hey guys, I add the XML code:
<?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=".alpha">
<android.support.design.widget.AppBarLayout
android:id="#+id/main.appbar"
android:layout_width="match_parent"
android:layout_height="350dp"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/main.collapsing"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="#+id/main.toolbar"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<ImageButton
android:id="#+id/imagetop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:src="#drawable/alpha"
app:layout_collapseMode="parallax" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-condensed"
android:lineSpacingExtra="8dp"
android:padding="16dp"
android:text="#string/alpha_title"
android:textColor="#color/colorPrimary"
android:textSize="24sp"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="16dp"
android:paddingRight="16dp">
<ImageButton
android:id="#+id/btStop_alpha"
android:layout_width="60dp"
android:layout_height="55dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:backgroundTint="#color/lightRed"
android:elevation="6dp"
android:padding="16dp"
app:srcCompat="#android:drawable/ic_media_pause" />
<ImageButton
android:id="#+id/btPlay_alpha"
android:layout_width="60dp"
android:layout_height="55dp"
android:layout_alignParentTop="true"
android:layout_gravity="center"
android:layout_toEndOf="#+id/btStop_alpha"
android:layout_toRightOf="#+id/btStop_alpha"
android:backgroundTint="#color/lightRed"
android:elevation="6dp"
app:srcCompat="#android:drawable/ic_media_play" />
<SeekBar
android:id="#+id/seekBar_alpha"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/btPlay_alpha"
android:layout_toRightOf="#+id/btPlay_alpha"
android:max="100" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="serif"
android:lineSpacingExtra="8dp"
android:padding="16dp"
android:text="#string/alpha_text"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
Thank you for your support!
I see youre extending Activity Instead of AppCompatActivity. In your xml, you are setting attribute srcCompat; make sure As of Android Support Library 23.3.0, support vector drawables can only be loaded via app:srcCompat .
you need to add vectorDrawables.useSupportLibrary = true to your build.gradle file
// Gradle Plugin 2.0+
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}

Categories