Toolbar image error - android

It's apparently a question that has been asked multiple times, but only has solutions on a case-by-case basis.I am making an aap with toolbar and when i go to second activity i want the toolbar to display icon as well as settings menu but unfortunately toolbar is appearing but icons are not showing also android monitor shows null pointer exception on these lines of code
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
Error pic
This is my activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.zaina.toolbar.MainActivity">
<include android:id="#+id/aap_bar" layout="#layout/aap_bar"></include>
<TextView
android:layout_below="#id/aap_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
This is my MainActivity.java
package com.example.zaina.toolbar;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.aap_bar);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu item) {
getMenuInflater().inflate(R.menu.menu_main, item);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem menuItem){
int id=menuItem.getItemId();
if(R.id.action_favorite==id){
startActivity(new Intent(this,aap.class));
return true;
}
if(R.id.action_settings==id){
Toast.makeText(MainActivity.this, "Hey you hit", Toast.LENGTH_SHORT).show();
return true;
}
return onOptionsItemSelected(menuItem);
}
}
This is my aapbar_xml.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:layout_height="wrap_content"
xmlns:aap="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/colorPrimary"
aap:Theme="#style/MyCustom"
app:popupTheme="#style/ThemeOverlay.AppCompat.Dark">
</android.support.v7.widget.Toolbar>
This is my activity_aap.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.zaina.toolbar.aap">
<include android:id="#+id/app_bar" layout="#layout/aap_bar"></include>
</RelativeLayout>
This is aap.java
package com.example.zaina.toolbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
public class aap extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aap);
Toolbar toolbar= (Toolbar) findViewById(R.id.aap_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu item) {
getMenuInflater().inflate(R.menu.menu_sub, item);
return true;
}
}
This is my menu_sub.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aap="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/za"
android:icon="#drawable/ic_chevron_left_black_24dp"
android:title="left"
aap:showAsAction="ifRoom"/>
<item android:id="#+id/zee"
android:title="sett"
aap:showAsAction="never"/>
</menu>

