Aligning ListView text element with TableLayout element - android

I have a TableLayout with two TextView elements representing the headings of a table, beneath the TableLayout I have a ListView each ListView item has a checkbox followed by two TextView elements representing the value for that column. So something like this
The problem is the right hand column isn't aligned with the TableRow second cell. Instead it appears just to the right of the end of the first ListView item text element. How can I acheive this?
Here's what I have
main_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/parentPanel"
tools:context=".MainActivity">
<TableLayout
android:id="#+id/surveys_table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin">
<TableRow android:layout_height="wrap_content">
<TextView
android:id="#+id/survey_name_header"
android:layout_weight="1"
android:background="#drawable/header_cell_shape"
android:text="#string/survey_name"
android:textColor="#color/white" />
<TextView
android:id="#+id/survey_class_header"
android:layout_weight="1"
android:background="#drawable/header_cell_shape"
android:text="#string/survey_class"
android:textColor="#color/white" />
</TableRow>
</TableLayout>
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/surveys_table" />
</RelativeLayout>
survey_list_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="33dp"
android:layout_marginTop="7dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="7dp"
android:layout_toRightOf="#+id/textView1" />
</RelativeLayout>
I would assume I need to modify the layout parameters in the addView() method of my ListAdapter but I'm not sure how?
#Override
public View getView(int position, View convertView, final ViewGroup parent) {
View view = null;
if (convertView == null) {
view = inflater.inflate(R.layout.survey_list_item, parent, false);
CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBox1);
TextView surveyName = (TextView) view.findViewById(R.id.textView1);
TextView surveyClass = (TextView) view.findViewById(R.id.textView2);
//What to do here?
} else {
view = convertView;
}
return view;
}

Related

List view updates view which are not touched

Sorry for the bad title.
I am adding a toggle button at run-time to the list view.
When I click on the button its state gets changed. But the problem is that when I scroll down other items get selected too.
For example, if click on first toggle button the 6th toggle button gets checked too.
This is a very weird behavior.
Here is the XML code for the acitivty
<?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="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/title_bar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/eros_now_title_bar">
<ImageView
android:id="#+id/back_button_image"
android:layout_width="#dimen/toolbar_imageview_width_height"
android:layout_height="#dimen/toolbar_imageview_width_height"
android:layout_centerVertical="true"
android:layout_margin="#dimen/back_image_margin"
android:src="#drawable/ic_arrow_back" />
<ImageButton
android:id="#+id/back_button"
android:layout_width="#dimen/toolbar_imagebutton_width_height"
android:layout_height="#dimen/toolbar_imagebutton_width_height"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:background="#drawable/npd_back_feedback" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/back_button_image"
android:layout_toRightOf="#+id/back_button"
android:fontFamily="sans-serif-regular"
android:text="Invite friends"
android:textColor="#color/white"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
<EditText
android:id="#+id/search_text"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#+id/title_bar"
android:layout_margin="15dp"
android:hint="Search"
android:textColorHint="#color/hint_color"
android:drawableRight="#drawable/search_blue_white"/>
<ListView
android:id="#+id/friends_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/search_text"
android:layout_above="#+id/invite_count_view"
android:layout_marginTop="15dp"
android:divider="#color/transparent">
</ListView>
<RelativeLayout
android:id="#+id/invite_count_view"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:textSize="18sp"
android:textColor="#color/eros_now_title_bar"
android:text="10 more invites to go"
android:layout_centerInParent="true"
/>
</RelativeLayout>
</RelativeLayout>
Here is the XML code for the view I am inflating in the adapter
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/card_view"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp">
<com.makeramen.roundedimageview.RoundedImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/person_pic"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="26dp"
android:layout_centerVertical="true"
app:riv_corner_radius="50dip"
android:src="#drawable/ic_person_grey600_36dp"/>
<TextView
android:id="#+id/name"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:singleLine="true"
android:layout_toRightOf="#+id/person_pic"
android:layout_marginLeft="17dp"
android:layout_centerVertical="true"
android:textColor="#color/black"
android:fontFamily="sans-serif-regular"
android:textSize="16sp"
android:text="Grumpy brother"
android:ellipsize="end"/>
<!--<ToggleButton-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_alignParentRight="true"-->
<!--android:layout_marginRight="35dp"-->
<!--android:layout_centerVertical="true"-->
<!--android:background="#drawable/invite_selector"-->
<!--android:textOn=""-->
<!--android:textOff=""-->
<!--android:text="" />-->
</RelativeLayout>
here is the code for getView
public View getView(int i, View convertView, ViewGroup parent) {
Log.e("getView", "name = " + contacts.get(i).getName());
if (convertView == null) {
convertView = LayoutInflater.from(activity).inflate(R.layout.friend_view, parent, false);
}
TextView name = (TextView) convertView.findViewById(R.id.name);
RoundedImageView image = (RoundedImageView) convertView.findViewById(R.id.person_pic);
ToggleButton selector = new ToggleButton(activity);
if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN)
selector.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.ic_add_black_24dp));
else
selector.setBackground(activity.getResources().getDrawable(R.drawable.ic_add_black_24dp));
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
params.addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE);
params.setMargins(0, 0, getPx(35), 0);
selector.setLayoutParams(params);
selector.setTextOff("");
selector.setText("");
selector.setTextOn("");
selector.setOnCheckedChangeListener(this);
selector.setTag(String.valueOf(i));
selector.setChecked(contacts.get(i).getSelectedState());
contacts.get(i).setButton(selector);
RelativeLayout card = (RelativeLayout) convertView.findViewById(R.id.card_view);
card.addView(selector);
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(activity.getContentResolver(), contacts.get(i).getURI());
} catch (IOException e) {
contacts.get(i).setURI(Uri.parse(""));
}
name.setText(contacts.get(i).getName());
if (!contacts.get(i).getURI().toString().equals(""))
image.setImageURI(contacts.get(i).getURI());
else
image.setImageDrawable(activity.getResources().getDrawable(R.drawable.ic_person));
return convertView;
}
I saw that you used contacts.get(i).getSelectedState() to set ToggleButton state in getview.
Did you use Adapter.notifyDatasetChanged() to update the listview's state?
If not, add that code after toggle the button.
Because your item view is reused so only create ToggleButton if convertView is null. If convertView is not null, get it from convertView and set it's properties.
Use setTag and findViewWithTag to identity your ToggleButton.

