SlidingMenu : Showing seconday menu on an event - android

I am using the awesome library SlidingMenu by jfeinstein10. So, everything is is going just right but one thing.
I am able to open the primary menu using the method toggle() in an event handler. But I also want the secondary menu to open on some event like button click.
I did something like
SlidingMenu right = getSlidingmenu();
right.setSecondaryMenu(rightMenuView)
and was thinking of doing right.toggle();
but the second statement above throws a NullPointerException.
Edit : Posting onCreate
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
leftMenuView = inflater.inflate(R.layout.left_menu, null, false);
rightMenuView = inflater.inflate(R.layout.right_menu, null, false);
customActionBarView = inflater.inflate(R.layout.custom_actionbar,null);
findAllViews();
setFontAwesome();
ab = getSupportActionBar();
ab.setDisplayShowCustomEnabled(true);
ab.setDisplayHomeAsUpEnabled(false);
ab.setDisplayShowHomeEnabled(false);
ab.setDisplayUseLogoEnabled(false);
ab.setCustomView(R.layout.custom_actionbar);
ivHome = (ImageView) findViewById(R.id.ab_home);
ivHome.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("TAG", "Tag");
toggle();
}
});
leftSlidingMenu = getSlidingMenu();
leftSlidingMenu.setMode(SlidingMenu.LEFT_RIGHT);
setBehindContentView(leftMenuView);
leftSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
leftSlidingMenu.setBehindOffset(100);
leftSlidingMenu.setFadeDegree(0.35f);
rightSlidingMenu = getSlidingMenu();
rightSlidingMenu.setSecondaryMenu(rightMenuView); //NPE Here
rightSlidingMenu.toggle();
}
Any idea how to open secondary menu on an event. Thank You

To show the second menu you can use:
getSlidingMenu().showSecondaryMenu(true);
The boolean parameter is the animation flag.

In BaseActivity modify as following
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
toggle();
return true;
case R.id.github:
//Util.goToGitHub(this); //remove this line
showSecondaryMenu(); //add this line
return true;
}
return super.onOptionsItemSelected(item);
}

Related

Popup Menu won't change its selected state

I have this code here, I created a popup menu when the user long-presses the "edit_text" view's area which displays a popup menu with "Red" "Yellow" radio button option which changes the background color of the "text_view", but I'm not sure why when I select the other option, like when red is currently selected, I select yellow and the other way around(I have the red option selected as the default state), the selected state does not change at all, red is still selected no matter how many times I press yellow. Could you guys help me with this please? Thank you very much.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit_text = (EditText)findViewById(R.id.edit_text_1);
text_view = (TextView)findViewById(R.id.textView1);
//==========_CREATE A POPUP MENU WHEN LONG-CLICK ON EDITTEXT AREA_==========\\
edit_text.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
final PopupMenu pop_up = new PopupMenu(getContext(), v);
getMenuInflater().inflate(R.menu.main, pop_up.getMenu());
//GROUP'S ID IS "group".
pop_up.getMenu().setGroupCheckable(R.id.group, true, true);
pop_up.show();
pop_up.setOnMenuItemClickListener(listener);
return true;
}
});
}
OnMenuItemClickListener listener = new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch(item.getItemId()) {
case R.id.red:
text_view.setBackgroundColor(Color.RED);
if (!item.isChecked()) {
item.setChecked(true);
}
return true;
case R.id.yellow:
text_view.setBackgroundColor(Color.YELLOW);
if (!item.isChecked()) {
item.setChecked(true);
}
return true;
default:
return false;
}
}
};
protected Context getContext() {
return this;
}
Looks like there's some logical issue with your code. Based on the documentation:
When a checkable item is selected, the system calls your respective
item-selected callback method (such as onOptionsItemSelected()). It is
here that you must set the state of the checkbox, because a checkbox
or radio button does not change its state automatically.
It's easy to observe that yours onLongClick(View v) get called every time and so the menu gets created every long click again (with initial items states). To fix the issue you can store checked / unchecked state (or current color) some there and set item states properly every time in onLongClick(View v). Like the following:
public class MainActivity extends ActionBarActivity {
private static final String TAG = "MainActivity";
private TextView mText;
// For persistent storage SharedPreferences should be used instead of local variable
private int mColor = Color.RED;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText edit = (EditText) findViewById(R.id.edit_text_1);
mText = (TextView)findViewById(R.id.textView1);
mText.setBackgroundColor(mColor);
edit.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Log.d(TAG, "onLongClick");
final PopupMenu pop_up = new PopupMenu(MainActivity.this, v);
final Menu menu = pop_up.getMenu();
getMenuInflater().inflate(R.menu.popup_menu, menu);
//GROUP'S ID IS "group".
menu.setGroupCheckable(R.id.group, true, true);
pop_up.show();
switch(mColor) {
case Color.RED:
menu.findItem(R.id.yellow).setChecked(false);
menu.findItem(R.id.red).setChecked(true);
break;
case Color.YELLOW:
menu.findItem(R.id.red).setChecked(false);
menu.findItem(R.id.yellow).setChecked(true);
break;
default:
break;
}
pop_up.setOnMenuItemClickListener(mMenuItemClickListener);
return true;
}
});
}
final OnMenuItemClickListener mMenuItemClickListener = new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch(item.getItemId()) {
case R.id.red:
mText.setBackgroundColor(Color.RED);
mColor = Color.RED;
return true;
case R.id.yellow:
mText.setBackgroundColor(Color.YELLOW);
mColor = Color.YELLOW;
return true;
default:
return false;
}
}
};
Also, I'd suggest to checkout ContextMenu which provides slightly more convenient way for creating long-click context menus. Please note that documentation of ContextMenu from onCreateContextMenu() clearly states that menu in proper state should be populated by the app every time:
Called when a context menu for the view is about to be shown. Unlike
onCreateOptionsMenu(Menu), this will be called every time the context
menu is about to be shown and should be populated for the view (or
item inside the view for AdapterView subclasses, this can be found in
the menuInfo)).

