how to display clickble cart with badge on menu bar in android - android

I am created a shopping cart and want to display the badge with number of product selected in cart on menu. on click of cart icon it should go to cart page where i display all the products in list.

i solved this problem by using button onclick event.
i placed cart image as button background and changing the text of cart.
Menu item code :
<item
android:id="#+id/item_samplebadge"
android:title="Cart"
app:showAsAction="always"
android:orderInCategory="100"
app:actionLayout="#layout/budge_layout"/>
Budge_layoout.xml (actionLayout:#layout/budge_layout)
<Button
android:id="#+id/counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="20dp"
android:background="#drawable/cart"
android:textStyle="bold"
android:textColor="#ffffff"
android:clickable="true"
android:gravity="top|center_horizontal"
android:textAlignment="center"
android:paddingTop="6dp"/>
in fragment calling onclick event
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_order_process,menu);
this.mMenu=menu;
RelativeLayout v=(RelativeLayout)menu.findItem(R.id.item_samplebadge).getActionView();
Button count1=(Button)v.findViewById(R.id.counter);
int cartsize=aController.getProductsArrayListSize();
count1.setText(String.valueOf(cartsize));
count1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
budge_click();
}
});
}
this code worked for my application

Related

Hide spinner in Android and display with the click of a button

I have created a spinner in my app which i want to be invisible when someone press the sos button then it should be visible for the user to select one option in it how can i solve it?
I have created a spinner in my app which i want to be invisible when
someone press the sos button
You can set a listener on the button that will set the visibility of the spinner.
Ex.
sosButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mySpinner.setVisibility(View.GONE);
}
});
it should be visible for the user to select one option in it how can i
solve it?
I'm not sure what this means. I thought you wanted the spinner to be invisible?
You can use the below code to hide and show the Spinner
//hide
spinner.setVisibility(View.GONE);
//show
spinner.setVisibility(View.VISIBLE);
Also,you can use the below code snippet to get the item selected by user;
spinner.setOnItemSelectedListener(this);
...
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
}
//Hide
spinner.setVisibility(View.GONE);
//Show
spinner.setVisibility(View.VISIBLE);
Android: How to make a Spinner invisible and then visible again?
#HumanOidRoBo you can do it by this code..
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Optional"
android:textSize="20sp" />
<Spinner
android:id="#+id/mySpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_marginLeft="5dp">
</Spinner>
</LinearLayout>
and in class add this on click event of SOS
sosButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mySpinner.setVisibility(View.VISIBLE); // for Show
// or
mySpinner.setVisibility(View.GONE); // for Hide
}
});

TextView in menu is always null after orientation change

I have a TextView functioning as a notification badge on a menu item. The problem is that the findViewById method always returns null after an orientation change, so when I try to modify its visibility with setVisibility it throws a NullPointerException. I've tried calling the onCreateOptionsMenu again by calling invalidateOptionsMenu in onRestart but it doesn't appear to help.
From what I can tell every other view is found and it's just this TextView that is being a nuisance.
The part where it crashes (this is called in onCreate) :
public void updateUnreadNotificationCount(final int unreadNotificationsCount) {
unreadNotificationCount = unreadNotificationsCount;
if (unreadNotificationCount == 0) {
notificationCounter.setVisibility(View.INVISIBLE);
}else {
notificationCounter.setVisibility(View.VISIBLE);
notificationCounter.setText(String.valueOf(unreadNotificationCount));
}
}
How the Textview gets created:
#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);
View count = menu.findItem(R.id.action_notifications).getActionView();
notificationCounter = (TextView) count.findViewById(R.id.textview_notification_count);
count.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(mCtx, NotificationListActivity.class);
startActivity(i);
}
});
notificationCounter.setText(String.valueOf(unreadNotificationCount));
return super.onCreateOptionsMenu(menu);
}
The XML for the menu item:
<item
android:id="#+id/action_notifications"
android:title="#string/action_notifications"
android:icon="#drawable/ic_mail_white_48dp"
android:actionLayout="#layout/actionbar_notification_icon"
android:showAsAction="always"
app:showAsAction="always"
android:orderInCategory="60"/>
The action layout for the TextView:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
style="#android:style/Widget.ActionButton">
<ImageView
android:id="#+id/imageview_notification"
android:src="#drawable/ic_mail_white_48dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="0dp"/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textview_notification_count"
android:layout_width="wrap_content"
android:minWidth="17sp"
android:textSize="12sp"
android:textColor="#ffffffff"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#null"
android:layout_alignTop="#id/imageview_notification"
android:layout_alignRight="#id/imageview_notification"
android:layout_marginRight="0dp"
android:layout_marginTop="3dp"
android:paddingBottom="1dp"
android:paddingRight="4dp"
android:paddingLeft="4dp"
android:background="#drawable/rounded_square"/>
</RelativeLayout>
Managed to fix the issue by moving the initialization of the TextView into onPrepareOptionsMenu and calling invalidateOptionsMenu() during onResume().