There may be few reasons of NullPointException, try to:
You are probably using a Theme that doesn't support ActionBar.
Trying using this theme:
android:theme="#android:style/Theme.Holo.Light".
try to catch:
if (getSupportActionBar() != null)
{
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
Or, try to change the id of app_bar in aap.java

findViewById method returning null.
Add id attribute to your toolbar
android:id="#+id/aap_bar
You are also using wrong namespace aap
Use app
xmlns:app="http://schemas.android.com/apk/res-auto"

Related

when adding navigation drawer to the application. this does not work

Since a few days ago I decided to program for Android and buy a book about that. There are several examples and I have achieved them satisfactorily but at the moment of programming the navigation drawer and running it on my cell phone it appears that it closed unfortunately. In the console of Android Studio does not appear any error and I was reviewing codes, example, etc and I have not been able to solve the problem.
Here you can see part of the application code to create and initialize the toolbar and the drawer layout
package com.example.luisenrique.clase_aplicacion;
import android.content.SharedPreferences;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
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 android.widget.Toast;
import com.example.luisenrique.clase_aplicacion.fragments.DetalleFragment;
import com.example.luisenrique.clase_aplicacion.fragments.SelectorFragment;
import static android.R.id.toggle;
import static com.example.luisenrique.clase_aplicacion.R.id.appBarLayout;
import static com.example.luisenrique.clase_aplicacion.R.id.drawer_layout;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private AdaptadorLibrosFiltro adaptador;
private AppBarLayout appBarLayout;
private TabLayout tabs;
private DrawerLayout drawer;
private ActionBarDrawerToggle toggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if((findViewById(R.id.contenedor_pequeno)!=null) && (getSupportFragmentManager().findFragmentById(R.id.contenedor_pequeno)==null)){
SelectorFragment primerFragment=new SelectorFragment();
getSupportFragmentManager().beginTransaction().add(R.id.contenedor_pequeno,primerFragment).commit();
}
adaptador=((Aplicacion)getApplicationContext()).getAdaptador();
appBarLayout=(AppBarLayout)findViewById(R.id.appBarLayout);
//Pestañas
TabLayout tabs=(TabLayout)findViewById(R.id.tabs);
tabs.addTab(tabs.newTab().setText("Todos"));
tabs.addTab(tabs.newTab().setText("Nuevos"));
tabs.addTab(tabs.newTab().setText("Leidos"));
tabs.setTabMode(TabLayout.MODE_SCROLLABLE);
tabs.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
#Override
public void onTabSelected(TabLayout.Tab tab){
switch (tab.getPosition()){
case 0: //Todos
adaptador.setNovedad(false);
adaptador.setLeido(false);
break;
case 1: //Nuevos
adaptador.setNovedad(true);
adaptador.setLeido(false);
break;
case 2: //Leidos
adaptador.setNovedad(false);
adaptador.setLeido(true);
break;
}
adaptador.notifyDataSetChanged();
}
#Override
public void onTabUnselected(TabLayout.Tab tab){}
#Override
public void onTabReselected(TabLayout.Tab tab){}
});
Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer=(DrawerLayout)findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle=new ActionBarDrawerToggle(this,drawer,toolbar,R.string.drawer_open,R.string.drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView=(NavigationView)findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item){
int id=item.getItemId();
if(id==R.id.nav_todos){
adaptador.setGenero("");
adaptador.notifyDataSetChanged();
}
else if(id==R.id.nav_epico){
adaptador.setGenero(Libro.G_EPICO);
adaptador.notifyDataSetChanged();
}
else if(id==R.id.nav_XIX){
adaptador.setGenero(Libro.G_S_XIX);
adaptador.notifyDataSetChanged();
}
else if(id==R.id.nav_suspense){
adaptador.setGenero(Libro.G_SUSPENSE);
adaptador.notifyDataSetChanged();
}
DrawerLayout drawer=(DrawerLayout)findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
public void onBackPressed(){
DrawerLayout drawer=(DrawerLayout)findViewById(R.id.drawer_layout);
if(drawer.isDrawerOpen(GravityCompat.START)){
drawer.closeDrawer(GravityCompat.START);
}
else{
super.onBackPressed();
}
}
}
<!--activity main.xml-->
the main activity is where the main view will be shown and it will be linked to app_bar_main_xml where the other components are located that have no relation to the navigation drawer and work correctly and therefore I will not put the 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"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!--El contenido de la actividad-->
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!--El contenido del navigation drawer-->
<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_main"
app:menu="#menu/activity_main_drawer"/>
</android.support.v4.widget.DrawerLayout>
<!--nav header-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="160dp"
android:background="#color/colorAccent"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingLeft="?attr/listPreferredItemPaddingLeft"
android:paddingRight="?attr/listPreferredItemPaddingRight"
android:paddingTop="16dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:src="#android:drawable/sym_def_app_icon"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:text="Audiolibros"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"/>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="http://www.androidcurso.com"/>
</LinearLayout>
<!--menu activity_main_drawer.xml-->
in the menu you will see how the navigation drawer will be sectioned. what items will be available and how they will be grouped.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item android:id="#+id/nav_todos"
android:icon="#android:drawable/ic_menu_gallery"
android:title="Todos los generos"/>
<item android:id="#+id/nav_epico"
android:icon="#android:drawable/ic_menu_gallery"
android:title="Poema épico"/>
<item android:id="#+id/nav_XIX"
android:icon="#android:drawable/ic_menu_gallery"
android:title="Literatura siglo XIX"/>
<item android:id="#+id/nav_suspense"
android:icon="#android:drawable/ic_menu_gallery"
android:title="Suspense"/>
</group>
<item android:title="Acciones adicionales">
<menu>
<item android:id="#+id/nav_preferencias"
android:icon="#android:drawable/ic_menu_manage"
android:title="Preferencias"/>
<item android:id="#+id/nav_compartir"
android:icon="#android:drawable/ic_menu_share"
android:title="Compartir"/>
</menu>
</item>
</menu>
It is to call the texts that are required in other classes, there are only two that are called opening and closing.
<!--Strings-->
<resources>
<string name="app_name">Clase_Aplicacion</string>
<string name="drawer_open">navigation drawer abierto</string>
<string name="drawer_close">navigation drawer cerrado</string>
</resources>

How to show an activity icon in an action bar? [duplicate]