Memory issue with items in BaseAdapter

I have a ListView where every row view is inflated from this xml layout (by activity layout inflater):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:baselineAligned="false"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/topic_list_item_selector"
android:orientation="horizontal"
android:paddingLeft="#dimen/list_item_padding_horizontal_big"
android:paddingBottom="#dimen/list_item_padding_vertical_big"
android:paddingTop="#dimen/list_item_padding_vertical_big" >
<LinearLayout
android:id="#+id/topics_list_edit_row_label_color"
android:layout_width="#dimen/label_rect_edge_size"
android:layout_height="#dimen/label_rect_edge_size"
android:orientation="vertical" >
<TextView
android:id="#+id/topics_list_edit_row_label_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#ffffff"
android:textSize="#dimen/label_text_size" />
</LinearLayout>
<LinearLayout
android:id="#+id/topics_list_edit_row_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="#+id/topics_list_edit_row_text_title"
style="#style/DefaultText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="title" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/topics_list_edit_row_text_detail_section"
style="#style/DetailText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2 sections"
android:textColor="#818181" />
<TextView
android:id="#+id/topics_list_edit_row_text_detail_point"
style="#style/DetailText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="85 points"
android:textColor="#818181" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
When I return this view in my Adapter via getView(), everything works normally.
But when I change some text in TextView via setText() in getView(), my app consumes about 1mb more with every screen rotation:
public class TopicListAdapter extends BaseAdapter{
...
public View getView(int position, View convertView, ViewGroup parent) {
View rowView;
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.topics_list_edit_row, parent, false);
...
Typeface myTypeface = Typeface.createFromAsset(context.getAssets(), "Roboto-Thin.ttf"); //Added
TextView labelT = (TextView) rowView.findViewById(R.id.topics_list_edit_row_label_text);
labelT.setTypeface(myTypeface); //Added
labelT.setText("A"); //!!!! when I delete this line everything works normally !!!!
...
}
...
}
}
I think maybe my activity context remains alive after every screen rotation but if this is the case then how does it happen and how do I avoid it?

Views in list are not at the corrects size

I have a list view and an adapter. My problem is that the rows in the list are always "wrap content" even though I specifically stated the height as "150dp".
This is my adapter:
private class LiveEventsAdapter extends BaseAdapter {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View contentView = convertView;
if (contentView == null) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
contentView = inflater.inflate(R.layout.live_match_row, null);
TypefaceManager.getInstance(LiveMatchActivity.this).assignTypeface(contentView);
}
if (events != null) {
Event event = events.getEventsList().get(position);
TypefaceManager typefaceManager = TypefaceManager.getInstance(LiveMatchActivity.this);
TextView eventText;
TextView minute;
ImageView eventIcon;
minute = (TextView) contentView.findViewById(R.id.live_match_minute);
if (event.getOrientation().equals("home")) {
eventText = (TextView) contentView.findViewById(R.id.home_team_event_text);
eventIcon = (ImageView) contentView.findViewById(R.id.home_team_event_img);
} else {
eventText = (TextView) contentView.findViewById(R.id.away_team_event_text);
eventIcon = (ImageView) contentView.findViewById(R.id.away_team_event_img);
}
minute.setText(event.getMinute() + "\'");
eventText.setText(event.getDescription());
minute.setTypeface(typefaceManager.getTypeface("bold"));
eventText.setTypeface(typefaceManager.getTypeface("bold"));
}
}
That's how I "setadapter":
ListView eventsListView = (ListView) findViewById(R.id.live_match_list);
eventsListView.setAdapter(new LiveEventsAdapter());
and that's my rows layout:
<?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="150dp"
android:background="#drawable/background_button" >
<TextView
android:id="#+id/live_match_minute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="82&apos;" />
<TextView
android:id="#+id/home_team_event_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="false"
android:layout_alignRight="#+id/home_team_event_img"
android:layout_centerVertical="true"
android:paddingLeft="4dp"
android:paddingRight="4dp" />
<ImageView
android:id="#+id/home_team_event_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_marginRight="4dp"
android:layout_toLeftOf="#+id/live_match_minute" />
<ImageView
android:id="#+id/away_team_event_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="false"
android:layout_centerVertical="true"
android:layout_marginLeft="4dp"
android:layout_toRightOf="#+id/live_match_minute" />
<TextView
android:id="#+id/away_team_event_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="false"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/away_team_event_img"
android:paddingLeft="4dp"
android:paddingRight="4dp" />
Why is it that the rows in my list are always set on the same size, no matter what I put in?
This will NOT work.
This is what you should do.
<?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" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#drawable/background_button" >
<TextView
android:id="#+id/live_match_minute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="82&apos;" />
</RelativeLayout>
</RelativeLayout>
Basically, add another layout and specify the height to that layout.
When working with adapter views specifying a height for the root layout doesn't work.

