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.
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 –