drop down menu on image button on action bar

what i have is an action bar .. and i have only logo in it and an image button and i want when i press on the button a spinner with a three values will appear and each one of them open a new activity , here is my code :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
android.app.ActionBar mActionBar = getActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text);
mTitleTextView.setText("My Own Title");
ImageButton imageButton = (ImageButton) mCustomView
.findViewById(R.id.imageButton);
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// i want the spinner to be add here
}
});
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
}
#Override
public boolean onNavigationItemSelected(int arg0, long arg1) {
// TODO Auto-generated method stub
return false;
}
ublic class MainActivity extends Activity{
...
...
/**
* On selecting action bar icons
* */
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_search:
// search action
return true;
case R.id.action_location_found:
// location found
LocationFound();
return true;
case R.id.action_refresh:
// refresh
return true;
case R.id.action_help:
// help action
return true;
case R.id.action_check_updates:
// check for updates action
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/**
* Launching new activity
* */
private void LocationFound() {
Intent i = new Intent(MainActivity.this, LocationFound.class);
startActivity(i);
}
}
Try This code :
For better understanding go through following link
http://www.androidhive.info/2013/11/android-working-with-action-bar/

Action bar Back button not working

with the help of these Android Docs.I am trying to do a action bar Back button.I get an Action Bar Back Button like these below image:
Output:
But My problem is After watching the Gallery images I press the action bar back button.
Then it is not working.But it have to go back to previous page.
Listed below are the codings.
GalleryActivity.java:
import android.app.ActionBar;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import com.fth.android.R;
public class GalleryActivity extends FragmentActivity {
private int position;
private static String id;
private static String name;
private DemoCollectionPagerAdapter mDemoCollectionPagerAdapter;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
position = getIntent().getExtras().getInt("position");
id = getIntent().getExtras().getString("id");
name = getIntent().getExtras().getString("name");
mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(getSupportFragmentManager());
// Set up action bar.
final ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
// getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME|ActionBar.DISPLAY_USE_LOGO|ActionBar.DISPLAY_HOME_AS_UP);
// Set up the ViewPager, attaching the adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mDemoCollectionPagerAdapter);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent upIntent = new Intent(this, HomeActivity.class);
upIntent.putExtra("position", position);
if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
TaskStackBuilder.from(this)
.addNextIntent(upIntent)
.startActivities();
finish();
} else {
NavUtils.navigateUpTo(this, upIntent);
}
return true;
}
return super.onOptionsItemSelected(item);
}
}
GalleryDetailFragment.java:
import com.sit.fth.model.GalleryDetail;
import com.sit.fth.util.APIServiceHandler;
import com.sit.fth.util.AppConstants;
import com.sit.fth.util.AppPromoPager;
public class GalleryDetailFragment extends BaseFragment implements
PromoPagerListener {
private TextView countView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
this.setHasOptionsMenu(true);
id = getArguments().getString("id");
name = getArguments().getString("name");
View view = inflater.inflate(R.layout.app_pager, null);
return view;
}
}
Anybody can help me if you know how to solve these.Thank You.
I solved these problem by adding the below coding in GalleryActivity.
ActionBar actionBar;
actionBar=getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
In MainActivity:
Previously,
public class HomeActivity extends BaseActivity
Then I change into
public class HomeActivity extends FragmentActivity
In GalleryFragment:
I use Intent to pass it to the GalleryActivity.
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Gallery gallery = (Gallery) arg0.getAdapter().getItem(arg2);
Intent intent = new Intent(getActivity(), GalleryActivity.class);
intent.putExtra("position", position);
intent.putExtra("id", gallery.getGalId());
intent.putExtra("name", gallery.getAlbumTitle());
startActivity(intent);
// mCallback.OnGalItemSelected(gallery.getGalId(),gallery.getAlbumTitle());
}
Please read this
you should have something like this:
<activity
android:name="com.sit.fth.activity.HomeActivity"
android:screenOrientation="portrait">
</activity>
<activity
android:name="com.sit.fth.activity.GalleryActivity"
android:screenOrientation="portrait"
android:parentActivityName="com.sit.fth.activity.HomeActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.sit.fth.activity.HomeActivity"/>
</activity>
then calling NavUtils.navigateUpFromSameTask(this) will cause navigating to parent activity (HomeActivity).
You need to call setDisplayHomeAsUpEnabled(true) method in the onCreate method and override onSupportNavigateUp() and call onBackPressed() in it as below. That's it. done :)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_help);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
You have just to add the following line to the activity in the manifest.xml. The parent activity is the activity to which you want to go back.
android:parentActivityName=".activities.MainActivity"
Try like
First of all you need to use addToBackStack() before commit() for Fragments
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
if(getSupportFragmentManager().getBackStackEntryCount()>0)
getSupportFragmentManager().popBackStack();
return true;
}
return super.onOptionsItemSelected(item);
}
Best and easy answer is add the parent activity name in Manifest file, so Actionbar back button will work.
For that under that Activity tag of Manifest File use
android:parentActivityName=".MyCustomParentActivity"
if the home button is shown. you should add an action to the home button through onOptionItemSelected fun (arrow in your case) by default there's no action. so it's totally normal that it's not working. Please add this fun to your activity :
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when {
item.itemId == android.R.id.home -> {
finish()
true
}
else -> super.onOptionsItemSelected(item)
}
}
None of the answers provided here worked for me. I had to put the switch inside the onMenuItemSelected method. I'm aware this is not what is stated in the Android documentation, but still, it worked, so I just thought I'd leave this here for people who run into the same issue. My problem involved an Activity instead of a Fragment though, but that should be pretty much the same.
class FooActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return false;
}
}
To me, I had to set mDrawerToggle.setToolbarNavigationClickListener(...) to a listener that triggers the back action. Otherwise it does nothing. This is what the source code of ActionBarDrawerToggle looks like:
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mDrawerIndicatorEnabled) {
toggle();
} else if (mToolbarNavigationClickListener != null) {
mToolbarNavigationClickListener.onClick(v);
}
}
});
So the default behaviour is actually to call our listener, and not do any magic on its own.
Use on SupportNavigateUp() method and call onBackPressed in this method.
onCreate
{
...
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
resetActionBar();
...
}
public void resetActionBar()
{
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
#Override
public void onBackPressed() {
FragmentManager fm = getSupportFragmentManager();
int count = fm.getBackStackEntryCount();
if(count == 0) {
// Do you want to close app?
showDialog();
}else{
super.onBackPressed();
}
}
#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();
Log.i("coming", "comming");
//noinspection SimplifiableIfStatement
if(id==android.R.id.home){
if (getSupportFragmentManager().getBackStackEntryCount() > 0)
onBackPressed();
else
drawerLayout.openDrawer(navigationView);
return true;
}
return super.onOptionsItemSelected(item);
}
Here is one more thing to check for in case the other answers here (or here or here or here) don't work.
I had copied some code from another activity that disabled the menu. Deleting this method (and applying the solutions given in the others answers) allowed the up button to work.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// hide the menu
return false;
}
In my case I had overridden the onCreateOptionsMenu method and I forgot to call super at the end.
Tool bar enable back button for Kotlin binding
setSupportActionBar(binding.toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_times_ic)

