4 Replies - 597 Views - Last Post: 24 February 2015 - 08:31 PM

#1 streek405   User is offline

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 721
  • Joined: 10-March 13

How to set a text view to a method?

Posted 22 February 2015 - 08:04 PM

I am trying to display a text view to the user when they press a button, but I am not really sure how to do it. I have a method that returns a string and I am trying to text my text view to that method. Here is what I got:
 <TextView
       android:id="@+id/text"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="myMethod"

       />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Leaderboard"
        android:id="@+id/leadButton"
        android:onclick= the text view? />




Player p = new Player("bob");
    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv = (TextView)findViewById(R.id.text);
        tv.setText(p.myMethod());

    }


I have also tried
Player p = new Player("bob");
    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv = (TextView)findViewById(R.id.text);
        tv.setonclickListener(new View.onclickListener(){
            public void onclick(View v){
                Toast.makeText(getApplicationContext(),
                  p.getScoreHistory(), Toast.LENGTH_LONG).show();
            }
        });

This post has been edited by streek405: 22 February 2015 - 08:18 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to set a text view to a method?

#2 AKMafia001   User is offline

  • </code.in.dream>
  • member icon

Reputation: 238
  • View blog
  • Posts: 738
  • Joined: 11-June 11

Re: How to set a text view to a method?

Posted 23 February 2015 - 02:26 AM

I didn't get your question completely.

Are you trying to show (make visible) a text view when a button is clicked? And stays hidden otherwise?

If so, you can set the Visibility of the TextView to either invisible or gone. The difference is the later is also hidden but doesn't occupies space in the layout. Then, in your button's click listener, set the Visibility of the TextView to VISIBLE.

From the xml layout, just add android:visibility="invisible" or gone to the TextView. And from your code, tv.setVisibility(View.VISIBLE);...

If that's not what you are trying to do, please feel free to explain your problem...

Hope it Helps!
Was This Post Helpful? 0
  • +
  • -

#3 streek405   User is offline

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 721
  • Joined: 10-March 13

Re: How to set a text view to a method?

Posted 23 February 2015 - 07:25 PM

View PostAKMafia001, on 23 February 2015 - 02:26 AM, said:

I didn't get your question completely.

Are you trying to show (make visible) a text view when a button is clicked? And stays hidden otherwise?

Kinda. Like if the user presses a button, it will take them to different "page", or at least pop up, with the text that the method returns.
Was This Post Helpful? 0
  • +
  • -

#4 AKMafia001   User is offline

  • </code.in.dream>
  • member icon

Reputation: 238
  • View blog
  • Posts: 738
  • Joined: 11-June 11

Re: How to set a text view to a method?

Posted 23 February 2015 - 11:33 PM

Well! In that case, to take them to a different page (different activity) you will need to have another Activity, and from the button's click listener start that activity using an Intent. If you want to just pop up (show a dialog) you can use AlertDialog. You can set up the AlertDialog its title, put in some content, set up the buttons and show it. Or you can show a totally custom dialog, say your custom dialog is full screen, with no title and buttons. For that you will need to extend the Dialog class.

Hope it Helps!
Was This Post Helpful? 1
  • +
  • -

#5 streek405   User is offline

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 721
  • Joined: 10-March 13

Re: How to set a text view to a method?

Posted 24 February 2015 - 08:31 PM

View PostAKMafia001, on 23 February 2015 - 11:33 PM, said:

Well! In that case, to take them to a different page (different activity) you will need to have another Activity, and from the button's click listener start that activity using an Intent. If you want to just pop up (show a dialog) you can use AlertDialog. You can set up the AlertDialog its title, put in some content, set up the buttons and show it. Or you can show a totally custom dialog, say your custom dialog is full screen, with no title and buttons. For that you will need to extend the Dialog class.

Hope it Helps!

THANK YOU!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1