0

I'm trying to increase the size of a particular textview from within the app. I would like to do it via a menu item selection but am having problems. I have tried the following:

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    // Create an options menu for the activity
    super.onCreateOptionsMenu( menu );

    incrseTxtMenu = menu.add( 0,4,0,"Increase Text Size" );
    incrseTxtMenu.setIcon( R.drawable.ic_menu_plus );
    incrseTxtMenu.setOnMenuItemClickListener( new MenuItem.OnMenuItemClickListener() 
    {
        @Override
        public boolean onMenuItemClick(MenuItem item) 
        {
            // handler.sendMessage( handler.obtainMessage() );
            TextView tempTxt = getTextView();
            tempTxt.setTextSize( 25 );

            return true;
        }
    });

    return true;
}

But this is throwing a null pointer exception. I also tried just using intro.setTextSize() but it throws the same error. How can I access the textview from within this menu item?

**Update

//Method used to fetch the textview
public TextView getTextView()
{
    return intro;
}

And the error from log cat:

AndroidRuntime FATAL EXCEPTION: main
java.lang.NullPointerException
at android.omni.Artist_activity$1.handleMessage( Artist_activity.java:32 )

Also btw - I'm trying to use a handler to update the GUI - am I correct in assuming that this is necessary right?

**Update 2 XML CODE

   <?xml version="1.0" encoding="utf-8"?>
    <ScrollView
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id = "@+id/tab_one_top_level"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">

      <LinearLayout
      android:orientation = "vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">

      <TextView

    android:id = "@+id/faq_Intro"
    android:layout_width = "wrap_content"
    android:layout_height = "wrap_content"
    android:text = "@string/faq_Intro"
    android:typeface = "monospace"
    android:textStyle = "bold"
    android:paddingBottom = "12dp"

      />

      <TextView

    android:id = "@+id/faq_Intro_Info"
    android:layout_width = "wrap_content"
    android:layout_height = "wrap_content"
    android:text = "@string/faq_Intro_Info"
    android:textSize = "10dp"
    android:typeface = "monospace"
    android:textStyle = "bold"

      />

      </LinearLayout>
</ScrollView>

Any thoughts?

My Code Solution

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    // Create an options menu for the activity
    super.onCreateOptionsMenu( menu );

    incrseTxtMenu = menu.add( 0,1,0,"Increase Text Size" );
    incrseTxtMenu.setIcon( R.drawable.ic_menu_plus );

    decrseTxtMenu = menu.add( 0,2,0,"Decrease Text Size" );
    decrseTxtMenu.setIcon( R.drawable.ic_menu_negate );

    return true;

}

@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
    // Increase size menu item
    if( item.getItemId() == 1 )
    {
        intro.setTextSize( myIntroSize += 5 );
        introInfo.setTextSize( myIntroInfoSize += 5 );

    }
    // Derease size menu item
    else if( item.getItemId() == 2 )
    {
        intro.setTextSize( myIntroSize -= 5 );
        introInfo.setTextSize( myIntroInfoSize -= 5 );
    }
    return true;
}

The onCreate() method simply initialises the textview as before. Oh and the values of myIntroSize and myIntroInfoSize can be whatever you want.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Katana24
  • 8,706
  • 19
  • 76
  • 118
  • what is getTextView()? Can you please post the error from the Log Cat? – Arif Nadeem Apr 08 '12 at 15:27
  • what is htese lines TextView tempTxt = getTextView(); in your code – Shankar Agarwal Apr 08 '12 at 15:28
  • That was an experiment which just returns a textview - I have updated my code above – Katana24 Apr 08 '12 at 15:37
  • So what is on Line 32 of Artist_activity.java? Where is the `intro` variable instantiated? – Blundell Apr 08 '12 at 15:43
  • In the onCreate() method like this: intro = (TextView)findViewById( R.id.faq_intro ), you know the usual way – Katana24 Apr 08 '12 at 15:45
  • And the id `faq_intro` is in your XML? What is on line 32? Thats your NPE – Blundell Apr 08 '12 at 15:47
  • You want to change the textsize of a TextView when the user tabs on a menu item? No you do not need a Handler. You need a handler when you want to run code outside of the UI thread and get notified to update the UI when that code finished. Your Exception comes from your Handler – 207 Apr 08 '12 at 15:51
  • I tried removing the handler part and just using intro.setTextSize() and get the same NPE, therefore it's not the handler doing it – Katana24 Apr 08 '12 at 16:05
  • @Katana24 As you can see in your stack trace your exception is caused by your `handleMessage()` method. When you removed the handler..that exception can not been thrown from that method anymore because that method does not exist anymore. So you can't get "the same NPE" – 207 Apr 08 '12 at 16:26
  • well i didn't mean the exact same one - i just meant a NPE in general. My bad – Katana24 Apr 08 '12 at 16:27
  • No problem. Edit your post and show us the NPE you get without using Handler in your code – 207 Apr 08 '12 at 16:29
  • I found the solution - it's my fault for not providing enough info. This particular activity is a TabActivity. The view exists within one of the tabs, or so I thought. I applied the same layout to both tabs but when I changed the code slightly and clicked on the other tab it was updating there and not on the first tab. Not really sure as to why though... – Katana24 Apr 08 '12 at 16:33
  • I have updated my code above to include my final solution to this though I have accepted another answer... Well tried to but couldn't get at it :D – Katana24 Apr 08 '12 at 17:09

