Tab icons disappeared in marshmallows

I have defined three different classes for handling tab icons. Below is the BaseActivity class of BaseActivity .

 public class MainScreenPagerAdapter extends FragmentPagerAdapter implements TabBarView.IconTabProvider { private int[] tab_icons = {R.drawable.launcher_inactive, R.drawable.chat_inactive, R.drawable.t3_inactive, R.drawable.launcher, R.drawable.chat_active, R.drawable.t3_active }; 

Then in TabView.java I defined setIcon as follows

 public void setIcon(int resId) { setIcon(getContext().getResources().getDrawable(resId)); } 

Since getDrawable deprecated, I think this is the only problem, I do not see tab icons

 public class TabView extends LinearLayout { private ImageView mImageView; private TextView mTextView; public TabView(Context context) { this(context, null); } public TabView(Context context, AttributeSet attrs) { this(context, attrs, android.R.attr.actionBarTabStyle); } public TabView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); TypedValue outValue = new TypedValue(); context.getTheme().resolveAttribute(android.R.attr.actionBarTabTextStyle, outValue, true); int txtstyle = outValue.data; int pad = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources() .getDisplayMetrics()); mImageView = new ImageView(context); mImageView.setLayoutParams(new LayoutParams(100, LayoutParams.MATCH_PARENT));//hadi andaze icon taba mImageView.setScaleType(ScaleType.CENTER_INSIDE); mTextView = new TextView(context); mTextView.setLayoutParams(new LayoutParams(50, LayoutParams.MATCH_PARENT)); mTextView.setGravity(Gravity.CENTER); // mTextView.setCompoundDrawablePadding(pad); mTextView.setTextAppearance(context, txtstyle); ; this.addView(mImageView); // this.addView(mTextView); this.setLayoutParams(new LayoutParams(determineScreenDensity(), LayoutParams.MATCH_PARENT)); //this.setLayoutParams(new LayoutParams(150, LayoutParams.MATCH_PARENT)); } public void setIcon(int resId) { Log.i("Case+","" +resId); setIcon(getContext().getResources().getDrawable(resId)); } public void setIcon(Drawable icon) { if (icon != null) { mImageView.setVisibility(View.VISIBLE); mImageView.setImageDrawable(icon); } else { mImageView.setImageResource(View.GONE); Log.i("icon","null" +null); } } public void setText(int resId, int ico) { setText(getContext().getString(resId), ico); } public void setText(CharSequence text, int ico) { mTextView.setText(text); mTextView.setCompoundDrawablesWithIntrinsicBounds(ico, 0, 0, 0); ; } 

then from class TabBarView.java I called setIcon()

  public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { mSelectedTab = position; mOffset = positionOffset; invalidate(); if (delegatePageListener != null) { delegatePageListener.onPageScrolled(position, positionOffset, positionOffsetPixels); } if (position == 0) { tabView1.setIcon(((IconTabProvider) pager.getAdapter()).getPageIconResId(3)); tabView1.setScaleX(1); tabView1.setScaleY(1); tabView2.setIcon(((IconTabProvider) pager.getAdapter()).getPageIconResId(1)); // tabView2.setScaleX(2); // tabView2.setScaleY(2); tabView3.setIcon(((IconTabProvider) pager.getAdapter()).getPageIconResId(2)); tabView3.setScaleX(1.1f); tabView3.setScaleY(1.1f); } else if (position == 1) { tabView1.setIcon(((IconTabProvider) pager.getAdapter()).getPageIconResId(0)); tabView1.setScaleX(0.7f); tabView1.setScaleY(0.7f); // tabView1.setBackgroundResource(R.drawable.transparent_button_2); tabView2.setIcon(((IconTabProvider) pager.getAdapter()).getPageIconResId(4)); //tabView2.setBackgroundResource(R.drawable.transparent_round_button); //tabView2.setScaleX(2); //tabView2.setScaleY(2); tabView3.setIcon(((IconTabProvider) pager.getAdapter()).getPageIconResId(2)); tabView3.setScaleX(1.1f); tabView3.setScaleY(1.1f); // tabView3.setBackgroundResource(R.drawable.transparent_button_2); } else if (position == 2) { tabView1.setIcon(((IconTabProvider) pager.getAdapter()).getPageIconResId(0)); //tabView1.setBackgroundResource(R.drawable.transparent_button_2); tabView2.setIcon(((IconTabProvider) pager.getAdapter()).getPageIconResId(1)); //tabView2.setBackgroundResource(R.drawable.transparent_button_2); tabView3.setIcon(((IconTabProvider) pager.getAdapter()).getPageIconResId(5)); // tabView3.setBackgroundResource(R.drawable.transparent_round_button); tabView3.setScaleX(1.7f); tabView3.setScaleY(1.7f); /* LayoutParams params = new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT ); params.gravity = Gravity.CENTER; params.setMargins(40,0,0,0); tabView1.setLayoutParams(params); tabView2.setLayoutParams(params); tabView3.setLayoutParams(params); // tabView2.setPadding(20,0,20,0); //tabView3.setPadding(20,0,0,0); */ } } 

The solution in this question cannot be applied to my code. I appreciate if you can help me with this matter.

+5
source share
3 answers

This work in my case

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { holder.thumbnail.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_launcher_background, context.getTheme())); } else { holder.thumbnail.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_launcher_background)); } 

// as per your requirement

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { setIcon(context.getResources().getDrawable(R.drawable.ic_launcher_background, context.getTheme())); } else { setIcon(context.getResources().getDrawable(R.drawable.ic_launcher_background)); } 
0
source

Your TabView code looks pretty simple. The only alternative I can think of is to try calling invalidate() inside setIcon() after setting drawable as is will try to redraw the layout.

0
source

If it works below API level 23, then I think you have a problem using getDrawable, we have some options that can be used instead of getContext().getResources().getDrawable()

It can be used with ContextCompat

 ContextCompat.getDrawable(getActivity(), R.drawable.drawble_name); 

Try this as getDrawable with resources deprecated above API level 22.

0
source

Source: https://habr.com/ru/post/1274892/


All Articles