Click event not working in android action bar item - android

I am trying to make a shopping cart android app and i want to show the cart with number of items.
attaching the screenshot of app below .
When clicking on the cart icon its does not going any where.. :(.
here is my code.
MainActivity.java
public class PandaActivity extends ActionBarActivity {
TextView notifCount;
RelativeLayout count;
int mNotifCount = 0;
protected ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.topbar));
actionBar.setHomeAsUpIndicator(getResources().getDrawable(R.drawable.backbutton));
}
#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_panda, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.badge) {
Intent cartIntent=new Intent(getApplicationContext(),CartShowActivity.class);
startActivity(cartIntent);
return true;
}
return super.onOptionsItemSelected(item);
}
}
menu_panda.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="com.sha.netstager.pandafoods.activity.PandaActivity">
<item
android:title="demo"
android:id="#+id/badge"
app:actionLayout="#layout/feed_update_count"
app:showAsAction="always">
</item>
</menu>
feed_update_count.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="48dp"
android:layout_height="fill_parent"
android:layout_gravity="right">
<!-- Menu Item Image -->
<ImageView
android:layout_width="48dp"
android:layout_height="fill_parent"
android:clickable="true"
android:src="#drawable/cart" />
<!-- Badge Count -->
<TextView
android:id="#+id/actionbar_notifcation_textview"
android:layout_width="18dp"
android:layout_marginRight="4dp"
android:layout_height="22dp"
android:layout_alignParentRight="true"
android:text="99"
android:textSize="16dp"
android:background="#f7134e"
android:textColor="#fff"/>
</RelativeLayout>
I tried many ways to solve this problem with onclick methord and some others. but dont know what is the error is happening here.
There is no messages or warnings in logcat when I click on the menu item in action bar.
Many Thanks..

Try to add onclick in your onCreateOptionsMenu, instead of current solution as given below.
#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, menu);
RelativeLayout rl_viewBag = (RelativeLayout) menu.findItem(R.id.badge).getActionView();
txt_bagCount = (TextView)rl_viewBag.findViewById(R.id.actionbar_notifcation_textview);
txt_bagCount.setText("10");
rl_viewBag.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cartIntent=new Intent(getApplicationContext(),CartShowActivity.class);
startActivity(cartIntent);
}
});
return true;
}

for better look of actionbar use extends AppCompatActivity this is the same look and feel will be provided which is been used by gmail.

Related

How to get and populate a menu item created through custom layout?

I've been trying to load an image into a menu item which has been created through a custom layout and have been checking on the official documentation and on posts such as these Custom view for Menu Item but..
That's my onCreateOptionsMenu ond OnPrepareOptionsMenu
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
final MenuItem alertMenuItem = menu.findItem(R.id.action_my_personal);
FrameLayout rootView = (FrameLayout) alertMenuItem.getActionView();
redCircle = (FrameLayout) rootView.findViewById(R.id.view_alert_red_circle);
countTextView = (TextView) rootView.findViewById(R.id.view_alert_count_textview);
ImageView hc_image_menu_inflated = (ImageView) rootView.findViewById(R.id.hc_image_menu);
rootView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onOptionsItemSelected(alertMenuItem);
}
});
if (menu.findItem(R.id.hc_image_menu) != null) {
loadMenuIcon(menu.findItem(R.id.hc_image_menu), my_image_url);
}
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.toolbar_menu_main, menu);
return true;
}
As you can see I have added these lines to avoid the app crashing, as menu.finditem(R.id.hc_image_menu) is returning as null and that's where the problem is, as I want to populate the ImageView that has the id hc_image_menu but am not being successful despite so many attempts
if (menu.findItem(R.id.hc_image_menu) != null) {
loadMenuIcon(menu.findItem(R.id.hc_image_menu), my_image_url);
}
My code for my toolbar_main_menu layout is this:
<item
android:id="#+id/action_my_personal"
app:actionLayout="#layout/toolbar_menu_hc"
android:orderInCategory="100"
android:title="My Personal Trainer"
app:showAsAction="always" />
And for my custom layout (toolbar_hc_menu) is this one:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="48dp"
android:layout_height="32dp"
android:layout_gravity="center">
<ImageView
android:id="#+id/hc_image_menu"
android:layout_marginRight="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/gavin"/>
<FrameLayout
android:id="#+id/view_alert_red_circle"
android:layout_width="14dp"
android:layout_height="14dp"
android:layout_gravity="top|end"
android:background="#drawable/date_circle"
android:visibility="gone"
tools:visibility="visible">
<TextView
android:id="#+id/view_alert_count_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#color/white"
android:textSize="10sp"
tools:text="3"/>
</FrameLayout>
Thanks very much for any help on that. Really much appreciated!! :)
Issue fixed. :)
At the end I just had to load the image into the ImageView after having got the alertMenuItem.
if (my_image_url!= null) {
Picasso.with(getApplicationContext()).load(my_image_url).into(hc_image_menu_inflated);
}

How to align action bar title in center of screen, not the center of action bar

I use this method to align action bar title to center
How to make android action bar title center aligned?
But my action bar has menu, and then action bar width is not same to screen
so the title is not exactly center.
Is there other way to center the title?
Code:
actionbar.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="YOUR ACTIVITY TITLE"
android:textColor="#000"
android:textSize="24sp" />
</LinearLayout>
the activity which used custom action bar
public class WriteText extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_write_text);
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getActionBar().setCustomView(R.layout.actionbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.write_tag, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Maybe you can change the actionbar xml file to make one your own style of actionbar.

Action Bar change effect

I'm trying to reproduce the Google Keep, Gmail, etc ActionBar change effects.
Like image bellow, when the user select a note or a mail, the action bar changes using its effects.
I would like to do the same.
Any help?
Looks like it's relatively new ability, called Contextual action mode.
Simply put, it's just specific 'context' menu for long-selected item. I think that the link above provides enough information. However, below is some simple example on how to use it:
main.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:clipChildren="false"
android:id="#+id/root">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:text="Do Long Press"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp" />
</RelativeLayout>
context_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:icon="#android:drawable/ic_menu_close_clear_cancel"
android:title="close"
android:showAsAction="always"
android:id="#+id/close_menu" />
</menu>
MyActivity.java:
public class MyActivity extends Activity implements ActionMode.Callback {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.button).setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(final View v) {
startActionMode(MyActivity.this);
return true;
}
});
}
#Override
public boolean onCreateActionMode(final ActionMode mode, final Menu menu) {
// Inflate a menu resource providing context menu items
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
return true;
}
#Override
public boolean onPrepareActionMode(final ActionMode mode, final Menu menu) {
// TODO: auto-generated block
return false;
}
#Override
public boolean onActionItemClicked(final ActionMode mode, final MenuItem item) {
switch (item.getItemId()) {
case R.id.close_menu:
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
#Override
public void onDestroyActionMode(final ActionMode mode) {
// TODO: auto-generated block
}
}
It looks like the following:
Execute Long press
Please note, that V item is default one and in context_menu.xml I've provided only X item.

How to implement actionbarsherlock

I am trying to use actionbar sherlock
I have implemented a sample project using actionbarsherlock
MainActivity.java
public class MainActivity extends SherlockActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from activity_main.xml
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// First Menu Button
menu.add("Help")
.setOnMenuItemClickListener(this.HelpButtonClickListener)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
// Second Menu Button
menu.add("Like")
.setOnMenuItemClickListener(this.LikeButtonClickListener)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
// Third Menu Button
menu.add("Exit")
.setOnMenuItemClickListener(this.ExitButtonClickListener)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return super.onCreateOptionsMenu(menu);
}
}
activity_main.xml
<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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/ActionBarSherlock" />
</RelativeLayout>
My Present output:: Sherlockbar is at bottom
What i am trying to do :: 2 sherlock bars one at top and one at bottom. both must be different bars
so that i can use them for different listeners
Is it possible ?
How to implement this !
You can implement only one action bar. There is no way to implement two action bar. Except when you split action bar. Refer this link to learn more about splitting action bar.

Android actoin bar get valules from linked layout

My android application has an action bar item to add employees.
So here what I did is call custom layout named add_employee when this action bar item clicked.
<item android:id="#+id/action_add"
android:icon="#drawable/ic_action_add_person"
android:title="#string/action_add"
android:showAsAction="ifRoom|collapseActionView"
android:actionLayout="#layout/add_employee"/>
Following image shows that custom layout:
This is a loyout xml file named add_employee. Add and cross marks are image views.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/imageView1"
android:layout_marginRight="12dp"
android:src="#drawable/ic_action_cancel" />
<EditText
android:id="#+id/editText1"
android:layout_width="228dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="40dp"
android:layout_toLeftOf="#+id/imageView1"
android:ems="10"
android:inputType="numberSigned" />
<ImageView
android:id="#+id/addemp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText1"
android:layout_marginRight="26dp"
android:layout_toLeftOf="#+id/imageView2"
android:src="#drawable/ic_action_add_person" />
</RelativeLayout>
Now I want to get the entered value to this edit text field when add image view clicked. This operation should be handled in the same java class where I set the option menu also.
public class MainPortal extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_portal);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.main_portal, menu);
return super.onCreateOptionsMenu(menu);
}
}
I have used setOnItemClickListener for imge view but it ends with a forced closed error. How can I do this task?
Please help me...
Thanks in advanced.
Did you create the actions in onOptionsItemSelected? Where is the code showing the implementation of the Action bar tabs?
this is how to implement an option on actionbar
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_compose:
composeMessage();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
there you can implement whatever and however...
plus showing more code and LogCat would help understanding your exact issue

Categories

Resources