I want to make buttons to adjust my text. There are buttons for making the TextView bigger, smaller, back and forth between italic and bold.
I want to edit the TextView called "text"
In the code below, when I clicked the "butBig" or "butSmall" button, both button does the same thing it makes my text size increase but not by 5, it became really big. When I clicked on either button again, the TextView disappear like it is too big for the frame.
butBig.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
float size = text.getTextSize();
text.setTextSize(size + 5.0F);
}
});
butSmall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
float size = text.getTextSize();
text.setTextSize(size - 5.0F);
}
});
For Italic and Bold, my code here does work but sometimes when I clicked either of these buttons, the app crashed for no reason..
butItalic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (text.getTypeface().getStyle() == Typeface.BOLD) {
text.setTypeface(null, Typeface.BOLD_ITALIC);
} else if (text.getTypeface().getStyle() == Typeface.ITALIC) {
text.setTypeface(null, Typeface.NORMAL);
} else if (text.getTypeface().getStyle() == Typeface.NORMAL) {
text.setTypeface(null, Typeface.ITALIC);
} else if (text.getTypeface().getStyle() == Typeface.BOLD_ITALIC) {
text.setTypeface(null, Typeface.BOLD);
}
}
});
butBold.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (text.getTypeface().getStyle() == Typeface.ITALIC) {
text.setTypeface(null, Typeface.BOLD_ITALIC);
} else if (text.getTypeface().getStyle() == Typeface.BOLD) {
text.setTypeface(null, Typeface.NORMAL);
} else if (text.getTypeface().getStyle() == Typeface.NORMAL) {
text.setTypeface(null, Typeface.BOLD);
} else if (text.getTypeface().getStyle() == Typeface.BOLD_ITALIC) {
text.setTypeface(null, Typeface.ITALIC);
}
}
});
Please help me with this
Thank you
Edit: Here's my logcat :)
--------- beginning of crash 11-05 05:34:17.581 2416-2416/com.example.textadjust E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.textadjust, PID: 2416 java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Typeface.getStyle()' on a null object reference at com.example.textadjust.MainActivity$3.onClick(MainActivity.java:57) at android.view.View.performClick(View.java:5198) at android.view.View$PerformClick.run(View.java:21147) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 11-05 05:34:19.734 2416-2416/? I/Process: Sending signal. PID: 2416 SIG: 9
What you should know so far is that the Size of TextSize your are doing using the method:
text.setTextSize(size + 5.0F);
The size units is Scalled Pixels and if you are wondering or want to use the size in dp
here is the method to change pixels to dp
:
public int getDp(int px) {
DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
int dp = Math.round(px / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
return dp;
}
Add this method in your clas for that! But alternatively ( I recommend ) you can use the method:
setTextSize(int unit, float size);
Where the parameter int unit
here i can take values like as given by this stackoverflow answer :
TypedValue.COMPLEX_UNIT_PX //Pixels
TypedValue.COMPLEX_UNIT_SP //Scaled Pixels
TypedValue.COMPLEX_UNIT_DIP //Device Independent Pixels
And for the other question add some logs for the crash! to figure out what might be the error and I will edit my answer to add that!!
But my suggestion so far reduce the conditions and use a switch
instead of if
and else
s and make sure the TextView
has a text before calling those methods. Add some logs to be sure whats going on!
EDIT FOR THE CRASH
As I guessed the cause for the crash is NullPointerException
. Which may be caused by your variable of TextView
being null
. Possible causes is it is not assigned just in onCreate()
or it has no text to display when assigned (I guess).
SOLUTION :
Because you are going to use the TextView
in your whole class declare it as a class variable and assign it onCreate
if its Activity
or onCreateView
if Fragment
: Just inside your class before any method:
private TextView text;
Then inside onCreate
assign it as:
text=(TextView)findViewById(R.id.your_text_view_id); //Replace with a real id
And set a default text or Typeface
in xml
or in java
code. Now you can access the TextView
variable everywhere in the class it will not be null
.
And as a tip again for performance and readability use switch
instead of if
and else
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.