Editing TextView from EditText on Button click


Last Updated:

  1. DizzyThermal

    DizzyThermal New Member This Topic's Starter

    Joined:
    Sep 6, 2010
    Messages:
    2
    Likes Received:
    0
    Hey guys, new to the forum :)

    I had a question on a piece of my code..

    What I'm trying to do is learn how to set a TextView's default text to whatever I edit in the EditText

    In Lamence terms..

    Default Text <---Label
    [Default Text] <---Text Box
    [Change] <---Button

    If I change the information in the Text Box, it will change the Label on the Change Button click..

    Now, it will change the text to 'false' and I can't figure out what I'm doing wrong..

    Here's my Main.java:

    Code (Text):
    1.  
    2. package com.testapp.android;
    3.  
    4. import android.app.Activity;
    5. import android.os.Bundle;
    6. import android.view.View;
    7. import android.view.View.OnClickListener;
    8. import android.widget.Button;
    9. import android.widget.EditText;
    10. import android.widget.TextView;
    11.  
    12. public class Main extends Activity {
    13.     /** Called when the activity is first created. */
    14.     @Override
    15.     public void onCreate(Bundle savedInstanceState) {
    16.         super.onCreate(savedInstanceState);
    17.         setContentView(R.layout.main);
    18.         changeButton = (Button)findViewById(R.id.change);
    19.         changeButton.setOnClickListener(new OnClickListener()
    20.         {
    21.             @Override
    22.             public void onClick(View v)
    23.             {
    24.                 changeLabel = (TextView)findViewById(R.id.label);
    25.                 changeLabel.setText(R.id.textField);
    26.             }
    27.         });
    28.     }
    29.     public Button changeButton;
    30.     public TextView changeLabel;
    31. }
    and here's my main.xml

    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3.     android:orientation="vertical"
    4.     android:layout_width="fill_parent"
    5.     android:layout_height="fill_parent"
    6.     >
    7. <TextView
    8.     android:id="@+id/label"
    9.     android:layout_width="fill_parent"
    10.     android:layout_height="wrap_content"
    11.     android:text="Default"
    12.     />
    13. <EditText
    14.     android:id="@+id/textField"
    15.     android:layout_width="fill_parent"
    16.     android:layout_height="wrap_content"
    17.     android:text="Default"/>
    18. <Button
    19.     android:id="@+id/change"
    20.     android:layout_width="wrap_content"
    21.     android:layout_height="wrap_content"
    22.     android:text="Change"/>
    23. </LinearLayout>
    24.  
    Thanks a bunch guys :) If you have any questions on what I'm trying to do feel free to question, I know it's kinda hard to understand with my ASCII drawing :p..
     

    Advertisement
  2. Rohit_

    Rohit_ New Member

    Joined:
    Sep 10, 2012
    Messages:
    1
    Likes Received:
    0
    to read more about android development must visit Bluerayplus.com
     

Share This Page

Loading...