Hoping to change widget attributes by selecting a menu item

by midicool » Thu, 05 Mar 2009 11:47:18 GMT

Sponsored Links

I am writing a program that involves a menu. Once you press the menu
and then select "Left",  it's supposed to change the attributes of
myButton12, an ImageButton.

However, I am getting a "myButton12 cannot be resolved" error message.
Please help me figure out what is wrong with this code.


The code:

package com.innodesign.testingbuttons;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsoluteLayout;
import android.widget.ImageButton;
import android.widget.Button;
import android.widget.EditText;
import android.net.Uri;
import android.content.Intent;

public class TestingButtons extends Activity {
       protected void onCreate(Bundle icicle) {

               final ImageButton myButton12 = (ImageButton)
               myButton12.setOnClickListener(new View.OnClickListener
               public void onClick(View v) {
               // Perform action on click

   public boolean onCreateOptionsMenu(Menu menu){

               menu.add(0, 1, 0, "Left");
               menu.add(0, 2, 0, "Right");
   return true;

       public boolean onOptionsItemSelected(MenuItem item){
               case 1:
               return true;
               case 2:
               return true;
       return true;

       public void shiftLeft(){
AbsoluteLayout.LayoutParams(10, 10,
10, 10));



Hoping to change widget attributes by selecting a menu item

by Jesse McGrew » Thu, 05 Mar 2009 13:14:11 GMT


You've defined myButton12 as a local variable for the onCreate()
method. It doesn't exist in shiftLeft(), so you're getting that error

Define myButton12 as a field of your activity class instead so it can
be used from the other methods:

  public class TestingButtons extends Activity {
      private ImageButton myButton12;
      /* ... */

Then remove the words "final ImageButton" from the line in onCreate()
where you assign to myButton12.


Sponsored Links

Other Threads

1. Regarding Device level APIs

Hi All,

I am new to Android. Please clear my doubts.

I could see there are many APIs available in Android.

If I have to create some device driver do I need to use Java(Android)
for that also. Or that will be in C?



2. How to mix 2D graphics with layouts

I'd like to build an activity that is mixing layouts with 2D graphics.
So for example, would like to introduce an edittext with an integer
value and based on that value the size of a circle is dynamically
adjusted and drawn. Any idea how to do this?

For plain graphics I am using this approach here - but how can it be
expanded to cover Android layouts (buttons, editboxes, textviews,
radiobuttons, etc.)?

public class DrawTest extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        setContentView(new GraphicsView(this));

    static public class GraphicsView extends View {
        public GraphicsView(Context context) {
        protected void onDraw(Canvas canvas) {


3. System application upgrade and system app versions?

4. Twitter example code, importing and "resource exists with a different case"

5. Combining free and paid versions of app into a single apk - a proposal to the Google engineers

6. NullPointerException on setBackgroundResource

7. Installing APK files to your g1 mobile using Vista