drop down menu on image button on action bar - android

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/

Related

Actionbar buttons not working

I am new to android development.I have made an app where the MainActivity consists of a Gridview with images and ActivityTwo is the fullscreen view of the selected image.I am trying to add a back button on the actionbar.The icon is visible but it is not clickable.Also i tried to add other menu items which are also visible but nothing happens on touching them.I have also tried adding onClickListener to the menu items as suggested in some posts but it didn't seem to work.
Below is the ActivityTwo code.
public class ActivityTwo extends ActionBarActivity implements OnClickListener {
protected int currentPosition;
Button share,back;
ImageView imageView;
ViewPager viewPager;
private Toolbar toolbar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
toolbar= (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
share = (Button)findViewById(R.id.button1);
share.setOnClickListener(this);
back=(Button)findViewById(R.id.button2);
back.setOnClickListener(this);
// get intent data
Intent i = getIntent();
// Selected image id
final int position = i.getExtras().getInt("position");
viewPager = (ViewPager) findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(position);
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
currentPosition = arg0;
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
#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;
}
if
(id==R.id.home)
{
NavUtils.navigateUpFromSameTask(this);
}
return super.onOptionsItemSelected(item);
}
public class ImagePagerAdapter extends PagerAdapter {
Integer[] icons = MainActivity.mThumbIds;
#Override
public int getCount() {
return icons.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = ActivityTwo.this;
imageView = new ImageView(context);
//ImageView imageView = (ImageView)findViewById(R.id.imageView);
// int padding = context.getResources().getDimensionPixelSize(
// R.dimen.padding_large);
imageView.setPadding(5, 5, 5, 5);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setImageResource(icons[position]);
imageView.setTag(position);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==share){
ImageView Imgv = (ImageView)viewPager.findViewWithTag(viewPager.getCurrentItem());
Drawable mDrawable = Imgv.getDrawable();
Bitmap mBitmap = ((BitmapDrawable)mDrawable).getBitmap();
String path = Images.Media.insertImage(getContentResolver(),
mBitmap, "Image Description", null);
Uri uri = Uri.parse(path);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share Image")); }
if(v==back)
{
finish();
}
}
}
Try to add this code to your toolbar for navigating back
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
For another item in your toolbar
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
Toast.makeText(context, "setting", Toast.LENGTH_SHORT).show();
break;
case R.id.action_share:
Toast.makeText(context, "share", Toast.LENGTH_SHORT).show();
break;
}
}
Hope this help
To make your app icon "clickable" as a back button, your Activities must follow a parent-child hierarchy.
Example:
Activity A is your main Activity.
Pressing something (say a ListView item or GridView item) will start Activity B.
In this way Activity A is what's called a parent Activity to Activity B.
You define this in XML in your manifest like so:
<activity
android:name="com.example.android.ActivityA"
android:label="#string/title_activity_a" />
<activity
android:name="com.example.android.ActivityB"
android:label="#string/title_activity_b"
android:parentActivityName="com.example.android.ActivityA">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.android.ActivityA" />
</activity>
This will make the app icon appear with a back arrow ( < ) to the left of it.
We then must handle the click event.
We so this from within our onOptionsItemSelected method in Activity B like so:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
// Create an intent to start Activity A.
Intent intent = new Intent(ActivityB.this, ActivityA.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
android.R.id.home is the ID of the click event we are listening for.
Sometimes you may wish to simulate a back button press instead of starting Activity A fresh with an intent. You could achieve that like so:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
// Simulate back button press.
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}

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 can I click an Android Action Bar item under program control?

PeterH posted the following code:
//initiate the button
button.performClick();
button.setPressed(true);
button.invalidate();
// delay completion till animation completes
button.postDelayed(new Runnable() { //delay button
public void run() {
button.setPressed(false);
button.invalidate();
//any other associated action
}
}, 800); // .8secs delay time
Can the same type of operation be performed for Action Bar items?
Well, you can save MenuItem as a field and then call onOptionsItemSelected(savedMenuItem). But as ActionBar items are MenuItems and not Buttons (of course, if your action bar isn't customized with view like http://www.vogella.com/articles/AndroidActionBar/article.html#actionbar_dynamic). But if your ActionBar is customized with view and that view has a Button, that Button's behaviour can be customized as considered in your code snippet.
Example:
public class MainActivity extends Activity {
MenuItem item;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
actionBar.setSubtitle("mytest");
actionBar.setTitle("TESTESTEST");
TextView tView = (TextView) findViewById(R.id.textView1);
tView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
onOptionsItemSelected(item);
}
});
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_settings) {
this.item = item;
Toast.makeText(this, "settings", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
}

SlidingMenu : Showing seconday menu on an event

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);
}

Code for menu items work in one class but not working in other

when i use this same code for menu item in other class then menu is shown. but when i implement in this class then don't show , can anyone tell me is there any problem with menu item if we use implement OnClickListener
public class MainPage extends Activity implements OnClickListener {
Context context;
ImageButton countingbutton, maximymbutton, minButton, compareButton,
statsbutton, review, memorygmae;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.newmainpage);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
countingbutton = (ImageButton) findViewById(R.id.countingbutton);
maximymbutton = (ImageButton) findViewById(R.id.maximumbutton);
minButton = (ImageButton) findViewById(R.id.minimunbutton);
compareButton = (ImageButton) findViewById(R.id.comparenum);
statsbutton = (ImageButton) findViewById(R.id.ibStats);
review = (ImageButton) findViewById(R.id.review);
countingbutton.setOnClickListener(this);
maximymbutton.setOnClickListener(this);
minButton.setOnClickListener(this);
compareButton.setOnClickListener(this);
statsbutton.setOnClickListener(this);
review.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate themenu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.cool_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub private static final String
switch (item.getItemId())
{
case R.id.aboutUs:
Intent about = new Intent(this, AboutUs.class);
startActivity(about);
break;
case R.id.exit:
super.finish();
System.exit(0);
break;
case R.id.mainmenu: Intent mainmenu = new Intent(this, MainPage.class);
startActivity(mainmenu);
break;
}
return false;
}

Categories

Resources