Listview with Textview and 2 Edittext in a single row in android

I want to have a single "Textview" and 2 "Edittext" in a single row.I have tried the following code:
Xml:
<?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="fill_parent"
android:orientation="horizontal"
>
<TextView
android:id="#+id/vegtext"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<EditText
android:id="#+id/vegquantity"
android:hint="Qty"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_weight="1"
/>
<EditText
android:id="#+id/vegprice"
android:layout_width="100dp"
android:layout_height="100dp"
android:hint="price"
android:layout_weight="1"
/>
</LinearLayout>
Getview in adapter:
#Override
public View getView(int position, View convertview, ViewGroup parent)
{
VeglistHolder holder;
// TODO Auto-generated method stub
if(convertview == null)
{
LayoutInflater inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertview = inflater.inflate(R.layout.veglistrow, null);
holder = new VeglistHolder();
holder.text = (TextView) convertview
.findViewById(R.id.vegtext);
holder.Qtyedit = (EditText) convertview
.findViewById(R.id.vegquantity);
holder.priceedit = (EditText) convertview
.findViewById(R.id.vegprice);
convertview.setTag(holder);
}
else
{
holder = (VeglistHolder) convertview.getTag();
}
holder.text.setText(vegitems[position]);
return convertview;
}
But the Edittext is not there in the row. Also some blank space is coming below the "TExtview" .I searched for "Lots of" post regarding this but i cant solve this.Please help me to solve this..Thanks in advance.
Try to use weightSum in main layout for equal partition as below
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="3"
android:orientation="vertical" >
Use a Realative layout. Modify the attributes to suit your requirement
<?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="match_parent"
>
<TextView
android:id="#+id/textView1"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="58dp"
android:text="TextView" />
<EditText
android:id="#+id/editText1"
android:layout_width="60dp" //set to whatever you desire
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView1"
android:layout_alignBottom="#+id/textView1"
android:layout_marginLeft="30dp"
android:layout_toRightOf="#+id/textView1"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="60dp"// //set to whatever you desire
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/editText1"
android:layout_alignBottom="#+id/editText1"
android:layout_alignParentRight="true"
android:layout_marginRight="70dp"
android:ems="10"
android:inputType="textPersonName" />
</RelativeLayout>
Well your problem is that you set the width and the height of the TextView to fill parent

How to center align a textview below imageview as a title in a List view

I am not able to align a text below imageview in a listview. below is my code.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="50dp" />
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/icon"
android:layout_alignParentBottom="true"
android:gravity="center"
android:singleLine="true"
android:layout_marginLeft="20dp"
android:textColor="#color/white"/>
</RelativeLayout>
and ListView is
<ListView android:id="#+id/grid_view"
android:layout_width="wrap_content"
android:layout_height="280sp"
android:layout_alignParentLeft="true"
android:layout_marginTop="20dp"
android:layout_toLeftOf="#+id/imgView"
android:scrollbars="none"
android:divider="#null"
android:gravity="center" >
</ListView>
java code to dynamically add text and image from array
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = l_Inflater.inflate(R.layout.gridview_layout, null);
holder = new ViewHolder();
holder.imageName = (TextView) convertView.findViewById(R.id.name);
holder.image = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.imageName.setText(name[position]);
holder.image.setImageResource(mThumbIds[position]);
return convertView;
}
not getting how to center text below imageview. please help me to solve this problem.
Simply use the layout_centerHorizontal attribute in your TextView:
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/icon"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:singleLine="true"
android:textColor="#color/white" />
In textview change
android:layout_width="wrap_content"
to
android:layout_width="match_parent"
You should use Linear layout to set orientation attribute. Set orientation to vertical for Linear layout and set imageview attribute android:layout_gravity="center"
Here is the complete code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:layout_marginBottom="#dimen/activity_horizontal_margin"
>
<ImageView
android:id="#+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/home" >
</ImageView>
<TextView
android:id="#+id/grid_item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cardiology"
android:layout_gravity="center"
android:layout_marginTop="5px"
android:textSize="20sp" >
</TextView>
</LinearLayout>
hope it will help

Categories

Resources