3 Answers3

3

Please try the following code and tell me if it works. I don't know what is wrong, it's just based on a few hunches -

    @Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    // Create an options menu for the activity
    super.onCreateOptionsMenu( menu );

    incrseTxtMenu = menu.add( 0,4,0,"Increase Text Size" );
    incrseTxtMenu.setIcon( R.drawable.ic_menu_plus );
    return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(item.getItemId() == 4){
         TextView tempTxt =  (TextView)findViewById( R.id.faq_intro);
            tempTxt.setTextSize( 25 );
    }

}
Abhinav Manchanda
  • 6,546
  • 3
  • 39
  • 46
  • Yes mate that works fine though I would suggest adding to the existing value in setTextSize instead of just 25. Thanks for the help mate :D – Katana24 Apr 08 '12 at 17:00
2

This snippet is working fine with me, try it. I guess your problem in how you get the TextView itself. " you should use findViewbyId(int id) "

     @Override
            public boolean onCreateOptionsMenu(Menu menu) {
            // Create an options menu for the activity
            super.onCreateOptionsMenu( menu );

            MenuItem incrseTxtMenu = menu.add( 0,4,0,"Increase Text Size" );
            incrseTxtMenu.setIcon( android.R.drawable.btn_plus );
            incrseTxtMenu.setOnMenuItemClickListener( new MenuItem.OnMenuItemClickListener() 
            {
                @Override
                public boolean onMenuItemClick(MenuItem item) 
                {
                    TextView tempTxt = (TextView) findViewById(R.id.text);
                    tempTxt.setTextSize( 25 );

                    return true;
                }
            });

        return true;
    }

Also its better to handle the menu selections using this function:

  @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        return super.onOptionsItemSelected(item);
    }

not using setOnMenuItemClickListener

edit: I am not using any handlers

Hazem Farahat
  • 3,790
  • 2
  • 26
  • 42
  • Why not use setOnMenuItemClickListener? Can you provide any resources to verify this? – Katana24 Apr 08 '12 at 16:36
  • 1
    My reference: the android documentation: http://developer.android.com/reference/android/view/MenuItem.html Scroll down to setOnMenuItemClickListener you will read "it is more efficient and easier to use onOptionsItemSelected(MenuItem)" – Hazem Farahat Apr 08 '12 at 16:40
  • Cool - though I wish they would give a reason. Thanks mate – Katana24 Apr 08 '12 at 16:57
  • Just different implementations for different people I guess. – Jared Burrows Apr 08 '12 at 17:00
1

I believe your problem is when you are calling "getTextView();"

TextView tempTxt = getTextView();
tempTxt.setTextSize( 25 );

Also, when you set the size of your text, you should use this to make sure they are pixels:

tempTxt.setTextSize(TypedValue.COMPLEX_UNIT_PX, 25);

Read here: TextView.setTextSize behaves abnormally - How to set text size of textview dynamically for different screens Android TextView setTextSize incorrectly increases text size

It is best if objects that are created have an ID from the XML by using "findViewById":

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Create an options menu for the activity
super.onCreateOptionsMenu( menu );

MenuItem incrseTxtMenu = menu.add( 0,4,0,"Increase Text Size" );
incrseTxtMenu.setIcon( android.R.drawable.btn_plus );
incrseTxtMenu.setOnMenuItemClickListener( new MenuItem.OnMenuItemClickListener() {
    @Override
    public boolean onMenuItemClick(MenuItem item) {
    TextView text = (TextView) findViewById(R.id.faq_Intro); //faq_Intro from XML
    tempTxt.setTextSize(TypedValue.COMPLEX_UNIT_PX, 25); //corrected

    return true;
    }
});

return true;
}

Also instead of using "OnMenuItemCLickListener", you can easily use "onOptionsItemSelected" with a case switch:

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){

    case R.id.increasesize:  
         //some code
         break;     

    case R.id.exit:  
        finish();
        break;         

    default:
        return true;
    }
Community
  • 1
  • 1
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185