1

I am trying to change the text in one of the textview by calling it in a function that is assigned to a button.. However it is not quite working as I hoped. I was wondering if some one could point out why isn't it working?

Here is the "MainActivity.java" file

package com.nblsoft.myapplication; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.TextView; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } String stri = new String("Hellow"); public void game(View view) { TextView textv = (TextView) findViewById(R.id.textView2); textv.setText(stri); } } 

and the "activity_main.xml" file

<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=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="@string/Welcome" android:id="@+id/textView" android:gravity="center" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="@string/lower_text" android:id="@+id/textView2" android:layout_below="@+id/editText" android:layout_centerHorizontal="true" android:layout_marginTop="34dp" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText" android:inputType="number" android:text="@string/guess" android:textSize="160dp" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:onClick="game"/> <Button style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ok" android:id="@+id/button" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> </RelativeLayout> 
3
  • not quite working as I hoped doesnt explain the problem that you are facing..what is really happening? any errors? Commented Jul 25, 2015 at 15:21
  • also, where is the code for your button? Commented Jul 25, 2015 at 15:23
  • you want call game when click in EditText?! you put android:onClick="game" in EditText ! Commented Jul 25, 2015 at 15:34

3 Answers 3

1

you want call game when click in EditText?! you put android:onClick="game" in EditText

if want call when click in Button you must add android:onClick="game" to your Button in activity_main.xml file like

<Button style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ok" android:id="@+id/button" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:onClick="game" /> 

or you can use this in MainActivity.java instead use android:onClick="game" in xml file

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button=(Button) findViewById(R.id.button); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub game(arg0); } }); } 

- remove android:onClick="game" from EditText

Sign up to request clarification or add additional context in comments.

Comments

0
 package com.nblsoft.myapplication; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.TextView; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textv = (TextView) findViewById(R.id.textView2); String stri = new String("Hellow"); Button btn = (Button) findViewById(R.id.button); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { textv.setText(stri); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } 

This should be working.By the way.Don't rush into android like this...you've got to learn a few things about how it works before you can play with it.Try to check the documentation and play with all that android offers.It's actually fun.It was for me and it still is as I've got to learn a lot ,still.

Comments

0

you call the method game() when you click the edittext "editText", and this will change the textview. If you want to change the value of the textview by clicking button "button", i think you just need to move the game() method to the Button field:

 <Button style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ok" android:id="@+id/button" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:onClick="game" /> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.