user1105826
user1105826

Reputation:

my first application android

hello i try to use simple application in android but i have many problems ;

i need when i click in button text change in "hh";

my main.xml

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


    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

my class

package com.my.Hello;



import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class HelloActivity extends Activity{
    Button button;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button = (Button)findViewById(R.id.button1);
        button.setText("hh");
        button.setOnClickListener(new View.OnClickListener()
        {
        public void onClick(View v)
        {
            button.setText("hh");
        }
        });

        setContentView(R.layout.main);
    }

    }

i have good resultat in UI but when i click in button not thing happen ?

Upvotes: 0

Views: 150

Answers (1)

Blundell
Blundell

Reputation: 76466

You're setting the text to "hh" in the onCreate() so clicking it would not change it.

Also, you are calling setContentView() twice, so the second time just invalidates everything you've already coded.

Try this:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            button.setText("hh");
        }
    });
}

Upvotes: 7

Related Questions