0

I am starting off with a simple app when a button changes the text I want to change it from saying 'hello world' to 'hello brody' when I push a button here is what I got

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="com.helloworld.app.MainActivity"> <TextView android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="145dp" android:textSize="50dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> <ToggleButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New ToggleButton" android:id="@+id/toggleButton" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> 

what can I do so that it changes the text ?

2
  • 1
    post your activity code what have you done. Commented Mar 11, 2014 at 3:44
  • What you want to change ToggleButton text or TextView text? Commented Mar 11, 2014 at 3:54

3 Answers 3

3

Try like this. It would help you

ToggleButton toggle = (ToggleButton) findViewById(R.id.togglebutton); toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { yourTextViewObject.setText("Hello World"); } else { yourTextViewObject.setText("Hello Brody"); } } }); 
Sign up to request clarification or add additional context in comments.

Comments

1

Very simple example:

public class MainActivity extends Activity { TextView textView; ToggleButton toggleButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textView = (TextView) findViewById(R.id.textView); toggleButton = (ToggleButton) findViewById(R.id.toggleButton); toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton btn, boolean checked) { textView.setText(checked ? "hello brody" : "hello world"); } } ); } } 

To jibe with the layout xml you've provided, you need to add the following line to the TextView tag:

android:id="@+id/textView" 

Comments

0

Hi have a look and try with the given code here

ToggleButton toggleButton= (ToggleButton) findViewById(R.id.togglebutton); toggleButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (toggleButton.isChecked()) { //do accordingly } else { //do accordingly } } }); 

2 Comments

how do i put these in what file
i dont know where to put this where do i put it in

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.