God
God

Reputation: 1248

ListView containing TextView and ListView containing EditText not on the same line

So the picture will give you a better understanding of the problem:

enter image description here

See , i added 3 TextView to the left ListView and 3 EditText to the right ListView but they are not on the same line. So how do i solve it?

My related to the issue XML:

 <LinearLayout
            android:id="@+id/inner_linear_layout_id"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@id/date_picker_id"
            android:baselineAligned="false"
            android:orientation="horizontal"
            android:weightSum="1" >

        <ListView
            android:id="@+id/dates_list_view_id"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5" 
            />

        <ListView
            android:id="@+id/comments_list_view_id"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5" 
            />
    </LinearLayout>

My related to the issue Java:

arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,datesListView.getId());
datesListView.setAdapter(arrayAdapter);
commentsListView.setAdapter(arrayAdapter);


date.setTextSize(20);
datesListView.addFooterView(date);
commentText.setId(998);
commentText.setInputType(EditorInfo.TYPE_CLASS_TEXT);       
commentText.setHint("Add a comment");
commentText.setTextSize(15);

commentsListView.addFooterView(commentText); 

date is a TextView and commentText is an EditText.

Upvotes: 2

Views: 411

Answers (1)

Matthias
Matthias

Reputation: 5764

Ok, here's one solution. Of course there are many other possible solutions.

Here's the XML for the list item. It describes the view for a single entry in your list. Here it is built with a TextView and an EditText:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">

    <TextView
            android:id="@+id/text_datetime"
            android:text="8:20p.m."
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    <EditText
            android:id="@+id/edit_comment"
            android:text="no comment"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

</LinearLayout>

That's the XML for the main activity which actually contains the ListView itself. But it does not indicate how the children (the list items) will look like because that's defined by the XML above:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >
    <ListView
            android:id="@+id/my_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />
</LinearLayout>

And that's the code for the MainActivity which includes some demo data that is bound to the ListView. This actually connects the ListView with it's items and the corresponding XML for the ListView Items:

package com.example.myapp;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.*;

public class MyActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ListView list = (ListView)findViewById(R.id.my_list);
        if (list != null) {
            final List<DemoItem> demoList = new ArrayList<DemoItem>();

            final SimpleDateFormat hourDateFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());
            final Calendar c = Calendar.getInstance();
            final Date now = c.getTime();
            final String now_ = hourDateFormat.format(now);

            demoList.add(0, new DemoItem(now_, "first comment"));
            demoList.add(1, new DemoItem(now_, "second comment"));
            demoList.add(2, new DemoItem(now_, "third comment"));

            DemoAdapter adapter = new DemoAdapter(this, demoList);
            list.setAdapter(adapter);
        }
    }

    class DemoItem {
        public String datetime;
        public String comment;

        public DemoItem(String datetime, String comment) {
            this.datetime = datetime;
            this.comment = comment;
        }
    }

    class DemoAdapter extends ArrayAdapter<DemoItem> {

        public DemoAdapter(Context context, List<DemoItem> items) {
            super(context, R.layout.list_item, items);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if (convertView == null)
                convertView = getLayoutInflater().inflate(R.layout.list_item, null);

            DemoItem item = super.getItem(position);

            EditText editComment = (EditText)convertView.findViewById(R.id.edit_comment);
            if (editComment != null)
                editComment.setText(item.comment);

            TextView txtDatetime = (TextView)convertView.findViewById(R.id.text_datetime);
            if (txtDatetime != null)
                txtDatetime.setText(item.datetime);

            return convertView;
        }
    }
}

That's how it looks like in the end:

looks like that

And if you won't use a ListView, then here's a RelativeLayout that does the same. But it's not dynamically and not bound to any adapter:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <TextView
            android:id="@+id/text_datetime1"
            android:text="8:20p.m."
            android:layout_toLeftOf="@+id/edit_comment1"
            android:layout_alignBaseline="@+id/edit_comment1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>

    <EditText
            android:id="@+id/edit_comment1"
            android:text="no comment"
            android:layout_alignParentRight="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    <TextView
            android:id="@+id/text_datetime2"
            android:text="8:20p.m."
            android:layout_toLeftOf="@+id/edit_comment2"
            android:layout_alignBaseline="@+id/edit_comment2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>

    <EditText
            android:id="@+id/edit_comment2"
            android:text="no comment"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/edit_comment1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    <TextView
            android:id="@+id/text_datetime3"
            android:text="8:20p.m."
            android:layout_toLeftOf="@+id/edit_comment3"
            android:layout_alignBaseline="@+id/edit_comment3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>

    <EditText
            android:id="@+id/edit_comment3"
            android:text="no comment"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/edit_comment2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

</RelativeLayout>

Upvotes: 1

Related Questions