Action bar options are not showing in android studio - android

enter image description hereI am new to android, right now I am referring the sunshine app by udacity.com tutorials. The code is the same, but still it is not showing the option settings and doesn't refresh on the action bar
How could I resolve my problem?
MainActivity. java code for main menu
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new ForecastFragment())
.commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
and ForecastFragment.java for refresh menu
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.forecastfragment, menu);
}
the main menu xml file code
<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.android.sunshine.app.MainActivity" >
<item android:id="#+id/action_settings"
android:orderInCategory="100" />
app:showAsAction="never"
android:title="#string/action_settings"
android:orderInCategory="100" /></menu>
the refresh menu is in forecastfragment.xml file code for refrsh menu is
<item android:id="#+id/action_refresh"
app:showAsAction="never"
android:title="#string/action_refresh"
android:orderInCategory="100" />
the manifest file is
<application android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
the style.xml file
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:icon">#drawable/ic_launcher</item>
</style>
<!--style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style-->
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

In your fragment's onCreateView method write
setHasOptionsMenu(true);
and add this super.onCreateOptionsMenu(menu, inflater); to your onCreateOptionsMenu
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.forecastfragment, menu);
}
you can add this below code in your activity_main.xml
<android.support.v7.widget.Toolbar
android:id="#+id/toolBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"></android.support.v7.widget.Toolbar>
and this in your MainActivity.java
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(mToolbar);
Your complete MainActivity should look like this
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(mToolbar);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
//setHasOptionsMenu(true);
return rootView;
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_main, menu);
}
}
}
and if you want to show your refresh menu then you need to put this code in your fragment setHasOptionsMenu(true);
and you activity_main.xml can be like this
<?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="#android:color/white"
android:orientation="vertical"
android:paddingBottom="8dp"
android:paddingLeft="20dp"
android:paddingRight="20dp" >
<android.support.v7.widget.Toolbar
android:id="#+id/toolBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"></android.support.v7.widget.Toolbar>
<FrameLayout
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.sunshine.app.MainActivity"
tools:ignore="MergeRootFrame" />
</LinearLayout>
it should work...

In your menu.xml you have to set app:showAsAction="always" and it will show.

Material design does'nt support ActionBar. So you cannot see ActionBar as well as App icon. If you want ActionBar like functionality then you can use toolbar like the following code does...
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);

Extend you baseClass from ActionBarActivity class.
This should take care of this issue.
public class MainActivity extends ActionBarActivity{
}

Related

Switch onCheckedChangedListner not registering clicks inside toolbar menu

I have a switch in my app toolbar to provide an option to the user to change the language.
When I am using the switch, the onCheckedChanged method is not working.
Further, it looks to me that onOptionsItemSelected is also not being hit as I don't see any log message on the screen.
Please note that the app is not crashing when I use the switch.
The clicks on options present inside the overflow menu are working properly. I am getting the log in those cases.
Switch aSwitch;
RelativeLayout langSwitch;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
context = this;
// Removed non relevant code here.
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
Log.d("tag_id",String.valueOf(id));
langSwitch = findViewById(R.id.app_bar_switch);
aSwitch = langSwitch.findViewById(R.id.langSwitch);
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (!b){
//Language1 code
Toast.makeText(context,"Language1 Selected",Toast.LENGTH_SHORT).show();
}else{
//Language2 code
Toast.makeText(context,"Language2 Selected",Toast.LENGTH_SHORT).show();
}
}
});
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.savedPost){
Intent intent = new Intent(this,SavedPost.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
Below is my 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.app_name.android.app_name.MainActivity">
<item
android:id="#+id/app_bar_switch"
android:orderInCategory="2"
android:title=""
app:actionLayout="#layout/switch_item"
app:showAsAction="always" />
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
<item
android:id="#+id/savedPost"
android:checkable="false"
android:orderInCategory="4"
android:title="#string/saved_post" />
</menu>
This is my layout file for switch item.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Switch
android:id="#+id/langSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textOff="#string/switch_text_off"
android:textOn="#string/switch_text_on"
android:focusable="true"
android:clickable="true"/>
</RelativeLayout>
Additional information.
If I create another menu item (code below) with showAsAction 'always' and click on this once, now when I use the switch the Toast message comes. I am clueless here as to why is it not happening for the first time. Also, how do I make it work without this menu item.
<item
android:id="#+id/english"
android:orderInCategory="3"
android:title="#string/switch_text_on"
app:showAsAction="always" />
To inflate an external layout over ActionBar that seems to first creating a view that holds your ChildView for your layout
like Example
RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
item.addView(child);
Try this in your code
Switch aSwitch;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
View sw = menu.findItem(R.id.app_bar_switch).getActionView();
aSwitch = (Switch) sw.findViewById(R.id.langSwitch);
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (!b){
//Language1 code
Toast.makeText(context,"Language1 Selected",Toast.LENGTH_SHORT).show();
}else{
//Language2 code
Toast.makeText(context,"Language2 Selected",Toast.LENGTH_SHORT).show();
}
}
});
return true;
}
Try
(compoundButton.isChecked())
In boolean
Or
just try another version of setonCheckedChangedListner
Example:
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){#Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if (!compoundButton.isSelected()) { Log.i("Yeah" , "Is Not Selected"); invertLock(-1); } else { if (Utilities.isLockEnabled(context)) { Log.i("Yeah" , "Is Locked"); Utilities.showLockEnabled(context); } else { Log.i("Yeah" , "Is Not Locked"); invertLock(1); } } }

how to set toolbar on FragmentActivity?

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) {
...
}

