I am trying to create a an android app with multiple textviews that when clicked can change a property of the textview such as font size. I wanted to know if there was a way to do this with one onclick method and only one onclick listener. I have the android:onClick="Click" set to a click function for all the text views.
Asked
Active
Viewed 258 times
-1
-
To answer your question keep a list of all your TextViews and update them in your `Click()` method. However, if you only want to change a cosmetic trait like using a larger font size for your entire app, you should use Themes. – Sam Jan 22 '18 at 23:01
-
I meant changing the font size of just the textview that was clicked. I know there is a generic way of doing this using the view that is passed into to the click method. I just don't know how. – Ahad Memon Jan 22 '18 at 23:05
2 Answers
0
You will have to add android:onClick="onClick"
to all textviews
Then in your activity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view) {
if (view instanceOf TextView) {
((TextView)view).setTextSize(14);
}
}
}

MoGa
- 635
- 6
- 14
-
1Why not make it `if (view instanceof TextView) { … }` That way you make it “safe”. – Martin Marconcini Jan 22 '18 at 23:22
0
changing the font size of just the textview that was clicked
See setTextSize():
public void Click(View v) {
((TextView) v).setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
}
Be wary of using getTextSize()
to set the new text size: TextView.setTextSize behaves abnormally - How to set text size of textview dynamically for different screens

Sam
- 86,580
- 20
- 181
- 179