I want to add icons on my TabView, but I am stuck because I can't use getResources()
in a custom TabAdapter. I already try to use getAplicationContext().getResources, getActivity.getResources()
, but i am still seeing the message "cannot resolve method"
. Someone can help me?
package com.glapps.mobile.codifynotes.Adapter;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import com.glapps.mobile.codifynotes.Activity.MainActivity;
import com.glapps.mobile.codifynotes.Fragment.CodifyFragment;
import com.glapps.mobile.codifynotes.Fragment.Configuracoes.ConfiguracoesFragment;
import com.glapps.mobile.codifynotes.Fragment.DecodifyFragment;
import com.glapps.mobile.codifynotes.R;
public class TabAdapter extends FragmentStatePagerAdapter {
private String[] tituloAba = {"CODIFICAR", "DECODIFICAR", "CONFIGURAÇÕES"};
private int[] imageResId = {
R.drawable.ic_action_lock_closed,
R.drawable.ic_action_lock_closed,
R.drawable.ic_action_settings_white
};
public TabAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position){
case 0:
fragment = new CodifyFragment();
break;
case 1:
fragment = new DecodifyFragment();
break;
case 2:
fragment = new ConfiguracoesFragment();
}
return fragment;
}
@Override
public int getCount() {
return tituloAba.length;
}
@Override
public CharSequence getPageTitle(int position) {
return tituloAba[position];
Drawable image = getResources().getDrawable(imageResId[position]);
image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
SpannableString sb = new SpannableString(" ");
ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return sb;
}
}
Error image
Thank you!
create a variable context
private Context context;
//in the Constructor, pass the context in the parametres
public TabAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
and you can use it by :
context.getResources().
Try this way
Drawable image = getActivity().getResources().getDrawable(imageResId[position]);
or
Drawable image = getContext().getResources().getDrawable(imageResId[position]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With