I managed to have a button and then when I click on it, I go to a new activity that is called "TUTORIALONE"
and then I want to display some text in this new activity
so I have something like this
Button b = (Button) findViewById(R.id.tutorial1);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent("my.android.TUTORIALONE"));
TextView tv = (TextView)findViewById(R.id.tutorial1);
tv.setText("this is some text);
}
});
the problem is that it first displays the text on my button, and then it shows me the new activity, how would I achieve displaying the text on the new activity?
thanks in advance
In the onCreate() of the new Activity, you can create a Button, set the text and then call setContentView() in order to show it. In case you want to show to this Activity a string from the current Activity, you can pass this String an an Intent extra and then recover it to the new Activity. Hope this helps!
We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.
Android App Development for BeginnersAn activity represents a single screen with a user interface just like window or frame of Java. Android activity is the subclass of ContextThemeWrapper class. The Activity class defines the following call backs i.e. events. You don't need to implement all the callbacks methods.
In your TUTORIALONE
activity you probably have an associated xml file for displaying content. Perhaps it iss set something like this
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.id.TUTORIALONE);
}
In the layout xml file for TUTORIALONE
just add something like this
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a TextView" />
EDIT
To change the text of this TextView
, do something like this in your TUTORIALONE
activity.
protected void onStart()
{
super.onStart();
TextView tv = (TextView)findViewById(R.id.text);
tv.setText("this string is set dynamically from java code");
}
Note that the id here (R.id.text) is the same as in the xml file ("@+id/text")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With