2

Here I put my code of 2 buttons.

I have to face one problem. Button works OK in MDPI view but I have a problem in the QVGA emulator.

In QVGA when I press the plus button then its size decreases y and when I press the minus button, no change occurs

if(v==im7)
{
    float size=tx3.getTextSize();
    float size1=tx2.getTextSize();

    Toast.makeText(this,"plus"+String.valueOf(size),2000).show();

    size=size+1;
    size1=size1+1;

    tx3.setTextSize(size);
    tx2.setTextSize(size1);                 
}

if(v==im8)
{
    /*float size=tx3.getTextSize();
    float size1=tx2.getTextSize();
    */
    Toast.makeText(this,"minus"+String.valueOf(size),2000).show();

    size=size-1;
    size1=size1-1;

    tx3.setTextSize(size);
    tx2.setTextSize(size1);         
}
forsvarir
  • 10,749
  • 6
  • 46
  • 77
sam_k
  • 5,983
  • 14
  • 76
  • 110

2 Answers2

0

So you want to increase and decrease size of text in a text box.. OK just follow this example..

Textview v= new Textview();

onButtonClickEvent()
{
    v.setTextSize(v.getTextSize()+5);
}
forsvarir
  • 10,749
  • 6
  • 46
  • 77
Siten
  • 4,515
  • 9
  • 39
  • 64
0

That will probably only work for mdpi screens. mdpi are the standard but ldpi and hdpi are scaled at 0.75% and 1.5% respectively.

Try this:

In your instance variables:

private DisplayMetrics metrics;
private int my_density;
private TextView your_textview;

In your onCreate, initialize your variables and find your view and set its onclicklistner (make sure to implement OnClickListener):

your_textview = (TextView)findViewById(R.id.txt_your_textview);
your_textview.setOnClickListener(this);
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
my_density = metrics.densityDpi;

Then the tricky part:

Depending on the screen density you need to scale the size you give setTextSize() accordingly. Really setTextSize() should take care of this but it doesn't. Use the method below as an example of how to increase the text for your TextView.

private void increaseFontSize(){
        float size = your_textview.getTextSize();
        int max_size = 30; //define your own max here
        if(size<=max_size){
            switch(my_density){
            case DisplayMetrics.DENSITY_LOW:
                size = (your_textview.getTextSize()+2) / 0.75f;
                break;

            case DisplayMetrics.DENSITY_MEDIUM:
                size = your_textview.getTextSize()+2;
                break;

            case DisplayMetrics.DENSITY_HIGH:
                size = (your_textview.getTextSize()+2) / 1.5f;
                break;

            default:
                break;
            }

            your_textview.setTextSize(size);
            your_textview.invalidate();

        }
        else{
            //beyond limit, don't do anything
        }
    }

Now in your onclick just call the method:

public void onClick(View view) {

    switch(view.getId()) {

    case(R.id.txt_your_textview):
        increaseFontSize();
        break;



}
MarcusB416
  • 70
  • 1
  • 5