i want to add items in toolbar but any item that adding, not showing in my app.
please help me to solve this
my app screenshot:
enter image description here
menu_map.xml is here:
<?xml version="1.0"?>
<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.map.myzing.mapzing.MapActivity">
<item android:id="#+id/add_alarm"
android:title="#string/action_addAlarm"
android:orderInCategory="100"
app:showAsAction="never"/>
</menu>
a part of MapActivity:
public class MapActivity extends AppCompatActivity
implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
if (servicesOK()) {
setContentView(R.layout.activity_map);
if (initMap()) {
mLocationClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mLocationClient.connect();
} else {
Toast.makeText(this, "can not be connected!!", Toast.LENGTH_SHORT).show();
}
} else {
setContentView(R.layout.activity_main);
}
}
#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_map, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
return true;
}
}
in other activity this toolbar is correct but this activity is not correct and show toolbar without any items or title!!!
please help me
Not sure what you are trying to do but you should get the toolbar view id after setContentView not before it.
Like this:
setContentView(R.layout.my_layout);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
Related
I have a MainActivity.java and NavigationDrawer. I want to when I click on items in navdrawer, start new layout. I know I can start new Activity, but I don't want a new activity. new layout is suitable for me. is this possible???
this is my ActivityMain.java:
public class MainActivity extends AppCompatActivity {
public Toolbar toolbar;
protected DrawerLayout drawerLayout;
NavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
toolbar = (Toolbar) findViewById(R.id.toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.a1);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_more_vert_black_24dp);
navigationView = (NavigationView) findViewById(R.id.a2);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
item.setChecked(true);
drawerLayout.closeDrawer(Gravity.RIGHT);
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.khat) {
if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) {
drawerLayout.closeDrawer(Gravity.RIGHT);
}
else {
drawerLayout.openDrawer(Gravity.RIGHT);
}
return super.onOptionsItemSelected(item);
}
else {
Toast.makeText(getApplicationContext(),"SOMETHING",Toast.LENGTH_LONG).show();
}
return true;
}
public void onBackPressed(){
if (drawerLayout.isDrawerOpen(Gravity.RIGHT)){
drawerLayout.closeDrawer(Gravity.RIGHT);
}else {super.onBackPressed();}
}
}
and this is my second layout that I want start when I click on items:`
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="#132740"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
android:textColor="#FFFFFF"
android:id="#+id/textView"
android:textSize="22sp"
android:layout_marginTop="20dp"
android:text="blah blah"/>
</RelativeLayout>
is this possible?
If is just the layout you wanna to change, there is a very simple soluction:
if (Case_A)
setContentView(R.layout.layout1);
else if (Case_B)
setContentView(R.layout.layout2);
But considering using Fragments:
https://developer.android.com/guide/components/fragments.html
With fragments, you will be able to divide your code in multiples class, for each layout. It will be more readable and light:
Here is an example
Use fragment in order to user multiple layout with different operation. If u just want to change the layout then u can set new layout on item click with setContentView(new layout); .
I want to set toolbar on my activity which extends FragmentActivity. I know that for use setSuppoertActionBar(toolbar) method we extends AppCompatActivity instead of FragmentActivity but I override the onMenuItemSelected(int featureId, MenuItem item) method which is final in AppCompatActivity and final method cannot override. so I'm restricted to extends FragmentActivity.
Here is my code:
public class MainActivity extends FragmentActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar); -> error is here
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()){
case R.id.action_search:
onSearchRequested();
break;
}
return super.onMenuItemSelected(featureId, item);
}
I saw many answers related to that question but everyone says extends AppCompatActivity instead of FragmentActivity but I want to set toolbar as well as override onMenuItemSelected(int featureId, MenuItem item) method.
what should I do, please help.
This thing is good when you are using NavigationDrawer
use this:-
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
then set Toolbar Title according to different fragment with different Titles in onNavigationItemSelected :-
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
sfm = getSupportFragmentManager();
Fragment fragment = new Fragment();
int id = item.getItemId();
if (id == R.id.nav_id) {
fragment = new YourFragment();
toolbar.setTitle("SET TOOLBAR NAME");
}else if (id == R.id.nav_id2) {
fragment = new YourFragment();
toolbar.setTitle("SET TOOLBAR NAME");
}
For single fragment, first customize your style.xml like this :-
<style name="YourStyleName" parent="Theme.AppCompat.Light.DarkActionBar">
// ToDo as you want to do or as per your requirement
</style>
then apply into your custom toolbar:-
<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/YourStyleName" >
// ...........
</android.support.v7.widget.Toolbar>
You can also create your own toolbar:
First set up the main theme to extend Theme.AppCompat.Light.NoActionBar
<style name="style_1" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
Remember to apply the theme:
#Override
protected void onCreate(Bundle savedInstanceState) {
this.setTheme(R.style.style_1);
// ...
}
then in your Activity's xml you can set your own custom toolbar:
<include layout="#layout/my_toolbar"/>
where #layout/my_toolbar may look like this:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="#dimen/toolbar_height"
app:contentInsetStart="0dp"
app:layout_collapseParallaxMultiplier="1.0">
<!-- insert your views here -->
</android.support.v7.widget.Toolbar>
First set style as
<style name="style_1" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
then
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("Home");
You should extend your Activity from AppCompatActivity as this includes support for Fragments and the Toolbar.
And then override the
onCreateOptionsMenu() and onOptionsItemSelected() methods
You shouldn't be overriding
onMenuItemSelected()
public class MainActivity extends AppCompatActivity { ...
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FragmentTransaction ft = getSupportFragmentManager.beginTransaction();
....
....
#Override
public boolean onCreateOptionsMenu(Menu menu) {
...
...
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
...
}
In my application I want to set a custom menu for the toolbar. The icon that I defined in the menu_settings.xml is an arrow, but when I start the application, it's a three-point menu instead of the arrow (as seen in the Pictures). After hours of research I still can't figure out why it loads this icon instead of the arrow (yes I checked the drawable ;) ). Thank you for solutions and hints!
SettingsActivity:
public class SettingsActivity extends PreferenceActivity {
private AppCompatDelegate mDelegate;
#Override
protected void onCreate(Bundle savedInstanceState) {
getDelegate().installViewFactory();
getDelegate().onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
Intent intent = getIntent();
boolean hasConfiguredSettings = intent.getBooleanExtra(HAS_CONFIGURED_SETTINGS, false);
if (hasConfiguredSettings) {
toolbar.setTitle("Wähle deine Linien!");
} else {
// Back button in Toolbar
toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(R.id.content, new SettingsFragment())
.commit();
}
private void setSupportActionBar(Toolbar toolbar) {
getDelegate().setSupportActionBar(toolbar);
}
private AppCompatDelegate getDelegate() {
if (mDelegate == null) {
mDelegate = AppCompatDelegate.create(this, null);
}
return mDelegate;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
Intent intent = getIntent();
boolean hasConfiguredSettings = intent.getBooleanExtra("HAS_CONFIGURED_SETTINGS", false);
if (hasConfiguredSettings) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_settings, menu);
return true;
}
return false;
}
}
menu_settings.xml:
<?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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".SettingsActivity">
<item
android:id="#+id/action_checked"
android:orderInCategory="101"
android:title="#string/configuration_action_button"
app:showAsAction="always"
android:icon="#drawable/ic_action_arrow_right"/>
</menu>
toolbar.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.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="#color/primary"
/>
The toolbar is included with:
<include
android:id="#+id/tool_bar"
layout="#layout/toolbar" />
You need to use AppCompatDelegate#getMenuInflater instead of Activity#getMenuInflater().
Replace the below line:
MenuInflater inflater = getMenuInflater();
with
MenuInflater inflater = getDelegate().getMenuInflater();
In my activity I use the following code for my two toolbars.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Creating The Toolbar and setting it as the Toolbar for the activity
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("My title");
toolbar2 = (Toolbar) findViewById(R.id.tool_bar_bottom);
setSupportActionBar(toolbar2);
...
}
#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, menu);
return true;
}
I want it to use menu_main.xml for the top toolbar and menu_bottom for bottom toolbar but for both top and bottom toolbar it uses menu_main.xml.
Can somebody explain how to do it correctly?
As you are using two ToolBars set the menu like this
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("My title");
The above toolbar inflate menu from onCreateOptionsMenu, menu CallBack listener will be onOptionsItemSelected
Now Second ToolBar
toolbar2 = (Toolbar) findViewById(R.id.tool_bar_bottom);
toolbar2.inflateMenu(R.menu.bottom_menu);//changed
//toolbar2 menu items CallBack listener
toolbar2.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem arg0) {
if(arg0.getItemId() == R.id.item_id){
}
return false;
}
});
My onCreateOptionsMenu works only in my MainActivity and when I try to put another onCreateOptionsMenu in another activity to inflate a different menu it does not display my menu bar (note that I have it setup exactly the same in both activities).
I do not get any errors, it just does not display my menu bar on my secondary activity.
Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.expanded_view_layout);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
//noinspection SimplifiableIfStatement
int id = item.getItemId();
if (id == R.id.action_back) {
finish();
}
return super.onOptionsItemSelected(item);
}
Also note that I changed getMenuInflater() to super.getMenuInflater() and got same result.
In case you are using tool bar in your activity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
mToolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(mToolbar);
}
try adding method of super in your callback onCreateOptionsMenu implementation.
return super.onCreateOptionsMenu(menu);
The problem in my case is if extend Activity, then onCreateOptionsMenu is not triggered. Did it manually with in onCreate:
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.inflateMenu(R.menu.menu_main);
Of course, toolbar is defined in CoordinatorLayout
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.AppBarLayout>
don't forget to call parent function like this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
....
}
you should building a new xml menu (Second.xml).
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);//Second es your new xml.
return true;
}
I was having the same problem: - I found the solution that worked for me.
1>Check to what you extend class: - In my case, I have tried my activity with extending AppCompatActivity class and the menu is showing proper and when I changed it to activity class then it stopped displaying.
Not Working: -
public class GridViewActivity extends Activity
2>Try to extend your class with AppCompatActivity class instead of activity class.
Working: -
public class GridViewActivity extends AppCompatActivity
Call setHasOptionsMenu(true) from onCreate.
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}