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. How to enable codec component PV log

Hi All,

i have enabled the complete pv log by doing the following step:

1.changed extlibs/pv/oscl/pvlogger/src/pvlogger.h:

/* Release mode-- No logging */
#define PVLOGGER_INST_LEVEL 0 ---- Changed this value to 5

and extlibs/pv/android/thread_init.cpp:

#if 0    --------- Changed this value to 1
    PVLoggerAppender *appender = new
AndroidLogAppender<TimeAndIdLayout,1024>();
...
...
#endif

but i am not getting codec components logs.
How i can enable particular component logs?

Thanks in advance,
Yogi
--~--~---------~--~----~------------~-------~--~----~

2. load native .so

I tried to load a native library from an existing Android application
and failed at calling the native function. Do any of you have any
advice?

1.      I added the following code to android application
Lunarlander.java
      //native method declaration
native void hello(String name);

static {
        System.out.println("libhello.so loaded");
        System.load("/data/app/libhello.so");
    }

I added my libhellp.so to lunalander.apk and then uploaded the apk to /
data/app. The application launches ok.

2.      I added the following code to android application
Lunarlander.java
        //native method declaration
native void hello(String name);
static {
        System.out.println("libhello.so loaded");
        System.load("/data/app/libhello.so");
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
                    //test loading hello lib and call hello function
        LunarLander myLunarLander = new LunarLander();
        myLunarLander.hello("Hi there!");
    }

I added my libhellp.so to lunalander.apk and then uploaded the apk to /
data/app. The application launches was forced to close and the error
message is he application Lunar lander has stopped unexpectedly.
Please try again

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

3. replace PV mp3 parser.

4. New SDK Available

5. The problem of installing Repo

6. Does android browser support plugins?

7. Optional Permissions