Text Size Change in Adapter class updates text size in all activities
Text Size Change in Adapter class updates text size in all activities
The following is my WordAdapter
class
WordAdapter
public class WordAdapter extends ArrayAdapter<Word> {
/** Resource ID for the background color for this list of words */
private int mColorResourceId;
/**
* Create a new {@link WordAdapter} object.
*
* @param context is the current context (i.e. Activity) that the adapter is being created in.
* @param words is the list of {@link Word}s to be displayed.
* @param colorResourceId is the resource ID for the background color for this list of words
*/
public WordAdapter(Context context, ArrayList<Word> words, int colorResourceId) {
super(context, 0, words);
mColorResourceId = colorResourceId;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// Check if an existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
// Get the {@link Word} object located at this position in the list
final Word currentWord = getItem(position);
// Find the TextView in the list_item.xml layout with the ID miwok_text_view.
TextView miwokTextView = (TextView) listItemView.findViewById(R.id.miwok_text_view);
// Get the Miwok translation from the currentWord object and set this text on
// the Miwok TextView.
miwokTextView.setText(currentWord.getMiwokTranslationId());
// Find the TextView in the list_item.xml layout with the ID default_text_view.
TextView defaultTextView = (TextView) listItemView.findViewById(R.id.default_text_view);
// Get the default translation from the currentWord object and set this text on
// the default TextView.
defaultTextView.setText(currentWord.getDefaultTranslationId());
TextView onClickTextView = (TextView) listItemView.findViewById(R.id.miwok_text_view_on_click);
onClickTextView.setText((currentWord.getTextOnClickId()));
final ImageView playIcon = (ImageView) listItemView.findViewById(R.id.play_icon);
final ImageView playIconPlaying = (ImageView) listItemView.findViewById(R.id.play_icon_playing);
// Find the ImageView in the list_item.xml layout with the ID image.
final ImageView imageView = (ImageView) listItemView.findViewById(R.id.image);
// Check if an image is provided for this word or not
if (currentWord.hasImage()) {
// If an image is available, display the provided image based on the resource ID
imageView.setImageResource(currentWord.getImageResourceId());
// Make sure the view is visible
imageView.setVisibility(View.VISIBLE);
} else {
// Otherwise hide the ImageView (set visibility to GONE)
imageView.setVisibility(View.GONE);
}
if (Activity1.isActive == true){
onClickTextView.setTextSize(55);
} else {
onClickTextView.setTextSize(35);
}
if (Activity3.isActive == true){
onClickTextView.setTextSize(55);
} else {
onClickTextView.setTextSize(35);
}
// add a tag to determine the position of the view, when the view is clicked.
imageView.setTag(position);
// Set the theme color for the list item
View textContainer = listItemView.findViewById(R.id.text_container);
// Find the color that the resource ID maps to
int color = ContextCompat.getColor(getContext(), mColorResourceId);
// Set the background color of the text container View
textContainer.setBackgroundColor(color);
// Return the whole list item layout (containing 2 TextViews) so that it can be shown in
// the ListView.
// ImageView imageView = (ImageView) listItemView.findViewById(R.id.list_item);
imageView.setOnClickListener(new ImageView.OnClickListener() {
@Override
public void onClick(View view) {
if(currentWord.isFlag){
currentWord.isFlag = false;
Word.flaggedPosition = -1;
}else{
if (Word.flaggedPosition >=0) {
Word.adapter.getItem(Word.flaggedPosition).isFlag = false;
}
currentWord.isFlag = true;
Word.flaggedPosition = position;
}
notifyDataSetChanged();
}
});
if(currentWord.isFlag)
{
imageView.setAlpha(60);
onClickTextView.setVisibility(View.VISIBLE);
}
else
{
imageView.setAlpha(255);
onClickTextView.setVisibility(View.INVISIBLE);
}
return listItemView;
}
}
its the onClickTextView
TextView is which i wish to change in Activities...
onClickTextView
the activity2
has no change in the textView size which is 35sp
activity2
the following is my VarnamalaFragment class
public class VarnamalaFragment extends Fragment {
/** to check whether Acitivity is Active */
static boolean isActive = true;
/** Stores the last selected position */
private int lastSelectedPosition = -1;
/** Handles playback of all the sound files */
private MediaPlayer mMediaPlayer;
/** Handles audio focus when playing a sound file */
private AudioManager mAudioManager;
// Create a list of words
private ArrayList<Word> words = new ArrayList<Word>();
/**
* This listener gets triggered whenever the audio focus changes
* (i.e., we gain or lose audio focus because of another app or device).
*/
private AudioManager.OnAudioFocusChangeListener mOnAudioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() {
@Override
public void onAudioFocusChange(int focusChange) {
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT ||
focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
// The AUDIOFOCUS_LOSS_TRANSIENT case means that we've lost audio focus for a
// short amount of time. The AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK case means that
// our app is allowed to continue playing sound but at a lower volume. We'll treat
// both cases the same way because our app is playing short sound files.
// Pause playback and reset player to the start of the file. That way, we can
// play the word from the beginning when we resume playback.
mMediaPlayer.pause();
mMediaPlayer.seekTo(0);
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
// The AUDIOFOCUS_GAIN case means we have regained focus and can resume playback.
mMediaPlayer.start();
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
// The AUDIOFOCUS_LOSS case means we've lost audio focus and
// Stop playback and clean up resources
releaseMediaPlayer();
}
}
};
/**
* This listener gets triggered when the {@link MediaPlayer} has completed
* playing the audio file.
*/
private MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
// Now that the sound file has finished playing, release the media player resources.
if(lastSelectedPosition != -1){
Word lastWord = words.get(lastSelectedPosition);
lastWord.isPlaying = false;
lastSelectedPosition = -1;
Word.adapter.notifyDataSetChanged();
}
releaseMediaPlayer();
}
};
public VarnamalaFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.word_list, container, false);
// Create and setup the {@link AudioManager} to request audio focus
mAudioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
words.add(new Word(R.string.letter_a, R.string.miwok_letter_a,
R.drawable.a_se_anaar,R.string.miwok_letter_a_only, R.raw.a_se_anaar_audio));
words.add(new Word(R.string.letter_aa, R.string.miwok_letter_Aa,
R.drawable.aa_se_aam, R.string.miwok_letter_Aa_only, R.raw.aa_se_aam_audio));
words.add(new Word(R.string.letter_e, R.string.miwok_letter_E,
R.drawable.e_se_emli,R.string.miwok_letter_E_only, R.raw.e_se_emli_audio));
// Create an {@link WordAdapter}, whose data source is a list of {@link Word}s. The
// adapter knows how to create list items for each item in the list.
Word.adapter = new WordAdapter(getActivity(), words, R.color.category_varnamala_vowels);
// Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
// There should be a {@link ListView} with the view ID called list, which is declared in the
// word_list.xml layout file.
ListView listView = (ListView) rootView.findViewById(R.id.list);
// Make the {@link ListView} use the {@link WordAdapter} we created above, so that the
// {@link ListView} will display list items for each {@link Word} in the list.
listView.setAdapter(Word.adapter);
// Set a click listener to play the audio when the list item is clicked on
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// Release the media player if it currently exists because we are about to
// play a different sound file
releaseMediaPlayer();
// Get the {@link Word} object at the given position the user clicked on
Word word = words.get(position);
word.isPlaying = true;
if(lastSelectedPosition != -1){
Word lastWord = words.get(lastSelectedPosition);
lastWord.isPlaying = false;
}
lastSelectedPosition = position;
Word.adapter.notifyDataSetChanged();
// Request audio focus so in order to play the audio file. The app needs to play a
// short audio file, so we will request audio focus with a short amount of time
// with AUDIOFOCUS_GAIN_TRANSIENT.
int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener,
AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
// We have audio focus now.
// Create and setup the {@link MediaPlayer} for the audio resource associated
// with the current word
mMediaPlayer = MediaPlayer.create(getActivity(), word.getAudioResourceId());
// Start the audio file
mMediaPlayer.start();
word.isPlaying = true;
Word.adapter.notifyDataSetChanged();
// Get the value of currentVolume
int currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
// If currentVolume is set to 0, show Toast
if (currentVolume ==0){
Toast.makeText(getContext(), "Please Turn the Volume Up ", Toast.LENGTH_SHORT).show();
}
// Setup a listener on the media player, so that we can stop and release the
// media player once the sound has finished playing.
mMediaPlayer.setOnCompletionListener(mCompletionListener);
}
}
});
return rootView;
}
@Override
public void onStart() {
super.onStart();
isActive = true;
}
@Override
public void onStop() {
super.onStop();
isActive = false;
// When the activity is stopped, release the media player resources because we won't
// be playing any more sounds.
releaseMediaPlayer();
}
/**
* Clean up the media player by releasing its resources.
*/
private void releaseMediaPlayer() {
// If the media player is not null, then it may be currently playing a sound.
if (mMediaPlayer != null) {
// Regardless of the current state of the media player, release its resources
// because we no longer need it.
mMediaPlayer.release();
// Set the media player back to null. For our code, we've decided that
// setting the media player to null is an easy way to tell that the media player
// is not configured to play an audio file at the moment.
mMediaPlayer = null;
// Regardless of whether or not we were granted audio focus, abandon it. This also
// unregisters the AudioFocusChangeListener so we don't get anymore callbacks.
mAudioManager.abandonAudioFocus(mOnAudioFocusChangeListener);
}
}
}
The problem i face is the app changes the text size to 55sp in all the activities including activity2
for which i have no IF..ELSE
block...
activity2
IF..ELSE
How do i change the textSize to 55sp in activity1
and activity3
in my Adapter
class
activity1
activity3
Adapter
I have logged and checked the logcat
for correct updation of the isActive
value to true
and false
when the activity1
and activity3
is active and not active....
logcat
isActive
true
false
activity1
activity3
@Sahil please can you explain in a bit easy way... i've updated my Adapter class in my question...
– amit
Jul 1 at 9:24
1 Answer
1
In your Case, You can check using class name. Like,
context.getClass().getSimpleName()
which return value "Activity1" .
updated class >>
public class WordAdapter extends ArrayAdapter<Word> {
private int mColorResourceId;
private Context mContext;
public WordAdapter(Context context, ArrayList<Word> words, int colorResourceId) {
super(context, 0, words);
mColorResourceId = colorResourceId;
mContext = context;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
final Word currentWord = getItem(position);
TextView miwokTextView = (TextView) listItemView.findViewById(R.id.miwok_text_view);
miwokTextView.setText(currentWord.getMiwokTranslationId());
TextView defaultTextView = (TextView) listItemView.findViewById(R.id.default_text_view);
defaultTextView.setText(currentWord.getDefaultTranslationId());
TextView onClickTextView = (TextView) listItemView.findViewById(R.id.miwok_text_view_on_click);
onClickTextView.setText((currentWord.getTextOnClickId()));
final ImageView playIcon = (ImageView) listItemView.findViewById(R.id.play_icon);
final ImageView playIconPlaying = (ImageView) listItemView.findViewById(R.id.play_icon_playing);
final ImageView imageView = (ImageView) listItemView.findViewById(R.id.image);
if (currentWord.hasImage()) {
imageView.setImageResource(currentWord.getImageResourceId());
imageView.setVisibility(View.VISIBLE);
} else {
imageView.setVisibility(View.GONE);
}
if (mContext.getClass().getSimpleName().equalsIgnoreCase("Activity1") || mContext.getClass().getSimpleName().equalsIgnoreCase("Activity3")) {
onClickTextView.setTextSize(55);
} else {
onClickTextView.setTextSize(35);
}
imageView.setTag(position);
View textContainer = listItemView.findViewById(R.id.text_container);
int color = ContextCompat.getColor(getContext(), mColorResourceId);
textContainer.setBackgroundColor(color);
imageView.setOnClickListener(new ImageView.OnClickListener() {
@Override
public void onClick(View view) {
if (currentWord.isFlag) {
currentWord.isFlag = false;
Word.flaggedPosition = -1;
} else {
if (Word.flaggedPosition >= 0) {
Word.adapter.getItem(Word.flaggedPosition).isFlag = false;
}
currentWord.isFlag = true;
Word.flaggedPosition = position;
}
notifyDataSetChanged();
}
});
if (currentWord.isFlag) {
imageView.setAlpha(60);
onClickTextView.setVisibility(View.VISIBLE);
} else {
imageView.setAlpha(255);
onClickTextView.setVisibility(View.INVISIBLE);
}
return listItemView;
}
}
my textView is declared in my
Adapter
class... where to correctly declare and access the textView inside the handleMessage
function...@ABK– amit
Jul 1 at 6:29
Adapter
handleMessage
my textView is defined in my
Adapter
as TextView textView = (TextView) listItemView.findViewById(R.id.textView)
which is inside a listView
– amit
Jul 1 at 6:37
Adapter
TextView textView = (TextView) listItemView.findViewById(R.id.textView)
listView
post your full code.
– ABK
Jul 1 at 7:04
posted full code of the WordAdapter class... @ABK
– amit
Jul 1 at 7:22
Please try my updated Answer.
– ABK
Jul 1 at 8:25
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
For start, Pass the context of activity in adapter's contructer, then check for instance (if mContext instanceof Activity1) , then make a static method in class where you have textview to be updated and pass textSize as parameter. call this method from adapter.
– Sahil
Jul 1 at 5:51