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;
}