I am trying to use icon fonts with ActionBar menu item using a custom layout. Applying a layout to the menu item does solve the problem and the icon font renders perfect but doing so make the menu item un-clickable. Is there a way to solve this?
I am trying to avoid using a custom action bar because I want to use font icons instead of images.
Here is my code so far:
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="48dp"
android:layout_height="match_parent"
android:layout_gravity="right">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
style="#style/action_bar_icons"
android:text="#string/ic_sliders"
android:clickable="true"/>
</RelativeLayout>
menu.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.hackerrank.projectx.SearchResultsActivity">
<item
android:id="#+id/action_filters"
android:title="#string/action_filters"
android:showAsAction="always"
android:onClick="onFilterClick"
android:actionLayout="#layout/action_search_result_filter" />
<item
android:id="#+id/action_toggle_view"
android:title="#string/action_custom_view"
/>
</menu>
You Can setOnClickListener on the actionLayout something like this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(mMenuResource, menu);
MenuItem item = menu.findItem(R.id.notfication);
FrameLayout badgeLayout = (FrameLayout) MenuItemCompat.getActionView(item);
badgeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent not = new Intent(DrawerActivity.this,NotificationActivity.class);
not.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(not);
}
});
return super.onCreateOptionsMenu(menu);
}
If you are using it inside a fragment try this:
onCreateView()
{
setHasOptionMenu(true);
...`enter code here`
}
Related
I've created a custom ActionBar with a simple TextView, the text is supposed to be aligned to the center. Then I added a MenuItem with onCreateOptionsMenu method. The result was that the each of the components were at each side of the ActionBar, the menu-item was placed at the left corner and the text-view was placed at the right corner. How can I make the text-view remain centered despite adding menu-items to the action-bar?
Here is my custom action_bar_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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_gravity="center"
android:text="#string/app_name"
android:textColor="#ffffff"
android:id="#+id/action_bar_text"
android:textSize="18sp"/>
</LinearLayout>
Here is my menu.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.example.rashish.myapplication.MainActivity">
<item
android:id="#+id/back_action"
android:orderInCategory="100"
android:title="#string/back"
app:showAsAction="always"/>
</menu>
Here is my onCreateOptionsMenu method:
public boolean onCreateOptionsMenu(Menu menu) {
this.mMenu = menu;
getMenuInflater().inflate(R.menu.menu_main, mMenu);
MenuItem item = menu.findItem(R.id.back_action);
item.setVisible(true);
return true;
}
and this is how I use my custom action bar in onCreate method
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.action_bar_layout);
//MORE CODE
Since i'm new here i was wondering how could i add a TextView in the ActionBar
It should be shown in the right corner of the action bar.
The text for example is "Tell a friend"
Thank you for your help.
You just usethis function in your activity you can use the Custom layout for Activity...
sActionBar = getActionBar();
TabActivityPacs.sActionBar.setDisplayShowHomeEnabled(false);
TabActivityPacs.sActionBar.setDisplayShowTitleEnabled(false);
TabActivityPacs.sActionBar.setDisplayShowCustomEnabled(true);
final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(
R.layout.custom_title,
null);
TabActivityPacs.sActionBar.setCustomView(actionBarLayout);
You can design watever you want in the Custom title layout
All the best..
First create a layout.xml file :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/tell_a_friend"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Tell a Friend"
/>
</LinearLayout>
then in menu.xml write :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/tell_a_friend"
android:showAsAction="ifRoom|collapseActionView"
android:actionLayout="#layout/layout"
/>
</menu>
then in Activity :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
then in onOptionsItemSelected() :
public boolean onOptionsItemSelected (MenueItem item){
switch (item.getItemId()){
case R.id.tell_a_friend:
Toast.makeText(this, "Tell a friend", Toast.LENGTH_LONG).show();
break;
}
return true}
hope this may work for you..
In my action bar, I have defined a menu item that can show text "DONE" by the code below:
Menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_register_text"
android:actionLayout="#layout/action_done_text"
android:title="#string/action_done"
android:showAsAction="always"/>
</menu>
action_done_text.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/expand_activities_button"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:clickable="true"
android:focusable="true"
android:addStatesFromChildren="true">
<TextView
android:id="#+id/register_action_bar_done"
android:layout_width="53dp"
android:layout_height="35dp"
android:layout_gravity="center"
android:layout_marginRight="10dip"
android:gravity="center"
android:text="DONE" />
</FrameLayout>
I have onCreateOptionsMenu implement properly in the code, and the view can show the text correctly, but just when I tap on the DONE text, onOptionsItemSelected is not called. To me, it seems like the click event is not recognized.
I was wondering if the above way is not a good way to add a text menu item?
Use this as shown in onOptionsItemSelected not called when using actionLayout (SherlockActionBar)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.map_menu, menu);
for (int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i);
if (item.getItemId() == R.id.menu_more) {
itemChooser = item.getActionView();
if (itemChooser != null) {
itemChooser.setOnClickListener(this);
}
}
}
return super.onCreateOptionsMenu(menu);
}
I need to know how can I change actionbar searchview icon when click on it? I'm currently using "Theme.Holo.Light" and I don't want to change this theme anymore. Is there anyway can change SearchView icon in ActionBar when click on it?
yes it is possible to do.
What you should do is create custom layout for the search menu option let say searchLayout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_search"
android:layout_width="40dip"
android:layout_height="40dip" />
</LinearLayout>
now use this layout in your menu option.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menuSearch"
android:actionLayout="#layout/searchLayout"
android:showAsAction="always"
android:title="search"/>
</menu>
now you just have to get the view from the menu option :
#Override
public boolean onPrepareOptionsMenu( Menu menu )
{
final MenuItem item = menu.findItem( R.id.menuSearch );
LinearLayout layoutSearchView = (LinearLayout) item.getActionView();
ImageView img = (ImageView) layoutSearchView.findViewById( R.id. img_search);
// now you have your image of search . on click you can change the image using set onclick listener of image or do what ever you want with the layout.
img.setOnClickListener( new View.OnClickListener()
{
#Override
public void onClick( View v )
{
// change your image
}
} );
}
sorry for any typo.
Hope this will help.
Note : I am doing change image code in onPrepareOptionsMenu in this snippet but I suggest to create a method and pass the menu item from onPrepareOptionsMenu and do the task in that method.
I've been trying to use the setActionView from the ActionBar in ICS
Seems like it should be straight forward but somehow I'm not getting the layout alignment that I would hope for. As you can see in the image below the 'target' icon is centered correctly within it's layout. But when I setActionBar(progress) the progress view is always aligned to the right whatever I try.
Here are the 2 states, before and after clicking the menu item. As you can see the progress view is always aligned to the right. I've tried changing the gravity options in the my progress layout xml from left to right to center and whatever I do change it doesn't seem to change anything.
I haven't found any info regarding this problem so I'm thinking I must be doing something wrong.
Do anyone have a clue?
Thanks for the help!
Here's my action bar menu layout 'action_bar_menu.xml'
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/locate"
android:title="locate"
android:icon="#drawable/locate"
android:showAsAction="ifRoom|withText" />
</menu>
Here's my progressbar layout 'inderterminate_progress.xml'
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ProgressBar android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="center"
android:indeterminate="true"
style="?android:attr/progressBarStyleInverse"/>
</FrameLayout>
And finally here's my testx Activity
public class HelloAndroidActivity extends Activity {
/**
* Called when the activity is first created.
* #param savedInstanceState If the activity is being re-initialized after
* previously being shut down then this Bundle contains the data it most
* recently supplied in onSaveInstanceState(Bundle). <b>Note: Otherwise it is null.</b>
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getActionBar().setTitle("Test");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.action_bar_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
if (R.id.locate == item.getItemId()) {
final MenuItem menuItem = item.setActionView(R.layout.inderterminate_progress);
new Thread(new Runnable() {
#Override
public void run() {
SystemClock.sleep(3000);
runOnUiThread(new Runnable() {
#Override
public void run() {
menuItem.setActionView(null);
}
});
}
}).start();
}
return true;
}
}
It seems that explicitly defining the style and adding a 4dip padding in the root of the layout to inflate as shown below solves this issue
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="4dip"
android:paddingRight="4dip"
style="?android:attr/actionButtonStyle" />
This seems to be used in the android stylesheet here
A little late but the right solution is not an absolute value such as 4dp. The right approach is to set the minWidth to ABS values:
android:minWidth="#dimen/abs__action_button_min_width"
Example progress bar:
<?xml version="1.0" encoding="utf-8"?>
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="#dimen/abs__action_button_min_width"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:layout_gravity="center"
style="#android:style/Widget.ProgressBar.Small"/>