how to change the position of the menu item of the action bar to left

i have an activity that has an action bar and i want to change the position of the menu item from right (default) to left. All the information that i have found so far, doesn't show a soluction with in the menu_main.xml , i quote code from the activity and menu_main.xml . Thank you.
public class GadgetFlowWeb extends AppCompatActivity {
View mView;
GadgetItem mData;
Context ctx;
private ImageButton mBtnReload;
private ImageButton mBtnNext;
private ImageButton mBtnPrev;
private ImageButton mBtnOpenIn;
private WebView mWebview;
private boolean mIsLoadFinish = false;
private ActionBar mActionBar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Log.d("billy","inside GadgetFlowWeb");
savedInstanceState = getIntent().getExtras().getBundle("data");
//Getting the actionBar
mActionBar = getSupportActionBar();
mActionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#f26253"))); //changing color of the action bar
if (savedInstanceState != null)
mData = (GadgetItem) savedInstanceState.getSerializable("data");
setContentView(R.layout.activity_gadget_flow_web);
/*
* vf: initialize the widgets
*/
if (mData != null) {
//Log.e("nhatdo", "data webview " + mData.gadget_buyLink);
((WebView) findViewById(R.id.wvBuyItem)).loadUrl(mData.gadget_buyLink);
mActionBar.setTitle(mData.gadget_title);
}
...
#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);
menu.findItem(R.id.action_search).setVisible(false);
menu.findItem(R.id.action_share).setVisible(false);
menu.findItem(R.id.action_back).setVisible(true);
return true;
}
}
code from 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=".MainActivity">
<!-- search -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_search"
android:title="search"
app:showAsAction="always"
android:visible="false"/>
<item android:id="#+id/action_share"
android:icon="#drawable/ic_share"
android:title="share"
app:showAsAction="ifRoom"
android:visible="false"/>
<item android:id="#+id/action_back"
android:icon="#drawable/ic_back"
android:title = "back"
app:showAsAction="always"
android:visible="false"/>
I wish there was an attribute like android:layout-Gravity = "left" , something like this.

Android menu of toolbar response to different fragment

Now my mainActivity.xml file like this:
<?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="match_parent"
android:orientation="vertical">
<include layout="#layout/tool_bar" />
<FrameLayout android:id="#+id/tab_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<include layout="#layout/bottom_bar" />
</LinearLayout>
This page has four tabs on the bottom,each tab is associated with a different fragment which will be added to android:id="#+id/tab_content".
The menu_main.xml like this
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_calendar"
android:icon="#drawable/ic_title_date"
android:orderInCategory="100"
android:title="#string/calendar"
app:showAsAction="always" />
<item
android:id="#+id/action_notification"
android:icon="#drawable/ic_title_notification"
android:orderInCategory="200"
android:title="#string/notification"
app:showAsAction="always" />
</menu>
I have these code in my MainActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
return super.onOptionsItemSelected(item);
}
Fragment2 and Fragment3 have these code:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
int id = item.getItemId();
if (id == R.id.action_calendar)
{
showDatePicker();
return true;
}
return super.onOptionsItemSelected(item);
}
android:id="#+id/action_calendar" menuItem is a datePicker,and i want to use this menuItem to be filter to load data for fragment2 and fragment3.
Here is my question,when I navigate to the fragment3 and click the calendar menu and then change to fragment2,click the calendar,the system call onOptionsItemSelected method in fragment 3 ,not in fragment2 ?
I want calendar menu response to different fragment separately .

actionBar setCustomView not workin

I want to change dynamically the aspect of my actionbar. Applying the custom view layout i've created before, but still not working. This is my code:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar mActionBar = getSupportActionBar();
mActionBar.setCustomView(R.layout.activity_main);
mActionBar.setDisplayShowTitleEnabled(false);
mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
ActionBar mActionBar = getSupportActionBar();
Toast.makeText(this,item.toString(),Toast.LENGTH_LONG).show();
if(item.toString() == "search"){
mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
mActionBar.setCustomView(R.layout.search);
}
return super.onOptionsItemSelected(item);
}
Custom layout xml
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout
xmlns:android="schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText android:id="#+id/etSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
</LinearLayout>
consider trying item.toString().equals("search")

Categories

Resources