1

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

Triniel
  • 41
  • 3

1 Answers1

0

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 elses 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.

Xenolion
  • 12,035
  • 7
  • 33
  • 48
  • I have edited my answer please check and comment if you still get some errors! @Triniel – Xenolion Nov 05 '17 at 11:53
  • I already used text=(TextView)findViewById(R.id.your_text_view_id); it doesn't work :( I still get the same NullPointerException. Thanks for the text size, it works perfectly!! Also, do you have any way to limit the font size when I increase it,? – Triniel Nov 05 '17 at 12:46
  • You will have to do that `onCreate` and also do `text.setText("anything");` and try something like `text.setTypeface(something);` in the same `onCreate` the issue is to make some default properties you cant get the typeface if it is not set yet or textview has no size! – Xenolion Nov 05 '17 at 12:57
  • Oh for the issue of limiting the font size or text size is using the the method `text.getTextSize()` the compare with your current size before increasing but it will return the value in `pixels` and use it to compare if the limit is exceeded before increasing. You can do the same in limiting down! – Xenolion Nov 05 '17 at 13:01
  • You can reply anytime! – Xenolion Nov 05 '17 at 13:12
  • I did use `text.getTextSize()` before but I didn't know it is in pixels, Thank you so much :) – Triniel Nov 05 '17 at 15:44
  • Oooh that's a it @Triniel all the best **Happy Coding!**. – Xenolion Nov 05 '17 at 15:45
  • I changed all my if-else to `switch (text.getTypeface().getStyle()) { case (Typeface.BOLD): text.setTypeface(null, Typeface.BOLD_ITALIC);` It doesn't work :( Is this the proper way of doing it? – Triniel Nov 05 '17 at 15:47
  • It should work but may be there is an error somewhere else! that is the correct way of doing it! – Xenolion Nov 05 '17 at 15:51
  • If you find a number of errors in that way there may be something or just go back to `if` and `else` if you cant see the problem! – Xenolion Nov 05 '17 at 15:56
  • Ctrl+Z or Cmd+Z – Xenolion Nov 05 '17 at 15:57
  • Thank you so muchh !! :You're the best :D – Triniel Nov 05 '17 at 15:57
  • Okay thank you too @Triniel **Happy Coding!** again! – Xenolion Nov 05 '17 at 16:02