How to set Button as MediaRouteButton? - android

As we set menuItem as the MediaRouteButton,
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.cast_item_menu, menu);
mediaRouteMenuItem = CastButtonFactory.setUpMediaRouteButton(getApplicationContext(), menu, R.id.media_route_menu_item);
return true;
}
Is there any way to set a button or an ImageButton as the MediaRouteButton in android?

Finally came up with a solution. Hope this will help someone.
Layout XML file
Create a MediaRouteButton using xml.
<android.support.v7.app.MediaRouteButton
android:id="#+id/cast"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_alignParentRight="true"
android:scaleType="center"
android:src="#drawable/ic_google_cast_logo"
android:visibility="visible" />
This will create the button you need. Then use it in your activity.
Activity
Instantiate the button and set the media button this way.
MediaRouteButton mediaRouteButton = (MediaRouteButton)findViewById(R.id.cast);
CastButtonFactory.setUpMediaRouteButton(getApplicationContext(),mediaRouteButton);
Happy coding...(y)

Related

OnCheckedChangeListener not invoked immediately

I have a switch within a menu that is show as an action on the action bar. The onCheckedChangeListener is not being invoked immediately after when I set the checked property to true. The switch does show up in the action bar, and every time it is pressed it the checkedchangelistener is invoked. However, it is not invoked in the beginning when it should be. How is this possible? Why is it not invoked? How do I fix this?
Here is the onCreateMenuOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.i(TAG, "onCreateOptionsMenu()");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
serviceSwitch = (Switch) menu.findItem(R.id.Switch).getActionView().findViewById(R.id.switchid);
serviceSwitch.setOnCheckedChangeListener(mCheckedChangeListener);
serviceSwitch.setChecked(false);
//OnCheckedChange is not being called here. I have set a breakpoint at the method.
//I have also looked at the log - nothing.
//It is being called when I click the switch however.
return true;
}
private OnCheckedChangeListener mCheckedChangeListener = new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.i(TAG, isChecked ? "true" : "false";
}
}
Here is the xml:
R.menu.mainmenu
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:title=""
android:id="#+id/Switch"
android:actionLayout="#layout/switch_layout"
android:showAsAction="always"></item>
</menu>
R.layout.switch_layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Switch
android:id="#+id/switchid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:paddingRight="20dp"
android:paddingLeft="20dp"
android:textOff="Off"
android:textOn="On"
android:checked="true"/>
</RelativeLayout>
R.layout.switch_layout
Switch checked value is already true in your layout file.
android:checked="true"
so when set its value to true its checked change listener is not called.
Hope you get my point.

Add/remove item menu menuItem dynamically

I have a problem, I want to add item in a toolbar (material design) programatically .
This is a my menu :
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" android:showAsAction="never" />
</menu>
I know change text , color , background, and #override listener
toolbar.setBackgroundColor
toolbar.setTextColor
toolbar.setNavigationIcon
toolbar.setText
I don't know how I can add a menu ítem with a category " android:orderInCategory="300"
Thanks.
Note : I have all fragment, without 1 Activity
Tree - > Activity - > Fragment1(here add menu item) - > Fragment2(add/remove menu item) - > Fragmentx ..
Try this Toolbar has option getMenu() which return menu
private static final String placeholder1="Something";
private static final int FIRST_ID=Menu.FIRST;
private static final int SECOND_ID=Menu.FIRST+1;
//To add an item
toolbar.getMenu().add(Menu.NONE,FIRST_ID,Menu.NONE,R.string.placeholder1);
toolbar.getMenu().add(Menu.NONE,SECOND_ID,Menu.NONE,R.string.placeholder2);
//and to remove a element just remove it with id
toolbar.getMenu().removeItem(FIRST_ID);
First of all put the item you want to display/hide in your xml file.
Let's say it's "action_settings" as mentioned in your code, override the "onCreateOptionsMenu"
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.your_menu, menu);
MenuItem item = menu.findItem(R.id.action_settings);
if (yourConditionToShow) {
item.setVisible(true);
} else {
item.setVisible(false);
}
return true;
}
If you do not have access to the toolbar, you can do the following:
1 - Add the item to the menu through onCreateOptionsMenu():
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_item_id, menu);
return true;
}
2 - Create a boolean equal to true if the item must be visible
3 - Hide or show the item according to the value of your boolean in onPrepareOptionsMenu()
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem mi = menu.findItem(R.id.my_item_id);
if(displayItem)
mi.setVisible(true);
else
mi.setVisible(false);
return super.onPrepareOptionsMenu(menu);
}
In your onCreateOptionmenu, try this code
//menu.add("groupId", "ItemId", "OrderID", "title")
MenuItem item = menu.add(1, 100, 300, "Settings");
item.setIcon(R.drawable.ic_launcher);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
you can also add setting like xml. You can check by itemid("100") in onOptionsItemSelected.
If you don't want to use 100 as ItemId, you can create ids resource file. And can use like this,
//R.id.action_settings is from ids resource file
MenuItem item = menu.add(1, R.id.action_settings, 300, "Settings");
1 Solution :
#Chitrang Thanks to idea, I solved with this line :
toolbar.inflateMenu(R.menu.menu_activity1);
toolbar.inflateMenu(R.menu.menu_activity2);
toolbar.inflateMenu(R.menu.menu_activityx);
Options select R.menu.item1,2,x
2 Solution :
Create custom toolbar : into "android.support.v7.widget.Toolbar" and can you managment all icons.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"></LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toolbar Title"
android:layout_gravity="center"
android:id="#+id/toolbar_title" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="right"
android:layout_marginRight="15dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="24dp"
android:id="#+id/imageView10"
android:src="#drawable/sun"
android:adjustViewBounds="true" />
</LinearLayout>
</LinearLayout>
In your menu XML file, you can add a menu item not visible (android:visible=false) and render visible when you want.

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.

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

Search Edittext in Sherlock Acionbar

I have a problem with search view in sherlock actionbar so i decided to use edit text instead. but when I want to use edit text my app crash (android 2.3). how could I use search edittext or searchview in sherlock activity for android 2.3?
here is my onCreateOptionsMenu function:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater minflater = getSupportMenuInflater();
minflater.inflate(R.menu.activity_main, menu);
View v = (View) menu.findItem(R.id.search).getActionView();
txtSearch = ( EditText ) v.findViewById(R.id.txt_search);
txtSearch.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
return false;
}
});
return true;
}
and this is my edittext xml source:
<EditText
android:id="#+id/txt_search"
android:inputType="text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cursorVisible="true"
android:hint="#string/hint"
android:imeOptions="actionDone"
android:textColor="#android:color/black"
android:textCursorDrawable="#android:color/black"
/>
and my search menu item:
<item
android:id="#+id/search"
android:title="Search"
android:icon="#android:drawable/ic_menu_search"
android:showAsAction="always|collapseActionView"
android:actionViewClass="#layout/search_layout"
/>
If anyone knows a way to use searchview in sherlock activity or use edittext for search in sherlock actionbar, notice me.
You should use
android:actionLayout="#layout/search_layout"
More info here: http://developer.android.com/guide/topics/ui/actionbar.html#ActionView
work for me:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.your_menu, menu);
// "0" is the number of menu item search in my example
menu.getItem(0).setActionView(R.layout.your_layout_search); //your layout with edittext
EditText editSearch = (EditText) menu.getItem(0).getActionView().findViewById(R.id.edit_search);
return super.onCreateOptionsMenu(menu);
}

Categories

Resources