Currently I have a listview bound to an adapter (in a DialogFragment):
adapter = new SimpleCursorAdapter(getActivity(),
android.R.layout.simple_list_item_2,
null,
new String[] { "_id", "name" },
new int[] { android.R.id.text1, android.R.id.text2 },
0);
listView.setAdapter(adapter);
However, I would like to show only the name, but still keep the id, because when the user selects an item, the id is returned to the calling activity.
Would I need to have a custom layout with a hidden TextView or is there a better way to get the id from the current item?
Ok You may go with Custom adapter.
But if you don't want to do it.
You can change the default behavior of your code by Overriding a bindView().
adapter = new SimpleCursorAdapter(NavDrawer.this,
android.R.layout.simple_list_item_1,
null,
new String[] { "_id", "name" },
new int[] { android.R.id.text1},
0){
#Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
String name = cursor.getString(cursor.getColumnIndex("name"));
String _id = cursor.getString(cursor.getColumnIndex("_id"));
TextView tv = (TextView) view.findViewById(android.R.id.text1);
tv.setText(name);
//here you can store your id on textview tag
tv.setTag(_id);
}
};
for getting the id
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView tv = (TextView) view.findViewById(android.R.id.text1);
String _id = tv.getTag().toString();
}
});
You can use this layout for your SimpleCursorAdapter :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/txt_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<TextView
android:id="#+id/txt_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Name it as custom_layout then change your SimpleCursorAdapter as follows:
adapter = new SimpleCursorAdapter(getActivity(),
R.layout.custom_layout,
null,
new String[] { "_id", "name" },
new int[] { R.id.txt_id, R.id.txt_name},
0);
listView.setAdapter(adapter);
The txt_id TextView will be hidden and only the txt_name will be visible to the user.
Instead creating again a wheel, you could use CursorAdapter.
Below example how to use it in fragment (I'm using getActivity() to get context).
//test: generating elements
ArrayList<String> values = new ArrayList<>();
for (int i = 0; i < 10; i++) {
values.add("word" + values.size());
}
//test: adding elements to cursor
MatrixCursor mc = new MatrixCursor(new String[]{"_id", "name"});
for (int i = 0; i < values.size(); i++) {
mc.addRow(new Object[]{i, values.get(i)});
}
listView = (ListView) inflate.findViewById(R.id.listView);
adapter = new CursorAdapter(getActivity(), mc, false) {
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(android.R.layout.simple_list_item_1, parent, false);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
((TextView) view.findViewById(android.R.id.text1)).setText(cursor.getString(cursor.getColumnIndex("name")));
//just example how to get _id
Log.d("xxx", "it's id is " + cursor.getString(cursor.getColumnIndex("_id")));
}
};
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("xxx", "clicked id:" + id);
}
});
listView.setAdapter(adapter);
Simply , just donot set text for the item that you donot want to show.. hope this works..
thanks
Related
private void populateListView(){
Cursor c = loginDataBaseAdapter.allData();
startManagingCursor(c);
String[] fromfields = new String[]{"_id", "NAME", "YEAR", "GENRE"};
int[] toViewIDs = new int[]{R.id.TVmovieID, R.id.TVmovieTtile, R.id.TVmovieYear, R.id.TVmoviegenre};
SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this, R.layout.mlayout, c, fromfields, toViewIDs);
lv.setAdapter(myCursorAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String s = parent.getItemAtPosition(position).toString();
Toast.makeText(Proba.this, "String"+s, Toast.LENGTH_SHORT).show();
}
});
}
This is a method I call onCreate of this Activity, I use toast just to make sure that im getting the right value, but I get the android.database.sqlite... . I want to get the value of just one field, but first I want to get the value of the whole list.
Correct way of implementation to get any fields
final String[] fromfields = new String[]{"_id", "NAME", "YEAR", "GENRE"};
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor cursor = myCursorAdapter.getItem(position);
int columnIndex = cursor.getColumnIndexOrThrow(fromfields[1])
String name = cursor.getString(columnIndex);
Toast.makeText(Proba.this, "Name is " + name, Toast.LENGTH_SHORT).show();
}
});
Cursor c = managedQuery(People.CONTENT_URI,null,null,null,People.NAME);
String[] cols = new String[]{People.NAME};
int[] views = new int[]{android.R.id.text1};
SimpleCursorAdapter sca = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_1,c,cols,views);
listview.setAdapter(adapter);
I'm using this code snippet to connect my ListView with Cursor.
I want to ask what
String[] cols = new String[]{People.NAME};
int[] views = new int[]{android.R.id.text1};
exactly does ??
and please explain about the arguments required for the constructor of SimpleCursorAdapter
It is a map, telling the adapter which columns from your cursor to use to fill which Widgets in your layout.
They get used in the order given. The data in the first column listed in the from array ( you called it cols ) will go into the first id listed in the to array ( you called it views), and so on.
The other parameters are the layout containing the view ids your specify in the to array and the cursor containing the data to be used in the array.
list_item.xml Refer this LINK
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
HelloListView.java
public class HelloListView extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, getNames()));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
private ArrayList<String> getNames(){
ArrayList<String> namesArray = new ArrayList<String>();
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME};
Cursor names = getContentResolver().query(uri, projection, null, null, null);
int indexName = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
names.moveToFirst();
do {
namesArray.add(names.getString(indexName));
} while (names.moveToNext());
return namesArray;
}
}
I've been having a lot of trouble with this problem. I have a listview that contains:
ImageView / contactName / TextView / CheckBox
The contactName in the listview is populated by reading the contacts on the phone from a SimpleCursorAdapter. All for elements show when the app runs, but the problem I'm having is connecting the checkboxes to their corresponding item in the list.
Through some research, I found that I must use a getView() to link the checkboxes with the items in the list, but through practice, I can't seem to get it to work right. Furthermore, none of the examples I've tried really explained how to apply getView(). The most full example I've been working from is from here:
http://androidcocktail.blogspot.com/2012/04/adding-checkboxes-to-custom-listview-in.html
The twist is that this reads and populates my listview with my contacts:
private void populateContactList() {
// Build adapter with contact entries
Cursor cursor = getContacts();
String[] fields = new String[] {
ContactsContract.Data.DISPLAY_NAME
};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_entry, cursor,
fields, new int[] {R.id.contactEntryText});
lv.setAdapter(adapter);
} // END POPULATECONTACTLIST
private Cursor getContacts()
{
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME
};
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" +
(chkboxAllVisible ? "0" : "1") + "'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
} // END GETCONTACTS
How do I link each checkbox to the a corresponding contact items in my listview?
Ok i have created a test project for you try to understand code if any problem you are having then ask I will try to help you...
HERE IS MY ONCREATE FUNCTION OF ACTIVITY.
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<String> elements = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
elements.add("elements " + i);
}
CheckBox master_cb = new CheckBox(getApplicationContext());
master_cb.setText("Check All");
//HERE IS THE LIST VIEW WHICH I HAVE CREATED IN MY XML FILE.
ListView lv = (ListView) findViewById(R.id.listView1);
//HERE I AM CREATING CUSTOM ADAPTER OBJECT.
my_custom_adapter adapter = new my_custom_adapter(this, android.R.layout.simple_list_item_1, elements);
lv.addHeaderView(master_cb);
lv.setAdapter(adapter);
master_cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Intent my_intent = new Intent("master_check_change");
my_intent.putExtra("check_value", isChecked);
sendBroadcast(my_intent);
}
});
}
HERE IS MY CUSTOM ADAPTER.
public class my_custom_adapter extends ArrayAdapter<String> {
private Context context = null;
ArrayList<String> elements = null;
private ArrayList<Boolean> itemChecked = null;
public my_custom_adapter(Context context, int type, ArrayList<String> elements)
{
super(context, type, elements);
this.elements = elements;
this.context = context;
itemChecked = new ArrayList<Boolean>();
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("master_check_change")) {
boolean check_value = intent.getBooleanExtra("check_value", false);
set_checked(check_value);
notifyDataSetChanged();
}
}
};
context.registerReceiver(receiver, new IntentFilter("master_check_change"));
set_checked(false);
}
// AS EVERY TIME LISTVIEW INFLATE YOUR VIEWS WHEN YOU MOVE THEM SO YOU NEED TO SAVE ALL OF YOUR CHECKBOX STATES IN SOME ARRAYLIST OTHERWISE IT WILL SET ANY DEFAULT VALUE.
private void set_checked(boolean is_checked)
{
for (int i=0; i < elements.size(); i++) {
itemChecked.add(i, is_checked);
}
}
//THIS IS SIMPLY A CLASS VIEW WILL HOLD DIFFERENT VIEWS OF YOUR ROW.
static class ViewHolder
{
public TextView tv;
public CheckBox cb;
public ImageView iv;
}
#Override
public View getView (final int position, View convertView, ViewGroup parent)
{
View rowView = convertView;
ViewHolder holder = null;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
// HERE I AM INFLATING LISTVIEW LAYOUT.
rowView = inflater.inflate(R.layout.inflated_layout, null, false);
holder = new ViewHolder();
holder.cb = (CheckBox) rowView.findViewById(R.id.checkBox1);
holder.tv = (TextView) rowView.findViewById(R.id.textView1);
holder.iv = (ImageView) rowView.findViewById(R.id.imageView1);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
if (holder != null) {
holder.tv.setText(elements.get(position));
holder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
itemChecked.set(position, isChecked);
}
});
if(position < itemChecked.size()) {
holder.cb.setChecked(itemChecked.get(position));
}
}
return rowView;
}
}
main.xml file is this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
inflated_layout code is :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="17dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/checkBox1"
android:layout_toRightOf="#+id/imageView1"
android:singleLine="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
if you want to use string array instead of arraylist then replace
String[] elements = new String[10];
for (int i = 0; i < 10; i++) {
elements[i] = "elements " + i;
}
// IN YOUR CUSTOM ADAPTER CUNSTRUCTOR
public my_custom_adapter(Context context, int type, String[] elements)
and some more changes accordingly
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_entry, cursor,
fields, new int[] {R.id.contactEntryText});
See in this particular code you are only mapping text source (in field ) with actual textView (R.id.contactEntryText)... So similarly you need to add... another field and corresponding view to map for Checkbox.
or better make a CustomAdapter, you can find tutorials on that and override getView method,you get maximum flexibility.You can do whatever you want to do.
This might help: http://windrealm.org/tutorials/android/listview-with-checkboxes-without-listactivity.php
don,t go with custom list view you can use default listview having the facility of check boxes but only one with each list item read listview on android developer site for list view property. listview having checkbox you just need to set multiselection list view
Edit 1:
follow the link : click here
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, GENRES));
final ListView listView = getListView();
listView.setItemsCanFocus(false);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
Ok, so this has been somewhat addressed alot on this site, however I do not believe the exact problem with what my code uses. I am filling a listView with CheckedTextViews which works completely. However when I click on an item it gets checked but when I scroll up and down random rows are also checked. I realize it must have something to do with how the ListView keeps track of the items. I am running into some errors at the moment. I attempted to fill a hashmap with the list of the rows so I can keep track which one is set to true and which are false. However I am not positive where to implement the map and try to fill it.
Here is my OnCreate
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewmenu);
//Get table name of menu clicked.
Bundle extras = getIntent().getExtras();
tableName = extras.getString("table");
// map each contact's name to a TextView in the ListView layout
String[] from = new String[] { "name" };
int[] to = new int[] { R.id.toppingCheckedTextView };
for(int i=0; i< from.length; i++){
map.put(i, false);
}
contactAdapter = new SimpleCursorAdapter(
ViewToppingListing.this, R.layout.toppings_list_item, null, from, to);
setListAdapter(contactAdapter); // set contactView's adapter
}
I attempt to place the map in the onCreate to fill it however it complains about a nullpointer.
Here is where I tried using the OnListItemClick method
#Override
protected void onListItemClick(ListView arg0, View arg1, int arg2, long arg3){
final int index = arg2 - arg0.getFirstVisiblePosition();
View v = arg0.getChildAt(index);
CheckedTextView ctv = (CheckedTextView) v.findViewById(R.id.toppingCheckedTextView);
if((Boolean)map.get(index) == true){
ctv.setChecked(true);
ctv.setVisibility(View.VISIBLE);
} else{
ctv.setVisibility(View.GONE);
}
}
I have read alot on this, and it seems that alot of solutions involves using getView(), however I don't know if that applies to my situation. Any help would be greatly appreciated!
First of all do you need a SimpleCursorAdapter? You set the adapter with a null cursor:
contactAdapter = new SimpleCursorAdapter(
ViewToppingListing.this, R.layout.toppings_list_item, null, from, to); // the third parameter is the cursor and you set it to null!
The behavior you see it's because of the ListView is recycling views and yes you'll have to implement your own adapter and override bindView(). The code bellow is based on another answer to a similar question maybe you'll want to look at it( Getting the selected View from ListView ). Here is an example:
public class TestCursorAdapter extends ListActivity {
MySimpleAdapter adapter;
private HashMap<Long, Boolean> positionHide = new HashMap<Long, Boolean>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] columns = new String[] { "_id", "name" };
MatrixCursor mc = new MatrixCursor(columns); // cursor for testing
for (int i = 1; i < 35; i++) {
long id = i;
mc.addRow(new Object[] { id, "Name" + i });
}
String[] from = new String[] { "name" };
int[] to = new int[] { R.id.checked_text };
adapter = new MySimpleAdapter(this,
R.layout.adapter_mysimpleadapter_row, mc, from, to);
setListAdapter(adapter);
}
private class MySimpleAdapter extends SimpleCursorAdapter {
public MySimpleAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
CheckedTextView ctv = (CheckedTextView) view
.findViewById(R.id.checked_text);
long pos = cursor.getLong(0); // the id from the cursor
if (positionHide.get(pos) == null) {
ctv.setChecked(false);
// we don't have this id in the hashmap so the value is by
// default false, the TextView is GONE
} else {
// we have the value in the Hashmap so see what it is and set
// the textview visibility from this value
Boolean tmp = positionHide.get(pos);
if (tmp.booleanValue()) {
ctv.setChecked(true);
} else {
ctv.setChecked(false);
}
}
}
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Boolean tmp = positionHide.get(id);
if (tmp == null) {
// if null we don't have this key in the hashmap so
// we add it with the value true
positionHide.put(id, true);
} else {
positionHide.put(id, !tmp.booleanValue());
// if the value exists in the map then inverse it's value
}
adapter.notifyDataSetChanged(); // notify the adapter that something has
// changed
}
}
I want to make a list from some data that I have in my database.
The first two sets of data in my database are first name and last name.
I want my list to show both first and last name instead of now where it only shows the first name. How do I do that? My code looks like this:
private void fillData()
{
Cursor contactCursor = mDbHelper.fetchAllReminders();
startManagingCursor(contactCursor);
String[] from = new String[]{DbAdapter.KEY_FIRST};
int[] to = new int[]{R.id.contactlist};
SimpleCursorAdapter contacts = new SimpleCursorAdapter(this, R.layout.list, contactCursor, from, to);
setListAdapter(contacts);
}
Here is a full implementation. You will need to create a custom row and a custom Array adapter.
Here is a full tutorial http://commonsware.com/Android/excerpt.pdf
This will tell you everything you need to know to get this done.
Also refer here where ive posted another example.
How to add an EditText to a ListView
EDIT: How to build a custom listview and return data from a databse
You first create a listview activity.
public class meeting_list extends ListActivity {
Cursor model = null;
meetingAdapter adapter = null;
//This should be what ever your database helper is from your SQLite base.
meetingHelper helper = null;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.meeting_list);
helper = new meetingHelper(this);
model = helper.getAll();
startManagingCursor
(model);
adapter = new meetingAdapter(model);
setListAdapter(adapter);
registerForContextMenu(getListView());
//Ondestroy is used to close the database to free up resources
#Override
public void onDestroy(){
super.onDestroy();
helper.close();
}
#Override
public void onListItemClick(ListView list, View view, int position, long id){
Intent i = new Intent(meeting_list.this, meeting_create_edit.class);
//
i.putExtra(ID_EXTRA, String.valueOf(id));
startActivity(i);
}
//Here create a class to extend the Cursor Adapter
class meetingAdapter extends CursorAdapter{
meetingAdapter(Cursor c){
super(meeting_list.this, c);
}
#Override
public void bindView(View row, Context ctxt, Cursor c) {
meetingHolder holder = (meetingHolder)row.getTag();
holder.populateFrom(c, helper);
}
#Override
public View newView(Context ctxt, Cursor c, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.mrow, parent, false);
meetingHolder holder = new meetingHolder(row);
row.setTag(holder);
return row;
}
}
//Here create a class to actually hold the view for the row in the listview.
static class meetingHolder{
private TextView mtitle = null;
private TextView maddress = null;
private ImageView Icon = null;
meetingHolder(View row){
mtitle=(TextView)row.findViewById(R.id.mtitle);
maddress = (TextView)row.findViewById(R.id.address);
Icon = (ImageView)row.findViewById(R.id.Micon);
}
//Here populate the row with the data from your database
void populateFrom(Cursor c, meetingHelper helper){
mtitle.setText(helper.getMettingTitle(c));
maddress.setText(helper.getAddress(c));
This should do it. Just substitute your informations where it should be. This is a tutorial ive put together for you.
Now i have tried to edit the code according to your guide, what i have done for now looks like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
mDbHelper = new DbAdapter(this);
mDbHelper.open();
fillData();
}
private void fillData()
{
Cursor contactCursor = mDbHelper.fetchAllReminders();
startManagingCursor(contactCursor);
String[] from = new String[]{DbAdapter.KEY_FIRST};
int[] to = new int[]{R.id.contactlist};
SimpleCursorAdapter contactsfirst = new SimpleCursorAdapter(this, R.layout.list, contactCursor, from, to);
String[] from2 = new String[]{DbAdapter.KEY_LAST};
int[] to2 = new int[]{R.id.contactlist};
SimpleCursorAdapter contactslast = new SimpleCursorAdapter(this, R.layout.list, contactCursor, from2, to2);
setListAdapter(contactsfirst,last);
}
And my xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="#+id/first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
/>
<TextView
android:id="#+id/last"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
/>
</LinearLayout>