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. emulator: warning: opening audio output failed

I'm using Linux Ubuntu.
When opening the emulator I get this message.

My sound output is done through ALSA Mixer.

Any solutions?

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

2. Floating View

Hello,

Im trying to create Custom Dialog/Activity looking simmilar to Maps-
I've tried to make it like in the Browsers find dialog but I've always
Dialog's title (or empty space) and border.
I also want to place AutoCompleteTextView in this Dialog/Activity. How
can I force list with suggested input to be shown over the EditText?

Thanks in advance.

Skyman

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

3. Redirecting to market search URL doesn't work

4. Messaging between 2 threads

5. Error retrieving a Serializable from a Bundle with a ClassLoader set

6. problem when debugging android application with eclipse

7. upgrade testing