This question already has answers here:
The application icon does not show on action bar
(6 answers)
Closed 5 years ago.
In my Android application, I'm trying to use setlogo(), ActionBar and android:icon but they don't work as expected: Output shows absolutely no icon and I'm really confusing about it. I need your help.
My code:
import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setIcon(R.mipmap.ic_launcher_round);
//recyclerview
recyclerView= (RecyclerView) findViewById(R.id.recycle_main);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_mainactivity,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.menu_add_contects:
startActivity(new Intent(MainActivity.this,AddContects.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
To set an icon in your ActionBar you need to call
setHomeAsUpIndicator() instead of setIcon()
Like this:
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setHomeAsUpIndicator(R.drawable.ic_arrow_back);
actionBar.setDisplayHomeAsUpEnabled(true);
}
You can set any image in toolbar using the following code.
<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" >
<RelativeLayout
android:layoutDirection="ltr"
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/camera"
android:src="#drawable/camera"
android:layout_toStartOf="#+id/search"
android:paddingStart="#dimen/margin_10"
android:paddingEnd="#dimen/margin_10"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<ImageView
android:id="#+id/search"
android:layout_alignParentEnd="true"
android:src="#drawable/search"
android:paddingStart="#dimen/margin_10"
android:paddingEnd="#dimen/margin_10"
android:layout_marginEnd="#dimen/margin_6"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>

Navigation Drawer Android layout created. How to create onClick actions?

I'm a complete noob to programming, so thank you for your patience.
In my main activity I have a random quote showing up, then I created a Navigation Drawer (with the help of a youtube tutorial of course). How do I make these "items" react when I click on them? For example, one of them is a glossary, so I will need to write a lot. Should I create an Activity or a Fragment (what's the difference anyway)? And how do I make a click on top of "Glossary" open that View?
Thank you once more :)
Edit: (Here are my relevant codes and layouts)
<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:id="#+id/activity_quote_of_the_dat"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hp.myapp.QuoteOfTheDat"
android:background="#color/colorAccent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/randomQuote" android:layout_marginTop="147dp"
android:textSize="24sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textAlignment="center"
android:textColor="#android:color/background_light" />
</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:id="#+id/drawerLayout"
app:headerLayout="#layout/navigation_header">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
and my menu xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/home"
android:icon="#mipmap/ic_home_black_24dp"
android:title="Home"/>
<item android:id="#+id/glossary"
android:icon="#mipmap/ic_book_black_24dp"
android:title="Glossary"/>
<item android:id="#+id/anxiety"
android:icon="#mipmap/ic_lock_outline_black_24dp"
android:title="Anxiety"/>
<item android:id="#+id/physicality"
android:icon="#mipmap/ic_pan_tool_black_24dp"
android:title="Physicality"/>
<item android:id="#+id/date"
android:icon="#mipmap/ic_favorite_black_24dp"
android:title="ToBeDefined"/>
</menu>
and my java file
package com.example.hp.myapp;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.TextView;
import java.util.Random;
import android.content.res.Configuration;
import android.support.v7.widget.Toolbar;
import android.widget.Toast;
public class QuoteOfTheDat extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quote_of_the_dat);
mDrawerLayout = (DrawerLayout) findViewById(R.id.activity_quote_of_the_dat);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
if (mToggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
First, you need to set onClickListener on your Navigation View like the following :-
yourNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch(menuItem.getItemId()) {
// Here in each case statement you need to pass the id of the menu items and then call startActivity(new Intent()) in each case statement.
case R.id.your_item:
startActivity(new Intent(currentClass.this, nextActivity.class));
break;
}

why my menu icon not showing in toolbar, just show blank toolbar

i try to add icon menu in toolbar. but my icon not showing. here my code. only show blank header. can someone help me...
this fragment class
package com.example.bams.task.fragments;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.example.bams.task.R;
/**
* Created by jack on 19/02/2017.
*/
public class AddProjectFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
Toolbar myToolbar = (Toolbar) getActivity().findViewById(R.id.my_toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(myToolbar);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_add_project, container,false);
return rootView;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//menu.clear();
inflater.inflate(R.menu.header, menu);
super.onCreateOptionsMenu(menu, inflater);
}
}
this is my custom toolbar layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/colorBgFragment"
android:fitsSystemWindows="true"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorBgFragment">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
android:ems="10"
android:id="#+id/editText"/>
</LinearLayout>
</LinearLayout>
and my menu icon
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/backBtn"
android:icon="#drawable/ic_back_filled"
app:showAsAction="always"
android:title="back"/>
</menu>
can anyone tell me whats wrong with my code?
Try this:
public class AddProjectFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_add_project, container,false);
Toolbar myToolbar = (Toolbar) rootView.findViewById(R.id.my_toolbar);
((AppCompatActivity)getActivity()).setSupportActionBar(myToolbar);
return rootView;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.header, menu);
super.onCreateOptionsMenu(menu, inflater);
}
}
Actually to include a action bar in fragment.Remove the on menu function and put relative layout inside your toolbar in layout.Inside this relative layout adda imageview with he appropriate position that you want.You are getting a blank space because of the empty toolbar area.Adding view inside it will show the image you want to show.

Title missing with ViewPager

I want something like the swiping pages in the Google Play Store.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.nielyouri.pluff.MainActivity">
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_height="0px"
android:layout_weight="1"
android:layout_width="match_parent">
<android.support.v4.view.PagerTitleStrip
android:id="#+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:textColor="#fff"
android:paddingTop="4dp"
android:paddingBottom="4dp" />
</android.support.v4.view.ViewPager>
</LinearLayout>
MainActivity.java
package com.nielyouri.pluff;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class MainActivity extends FragmentActivity {
//private static final String TAG = MainActivity.class.getSimpleName();
private DayAdapter mDayAdapter;
private ViewPager mViewPager = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDayAdapter = new DayAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mDayAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.about:
Intent intent = new Intent(this, AboutActivity.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
But when i run the app simulated it shows up without the "Pluff" title...
My fragment and adapter
http://pastebin.com/2utCxSZx
http://pastebin.com/eexzKpb6
Tried multiple things found on the internet, changing xml etc doesn't do the trick.
In xml you have to put below Toolbar code for the action bar:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
In java file:
a. extend class with AppCompatActivity
b. you have to put the below code for set the title:
Toolbar mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar);
mActionBarToolbar.setTitle("Pluff");
setSupportActionBar(mActionBarToolbar);
In manifest file you have to set theme for the activity, see below:
<activity
android:name=".MainActivityPage"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light.NoActionBar"
>
</activity>

Categories

Resources