0

Hello guys right now I'm working on an app in which there are many quotes, in a TextView and I want to change the size of TextView on button click, like if Small button is clicked then the TextView should become small, if Large is clicked it should be large, etc. But each time I press the Button app force closes! What's the problem? Here's OnClickListener for the Button:

small = (Button) findViewById(R.id.small);
small.setOnClickListener(new View.OnClickListener() {
 public void onClick(View v) {
 TextView tv = (TextView)findViewById(R.id.quotes);
 tv.setTextSize(20);
 }
 });

P.S: The TextView and the Button are in different Activities.

asked Apr 14, 2015 at 17:57
3
  • TextView and Button are in different Activities ? Then it's not going to work because findViewById is hooked to layout of one Activity. Commented Apr 14, 2015 at 18:02
  • Any solution for this problem? Commented Apr 14, 2015 at 18:05
  • Please include more information. For example, it is possible to change what happens in one activity from another through using Intent.putExtra, LocalBroadcastManager and an EventBus library. It is also possible to set something like preferred font size in SharedPreferences. It all depends on your situation. Commented Apr 14, 2015 at 18:09

1 Answer 1

2

There is no way to change the TextView size by Button from one Activity in other Activity. You will get NullPointerException because the findViewById method will not find view and will return null. You should use SharedPreferences. You should save text size value when you click Button and read it in the second Activity.

In your setting Activity:

 small.setOnClickListener(new View.OnClickListener() {
 public void onClick(View v) {
 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(YourActivity.this.getBaseContext());
 sharedPreferences.edit()
 .putFloat("FONT_SIZE", 22)
 .apply();
 }
 });

In your activity where you have TextView

 TextView tv = (TextView)findViewById(R.id.quotes);
 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(YourActivity.this.getBaseContext());
 tv.setTextSize(sharedPreferences.getFloat("FONT_SIZE", 13/*DEFAULT VALUE*/));
Ajinkya More
4251 gold badge3 silver badges16 bronze badges
answered Apr 14, 2015 at 18:03
Sign up to request clarification or add additional context in comments.

4 Comments

Is there any solution for that? Please tell me because from 2 months I'm developing this app...Thank You very much!
You can also set your value in some singleton but your issue sounds like some setting. If you want to create something like setting it is a right solution
so this solution is right for you. Do you know how do this? I can give you some example if you want
Oh Thank you very much!!! I really appreciate if you give an example :-) Give me an example and I'll definitely accept it!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.