I want to add a standard Up Button icon ( < like this ) in action bar.
I added this in the styles:
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:homeAsUpIndicator">#drawable/navigation_previous_item</item>
</style>
I enabled home Button in onCreate:
getActionBar().setHomeButtonEnabled(true);
And added action event:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case android.R.id.home:
onBackPressed();
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
Problem is that i can't see the ( < ) icon. Application icon is padded to the right, like there is a place for my drawable, but it doesn't show me the icon . . .
try getActionBar().setDisplayHomeAsUpEnabled(true); and it should work
Related
I am working on android UX design. I am using RelativeLayout. I want to do the below screen in android.
I want to know how to show an icon at the top of the screen (red marked).
When i click that icon, i want to show the information about the app with a black background.
I am new to android and i don't know how to do this. Need some help to do.
That's an ActionBar (Toolbar) Menu icon:
You need to add a menu file inside res/menu, like: main.xml formed this way:
<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=".MainActivity">
<item
android:id="#+id/action_main"
android:title="#string/action_main"
android:orderInCategory="100"
android:icon="#drawable/ic_launcher"
app:showAsAction="ifRoom" />
</menu>
Over "android:icon" goes the one you want, "android:title" the one you want when the user long press the icon.
EDIT Check the updated code with comments
To have listener (onClick actions) do this over the Activity:
#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_main){
Toast.makeText(getApplicationContext(), "Main action is selected!", Toast.LENGTH_SHORT).show();
// Toast only have a small duration to show something,
// even when you long press the item, the Title is also a
// Toast. You can literally do anything here. Show stuffs,
// hide it, open activities, close the app, etc
return true;
}
return super.onOptionsItemSelected(item);
}
add in menul
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/phone"
android:title="#string/phone"
android:icon="#drawable/phone"
android:showAsAction="always"
/>
</menu
After that in java
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()){
case R.id.phone:
Toast.makeText(getBaseContext(), "You selected Phone", Toast.LENGTH_SHORT).show();
break;
}
return true;
I have to implement the following functionality. See the picture.
Can I change the functionality of Navigation drawer? I haven't any found fix for my problem.
I hope you can help me, thanks in advance.
Yes it's possible. However I advise you against it because it kind of breaks the UI.
If you choose to do it though, first you need to be able to access the overflow menu (circled button). This was answered before here, so I'll just provide a style.xml snippet:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionOverflowButtonStyle">#style/Overflow</item>
</style>
<style name="Overflow" parent="Widget.AppCompat.ActionBar">
<item name="android:contentDescription">OVERFLOW</item>
</style>
Then, when the home (navigation drawer) button is clicked you open the overflow menu:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
final View decor = getWindow().getDecorView();
ArrayList<View> results = new ArrayList<>();
decor.findViewsWithText(results, "OVERFLOW",
View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
if (results.size() == 1) {
results.get(0).performClick();
return true;
}
}
return super.onOptionsItemSelected(item);
}
States of actionBar
pressed state::
Default state::
What i am doing ::
I am using the splitted actionbar
On click of icon in bottom, I am changing the fragment
Functionality is working fine
What i want::
Is it possible to keep the pressed state of the actionbar item until
i click the next actionbar icon
Then when i say click the star in figure, i want to keep the star in
pressed state as blue color and search icon to return to normal state
Code that i use::
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.searchID){
//SEARCH Button Handling
//((View) item).setBackgroundResource(R.color.actionBarIconPressed);
//item.setIcon(R.color.actionBarIconPressed);
item.setEnabled(true);
ft1=getSupportFragmentManager().beginTransaction();
ft1.hide(fragment1);
//Condition to check whether the fragment is already in container & based on that do appropriate actions
if (getSupportFragmentManager().findFragmentByTag("SearchFragmentTag")==null){
ft1.add(R.id.content_frame, fragSearch, "SearchFragmentTag");
ft1.addToBackStack(null);
ft1.commit();
}else{
ft1.remove(fragSearch);
ft1.add(R.id.content_frame, fragSearch, "SearchFragmentTag");
ft1.commit();
}
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getSupportMenuInflater();
menuInflater.inflate(R.menu.actionbar_sort_menu, menu);
return super.onCreateOptionsMenu(menu);
}
My Questions::
How to modify my code to achieve this feature ?
Is it possible ?
What are the possible ways ?
In onOptionsItemSelected, you can cast the MenuItem into a View and then, change its background. I got a start answer however I think you can improve it and change it according to your needs:
Init your ids in an array:
int[] listItemId = { R.id.searchID, R.id.ratindID, R.id.likeID, R.id.shareID };
Call your options item selected method:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.searchID:
// call private method with int 1
onItemChangeBackground(1);
... // do some stuff
return true;
case R.id.ratindID:
onItemChangeBackground(2);
... // do some stuff
return true;
case R.id.likeID:
onItemChangeBackground(3);
...
return true;
case R.id.shareID:
onItemChangeBackground(4);
...
return true;
}
return super.onOptionsItemSelected(item);
}
Change the background item by switching an int value:
private void onItemChangeBackground(int i) {
// Loop to reinit the background items
for(int ii = 0; ii < listItemId.length; ii++) {
// Cast the MenuItem into a View
( (View) findViewById(listItemId[ii]) )
// And set the default background (for example dark)
.setBackgroundColor(getResources().getColor(R.color.dark));
}
// Selected background
switch(i) {
case 1: ( (View) findViewById(R.id.searchID) )
.setBackgroundColor(getResources().getColor(R.color.blue));
break;
case 2: ( (View) findViewById(R.id.ratindID) )
.setBackgroundColor(getResources().getColor(R.color.blue));
break;
case 3: ( (View) findViewById(R.id.likeID) )
.setBackgroundColor(getResources().getColor(R.color.blue));
break;
case 4: ( (View) findViewById(R.id.shareID) )
.setBackgroundColor(getResources().getColor(R.color.blue));
break;
}
}
You can also set a drawable with a selector by using setBackgroundDrawable() when you want to reinitialize the background items (as you do in the styles.xml). Tested, it works!
Another solution might be to use invalidateOptionsMenu(). This will clear and redraw the menu by recalling onCreateOptionsMenu() again. Keep in mind without call invalidateOptionsMenu() the last method will never be recalled, it's only called at the first rime of the activity's creation. That's why we need to invalidate its content:
Update an integer for each item selected:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.searchID){
nbItemSelected = 1;
invalidateOptionsMenu();
...
} else if(...) {
nbItemSelected = 2;
invalidateOptionsMenu();
} ...
}
Then, change the background while you create the options:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_layout, menu);
if(nItemSelected != 0) {
// Find item, cast the selected item to a view
( (View) menu.findItem(listItemId[nItemSelected]) )
// Change its background
.setBackgroundColor(getResources().getColor(R.color.blue));
}
...
return true;
}
I guess it is possible in this way too, it might work.
Are you creating a TabBarView like for example those you can find in iOs?
If so, an split actionbar is not the best component, it is better to use a ViewPagerIndicator with a ViewPager component. In there you can create your custom icons with your custom icons states and backgrounds.
I have an spare snippet of code I created long time ago for an application that required that component. Maybe you can look it around and take what you need (but I advice you that the code is not the best you can find as I was in a hurry, things can be done better, much better):
https://gist.github.com/aldoborrero/70fbeb062fab0ccc7d87
#Casper did you tried setting custom selector for your ActionBar?
Refer one below
drawable/tab_bg_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Active tab -->
<item android:drawable="#drawable/tab_selected_customtabs" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/>
<!-- Inactive tab -->
<item android:drawable="#drawable/ab_transparent_customtabs" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>
<!-- Pressed tab -->
<item android:drawable="#drawable/tab_selected_pressed_customtabs" android:state_pressed="true"/>
<!-- Selected tab -->
<item android:drawable="#drawable/tab_selected_focused_customtabs" android:state_focused="true" android:state_pressed="false" android:state_selected="true"/>
</selector>
I am creating an app wherein I have three items in the action bar. I want that when I click on one of the item, it should get highlighted.
I went through few examples available but concluded that I will have to create style using
ActionBar Style Generator tool.
I referred the following link.. but was not satisfied
how to highlight a menu item on selection?
Is this the only way to go about?
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:actionBarItemBackground">#drawable/action_bar_item_selector</item>
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:drawable="#drawable/ic_action_blue" />
<item android:state_pressed="true"
android:drawable="#drawable/ic_action_green" />
<item android:drawable="#android:color/transparent" />
</selector>
But the selection does not remain stagnant as it is for state_pressed and state_focused.
I want the item to remain highlighted till the user is on that activity.
More elegant way of handling the issue , is to simply have a boolean value that will check
both states , If you are using the same drawable with different state colors , i will recommend
you to use the colorFilter method to apply to your drawables.
private boolean mSomeValue = true;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// determine the menu item you want to highlight when selected
switch(item.getItemId) {
case yourItem : {
if (mSomeValue) {
item.getIcon().setColorFilter(YourColor, PorterDuff.Mode.MULTIPLY);
mSomeValue = false;
} else {
mSomeValue = true;
item.getIcon().setColorFilter(Default, PorterDuff.Mode.MULTIPLY);
}
break; ...
}
}
return super.onOptionsItemSelected(item);
}
You can define a theme and inherit the Action bar theme you want (for instance Theme.Sherlock.Light.DarkActionBar), set the value of the android:actionBarItemBackground attribute to override the default one in the inheritted theme.
<style name="MyTheme" parent="Theme.Sherlock.Light.DarkActionBar">
<item name="android:actionBarItemBackground">custom_selector</item>
</style>
I am creating an app wherein I have three items in the action bar. I
want that when I click on one of the item, it should get highlighted.
I am not sure if the following will work properly:
Create two sets of icons for each of the menu items: one for the default look, the other highlighted (in whatever way you feel: change the color, change the background color etc.). Declare a global Menu variable to hold the menu item:
// Global variable // Not good practice, but needed in this case
Menu mainMenu;
....
....
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_name, menu);
mainMenu = menu;
return true;
}
In onMenuItemSelected(int featureId, MenuItem item):
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
for (int i = 0; i < mainMenu.size(); i++) {
// Set default icons
if (mainMenu.getItem(i).getItemId() == R.id.action1) {
mainMenu.getItem(i).setIcon(R.drawable.icon_default1);
} else if (mainMenu.getItem(i).getItemId() == R.id.action2) {
mainMenu.getItem(i).setIcon(R.drawable.icon_default2);
} else if (.....) {
.....
}
}
if (item.getItemId() == R.id.action1) {
item.setIcon(R.drawable.icon_highlighted1);
} else if (item.getItemId() == R.id.action2) {
item.setIcon(R.drawable.icon_highlighted2);
} else if(.....) {
.....
}
return super.onMenuItemSelected(featureId, item);
}
I guess you can fine-tune this by checking against item.getItemId() inside the for-loop itself.
Ok, so i suggest you to create two different items in your menu.xml:
<item
android:id="#+id/action_item1"
/>
<item
android:id="#+id/action_item2"
android:visible="false"
/>
Then, in the activity in which you want to highlight your item, override the ononPrepareOptionsMenu , use the menu paremeter to catch your desired item by using the menu.findItem(R.id.your_item_id). And finally toggle the visibility of your items.
You will need to refresh your action bar items automatically by using invalidateOptionsMenu
Let me know if that helps !
When I'm on Android App and i click on menu button at mobile device, settings menu appear. How i can add new item Facebook like at this setting menu ?
Maybe this is what you are looking for?
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/like"
android:title="#string/menu_facebook_like"
android:icon="#drawable/ic_menu_like"
android:showAsAction="ifRoom" />
</menu>
Then on your code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle menu item selection
switch (item.getItemId()) {
case android.R.id.like:
// Do what you want when "like" is pressed
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Of course you have to find a "like" icon (ask to Google) and resize it for the ActionBar/Menu.