darshan
darshan

Reputation: 4579

Issue in Increasing/Decreasing Text Size

I am having 2 buttons in my app layout & 1 EditText...

I want to increase the EditText's text size with the Plus button & Decrease with the Minus button... But when I click the Minus button, instead of decreasing the size, it INCREASES the size...

here's my code -

plus.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){

                float org_size = ed.getTextSize();
                Log.d("Size", "Original Float Value" + org_size);

                float pixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources() .getDisplayMetrics());
                float new_size = org_size + pixels;
                if(org_size >= 90f){
                    Toast.makeText(MainActivity.this, "That's the Max Size available!" , Toast.LENGTH_SHORT).show();

                }
                    else{
                ed.setTextSize(new_size);
                }
            }
        });

    minus.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){

                float org = ed.getTextSize();
                Log.d("Size", "Original Float Value" + org);

                float pix = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources() .getDisplayMetrics());
                float snew = org - pix;

                if(org <= 30f){
                    Toast.makeText(MainActivity.this, "That's the Minimum Size available!" , Toast.LENGTH_SHORT).show();

                }
                else{
                    ed.setTextSize(snew);
                }
            }
        });

Upvotes: 0

Views: 43

Answers (1)

Clint Paul
Clint Paul

Reputation: 313

Please change your code like this and check again,

 minus.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){

            float org = ed.getTextSize();
            Log.d("Size", "Original Float Value" + org);

            float scaledDensity = getApplicationContext().getResources().getDisplayMetrics().scaledDensity;
            float sp =  org/scaledDensity;



            float pix = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources() .getDisplayMetrics());
            float snew = sp - pix;

            Log.d("Size", "snew" + snew);

            if(org <= 30f){
                Toast.makeText(MainActivity.this, "That's the Minimum Size available!" , Toast.LENGTH_SHORT).show();

            }
            else{
                ed.setTextSize(snew);
            }
        }
    });

Upvotes: 1

Related Questions