How to create two TextView without write the XML file

by rjy...@gmail.com » Mon, 04 May 2009 05:57:27 GMT


Sponsored Links
 Hi
   I will create twi TextView in the main layout, but only last
TextView can be showed.

My codes:

package myapp.helloworld;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.graphics.Color;

public class helloworld extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView tv1 = new TextView(this);
        tv1.setText("This is sample.");
        tv1.setTextColor(Color.RED);
        setContentView(tv1);

        TextView tv2 = new TextView(this);
        tv2.setText("This is yellow.");
        tv2.setTextColor(Color.YELLOW);
        setContentView(tv2);
    }

--~--~---------~--~----~------------~-------~--~----~



How to create two TextView without write the XML file

by radiolistener » Mon, 04 May 2009 10:44:00 GMT


 try this, in the layout you are building off of put the following:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=" http://schemas.android.com/apk/res/ 
android"
        android:id="@+id/ll"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
</LinearLayout>





In the code, do the following:
import android.app.Activity;
import android.os.Bundle;
import android.app.Activity;
import android.os.Bundle;
import android.text.Layout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.graphics.Color;
public class helloworld extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView tv1 = new TextView(this);
        tv1.setText("This is sample.");
        tv1.setTextColor(Color.RED);
        //setContentView(tv1);
        LinearLayout k = (LinearLayout) findViewById(R.id.ll);
        k.addView(tv1);
        TextView tv2 = new TextView(this);
        tv2.setText("This is yellow.");
        tv2.setTextColor(Color.YELLOW);
        //setContentView(tv2);
        k.addView(tv2);
    }
}





I think it will achieve what you are looking for.
John aka Radiolistener
--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


Other Threads

1. New tutorial: Image & Text-Only Buttons

There are some great Android screenshots out there!  I've been working
on improving Pocket Journey's UI as I previously focused mostly on the
MediaPlayer functionality.  I now want to pass on a few new UI
techniques I've uncovered.

This new tutorial is very simple and will add to your UI (user
interface) development arsenal.

How?

Buttons that display as simple text or as images are basic elements of
any application. By following the two steps below, these buttons can
be easily created with Android by simply extending the current Button
and ImageButton classes.

You can see the tutorial here:  blog.pocketjourney.com

Cheers,
Biosopher
--~--~---------~--~----~------------~-------~--~----~

2. How to get another application's data path?

Hi all,

In my application, I want to access another application's data path, and
I only know that application's name. then how can I do for this?

Thanks.
Frank


--~--~---------~--~----~------------~-------~--~----~

3. I wana ask about creating own views

4. ImageView/ImageButton question

5. GTalk Sender unable to make session

6. getting Media File metadata?

7. Console error message