Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

displaying a string on the textview when clicking a button in android

Tags:

android

I'm very new to Android development and just started to study.

What I'm trying is to add a button and when that button is pressed a text "my first project" to get displayed in the text view.

With the help of some experts I created the button and text view.

So the button is showing in the simulator but when I click that button nothing happens.

Can anyone please help me with how can I display the text when pressing the button?

.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txtView"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:text="@string/hello" />

    <Button
   android:id="@+id/mybtn"
   android:layout_width="50dp"
   android:layout_height="30dp"       />
    <TextView
   android:id="@+id/viewwidth"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"     />
<TextView
   android:id="@+id/viewheight"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"    />

</LinearLayout>

.java

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.widget.Toast;

public class NameonbuttonclickActivity extends Activity implements View.OnClickListener {
    Button mybtn;
    TextView txtView;
    String hello;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.main);
        super.onCreate(savedInstanceState);
        mybtn= new Button(this);
        txtView=new TextView(this);
        mybtn.setOnClickListener(this);
        printmyname();

        mybtn = (Button)findViewById(R.id.mybtn);
        txtView=(TextView)findViewById(R.id.txtView);
        txtView = (TextView)findViewById(R.id.viewwidth);
        txtView = (TextView)findViewById(R.id.viewheight);

        hello="This is my first project";

        //setContentView(mybtn);
       // setContentView(R.layout.main);
    }

    public void onClick(View view){
        txtView.setText(hello);         

    //printmyname();
     Toast.makeText(NameonbuttonclickActivity.this, hello, Toast.LENGTH_LONG).show();               

    }            
    private void printmyname(){
        System.out.println("coming");       

    }
}
like image 260
suji Avatar asked Feb 08 '12 11:02

suji


4 Answers

You can do like this:

 String hello;
public void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.main);
    super.onCreate(savedInstanceState);

    mybtn = (Button)findViewById(R.id.mybtn);
    txtView=(TextView)findViewById(R.id.txtView);
    txtwidth = (TextView)findViewById(R.id.viewwidth);
    hello="This is my first project";


    mybtn.setOnClickListener(this);
}
public void onClick(View view){
    txtView.setText(hello);
}

Check your textview names. Both are same . You must use different object names and you have mentioned that textview object which is not available in your xml layout file. Hope this will help you.

like image 94
Dhruvisha Avatar answered Oct 08 '22 07:10

Dhruvisha


First create xml file as follows. Create one textview and a button:

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" >

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

  <Button
    android:id="@+id/mybutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click Me" />

  <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

</LinearLayout>

The first TextView is created by default. You can leave or remove it if you want. Next one is to create a button The next one is TextView where you want to display text.

Now coming to the main activity code... package com.android.example.simple;

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

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

    final TextView textView=(TextView)findViewById(R.id.textView1);
    final Button button1 =  (Button)findViewById(R.id.mybutton);

    //Implement listener for your button so that when you click the 
    //button, android will listen to it.             

     button1.setOnClickListener(new View.OnClickListener() {             
        public void onClick(View v) {                 
        // Perform action on click 
            textView.setText("You clicked the button");

        }         });
    }
}
like image 37
DSP Avatar answered Oct 08 '22 06:10

DSP


This is because you DON'T associated the OnClickListener() on the button retrieve from the XML layout.

You don't need to create object because they are already created by the Android system when you inflate XML file (with the Activity.setContentLayout( int resourceLayoutId ) method).

Just retrieve them with the findViewById(...) method.

like image 28
Sly Avatar answered Oct 08 '22 06:10

Sly


Just check your code in .java class

You had written below line

 mybtn.setOnClickListener(this);

before initializing the mybtn object I mean

mybtn = (Button)findViewById(R.id.mybtn);

just switch this two line or put that line "mybtn.setOnClickListener(this)" after initializing your mybtn object and you will get the answer what you want..

like image 33
Dhrumil Shah - dhuma1981 Avatar answered Oct 08 '22 07:10

Dhrumil Shah - dhuma1981