How to open New activity on click on Button in Action Bar?

This is my ActionBar, it has two buttons:
private void showActionBar() {
LayoutInflater inflator = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.activity_main_actions, null);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setCustomView(v);
}
I used this method to show buttons. I called this method in onCreate.
Now i want when i click on Any button which is in action bar New activity open.
For example i have AskActivity.java and MessageActivity.java
now when i click on ASK button AskActivity.java opens.
Is this possible?
I have used this but its not working.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_ask:
Intent i = new Intent(getApplicationContext(), AskActivity.class);
startActivity(i);
return true;
case R.id.action_message:
Intent ij = new Intent(getApplicationContext(), MessageActivity.class);
startActivity(ij);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I think it's because the onOptionsItemSelected method is related to MenuItem and not the CustomView. The two buttons are not option menu items, they are buttons inside the layout activity_main_actions. You have two choices - either create a new on click listener, as follows:
Button action_ask = (Button) findViewById(R.id.action_ask);
action_ask.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v){
// do something
}
}
Or, use the on click attribute method:
<Button
android:id="#+id/action_ask"
...
android:onClick="actionAskClicked" />
And then inside your Activity:
public void actionAskClicked() {
// do something
}
Same for the other button action_message. Hope this helps.
You need to create a method to open an activity from menu button click:
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_ask:
startActivity(AskActivity.class);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
protected void StartActivity(Class<?> cls) {
Intent intent = new Intent(activity, cls);
activity.startActivity(intent);
}

