Restnom
Restnom

Reputation: 134

Changing ImageView size

I'm trying to have an image show up when the name of it (TextVIew) is clicked. I have the code set up to what I believe is the right thing but the app force closes when I launch it. This is just a test code, so its not my actual app, but the idea is the same so can someone help me figure out why this is crashing?

java file:

TextView text = (TextView) findViewById(R.id.text);
ImageView image = (ImageView) findViewById(R.id.image);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    text.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            image.getLayoutParams().height=20;
        }
    });
}

xml file:

<ImageView
    android:id="@+id/image"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:gravity="center"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:text="show image" />

Upvotes: 0

Views: 60

Answers (2)

janzoner
janzoner

Reputation: 1430

You have to have this code (below) inside onCreate()

TextView text = (TextView) findViewById(R.id.text);
ImageView image = (ImageView) findViewById(R.id.image);

Try it.

Update:

The code could look like this:

public class MainActivity extends Activity {

    ImageView image = null;
    TextView text = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        text = (TextView) findViewById(R.id.text);
        image = (ImageView) findViewById(R.id.image);

        text.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (image.getVisibility() == View.GONE) {
                    image.setVisibility(View.VISIBLE);
                    text.setText("hide the image");
                } else {
                    image.setVisibility(View.GONE);
                    text.setText("show the image");
                }
            }
        });

    }
}

and layout is here:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:visibility="gone" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="show the image" />

</LinearLayout>

Upvotes: 1

C.d.
C.d.

Reputation: 9995

Fill your elements in onCreate, not as a field, after setting the content view;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView text = (TextView) findViewById(R.id.text);
    ImageView image = (ImageView) findViewById(R.id.image);
    ...

And try setting the size of the ImageView not directly from layoutparams but with setHeight/setWidth functions

Upvotes: 0

Related Questions