Can not change value of textsize in ListView in different fragment class
I want to change text size in ListView
in different fragment class. I declared the variable in other class as
public static TextView title;
then I change the value of that textsize by using class fragment.
title.setTextSize(30);
when I click back to other activity it doesn't work.
Here is my code:
public class Setting extends AppCompatActivity {
private RadioGroup Text_Size;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
Text_Size = (RadioGroup) findViewById(R.id.radiogroup);
Text_Size.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.small:
HotNewsFrag.title.setTextSize(15);
break;
case R.id.medium:
HotNewsFrag.title.setTextSize(20);
break;
case R.id.big:
HotNewsFrag.title.setTextSize(30);
break;
default:
break;
}
}
});
LinearLayout back = (LinearLayout) findViewById(R.id.back1);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent setting = new Intent(Setting.this, MainActivity.class);
startActivity(setting);
}
});
}
}
Here is my fragment:
public class HotNewsFrag extends Fragment {
private View view;
private ListView listOfNews;
ListViewAdapter adapter;
ArrayList<HotNews> arraylist1;
public static String newsid;
public static TextView title;
}
The way you are handling this is completely wrong.
BIG PROBLEM: defining the text View as Static would cause memory leak and prevent your view and activity from being garbage collected.
here are two approaches :
FIRST : SHARED PREFERENCES AND ONRESUME
you can store the text size in a sharedPreference and in your activity or fragments, you override onResume and set text size to our textsview again by the value in sharedPreference which you stored before
SECOND : BROADCAST RECEIVERS
You can use broadcast receivers and register them wherever you like. whenever the user change the text size, you send local broadcast and change text size wherever you register that.
Text_Size.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
int txtSize=0;
switch (checkedId)
{
case R.id.small:
txtSize=15;
break;
case R.id.medium:
txtSize=20;
break;
case R.id.big:
txtSize=30;
break;
default:
break;
}
Setting.this.getSharedPreferences("MY_SHARED",0).edit().putInt("FONT_SIZE",txtSize).commit();
}
});
In your fragment
@Override
public void onResume() {
super.onResume();
title.setTextSize(getActivity().getSharedPreferences("MY_SHARED",0).getInt("FONT_SIZE",20));
ADAPTER_OF_YOUR_LISTVIEW.notifyAll();
}
如果你只是想让文本变得强调,请尝试使用SetTypeFace.BOLD