I have created an application which has a RecyclerView and each item has an option menu, which will show the popup to share. Everything is working fine but when I try to click on the option menu it crashes. I am getting a weird error. I google it and it seems like there is something wrong with my theme. I tried many solutions available on the internet but none of them working for me.
Here is my theme:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:textColorPrimary">#ffffff</item>
<item name="android:textColorSecondary">#color/about_us_link_text_color</item>
</style>
This is my Adapter class
holder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PopupMenu popUp = new PopupMenu(context,holder.buttonViewOption);
popUp.inflate(R.menu.option_menu);
popUp.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.share:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT,news.get(position).getUrl());
intent.putExtra(Intent.EXTRA_SUBJECT,"check out this site");
context.startActivity(Intent.createChooser(intent,"share"));
break;
case R.id.save:
break;
}
return false;
}
});
popUp.show();
}
});
option_menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/share"
android:title="Share" />
<item
android:id="#+id/save"
android:title="Save" />
</menu>
The error I am getting is :
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 6: TypedValue{t=0x1d/d=0xffff4081 a=3 r=0x7f0c001a}
at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:705)
at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:6890)
at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:7071)
at android.widget.FrameLayout$LayoutParams.<init>(FrameLayout.java:446)
at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:386)
at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:385)
at android.view.LayoutInflater.inflate(LayoutInflater.java:502)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.support.v7.view.menu.MenuAdapter.getView(MenuAdapter.java:93)
at android.support.v7.view.menu.MenuPopup.measureIndividualMenuWidth(MenuPopup.java:160)
at android.support.v7.view.menu.StandardMenuPopup.tryShow(StandardMenuPopup.java:153)
at android.support.v7.view.menu.StandardMenuPopup.show(StandardMenuPopup.java:187)
at android.support.v7.view.menu.MenuPopupHelper.showPopup(MenuPopupHelper.java:290)
at android.support.v7.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:175)
at android.support.v7.view.menu.MenuPopupHelper.show(MenuPopupHelper.java:141)
at android.support.v7.widget.PopupMenu.show(PopupMenu.java:233)
at com.kotlin.whatshappening.activity.adapter.NewsAdapter$2.onClick(NewsAdapter.java:142)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21158)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Can someone tell me what I am doing wrong?
Instead of:
PopupMenu popUp = new PopupMenu(context, holder.buttonViewOption);
Perform:
PopupMenu popUp = new PopupMenu(view.getContext(), holder.buttonViewOption);
Try this
popUp.getMenuInflater().inflate(R.menu.option_menu, popUp.getMenu());
I hope this helps you.
First create a method in you adapter class like the following.
private void showPopup(View view, String url) {
PopupMenu popup = new PopupMenu(activityContext, view);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.option_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.share:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT,url);
intent.putExtra(Intent.EXTRA_SUBJECT,"check out this site");
activityContext.startActivity(Intent.createChooser(intent,"share"));
break;
case R.id.save:
break;
}
return true;
}
});
popup.show();
}
And in your onBindViewHolder method call the above function,
holder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showPopup(view, news.get(holder.getAdapterPosition()).getUrl());
}
});
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
1)This is Code declare my id at menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="home"
android:icon="#drawable/home"
android:title="HOME"/>
<item
android:id="category"
android:icon="#drawable/category"
android:title="CATEGORY"/>
<item
android:id="basket"
android:icon="#drawable/basket"
android:title="BASKET"/>
<item
android:id="me"
android:icon="#drawable/me"
android:title="ME"/>
</menu>
This is code i call my id
private BottomNavigationView.OnNavigationItemSelectedListener navigation =
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.home:
Toast.makeText(HomeActivity.this, "HOME", Toast.LENGTH_SHORT).show();
break;
case R.id.category:
Toast.makeText(HomeActivity.this, "HOME", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
};
I have error when i call category id
case R.id.category:
but at case R.id.home: ,the code is right means no wrong.
Question : Who know why i cannot call my id?
can anyone give suggest for me, i take long time to fix this code. mybe i miss something like dot or comma? hahahaha
<item android:id="home"
should be
<item android:id="#+id/home"
the same applies to the others
The issue is with all the ids in your menu XML file
change it to the following:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
item android:id="#+id/home"
android:icon="#drawable/home"
android:title="HOME"/>
<item
item android:id="#+id/category"
android:icon="#drawable/category"
android:title="CATEGORY"/>
<item
item android:id="#+id/basket"
android:icon="#drawable/basket"
android:title="BASKET"/>
<item
item android:id="#+id/me"
android:icon="#drawable/me"
android:title="ME"/>
</menu>
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" />
Okay, so i got a small problem that i really don't know how to fix this.
So i have an application where i use my own style for an Action Bar
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="windowActionModeOverlay">true</item>
<item name="actionBarStyle">#style/AppTheme.ActionBar</item>
<item name="android:textColorSecondary">#color/action_bar_bg</item>
</style>
<style name="AppTheme.ActionBar" parent="Widget.AppCompat.ActionBar">
<item name="height">30dp</item>
<item name="background">#color/action_bar_bg</item>
</style>
</resources>
The menu that is displayed
<?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" >
<item android:id="#+id/sortByStatus"
android:title="#string/sort_by_status"
app:showAsAction="never" />
<item android:id="#+id/sortByName"
android:title="#string/sort_by_name"
app:showAsAction="never" />
<item android:id="#+id/sortByCompany"
android:title="#string/sort_by_company"
app:showAsAction="never" />
</menu>
This works great for almost all places, except when i try to display my options menu. I have added the options menu and the handler for it, but when i click on the options menu then the default top icons (from the notification bar) also shows inside my options menu, and i don't want those to show there. It also starts showing the default bottom navigation buttons for Sony phones.
Enabling the menu from onResume
#Override
public void onResume() {
super.onResume();
AppCompatActivity appCompatActivity = (AppCompatActivity) getActivity();
if (appCompatActivity != null && appCompatActivity.getSupportActionBar() != null)
appCompatActivity.getSupportActionBar().setTitle(getResources().getString(R.string.presense_status));
setHasOptionsMenu(true);
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
and then the menu onCreate
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_presense_status, menu);
}
and then the handler for selected item
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
getActivity().getFragmentManager().popBackStackImmediate();
break;
case R.id.sortByStatus:
if (null != mDataSource) {
// update data list
reloadData();
}
break;
case R.id.sortByName:
if (null != mDataSource) {
// update data list
reloadData();
}
break;
case R.id.sortByCompany:
if (null != mDataSource) {
// update data list
reloadData();
}
break;
default:
super.onOptionsItemSelected(item);
}
return true;
}
I really can't see where the problem is coming from or how to remove it so that this does not happen. This happens inside a Fragment that is started by my main Activity, meaning i only have one Activity that spawns different fragments for the different in-app tasks.
Any help or thought would be greatly appreciated.
I still don't really know why the notification bar was affected by what i did, but forcing the application to fullscreen in this view stops the problem, so i guess that's an acceptable workaround.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
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>