Reputation: 8959
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.
Upvotes: 0
Views: 809
Reputation: 55517
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;
}
Upvotes: 1
Reputation: 6641
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 );
}
}
Upvotes: 3
Reputation: 3790
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
Upvotes: 2