Fraxion
Fraxion

Reputation: 1

Using onClick in embedded LinearLayout

solved - changing layout_height to wrap_content in inner layout helped

I started playing with Android development yesterday and I have a fairly basic question regarding embedding layouts. When I embed one LinearLayout in another and put a Button in it, onClick method doesn't get called. If I omit the second LinearLayout, everything works fine. Do you have any idea what am I doing wrong?

My layout file follows.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
>
        <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/info"
        />

        <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/prompt"
        />

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
        >
                <EditText
                        android:id="@+id/frequency"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:inputType="numberDecimal"
                />

                <Button
                        android:id="@+id/submit"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/submit"
                        android:onClick="handleSubmit"
                />
        </LinearLayout>

        <TextView
                android:id="@+id/result"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
        />
</LinearLayout>

And the code:

package pl.test.android.step;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;

public class Step extends Activity
{
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
        }

        public void handleSubmit(View view)
        {
                TextView resultObj = (TextView) findViewById(R.id.result);
                resultObj.setText("click");
        }
}

Upvotes: 0

Views: 401

Answers (2)

AliSh
AliSh

Reputation: 10619

you are using android:layout_height="fill_parent" for your second linearLayour, just change it to android:layout_height="wrap_content" . it will work correctly.

second LinearLayout is completely fill the android page(main LinearLayout) and "result textView" is out of page. Code is correctly work but you can't see your textView.

Upvotes: 0

goodm
goodm

Reputation: 7295

Try:

public void handleSubmit(View view)
{
    switch(view.getId())
    {
        case R.id.result:
        //Your method
        break;
    }
}

Upvotes: 1

Related Questions