android - Unable to change TextView size on button click, why? -
hello guys right i'm working on app in there many quotes, in textview
, want change size of textview
on button click, if small button clicked textview
should become small, if large clicked should large, etc. each time press button
app force closes! what's problem? here's onclicklistener
button
:
small = (button) findviewbyid(r.id.small); small.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { textview tv = (textview)findviewbyid(r.id.quotes); tv.settextsize(20); } });
p.s: textview
, button
in different activities
.
there no way change textview
size button
1 activity
in other activity
. nullpointerexception
because findviewbyid method not find view , return null. should use sharedpreferences
. should save text size value when click button
, read in second activity
.
in setting activity:
small.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { sharedpreferences sharedpreferences = preferencemanager.getdefaultsharedpreferences(youractivity.this.getbasecontext()); sharedpreferences.edit() .putfloat("font_size", 22) .apply(); } });
in activity have textview
textview tv = (textview)findviewbyid(r.id.quotes); sharedpreferences sharedpreferences = preferencemanager.getdefaultsharedpreferences(youractivity.this.getbasecontext()); tv.settextsize(sharedpreferences.getfloat("font_size", 13/*default value*/));
Comments
Post a Comment