- Hello friends welcome to Tech Punch.In this blog we will learn to adjust text size or color on button click
- Create new project
- Go to activity xml file
- Drag and drop textView on canvas
- Drag and drop three buttons on canvas
- Give name to first button as Increment or ‘+’.
- Give name to second button as decrement or ‘-’.
- Now go to java file
- Write onclick method for ‘+’ button as increment
- Inside that method get current size of text by using getTextSize() method.
- As shown in code below.
- After that increase text size using setTextSize() method.
- Write same function for ‘– ‘as decrement
- Inside that method do same this as we write in increment and instead of increasing size decrease the size.
- For color method firstly declare three integer variables as ‘r’, ’g’, ’b’.
- Set color to text by using setTextColor() method.
- And increase the value of both ‘r’, ’g’, ’b’.
public class MainActivity extends AppCompatActivity { TextView t1; int r=0,g=0,b=0; float size; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); t1=findViewById(R.id.textView2); } public void increment(View v){ size=t1.getTextSize(); if(size<=150){ t1.setTextSize(TypedValue.COMPLEX_UNIT_PX,size+4); } } public void decrement(View v){ size=t1.getTextSize(); if(size>=20){ t1.setTextSize(TypedValue.COMPLEX_UNIT_PX,size-4); } } public void col(View v){ t1.setTextColor(Color.rgb(r,g,b)); r+=15; g+=30; b+=45; } }
No comments:
Post a Comment