I am trying to add toolbar to my app but somehow it is not working. Neither title not action icons are appearing. Even " : " menu is also not appearing. Here are my codes:
Style.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
Menu_main.xml:
<menu 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"
tools:context="com.example.vaibhav.thirdeye.MainActivity">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
<item android:id="#+id/next"
android:title="#string/next"
android:orderInCategory="200"
android:icon="#drawable/next_arrow"
app:showAsAction="always"/>
</menu>
content_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#drawable/back_splash"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.vaibhav.thirdeye.MainActivity"
tools:showIn="#layout/activity_main"
app:theme="#style/AppTheme.NoActionBar">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
MainActivity.java:
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.vaibhav.thirdeye.MESSAGE";
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Adding Custom toolbar
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.next_arrow);
toolbar.setTitle("ThirdEye");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.action_settings:
Toast settings = Toast.makeText(this, "Voila!", Toast.LENGTH_SHORT);
settings.show();
case R.id.next:
Toast next = Toast.makeText(this, "Voila!", Toast.LENGTH_SHORT);
next.show();
}
return true;
}
}
app_bar.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/icon_size"
android:background="#color/colorPrimary">
</android.support.v7.widget.Toolbar>
Output Screenshot:
Any helps????
Remove extra toolbars in your MainActivity's activity_main.xml layout, it will solve your problems.
Related
I am really stuck with this.
I want to use a CAB in a fragment, so I'm using
actionMode = ((MainActivity)getActivity()).startActionMode(actionModeCallbacks);
to call startActionMode
I've been changing themes and adding and testing several lines of code (together and separated) to xml styles like:
<item name="android:windowActionModeOverlay">true</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowActionBar">false</item>
<item name="windowActionBar">false</item>
<item name="windowActionModeOverlay">true</item>
<item name="windowActionBarOverlay">true</item>
Relevant information:
I have my toolbar defined inside and <include/>in my main activity.
Toolbar is included just before the frame of MainActivity, between that frame and the DrawerLayout, so I can use the toolbar in the fragments.
I have tested the CAB inside Main Activity, but it also gets above
the Toolbar.
I also tried a dirty workoaround toggling Toolbar visibility when CAB
is created/destroyed, but it also doesn't work!
I'm using android.view.ActionMode instead of
android.support.v7.view.ActionMode because I can't call
startActionMode if I use Support Library (?).
My toolbar (*android.support.v7.widget.Toolbar):
toolbar = findViewById(R.id.toolbar);
mDrawerLayout = findViewById(R.id.drawer_layout);
toolbar.setTitle(R.string.addWordLabel_Act);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.ic_menu_white_24dp);
XML (toolbar)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#FAFAFA"
android:theme="#style/AppTheme.NoActionBar"
app:collapseIcon="#drawable/ic_arrow_back_white_24dp"
app:contentInsetStartWithNavigation="0dp"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:title="#string/addWordLabel_Act"
app:titleTextColor="#color/colorAccent"
/>
</android.support.constraint.ConstraintLayout>
ActionMode.Callback
private ActionMode actionMode;
private ActionMode.Callback actionModeCallbacks = new ActionMode.Callback()
{
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.action_mode_menu, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.share_actionMode:
shareWord();
mode.finish();
return true;
case R.id.deleteWords_actionMode:
deleteWords();
mode.finish();
return true;
default:
return false;
}
}
#Override
public void onDestroyActionMode(ActionMode mode) {
actionMode = null;
}
};
XML (CAB)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Share"
android:icon="#drawable/ic_share_white_24dp"
android:id="#+id/share_actionMode"/>
<item android:id="#+id/deleteWords_actionMode"
android:title="Delete"
android:icon="#drawable/ic_round_delete_outline_white_24px"/>
</menu>
And styles...
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/lightAccent</item>
<item name="colorSecondary">#color/colorSecondary</item>
<item name="android:fontFamily">#font/sans_serif_roboto</item>
<item name="alertDialogTheme">#style/AlertDialogStyle</item>
<item name="android:windowActionModeOverlay">true</item>
</style>
<style name="AppTheme.NoActionBar"
parent="Theme.MaterialComponents.NoActionBar">
</style>
I don't know what else I can do.
Solved
I completely forgot to check Manifest, and that was the problem!
The AppTheme was set to "Theme.AppCompat.Light.NoActionBar" instead of "AppTheme". Thus, any change I made in AppTheme didn't take effect on the App.
I completely forgot to check Manifest, and that was the problem! The AppTheme was set to "Theme.AppCompat.Light.NoActionBar" instead of "AppTheme". Thus, any change I made in AppTheme didn't take effect on the App.
When I add a header to the navigation drawer I have this error
android.view.InflateException: Binary XML file line #0: Error
inflating class android.support.design.widget.NavigationView but
without header, it works properly.
first file is MainActivity it contains four fragments and bottom navigation
second is the acivity_main.xml layout
I think error may be in styles.xml file thats why I have put it also.
public class MainActivity extends AppCompatActivity {
private BottomNavigationView.OnNavigationItemSelectedListener
mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.home:
setTitle("Home");
HomeFragment fragmentTasks = new HomeFragment();
FragmentTransaction fragmentTransactionTasks = getSupportFragmentManager().beginTransaction();
fragmentTransactionTasks.replace(R.id.frame_layout, fragmentTasks);
fragmentTransactionTasks.commit();
return true;
case R.id.notification:
setTitle("Notification");
NoteFragment fragmentQA = new NoteFragment();
FragmentTransaction fragmentTransactionQa = getSupportFragmentManager().beginTransaction();
fragmentTransactionQa.replace(R.id.frame_layout, fragmentQA);
fragmentTransactionQa.commit();
return true;
case R.id.profile:
setTitle("Profile");
ProFragment fragmentCashOut = new ProFragment();
FragmentTransaction fragmentTransactionCashout = getSupportFragmentManager().beginTransaction();
fragmentTransactionCashout.replace(R.id.frame_layout, fragmentCashOut);
fragmentTransactionCashout.commit();
return true;
case R.id.help:
setTitle("Help");
HelpFragment fragmentDiscuss = new HelpFragment();
FragmentTransaction fragmentTransactionDiscuss = getSupportFragmentManager().beginTransaction();
fragmentTransactionDiscuss.replace(R.id.frame_layout, fragmentDiscuss);
fragmentTransactionDiscuss.commit();
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navigation =findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
setTitle("Home");
HomeFragment fragmentTasks = new HomeFragment();
FragmentTransaction fragmentTransactionTasks = getSupportFragmentManager().beginTransaction();
fragmentTransactionTasks.replace(R.id.frame_layout, fragmentTasks);
fragmentTransactionTasks.commit();
}
}
//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: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.design.widget.CoordinatorLayout
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hp.votingsystemv1.Activities.MainActivity">
<FrameLayout
android:id="#+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/colorPrimary"
app:menu="#menu/bottombar_menu"
app:itemTextColor="#color/colorIcons"
app:itemIconTint="#color/colorIcons">
</android.support.design.widget.BottomNavigationView>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
app:itemTextColor="#color/colorPrimaryText"
app:menu="#menu/drawer_menu"
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"
android:fitsSystemWindows="true" />
</android.support.v4.widget.DrawerLayout>
//nav_header.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="170dp"
android:background="#color/colorPrimary"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/iv_display_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:src="#drawable/no_image"
app:civ_border_color="#color/colorGrey"
app:civ_border_width="1dp"/>
<TextView
android:id="#+id/tv_display_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:textStyle="bold"
android:textSize="20sp"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
tools:text="Yousra Mahdi"/>
</LinearLayout>
//style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
<style name="AppTheme1" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="Base.ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="AppTheme.ErrorTextAppearance" parent="TextAppearance.AppCompat">
<item name="android:textAppearance">#style/TextAppearance.AppCompat.Caption</item>
<item name="android:textColor">#color/colorPrimary</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
<!-- Fullscreen application theme. -->
<style name="FullscreenTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
<style name="Theme.MyDialog" parent="#style/Theme.AppCompat.Light.Dialog">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.Dark.Dialog" parent="Theme.AppCompat.Dialog">
<item name="colorAccent">#color/colorIcons</item>
<item name="android:textColorPrimary">#color/colorAccent</item>
<item name="android:background">#color/colorGrey</item>
</style>
</resources>
I think you should try commenting CircleImageView and textview and test if it works. Some child view in nav_header might be having an issue getting inflated.
I have tried your code on my IDE and it works absolutely fine
I created NavigationDrawer class and now I`m creating new Activities which extends Navigation Drawer class. Unfortunately I am constantly getting such errors:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
here is the code for the NavigationDrawer Class:
public class Requests extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
public DrawerLayout drawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.requests);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
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.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
Intent in = getIntent();
if (in.hasExtra("warehouse"))
{
id = R.id.nav_warehouse;
}
else if (in.hasExtra("cash"))
{
id = R.id.nav_cash;
}
if (id == R.id.nav_warehouse) {
Intent i = new Intent(getApplicationContext(), WarehouseRequests.class);
startActivity(i);
} else if (id == R.id.nav_cash) {
Intent i = new Intent(getApplicationContext(), CashRequests.class);
startActivity(i);
} else if (id == R.id.nav_log_out) {
Intent i = new Intent(getApplicationContext(), LoginActivity.class);
i.putExtra("logout", 1);
startActivity(i);
}
return true;
}
}
Here is the XML files for NavigationDrawer:
requests.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
android:background="#dff3f7"
>
<include
layout="#layout/app_bar_requests"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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_requests"
app:menu="#menu/warehouse_requests_drawer" />
</android.support.v4.widget.DrawerLayout>
app_bar_request.xml:
<?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.example.sanzharaubakir.exgroup.Requests">
<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"
android:theme="#style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_requests" />
</android.support.design.widget.CoordinatorLayout>
and content_requests.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="#+id/container"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.sanzharaubakir.exgroup.Requests"
tools:showIn="#layout/app_bar_requests">
</RelativeLayout>
Here is the code for my Activity:
public class WarehouseRequests extends Requests {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.warehouse, null, false);
drawer.addView(contentView, 0);
}
}
and here is what I have in styles:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
</resources>
add this to your styles.xml file
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
and apply this style to your Activity in the manifest.xml file.
android:theme="#style/AppTheme.NoActionBar"
android:theme="#style/AppTheme.NoActionBar"
add this to your warehouserequests activity in manifest file
I'm learning Material Design from video tutorial provided by slidenerd.In that they put one next icon on toolbar,everything is working in that tutorial but when i'm implemented it the icon is not appearing.
Here,i'm attaching my files please have a look and help me out at my learning stage.Please!!!
MainActivity.java
package materialtest.example.ritzipsy.mytoolbar5;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
ActionBar ab = getSupportActionBar();
// Enable the Up button
ab.setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.id_settings) {
Toast.makeText(this, "You have Clicked Setting menu", Toast.LENGTH_SHORT).show();
}
if (id == R.id.id_navigate) {
Toast.makeText(this, "You have Clicked Next menu", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
}
activity.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="materialtest.example.ritzipsy.mytoolbar5.MainActivity">
<include android:id="#+id/app_bar"
layout="#layout/app_bar"></include>
</RelativeLayout>
my_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools">
<item
android:id="#+id/id_settings"
android:title="Settings"
app:showAsAction="never">></item>
<item
android:id="#+id/id_navigate"
android:icon="#drawable/ic_next_arrow"
android:orderInCategory="200"
android:title="Next"
app:showAsAction="always"></item>
</menu>
app_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.co0m/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary">
</android.support.v7.widget.Toolbar>
style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
Got Output as
Expected output to be
Just change your menu xml as below. The problem is with this line
xmlns:app="http://schemas.android.com/apk/tools"
<?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/id_settings"
android:title="Settings"
app:showAsAction="never"></item>
<item
android:id="#+id/id_navigate"
android:icon="#drawable/ic_next_arrow"
android:orderInCategory="200"
android:title="Next"
app:showAsAction="always"></item>
</menu>
In my project i would to set a custom actionBar (with a custom layout) after a specific action.
I have this simple Activity:
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity2 extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_activity2, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
//apriImpostazioni ();
return true;
case R.id.information_item:
//apriInformazioni ();
return true;
case R.id.search_item:
apriBarraRicerca ();
System.out.println ("IL BOTTONE RICERCA E' PREMUTO");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void apriBarraRicerca () {
getActionBar ().setCustomView(getLayoutInflater().inflate(R.layout.search_layout, null));
}
}
Where "menu_main_activity2" is this xml:
<menu 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"
tools:context="zipangulu.myapplication.MainActivity2">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
<item android:id="#+id/information_item"
android:title="Info"
android:icon="#drawable/info"
android:orderInCategory="100"
app:showAsAction="always"/>
<item android:id="#+id/search_item"
android:title="Search"
android:icon="#drawable/search_icon"
android:orderInCategory="100"
app:showAsAction="always"/>
</menu>
I would, pressing the search_item on the main actionbar, set a custom actionBar whith this layout:
<?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="wrap_content"
android:background="#4584d3">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/back_button"
android:background="#null"
android:src="#drawable/back_icon"/>
<AutoCompleteTextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="6"
android:id="#+id/campo_ricerca"/>
<Spinner
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:spinnerMode="dropdown"
android:id="#+id/spinner_anno"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/search_icon"
android:id="#+id/avvia_ricerca"/>
</LinearLayout>
But at runtime I have a NullPointerException in the body of "apriBarraRicerca" method..why?How can I fix this problem?
EDIT: as suggested I replaced "getActionBar ()" with "getSupportActionBar ()", now I don't have any exception but nothing happens.
EDIT2: I added getSupportActionBar().setDisplayShowCustomEnabled(true); and now my custom actionbar is visible but not as i want, look at the following image:
http://bit.ly/1Dc2kGg
The bar is visible but cutted, and are visible also the items of the previous actionBar..
You can try do the same thing with the android.support.v7.widget.ToolBar
Your Layout which you can populate as you desire:
<?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:background="#688A08"
android:layout_height="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>
You have to modify the values->styles.xml:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
</resources>
And the in the onCreate() method you do the following:
Toolbar tb = (Toolbar)findViewById(R.id.top_bar);
if(tb != null){
setSupportActionBar(tb);
}
Hope it helps!!!