See the top rated post in this thread. Click here

Results 1 to 4 of 4
  1. #1

    Changing text in textView

    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

    Code:
    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

    Code:
    <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>

  2. #2
    where is the onClickListener for your button? you should have an onClickListener for the button and call the function to change the text.



  3. #3
    Where is onCreate()? i think you need so many help



  4. #4
    Insert
    Code:
    android:onClick="game"
    to your button in your xml layout (activity_main.xml). Now when you click on the button the game function will be called.


    1 members found this post helpful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •