Android menu of toolbar response to different fragment - android

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 .

Related

Menu options does not respond when clicked

I had a menu in my app and it works correctly, now I add a TabLayout and the menu options does not respond to user click now no more.
Here is my activity_main.xml code:
<FrameLayout 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.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<include layout="#layout/toolbar"/>
<android.support.design.widget.TabLayout
android:id="#+id/tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill">
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewPaper"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
My Activity onCreate method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
preferences = PreferenceManager.getDefaultSharedPreferences(this);
itemAdapter = new ClientAdapter(this, null);
tab = (TabLayout)findViewById(R.id.tab);
paper = (ViewPager)findViewById(R.id.viewPaper);
paperAdapter = new ViewPaperAdapter(getSupportFragmentManager());
HomeFragment homeFragment = new HomeFragment();
DoneFragment doneFragment = new DoneFragment();
paperAdapter.addFrament(homeFragment, "Home");
paperAdapter.addFrament(doneFragment, "Done");
paper.setAdapter(paperAdapter);
tab.setupWithViewPager(paper);
}
onCreateOptionMenu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem menuItem = menu.findItem(R.id.action_search);
searchView = (SearchView)MenuItemCompat.getActionView(menuItem);
searchView.setOnQueryTextListener(this);
return super.onCreateOptionsMenu(menu);
}
And onOptionItemSelected method:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.refresh:
//Check the internet connection
if(Util.isConnected(this)){
// showProgress();
//Check if there is not pending item
sincronize(this);
dbManager.borrar();
new HomeFragment().getTarjetas();
}else{
Toast.makeText(this, "No hay conexion a internet.", Toast.LENGTH_LONG).show();
}
break;
}
return super.onOptionsItemSelected(item);
}
The menu.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="developer.technologies.agilisa.acardmobile.MainActivity">
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
android:orderInCategory="100"
android:title="#string/Search"
app:showAsAction="always|collapseActionView"
android:elevation="8dp"
app:actionViewClass="android.support.v7.widget.SearchView"/>
<item
android:id="#+id/refresh"
android:title="#string/refresh"
android:icon="#drawable/refresh"
android:orderInCategory="200"
app:showAsAction="always|collapseActionView"/>
<item
android:id="#+id/cerrar"
android:orderInCategory="10"
app:showAsAction="never"
android:title="#string/cerrar_session"/>
Can some one help me to solve that?
Try this changes:
Change Your Root layout To LinearLayout or Coordinator Layout-- As framelayout sometimes consume the click event of items under it.
1.in onCreateOptionsMenu()
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//Your code
return true;
}
2.And in onOptionsItemSelected() add return true
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.refresh:
// your code
return true; //add this
default:
return super.onOptionsItemSelected(item);
}
}

Android toolbar : Why three dots does not appear ?

Why I create toolbar I do not have three dots who will appear. How can I do ?
public class Main extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
}
And 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="woowin.woowin.Main">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#393939"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
/>
</RelativeLayout>
And can you explain me, how add options in this three dots after that ?
thank you
you forgot to add your menu in your activity use below code to add menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.your_menu, menu);
return true;
}
You need to add at least one menu item either via XML or in java code using add into your menu to see the menu options
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0,0,0,"title");
return true;
}

Action bar options are not showing in android studio

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

Get an item appearing twice in the menu, but only one defined in res/menu/ xml file

I set-up a list of items for the MainActivity. Today only one item this menu.
<?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="fr.vincelec.sunshine.sunshine.app.ForecastFragment">
<item android:id="#+id/action_refresh"
android:title="#string/refresh"
app:showAsAction="never"/>
</menu>
Also Here is the code to enable the menu:
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
inflater.inflate(R.menu.forecastfragment, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
if (id == R.id.action_refresh) {
FetchWeatherTask weatherTask = new FetchWeatherTask();
weatherTask.execute();
return true;
}
return super.onOptionsItemSelected(item);
}
But when I launch the program in my Android phone, and click on the menu, I see twice the item "Refresh", not only one item ...
I checked if there is something in MainActivity.java or main.xml but without success, It's only on my fragment file "ForecastFragment.java" (where the piece of code shown before is from) that I call the R.menu.forecastfragment .
Clear the menu items before you inflate it.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
menu.clear();
inflater.inflate(R.menu.forecastfragment, menu);
}

Fragment's Menu issues

Hi there, everything i click on my fragment. It adds a menu button to action bar . So if i click it 3 times. It adds the menu button 3 times..
Does anyone know where went wrong???
This is my code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent i = null;
switch (item.getItemId()) {
case R.id.refresher:
i = new Intent(this.context, CameraInfoActivity.class);
startActivity(i);
return true;
case R.id.settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu, inflater);
inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.menu_indoor, menu);
}
and i added
setHasOptionsMenu(true);
at my OnCreateView .
this is my menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/refresher"
android:menuCategory="system"
android:orderInCategory="100"
android:icon="#drawable/ic_action_refresh"
android:title="#string/menu_add"
android:showAsAction="always" />
<item android:id="#+id/settings"
android:menuCategory="system"
android:orderInCategory="100"
android:icon="#drawable/ic_action_settings"
android:title="#string/menu_settings"
android:showAsAction="never" />
try to remove this code:
inflater = getActivity().getMenuInflater();
you're already have inflater
try to call
setHasOptionsMenu(true);
in fragment's constructor, not in onCreateView

Categories

Resources