change text with a button for nood

Put your problem here if it does not fit any of the other categories.

change text with a button for nood

Postby bszaronos » Wed Jun 09, 2010 5:40 pm

Hello all,

Can someone post how to make a button change the text in a label. Basically I want to press a button labeled 'press me' and when you do a label changes to 'you pressed me'.

I am new to android ( duh ), I am use to doing this in visual basic, or real basic on the mac, but am kinda lost on this.

Thanks
bszaronos
Freshman
Freshman
 
Posts: 2
Joined: Wed Jun 09, 2010 5:27 pm

Top

Re: change text with a button for nood

Postby phix » Wed Jun 09, 2010 6:36 pm

Assuming you have a textview and buton like this:

Code: Select all
<TextView android:id="@+id/labelText"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

<Button android:id="@+id/pushBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Press Me" />

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. Button pressMe = (Button)findViewById(R.id.pushBtn);
  3. TextView label = (TextView)findViewById(R.id.labelText);
  4. pressMe.setOnClickListener(new OnClickListener(){
  5.     public void onClick(View v) {
  6.          label.setText("You pressed it.");
  7.     }
  8. });
  9.  
  10.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
Posting questions without code is like going to the doctor and saying "it hurts, fix it."
phix
Master Developer
Master Developer
 
Posts: 230
Joined: Mon Mar 15, 2010 10:37 am

Re: change text with a button for nood

Postby bszaronos » Wed Jun 09, 2010 6:46 pm

Ok I got it to work. Thank you soo much.

Now I want the text to reset back to what it was before the button was pressed. In VB i would use a timer.
So now my text says "You didn't press me". When I press the button it says "You pressed me". After a few seconds I want it to go back to "You didn't press me."


This is my main.xml
Code: Select all
<?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/hello"
    />   
<Button 
    android:id="@+id/btnClickMe"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Click Me!"
    />
    <TextView android:id="@+id/labelText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView Label"
/>
<DigitalClock android:id="@+id/DigitalClock01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>

This is my java file.
Code: Select all

package helloandroid.test.com;

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


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

        //---the button is wired to an event handler---
        Button btn1 = (Button)findViewById(R.id.btnClickMe);
        btn1.setOnClickListener(btnListener);
        TextView label = (TextView)findViewById(R.id.labelText);
        label.setText("You didn't press me yet");
    }

    //---create an anonymous class to act as a button click listener---
    private OnClickListener btnListener = new OnClickListener()
    {
        public void onClick(View v)
        {                       
            Toast.makeText(getBaseContext(),
                    "You pressed me, You really did.",
                    Toast.LENGTH_LONG).show();
            TextView label = (TextView)findViewById(R.id.labelText);
            label.setText("It Worked");
           
         
        }
       
    };   
   
    }


Thanks for the help
bszaronos
Freshman
Freshman
 
Posts: 2
Joined: Wed Jun 09, 2010 5:27 pm

Re: change text with a button for nood

Postby anarche » Thu Jun 10, 2010 1:40 am

Use a Timer
http://developer.android.com/reference/ ... Timer.html

if you need help with basic stuff like this, can I suggest the Tutorials that came with Android - the notepad tut is excellent, especially for understanding the Android ecosystem
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 18 guests