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. Contact database 鈥?Group c reation ?

See 
http://code.google.com/android/reference/android/provider/Contacts.Groups.html.
You should be able to insert into that table to create the actual
group, and then you can add people to the group using
http://code.google.com/android/reference/android/provider/Contacts.People.html#addToGroup(android.content.ContentResolver,%20long,%20long).

-Jeff




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

2. openOrCreateDatabase or what?

Prior to release 0.9 one could do the following to open or create a
SQLite database:

try {
  db = context.openDatabase(DATABASE_NAME, null);
} catch (FileNotFoundException e) {
  db = context.createDatabase(DATABASE_NAME, DATABASE_VERSION,
                                                Context.MODE_PRIVATE, null);
 createTables();
 insertSampleData();
}

So if the application can't open the database it creates a new one and
creates all tables and inserts test data.

Now both methods are gone and there is a single method
openOrCreateDatabase().

This method doesn't allow the programmer to distinguish between
opening an existing and creating a new database.
To decide if it should call createTables() and insertSampleData()
additional checks are necessary.

Can someone explain the background why this change was introduced? I
always prefer a clear contract of the API like different methods for
open(), close(), create(). Or did I miss something?
--~--~---------~--~----~------------~-------~--~----~

3. AutoCompleteTextView showing filtered results

4. Use Android as embedded platform

5. Skin editors (?)

6. How to set AlertDialog button focus?

7. Custom Components in a different package?