I'm trying to build a drop down menu in the Action Bar Compat, as per the dev example: http://developer.android.com/guide/topics/ui/actionbar.html#Dropdown
I can't get onNavigationItemSelected to fire though.
The spinner dropdown item in the action bar is being generated, to the right of the actionbar's Title. When I press it I do see the triangle icon turn blue. The OnNavigationListener and Sting[]strings are being ran in OnCreate.
How would I get this to work?
public class Main extends ActionBarActivity {
private ActionBar actionBar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
spinner = new Spinner(this);
SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this,
R.array.operating_systems, android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(mSpinnerAdapter);
ActionBar.OnNavigationListener mOnNavigationListener = new ActionBar.OnNavigationListener() {
String[] strings = getResources().getStringArray(R.array.operating_systems);
#Override
public boolean onNavigationItemSelected(int position, long itemId) {
ListContentFragment newFragment = new ListContentFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container, newFragment, strings[position]);
ft.commit();
return true;
}
};
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_share:
Toast.makeText(this, "Refresh selected", Toast.LENGTH_SHORT)
.show();
break;
case R.id.action_settings:
Toast.makeText(this, "Settings selected", Toast.LENGTH_SHORT)
.show();
break;
default:
break;
}
return true;
}
}
Doesn't look like you're calling ActionBar.setListNavigationCallbacks.
From step 4 in the example:
Set the callback for the drop-down list with setListNavigationCallbacks(). For example:
actionBar.setListNavigationCallbacks(mSpinnerAdapter, mNavigationCallback);
Related
Basically my project have to have different toolbars, one for each fragment switched by the bottom navigation bar.
The fragment toolbars items are clickable, when I long press, shows the title item etc, but they don't perform the action. For example, When I set to show a toast when click on that item, he'll not perform. Any Suggestion? Thanks.
public class FeedFragment extends Fragment {
public static final String TAG = "Tag Free";
DialogFragment mDialog;
FloatingActionButton fab;
RevealFrameLayout reveal;
FrameLayout frame;
private ConstraintLayout layoutDialog;
boolean isOpen = false;
public FeedFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_feed, container, false);
/** Toolbar **/
Toolbar myToolbar = (Toolbar) view.findViewById(R.id.my_toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(myToolbar);
ActionBar ab = ((AppCompatActivity) getActivity()).getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(false);
// navigation bottom
fab = getActivity().findViewById(R.id.fab_button);
fab.setImageDrawable(getResources().getDrawable(R.drawable.ic_pencil_black_24dp));
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentTransaction ft = getFragmentManager().beginTransaction().setCustomAnimations(R.anim.hold, R.anim.slide_down);
Fragment prev = getFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
DialogFragment dialogFragment = new PostDialog();
dialogFragment.show(ft, "dialog");
dialogFragment.setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogFragmentTheme);
}
});
return view;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.appbar_feed, menu);super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_personProfile:
Toast.makeText(getActivity(),"not responding",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getActivity(), MyUserProfileActivity.class);
startActivity(intent);
return true;
case R.id.action_settings:
Toast.makeText(getActivity(),"not responding",Toast.LENGTH_SHORT).show();
return true;
default:
// If we got here, the user's action was not recognized.
// Invoke the superclass to handle it.
return super.onOptionsItemSelected(item);
}
}
}
You are not chaining to the superclass in the activity methods.
Refactor your code as below :
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.appbar_feed, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_personProfile:
Toast.makeText(getActivity(),"not responding",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getActivity(), MyUserProfileActivity.class);
startActivity(intent);
break;
case R.id.action_settings:
Toast.makeText(getActivity(),"not responding",Toast.LENGTH_SHORT).show();
break;
default:
// If we got here, the user's action was not recognized.
// Invoke the superclass to handle it.
break;
}
return super.onOptionsItemSelected(item);
}
Hope now your code will work fine.
In my app I have created few buttons in one fragment which will redirect to several activity based on the click. Now if user click button1, they will get a floating context menu conataing the list of the comapny, such as company1, compan2...etc. I have followed this post Opening a floating menu (context menu) in Android? to develop this feature in my app. But the problem is that this code is implemented in Activity, where in my case I want ti implement it in Fragment. I wrote the code but nothig happen with the button click. Now how can I generate this menu on button click
My contextmenu is
<?xml version="1.0" encoding="utf-8"?>
<item android:id="#+id/company_1"
android:title="#string/company_1"></item>
<item android:id="#+id/company_2"
android:title="#string/company_2"></item>
<item android:id="#+id/company_3"
android:title="#string/company_3"></item>
<item android:id="#+id/company_4"
android:title="#string/company_4">
</item>
<item android:id="#+id/company_5"
android:title="#string/company_5"></item>
<item android:id="#+id/company_6"
android:title="#string/company_6">
</item>
My Fragment Class is
public class MainFragment extends Fragment implements View.OnClickListener{
public MainFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
Button button1=(Button) view.findViewById(R.id.button1);
button1.setOnClickListener(this);
registerForContextMenu(button);
Button button2=(Button) view.findViewById(R.id.button2);
button2.setOnClickListener(this);
Button button3=(Button) view.findViewById(R.id.button3);
button3.setOnClickListener(this);
// Inflate the layout for this fragment
return view;
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
if(v.getId()==R.id.button1){
this.getActivity().getMenuInflater().inflate(R.menu.contextmenu_company,menu);
}
super.onCreateContextMenu(menu, v, menuInfo);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
setHasOptionsMenu(true);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.company_1:
Toast.makeText(getActivity().getApplicationContext(),"caompany1code",Toast.LENGTH_LONG).show();
return true;
case R.id.company_2:
......
case R.id.company_3:
.....
case R.id.company_4:
....
return true;
case R.id.company_5:
.....
return true;
case R.id.company_6:
.....
return true;
}
return super.onContextItemSelected(item);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button1:
Toast.makeText(getActivity(),"My colleagues clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.button2:
Toast.makeText(getActivity(), "News clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.button3:
Toast.makeText(getActivity(), "Navigator clicked", Toast.LENGTH_SHORT).show();
break;
}
}
In MainActivity Class
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
NavigationView navigationView = null;
Toolbar toolbar = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Set the fragment initially
MainFragment fragment = new MainFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout 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) 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();
}
}
#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);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
.....
}
Well i found something here :
Try this in your fragment :
#Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
if(v.getId()==R.id.button1{getActivity().getMenuInflater().inflate(R.menu.contextmenu_company,menu);
}
}
You are also calling this wrong:
//Instead of button use button1.
registerForContextMenu(button1);
Maybe this will help.
You did use
onCreateOptionsMenu(Menu menu)
in your Activity but you are trying to use
onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo)
in your fragment.
Use onCreateOptionsMenu(Menu menu) in your fragment as well and set setHasOptionsMenu(true) in your onCreateView method.
I have a < button on actionBar beside the app title and a + button.
I create the < button by using code below
final ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.mipmap.back_to);
and then
#Override
public boolean onCreateOptionsMenu(Menu menu) { // for + button
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.create_new_details, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.home: // back to previous activity, < button
onBackPressed();
break;
case R.id.addDetails:
mClickedPosition = -1;
View menuItemView = findViewById(R.id.addDetails);
PopupMenu po = new PopupMenu(this, menuItemView); //for drop-down menu
po.getMenuInflater().inflate(R.menu.popup_details, po.getMenu());
po.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(getApplication(), "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
if ("Add Work Details".equals(item.getTitle())) {
Intent intent = new Intent(getApplication(), Add_Details_Information.class); // go to Details class
startActivityForResult(intent, PROJECT_REQUEST_CODE);
} else {
}
return true;
}
});
po.show(); //showing popup menu
}
return super.onOptionsItemSelected(item);
}
But only the + button works. How do I back to previous activity when < button is clicked?
Just change R.id.home to android.R.id.home.
I created fragment and arrange its visibilty based onclick event of menu item. When i click first, it works properly but click again fragment visibility not hide. Explanation of image; When i click settings gray fragment is showing, when click again i want to hide gray fragment but it is not doing this stuff.
Here is my code;
#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);
MenuItem item = menu.findItem(R.id.action_settings);
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
if (findViewById(R.id.frameLayout) != null)
{
FragmentTest fragmenttest = new FragmentTest();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
if (fragmenttest.isVisible()) {
ft.hide(fragmenttest);
} else {
if (fragmenttest.isAdded()) {
} else {
ft.add(R.id.frameLayout, fragmenttest, "Hello Fragment");
}
ft.show(fragmenttest);
}
ft.commit();
}
return false;
}
});
return true;
}
move
FragmentTest fragmenttest = new FragmentTest();
to the top of activity.
How do I realize options and context menus in a "clean" and proper way, when an Activity switches between several fragments?
Here example code using a NavigationDrawer:
public class MainActivity extends Activity implements OnFragmentInteractionListener {
...
#Override
public void onNavigationDrawerItemSelected(int position) {
FragmentManager fragmentManager = getFragmentManager();
switch (position) {
case 0:
fragmentManager.beginTransaction().replace(R.id.container, Fragment1.newInstance(null, null)).commit();
break;
case 1:
fragmentManager.beginTransaction().replace(R.id.container, Fragment2.newInstance(null, null)).commit();
break;
}
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// ???
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
// ???
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
// ???
return super.onOptionsItemSelected(item);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
//???
super.onCreateContextMenu(menu, v, menuInfo);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
// ???
return super.onContextItemSelected(item);
}
#Override
public void onFragmentInteraction(String id) {
// ???
}
}
public class Fragment1 extends Fragment {
// ???
// option menu item A
// listView1 context menu item X
}
public class Fragment2 extends Fragment {
// ???
// option menu item B
// listView 2 context menu item Y
}
Maybe I am on the wrong track. I am a bit confused...
I understand you want to have different menus for each fragment? You can specify a menu for each fragment and it will only show when that fragment is attached to the Activity. The code below all belongs in a fragment.
You must tell the fragment it has an options menu:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
Then inflate the the menu for this fragment:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.about, menu);
}
Then handle each item in the menu when it is clicked:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle menu item click here
return super.onOptionsItemSelected(item);
}
You can also specify a "global" menu in the activity and those menu item will be shown as well as the menu items for the current fragment.