Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a new activity and then display text in it with android programming

Tags:

android

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

like image 917
John John Avatar asked Dec 25 '11 22:12

John John


People also ask

How do I add text to activity?

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!

How do I pass a string from one activity to another in Android?

We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.

How do you creating the first activity in Android explain with an example?

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.


1 Answers

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")

like image 79
vidstige Avatar answered Sep 22 '22 02:09

vidstige