I'm trying to use the default android action bar with a custom view:
<style name="ActionBar" parent="android:Widget.Material.ActionBar.Solid">
<item name="android:displayOptions">showCustom</item>
<item name="android:customNavigationLayout">#layout/customNavigationLayout</item>
</style>
The options menu contains a single item which should always show with text:
<item
android:id="#+id/action_cancel"
android:showAsAction="always|withText"
android:title="#string/action_cancel" />
Now I'm having the issue with the selectable background, which is still the size of an action icon:
How can I setup the action bar to apply a selectable background which fills the whole box of the item?
You can try setting the android:actionBarItemBackground attribute in styles, like so:
<style name="AppTheme" parent="android:Theme.Material">
...
<item name="android:actionBarItemBackground">?android:selectableItemBackground</item>
</style>
Use one of the following solutions:
Solution 1:
Add <item name="actionButtonStyle">#style/ActionButtonStyle</item> to your base application theme with for exmaple:
<style name="ActionButtonStyle" parent="android:Widget.Material.Button">
<item name="android:textColor">#android:color/black</item>
<item name="android:textSize">16sp</item>
<item name="android:background">#drawable/button_background</item>
</style>
and
button_background
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#color/colorAccent"/>
<item android:state_focused="true"
android:drawable="#color/colorPrimaryDark"/>
<item android:drawable="#color/colorPrimary"/>
</selector>
Solution 2:
Use menuItem.setActionView to apply a custom layout to your menu item as follows:
layout_action_cancel (custom menu item layout):
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="#+id/b_action_cancel"
android:gravity="center"
android:background="#drawable/button_background">
</Button>
Then in your onCreateOptionsMenu apply the custom Layout to the menu
item:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
LayoutInflater layoutInflater = getLayoutInflater();
View layout = layoutInflater.inflate(R.layout.layout_action_cancel, null, false);
menu.getItem(0).setActionView(layout);
return super.onCreateOptionsMenu(menu);
}
and in onPrepareOptionsMenu set an OnClick Listener (replacing the
onOptionsItemSelected)
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
View layout = menu.getItem(0).getActionView();
if(layout instanceof Button){
Button b_action_cancel = layout.findViewById(R.id.b_action_cancel);
b_action_cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//your code
}
});
}
return super.onPrepareOptionsMenu(menu);
}
You can try applying drawable withe selected and normal state at menu item property :
<item
android:id="#+id/action_cancel"
android:showAsAction="always|withText"
android:title="#string/action_cancel"
android:icon="#drawable/mybuttonbackground" />
Related
I am trying to implement shared animation transition when someone clicks on recycler view item and navigate to detail activity.I want to show effect on image view when its size increases in detail activity.There is no transition effect is showing.
Below is my code:
themes.xml
<style name="Theme.ITunes" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">#color/purple</item>
<item name="colorPrimaryVariant">#color/purple_dark</item>
<item name="colorOnPrimary">#color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">#color/teal_200</item>
<item name="colorSecondaryVariant">#color/teal_700</item>
<item name="colorOnSecondary">#color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
<item name="android:windowContentTransitions">true</item>
</style>
item_layout.xml
<ImageView
android:layout_width="200dp"
android:layout_height="170dp"
android:id="#+id/trackImg"
android:scaleType="fitXY"
android:transitionName="imageTransition"/>
activity_video_detail.xml
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:id="#+id/imgDetail"
android:scaleType="fitXY"
android:transitionName="imageTransition"/>
VideoAdapter.class
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(context, VideoDetail.class);
i.putExtra("name",model.getTrackName());
i.putExtra("image",model.getArtworkUrl100());
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation((Activity) context,holder.trackImg,"transition");
context.startActivity(i,options.toBundle());
}
});
VideoDetail.class
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
if(item.getItemId() == android.R.id.home){
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
#Override
public void onBackPressed() {
supportFinishAfterTransition();
}
What can I try to resolve this?
The string passed to makeSceneTransitionAnimation should match transitionName in the receiving activity, so use imageTransition instead of transition
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation((Activity) context,holder.trackImg,"imageTransition");
It's also a good idea to use string resources to get rid of magic strings
In my app I need to use the ActionMode.
I have followed several Guides and Tutorials (https://medium.com/asos-techblog/style-actionmode-on-android-5e613fa77c32, https://developer.android.com/guide/topics/ui/menus e.g.) on implementing my Contextual Action Menu but as you can see in this image:
https://i.stack.imgur.com/ag2Mp.jpg
There is part of the titleBar still visible on the bottom. My question is if there is anything i can do to change this behaviour.
I also tried to change the style of my ActionModeBar but when i change the styling it becomes invisible (only showing the icons) and the titlebar is completely visible.
I have not made any changes to the ActionMode except the menu that contains the buttons:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/stopCab"
android:title="Stop contextual action bar"
app:showAsAction="ifRoom"
android:icon="#drawable/ic_baseline_cancel_24"/>
<item android:id="#+id/deleteCab"
android:title="Delete selected items"
app:showAsAction="ifRoom"
android:icon="#drawable/ic_delete_white_24dp"/>
</menu>
Initializing the ActionMode:
private ActionMode.Callback actionModeCallback = new ActionMode.Callback() {
#Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.contextual_action_bar_menu, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.deleteCab:
//TODO: do something
return true;
case R.id.stopCab:
//TODO: do something
return true;
default:
return false;
}
}
#Override
public void onDestroyActionMode(ActionMode actionMode) {
FieldChooserActivity.this.actionMode = null;
}
};
And starting the ActionMode:
FieldChooserActivity.this.actionMode = startActionMode(FieldChooserActivity.this.actionModeCallback);
In my Style the AppTheme parent is Theme.AppCompat.Light.DarkActionBar
And each Activity has
android:theme="#style/AppTheme.NoActionBar">
in AndroidManifest.
Maybe this is the fault why i cant change the style without the ActionMode becoming invisible.
I hope someone can help me with this matter and thanks in advance!
I found a solution that fixed my problem (where the ActionMode bar doesn't cover the Toolbar).
As i looked at the layout of my activity i saw that in the Toolbar is a height attribute called '?attr/actionBarSize':
<androidx.appcompat.widget.Toolbar
android:id="#+id/fieldChooserToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay">
I then added the height to a custom style for my ActionMode:
<style name="ActionModeTheme" parent="AppTheme">
<item name="windowActionModeOverlay">true</item>
<item name="actionModeCloseDrawable">#drawable/ic_delete_white_24dp</item>
<item name="actionMenuTextColor">#android:color/white</item>
<item name="height">?attr/actionBarSize</item>
<item name="background">#color/colorSecondary</item>
<item name="android:src">#drawable/ic_baseline_close_24</item>
</style>
The last part was that i added following line to my AppTheme-styling:
<item name="actionModeStyle">#style/ActionModeTheme</item>
And then it worked!
The second problem where my ActionMode bar was invisible was most likely caused by adding the actionModeStyling to the AppTheme.NoActionBar.
I wanted to create an option menu which shows up similar to Whatsapp even if the device has its own "hardware" option button. The solution I found online worked just fine until I wanted to add another action to the Bar.
Menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title=""
android:id="#+id/addViewedMovie"
android:icon="#mipmap/ic_add_to_queue_black_24px"
android:showAsAction="always"
></item>
<item
android:title=""
android:id="#+id/menu_overflow"
android:icon="#mipmap/ic_more_vert_white_48dp"
app:showAsAction="always">
<menu>
<item
android:id="#+id/show_settings"
app:showAsAction="never"
android:title="#string/setttings"/>
<item
android:id="#+id/show_help"
app:showAsAction="never"
android:title="#string/help"/>
<item
android:id="#+id/show_about"
app:showAsAction="never"
android:title="#string/about"/>
</menu>
</item>
</menu>
And this in my MainActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
With this the app forces the addViewedMovie Action into an Overflow-Menu. Does anyone know how to prevent this?
Try this code and tell me if its work.. you should use app:showAsAction not android:showAsAction also you do not need to use submenu to show 3 dots.. just below code works fine also show 3 dots
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title="Movie"
android:id="#+id/addViewedMovie"
android:icon="#mipmap/ic_add_to_queue_black_24px"
app:showAsAction="always"/>
<item
android:id="#+id/show_settings"
app:showAsAction="never"
android:title="#string/setttings"/>
<item
android:id="#+id/show_help"
app:showAsAction="never"
android:title="#string/help"/>
<item
android:id="#+id/show_about"
app:showAsAction="never"
android:title="#string/about"/>
</menu>
Create a folder name Menu in your res folder.
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/search"
android:icon="#drawable/ic_launcher"
myapp:showAsAction="ifRoom" />
<item android:id="#+id/abc"
android:title="ABC" />
<item android:id="#+id/abc1"
android:title="ABC1" />
<item android:id="#+id/abc2"
android:title="ABC2" />
</menu>
In java file put this:-
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
return true;
case R.id.abc:
return true;
case R.id.abc1:
return true;
case R.id.abc2;
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Simple.
use app instead of android as app is for design support library.
And so, in Gradle build in dependencies, you must have: compile com.android.support:design..... like that. But, I have seen that you already used it below. So,
Just use: app:showAsAction="never" instead of android:showAsAction="always" in your addViewedMovie item.
Remember: never means it will be on overflow menu item. always means is will show in action bar.
If you are using in a fragment, add below line in the onCreate
setHasOptionsMenu(true);
And two more functions
#Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
}
Inflate your views in the above method. Listen to the action in the below one.
#Override public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
I'm working in my app and have a searchview inside toolbar but the hint has a small padding after the cursor.
How can I remove the padding?
Here is my searchview:
and I would like that:
here is my onCreateOptionsMenu:
SearchManager searchManager=(SearchManager)getSystemService(Context.SEARCH_SERVICE);
MenuItem item=menu.findItem(R.id.procura);
Drawable drawable = menu.findItem(R.id.procura).getIcon();
if (drawable != null) {
drawable.mutate();
drawable.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_ATOP);
}
searchView=(SearchView) MenuItemCompat.getActionView(item);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setQueryHint(getResources().getString(R.string.app_hint));
try {
Field mDrawable = SearchView.class.getDeclaredField("mSearchHintIcon");
mDrawable.setAccessible(true);
Drawable drw = (Drawable) mDrawable.get(searchView);
drw.setBounds(0, 0, 0, 0);
} catch (Exception e) {
e.printStackTrace();
}
My styles:
<style name="SearchViewMy" parent="Widget.AppCompat.Light.SearchView">
<item name="submitBackground">#color/colorPrimary</item>
<item name="queryBackground">#color/colorPrimary</item>
<item name="android:colorFocusedHighlight">#color/colorAccent</item>
</style>
My menu:
<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/procura"
android:title="#string/app_hint"
android:icon="#mipmap/ic_search_black_24dp"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>
I found the solution for my problem.
Just add this in the styles:
<item name="searchHintIcon">#null</item>
The styles now is that:
<style name="SearchViewMy" parent="Widget.AppCompat.Light.SearchView">
<item name="submitBackground">#color/colorPrimary</item>
<item name="queryBackground">#color/colorPrimary</item>
<item name="searchHintIcon">#null</item>
<item name="android:colorFocusedHighlight">#color/colorAccent</item>
</style>
And onCreateOptionsMenu now is that:
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
SearchManager searchManager=(SearchManager)getSystemService(Context.SEARCH_SERVICE);
MenuItem item=menu.findItem(R.id.procura);
searchView=(SearchView) MenuItemCompat.getActionView(item);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setQueryHint(getResources().getString(R.string.app_hint));
return true;
Im trying to do a menu with only one icon, but instead of that icon, i am getting the typican three dots, and inside my option. So, i only want one icon. That is my XML code:
<?xml version="1.0" encoding="utf-8"?>
<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.bartek.gestionpea.MainActivity">
<item
android:id="#+id/ayuda"
android:icon="#android:drawable/ic_dialog_info"
android:title="Ayuda"
app:showAsAction="ifRoom"/>
</menu>
Here goes my class:
public class MainActivity extends Activity {
PeniaSQLiteHelper usdbh;
private SQLiteDatabase db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().setTitle("GESTION");
Button btnCrearPena = (Button) findViewById(R.id.btncrearpena);
btnCrearPena.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, CrearNueva.class);
startActivity(i);
}
});
}
#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_principal, menu);
return true;
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case R.id.ayuda:
return true;
}
return super.onMenuItemSelected(featureId, item);
}
}
You are using some things for the native action bar (e.g., inheriting from Activity) and some things for the appcompat-v7 action bar backport (e.g., app:showAsAction). Choose one and stick with it:
If you wish to use the native action bar, use android:showAsAction
If you wish to use the appcompat-v7 action bar backport, inherit from AppCompatActivity (in the latest appcompat-v7) or ActionBarActivity (for prior versions of appcompat-v7)
Since you use the standard Activity, try this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/ayuda"
android:icon="#android:drawable/ic_dialog_info"
android:title="Ayuda"
android:showAsAction="always" />
</menu>
You can modify the app theme (in themes.xml):
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:actionOverflowButtonStyle">#style/MyActionButtonOverflow</item>
</style>
<!-- Style to replace actionbar overflow icon. set item 'android:actionOverflowButtonStyle' in AppTheme -->
<style name="MyActionButtonOverflow" parent="android:style/Widget.Holo.Light.ActionButton.Overflow">
<item name="android:src">#drawable/your_icon_here</item>
<item name="android:background">?android:attr/actionBarItemBackground</item>
<item name="android:contentDescription">"options"</item>
</style>
More info here
Note that when using appcompat themes, you sometimes need to set xml attibutes twice:
once with and once without the android namespace
<item name="android:windowNoTitle">true</item>
<item name="windowNoTitle">true</item>