Reputation: 3131
I have been searching for this but was not able to implement it. My problem is i have two menu in the toolbar action_settings
and repeat_entry
,when i click action_settings fragment category settings should show and when i click repeat_entry
Repeat entry Fragment should show
Here 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=".MainActivity">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
<item android:id="@+id/repeat_entry" android:title="Repeat Entry"
android:orderInCategory="100" app:showAsAction="never" />
<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
</menu>
heres my onCreateOptionsMenu and onOptionsItemSelected MainActivity.java
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
menu.removeItem(R.id.action_search);
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
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();
Fragment fr = null;
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
selectTabToShow(String.valueOf(R.id.action_search));
CategorySettings fragemnt = (CategorySettings) getFragmentManager().findFragmentById(R.id.categorySettings);
fr = new CategorySettings();
}else if(id == R.id.repeat_entry){
Toast.makeText(MainActivity.this,"ssdf sdf",Toast.LENGTH_SHORT).show();
fr = new RepeatEntry();
}
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.categorySettings,fr);
fragmentTransaction.commit();
return super.onOptionsItemSelected(item);
}
Heres a part of my activity_main.xml
<LinearLayout
android:id="@+id/category_cont"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:name="me2.nakame.hp.moneytracer.CategorySettings"
android:id="@+id/categorySettings"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
this is my fragment RepeatEntry and CategorySettings is almost alike
public class RepeatEntry extends Fragment{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
View v = inflater.inflate(R.layout.repeat_entry,container,false);
return v;
}
}
my problem is only CategorySettings is showing
Upvotes: 1
Views: 3702
Reputation: 3131
I already solved my problem by just removing the <fragment>
and use .replace method for fragment transaction
This is the updated part of the activity_main.xml where i remove the fragment
<LinearLayout
android:id="@+id/category_cont"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
This is now the update method of onOptionsItemSelected
under my main activity
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();
Fragment fragment = null;
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
fragment = new CategorySettings();
}else if(id == R.id.repeat_entry){
fragment = new RepeatEntry();
}
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.category_cont,fragment);
ft.setCustomAnimations(android.R.animator.fade_in,android.R.animator.fade_out);
ft.commit();
return super.onOptionsItemSelected(item);
}
Upvotes: 1