Jump to content

Need Help Android Java Over writing a TextView with two methods.

Go to solution Solved by organ_pleasin,

Based on first impressions: have you tried to change onClicktwo to just onClick in the punchLine on click listener? If it's not that I'll get back to you later in the day when I'm infront of my computer.

I am in the process of making an app for class. The needs two buttons and one Text View.

 

One of the buttons is the setup: sets the joke up and changes the text view.

 

Second Button is the punch Line: changes the textview again to reveal the joke. 

 

I am having troubles by over writing the text view the second time around. IT works on the first try to set up the joke. I can't get it to over ride the second time to show the joke. 

First time posting Android studio code here. 

So bare with me and Thanks again!

 

Android Studio 2.2

 

 

 

package com.example.sergio.jokeapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final TextView firstTextView = (TextView) findViewById(R.id.textView);

        Button setUp_button = (Button) findViewById(R.id.setup_button);

        Button punchLine_button = (Button) findViewById(R.id.punchLine_button);
        
        
        setUp_button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                firstTextView.setText("Why did the chinken cross the road?");
            }
        });
        
        punchLine_button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClicktwo(View view)
            {
                firstTextView.setText("To get to the other side");
            }
        });
        


    }
}
   <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:layout_centerHorizontal="true"
        android:textSize="20dp"
        android:id="@+id/textView" />

    <Button
        android:text="@string/setup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/setup_button"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="54dp" />

    <Button
        android:text="@string/punch_line"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/punchLine_button"
        android:layout_below="@+id/setup_button"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="79dp" />

 

Link to comment
Share on other sites

Link to post
Share on other sites

Based on first impressions: have you tried to change onClicktwo to just onClick in the punchLine on click listener? If it's not that I'll get back to you later in the day when I'm infront of my computer.

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, organ_pleasin said:

Based on first impressions: have you tried to change onClicktwo to just onClick in the punchLine on click listener? If it's not that I'll get back to you later in the day when I'm infront of my computer.

Thank you very much! 

 

I see why the abstract is there now. Simple fix and yet I am not surprise that I couldn't see that. 

First day working with Android Studio and just getting the hang of the IDE. 

 

 

 

thanks again organ_pleasin! 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×