OnOptionsItemSelected not being called for Action Bar Menu Item with custom actionLayout

I'm trying to implement a notification icon in my actionbar to show the count of notifications. Something like
I've added a custom layout file for it NotificationIcon.xml:
<!-- Menu Item Image -->
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:clickable="true"
android:src="#drawable/notification" />
<!-- Badge Count -->
<TextView
android:id="#+id/actionbar_notifcation_textview"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:minWidth="20dp"
android:layout_alignParentRight="true"
android:gravity="center_horizontal"
android:background="#drawable/circle_green"
android:fontFamily="sans-serif-black"
android:textStyle="bold"
android:text="0"
android:textColor="#FFFFFF" />
</RelativeLayout>
And used it in my menu as main_activity_actions.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_add"
android:title="#string/AddTag"
android:icon="#+drawable/ic_action_new"
android:showAsAction="always" />
<item
android:id="#+id/notification_icon"
android:title="#string/PendingJobs"
android:actionLayout="#layout/notificationIcon"
android:icon="#+drawable/notification"
android:showAsAction="always" />
<item
android:id="#+id/gps_status_icon"
android:title="#string/GPS"
android:icon="#+drawable/gps_grey"
android:showAsAction="always" />
</menu>
The UI looks fine but the OnOptionsItemSelected is not being called for the notification icon. It works fine for the other two.
I did google this and ound this link: onOptionsItemSelected not getting called when using custom action view
I tried to implement it in my main activity:
public override bool OnCreateOptionsMenu(IMenu menu)
{
actionBarMenu = menu;
MenuInflater.Inflate(Resource.Menu.main_activity_actions, menu);
var notificationMenuItem = menu.FindItem(Resource.Id.notification_icon);
notificationMenuItem.ActionView.Click += (sender, args) => {
this.OnOptionsItemSelected(notificationMenuItem);
};
return base.OnCreateOptionsMenu(menu);
}
but it does not works for me. It never fires the click event.
Please help.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
final View notification_icon= menu.findItem(R.id.notification_icon).getActionView();
notification_icon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// write ur code here
}
});
return super.onCreateOptionsMenu(menu);
}
use this code......hope it helps :)
i struggle with the same Problem and found following Solution:
First of all: i really don't know why but if you delete the
android:clickable="true"
then ... and ONLY then you get the click event!!!
Second: you have to set the Listener...
...
item.getActionView().setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do Your Stuff.....
}
});
May someone can Explain why this is with the "android:clickable" Thing... i tough the standard value IS true??
You had made ImageButton clickable, and then written the clickListener for the Layout of menuItem. You can observe that if you click outside the ImageButton but inside the MenuItem layout, your listener will work. The reason is simple clickListener for ImageButton has nothing to do, on setting Clickable = true defines whether this button reacts to click events. Just simply set Clickable = false, your button will not react to its click event, eventually your whole layout will react to your defined clickListener and then your problem will be solved.

How to set spinner in action bar for android application?

I am using action bar for my application. I am getting my preview in device like this
My spinner didn't set properly. I added button as item in menu. My code is here... http://pastie.org/8482541
When i run in my device only it shows some what different.
How can i do this? Can any body help me? Thanks in advance.
try like this for each menu item you want
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.item1);
item.getActionView().setBackgroundDrawable(new ColorDrawable(/*some color you need */));
return super.onPrepareOptionsMenu(menu);
}
use this custom menu
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#id/item1"
android:showAsAction="always"
android:actionLayout="#layout/action_text"/>
</menu>
Use this:
You need to take one button and set any image as its background.Then on click of the button call Spinner.performClick() to open the spinner.
Below is code to implement the same thing. In xml file:
<Button
android:id="#+id/font"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="50dp"
android:layout_weight="0.5"
android:background="#drawable/textsel" />
<Spinner
android:id="#+id/spin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_weight="0.5"
android:dropDownHorizontalOffset="0dp"
android:dropDownVerticalOffset="20dp"
android:dropDownWidth="500dp"
android:paddingTop="2sp"
android:spinnerMode="dropdown" >
</Spinner>
In Java class:
Spinner spin = (Spinner) findViewById(R.id.spin);
Button typetext = (Button) findViewById(R.id.font);
typetext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
spin.performClick();
}
});

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