How to close the menu when I click the button?

I use the GridView and set to MultiChoiceModeListener.
When I select the item from GridView , it will call the onCreateActionMode and the onActionItemClicked like the following code.
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
View v = LayoutInflater.from(getActivity()).inflate(R.layout.actionbar_layout, null);
mActionText = (TextView) v.findViewById(R.id.action_text);
mActionText.setText(formatString(fileListView.getCheckedItemCount()));
mode.setCustomView(v);
getActivity().getMenuInflater().inflate(R.menu.action_menu, menu);
return true;
}
And the menu will show how many item I have select like the following picture.
When I click the button , it will transmit the item which I have select to a new Fragment.
The following code is for button
download_button = (ImageButton) view.findViewById(R.id.download_button) ;
download_button.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment = DownloadPage.newInstance(null, null, null, checkedItems) ;
MainActivity.addFragment(FileBrowserFragment.this, fragment);
menu.finish(); //can not call menu.finish();
}
But when it turn to the new fragment , the menu doesn't disappeared.
How to close the menu when I click the button and turn to the new fragment???
Think you are looking for finish(); on ActionMode see this example:
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// Respond to clicks on the actions in the CAB
switch (item.getItemId()) {
case R.id.menu_delete:
deleteSelectedItems();
mode.finish();
return true;
}
}
If you want to finish by button click, register listener to your button and then put the finish() method inside that.
EDIT
Try this:
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// Respond to clicks on the actions in the CAB
switch (item.getItemId()) {
case R.id.menu_delete:
download_button.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment = DownloadPage.newInstance(null, null, null, checkedItems) ;
MainActivity.addFragment(FileBrowserFragment.this, fragment);
deleteSelectedItems();
mode.finish();
}
});
return true;
}

Categories

Resources