0

I have a game that adds a point every time a player passes a ball through a goal. I added a TextView that I want updated every time a goal and initialized it with the text "1" and gave it the id "scoreTextView". I tried to declare a TextView variable in my main activity and using addView but my game crashes everytime.I have also tried passing in Layout params along with the TextView to be displayed after reading another StackOverflow question but that also caused my game to crash. When I dont declare a TextView varible and dont add the scoreTextView to the mainView (that is the name of my FrameLayout varible) in my main activity the game actually runs and displays the 1 but if I can't access the scoreTextView from my activity I have no way to change it. So my question is how do I create a TextView in a FrameLayout that I actually can use and change dynamically as needed in my main activity?

I have already looked at this and tried it but they didn't work: Add a Textview to a FrameLayout in a determined position

Community
  • 1
  • 1
Benyam Ephrem
  • 448
  • 6
  • 20
  • 1
    show your code here that trigger when user make a goal – Randyka Yudhistira Feb 26 '15 at 06:47
  • @RandykaYudhistira Well I haven't added that yet but I just want a simple way to change the TextView so I can implement that in the code to change the TextView ever time the user makes a goal. Since I have no way to use it in the activity (without the app crashing) I can't make it dynamically changing......so I just want a simple example of changing a TextView in a FrameLayout so I can dynamically implement it. THANK YOU!! – Benyam Ephrem Feb 26 '15 at 07:08
  • show us your Log message? – Martynas Feb 26 '15 at 07:20

3 Answers3

0
<FrameLayout
    android:id="@+id/yourFrameLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/lin2"
    android:layout_below="@+id/lin1"
    android:orientation="vertical" >        

    <TextView
        android:id="@+id/messageTextView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:textSize="20sp"
        android:text="Put your message over here" />
</FrameLayout>

and then you can visible or invisible this messageTextView according to your need and this is quite easy way to access . hope it ll help you

Vaishali Sutariya
  • 5,093
  • 30
  • 32
  • Thank you for resonding, but how would I access the TextView from the main activity, that's is my trouble, my app crashes when I try to declare a TextView varible then add it to the FrameLayout. So how would I use it in the main activity so I can dynamically change it in my code? – Benyam Ephrem Feb 26 '15 at 07:15
  • messageTextView = (TextView) findViewById(R.id.messageTextView); this much you have to add in your activity and then messageTextView.setText(" set Text whatever you want to write ") – Vaishali Sutariya Feb 26 '15 at 07:18
0

Maybe your problem is that you are trying to update TextView from non UI thread? The Log message would really help...

If this is the case, then try adding this:

runOnUiThread(new Runnable() {
    public void run() {
        // Update TextView here
TextView scoreTextView = (TextView)findViewById(R.id.scoreTextView);
scoreTextView.setText("Goals scored: " + goals);
    }
});
Martynas
  • 627
  • 2
  • 8
  • 28
  • Glad to help you :) Next time give error log too :) – Martynas Feb 26 '15 at 07:30
  • Ok, I actually just checked my code again and the real problem was I declared my TextView varible outside of OnCreate...I just got so used to using ButterKnife I forgot I wasn't using it for this program. Still thank you though and I'll make sure to give an error log next time. – Benyam Ephrem Feb 26 '15 at 07:35
  • @Benyam Ephrem I had the same problem and I had the TextView declared outside too :D – Tom Lenc Jul 07 '15 at 17:43
0
TextView textView = (TextView) findViewById(R.id.messageTextView);
textView.setText("this is where you add what you want the text to say");

This should be done in the activity class attached to the layout.

JimboSlice
  • 55
  • 2
  • 6