How to make custom ListView like attached image - android

I'm working with RSS Feed. I use ListView with Adapter to display data in custom view containing "LinearLayout with Image and Text". This view is repeated in each row. But, I need to make custom view like the image below to display data with different view.

Steps:
The tabbar can be done with horizontal scrollview with buttons inside. on button click change that button width and height. and reset last selected one.
the colored horziontal line can be another linear layout. you change its color on button click.
you can have to different layouts in the same listview. one for two items and one for one.
in adapter getCount method you should calculate the number of rows you have. beased on the algorithm used to decide which rows can have one item and which rows can have two.
That listview is not that complicated as you may think.
Working Example:
I created a github repository for it.
Adapter Class:
public class ArticlesAdapter extends ArrayAdapter<Article> {
int mod;
public ArticlesAdapter(Context context, int resource, ArrayList<Article> teams) {
super(context, resource, teams);
}
#Override
public View getView(int position, View view, ViewGroup parent) {
int resource = R.layout.single_item;
if (position % 2 == 0)
resource = R.layout.double_item;
if (position == getCount()-1 && mod == 1)
resource = R.layout.single_item;
if (view == null || view.getTag() != resource)
{
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(resource, null);
view.setTag(resource);
}
TextView tv1 = (TextView)view.findViewById(R.id.textView1);
ImageView iv1 = (ImageView)view.findViewById(R.id.imageView1);
int index1 = (position * 3 / 2) + (position * 3) % 2;
Article article1 = getItem(index1);
tv1.setText(article1.getDesc());
iv1.setImageResource(article1.getImageUrl());
if (resource == R.layout.double_item)
{
TextView tv2 = (TextView)view.findViewById(R.id.textView2);
ImageView iv2 = (ImageView)view.findViewById(R.id.imageView2);
int index2 = index1 + 1;
Article article2 = getItem(index2);
tv2.setText(article2.getDesc());
iv2.setImageResource(article2.getImageUrl());
}
return view;
}
#Override
public int getCount() {
int count = super.getCount();
mod = count % 3;
count = count / 3 * 2;
return count + (mod > 0 ? 1 : 0);
}
}
Main Activity Layout:
<LinearLayout 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:orientation="vertical">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="#dimen/tabbar_height"
android:background="#android:color/black"
android:scrollbars="none">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="bottom"
android:baselineAligned="false">
<Button
android:id="#+id/button1"
android:layout_width="#dimen/button_width"
android:layout_height="match_parent"
android:background="#color/red"
android:text="#string/top_title"
android:textColor="#color/white"
android:textSize="#dimen/tabbar_text_size"
android:textAllCaps="false"
android:tag="0"
android:onClick="tabClicked"/>
<Button
android:id="#+id/button2"
android:layout_width="#dimen/button_width"
android:layout_height="#dimen/button_height"
android:background="#color/orange"
android:text="#string/entertain_title"
android:textColor="#color/white"
android:textSize="#dimen/tabbar_text_size"
android:textAllCaps="false"
android:tag="1"
android:onClick="tabClicked"/>
<Button
android:id="#+id/button3"
android:layout_width="#dimen/button_width"
android:layout_height="#dimen/button_height"
android:background="#color/green"
android:text="#string/world_title"
android:textColor="#color/white"
android:textSize="#dimen/tabbar_text_size"
android:textAllCaps="false"
android:tag="2"
android:onClick="tabClicked"/>
<Button
android:id="#+id/button4"
android:layout_width="#dimen/button_width"
android:layout_height="#dimen/button_height"
android:background="#color/blue"
android:text="#string/biz_title"
android:textColor="#color/white"
android:textSize="#dimen/tabbar_text_size"
android:textAllCaps="false"
android:tag="3"
android:onClick="tabClicked"/>
<Button
android:id="#+id/button5"
android:layout_width="#dimen/button_width"
android:layout_height="#dimen/button_height"
android:background="#color/purple"
android:text="#string/sport_title"
android:textColor="#color/white"
android:textSize="#dimen/tabbar_text_size"
android:textAllCaps="false"
android:tag="4"
android:onClick="tabClicked"/>
<Button
android:id="#+id/button6"
android:layout_width="#dimen/button_width"
android:layout_height="#dimen/button_height"
android:background="#color/pink"
android:text="#string/games_title"
android:textColor="#color/white"
android:textSize="#dimen/tabbar_text_size"
android:textAllCaps="false"
android:tag="5"
android:onClick="tabClicked"/>
<Button
android:id="#+id/button7"
android:layout_width="#dimen/button_width"
android:layout_height="#dimen/button_height"
android:background="#color/yellow"
android:text="#string/tech_title"
android:textColor="#color/white"
android:textSize="#dimen/tabbar_text_size"
android:textAllCaps="false"
android:tag="6"
android:onClick="tabClicked"/>
</LinearLayout>
</HorizontalScrollView>
<LinearLayout
android:id="#+id/tabbarLineLL"
android:layout_width="match_parent"
android:layout_height="6dp"
android:background="#color/red"
android:orientation="vertical">
</LinearLayout>
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:divider="#android:color/darker_gray"
android:dividerHeight="1dp">
</ListView>
</LinearLayout>
Tab Clicked Method:
public void tabClicked(View view)
{
LinearLayout.LayoutParams tempLL;
// reset current selected button size
Button currentButton = tabs[selected_index];
tempLL = (LinearLayout.LayoutParams)currentButton.getLayoutParams();
tempLL.width = (int) getResources().getDimension(R.dimen.button_width);
tempLL.height = (int) getResources().getDimension(R.dimen.button_height);
currentButton.setLayoutParams(tempLL);
// change selected tab
selected_index = Integer.parseInt(view.getTag().toString());
// change color for the new selected button
tempLL = (LinearLayout.LayoutParams)view.getLayoutParams();
tempLL.width = (int) getResources().getDimension(R.dimen.button_width);
tempLL.height = (int) getResources().getDimension(R.dimen.tabbar_height);
view.setLayoutParams(tempLL);
// change tabbar line color
ColorDrawable buttonColor = (ColorDrawable) view.getBackground();
tabbarLineLL.setBackgroundColor(buttonColor.getColor());
setupAdapter();
}
public void setupAdapter()
{
// setup adapter for selected tab
articlesAdapter = new ArticlesAdapter(this, 0, articles.get(selected_index));
listview.setAdapter(articlesAdapter);
articlesAdapter.notifyDataSetChanged();
}

Related

Android Custom ListView. Showing different row layout after clicking on it

I'm newbie in android studio and I face this problem for two hours. Was looking for some olution in other answers and other websites without any result.
I've got fragment with ListView which contain some tasks to do. All tasks are stored in Database and I'm using Custom CursorAdapter to represent these data.
I have got two different row_layouts, one is with just image, title of task and deadline. Second one has the same stuff, two buttons and description of single task. User after clicking on single row in ListView should see these additional buttons and TextView. I've tried to to by myself but i get lost.
Here is adapter code:
public class ChallengeAdapter extends CursorAdapter {
private int position = 1;
public static class ChallengeViewHolder {
ImageView challengeIcon;
TextView challengeTitle;
TextView challengeDeadline;
TextView challengeDescription;
Button bFinished;
Button bRemove;
}
public static final int[] challengeIcons = {
R.drawable.brain_icon,
R.drawable.book_icon,
R.drawable.workout_icon
};
public ChallengeAdapter(Context context, Cursor cursor) {
super(context, cursor, 0);
}
public void selectedItem(int position) {
this.position = position;
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
ChallengeViewHolder holder = new ChallengeViewHolder();
if(this.position == cursor.getPosition()) {
View view2 = LayoutInflater.from(context).inflate(R.layout.extended_row_challenge_list, parent, false);
holder.challengeIcon = (ImageView) view2.findViewById(R.id.ChallengeIconExtended);
holder.challengeTitle = (TextView) view2.findViewById(R.id.ChallengeTitleExtended);
holder.challengeDeadline = (TextView) view2.findViewById(R.id.ChallengeDeadlineExtended);
holder.challengeDescription = (TextView) view2.findViewById(R.id.ChallengeDescriptionExtended);
holder.bFinished = (Button) view2.findViewById(R.id.bFinished);
holder.bRemove = (Button) view2.findViewById(R.id.bRemove);
view2.setTag(holder);
return view2;
}
View view = LayoutInflater.from(context).inflate(R.layout.row_challenge_list, parent, false);
holder.challengeIcon = (ImageView) view.findViewById(R.id.ChallengeIcon);
holder.challengeTitle = (TextView) view.findViewById(R.id.ChallengeTitle);
holder.challengeDeadline = (TextView) view.findViewById(R.id.ChallengeDeadline);
view.setTag(holder);
return view;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
ChallengeViewHolder holder = (ChallengeViewHolder) view.getTag();
holder.challengeTitle.setText(cursor.getString(cursor.getColumnIndexOrThrow("title")));
holder.challengeDeadline.setText(cursor.getString(cursor.getColumnIndexOrThrow("deadline")));
holder.challengeIcon.setImageResource(challengeIcons[cursor.getInt(cursor.getColumnIndexOrThrow("icon_id"))]);
}
}
Here comes code of single row:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/normalRowLayout"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/ChallengeIcon"
android:layout_width="80dp"
android:layout_height="80dp"
/>
<TextView
android:id="#+id/ChallengeTitle"
android:layout_width="270dp"
android:layout_height="25dp"
android:textStyle="bold"
android:textSize="18sp"
android:text="HERE COMES THE BASS!"
android:layout_toRightOf="#+id/ChallengeIcon"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:textColor="#3696E4"
/>
<TextView
android:id="#+id/ChallengeDeadline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="28 days 6 hours 7 minutes 16 secunds"
android:layout_toRightOf="#+id/ChallengeIcon"
android:layout_marginLeft="15dp"
android:layout_below="#+id/ChallengeTitle"
android:layout_marginTop="15dp"
android:textColor="#FF0000"
/>
and extended row:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/extendedRowLayout"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:id="#+id/ChallengeIconExtended"
android:layout_width="80dp"
android:layout_height="80dp"
/>
<TextView
android:id="#+id/ChallengeTitleExtended"
android:layout_width="270dp"
android:layout_height="25dp"
android:textStyle="bold"
android:textSize="18sp"
android:text="HERE COMES THE BASS!"
android:layout_toRightOf="#+id/ChallengeIconExtended"
android:layout_marginLeft="25dp"
android:layout_marginTop="15dp"
android:textColor="#3696E4"
/>
<TextView
android:id="#+id/ChallengeDescriptionExtended"
android:layout_width="290dp"
android:layout_height="wrap_content"
android:textSize="15sp"
android:layout_toRightOf="#+id/ChallengeIconExtended"
android:layout_marginLeft="5dp"
android:layout_below="#+id/ChallengeTitleExtended"
android:layout_marginTop="15dp"
android:text="description description description description description description description description description description description description vdescription description description description description description description"
android:textColor="#000000"
/>
<TextView
android:id="#+id/ChallengeDeadlineExtended"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="28 days 6 hours 7 minutes 16 secunds"
android:layout_toRightOf="#+id/ChallengeIconExtended"
android:layout_marginLeft="25dp"
android:layout_below="#+id/ChallengeDescriptionExtended"
android:layout_marginTop="15dp"
android:textColor="#FD1103"
/>
<Button
android:id="#+id/bFinished"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_below="#+id/ChallengeDeadlineExtended"
android:layout_marginTop="10dp"
android:text="Finished"
android:background="#drawable/roundedbuttongreen"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
/>
<Button
android:id="#+id/bRemove"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_below="#+id/ChallengeDeadlineExtended"
android:layout_marginTop="10dp"
android:text="Remove"
android:background="#drawable/roundedbuttonblue"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
/>
Also adding Photo how it looks like when i set position in adapter to 1:

how to set the specific row item option(visible, invisible) in listview in android

frist my english skill weak.
description>
this is list view.
ㅡㅡㅡㅡㅡㅡㅡㅡ
ㅣㅡㅡㅡㅡㅡㅣㅢ <-- this is button , i init set invisible
ㅣㅡㅡㅡㅡㅡㅣㅢ
ㅣㅡㅡㅡㅡㅡㅣㅢ
ㅣㅡㅡㅡㅡㅡㅣㅢ
ㅣㅡㅡㅡㅡㅡ ////// <-- i want make visible button
ㅣㅡㅡㅡㅡㅡ ////// <-- specific position
I make the custom ListView
ListView row contains texts, Button.
The Button is set invisible option in xml file.
then, I want set the visible specific row button.
I tried that and failed
after make ArrayList for ListView, marking matching position like this
for(i=0; i<arraylist.size(); i++){
int t41=Integer.parseInt(arraylist.get(i).getm());
if(month == t41){
confirm_replay[i]=true;
temp55=i;
}
}
I can set the textValue. through adapter.getItem(int position).
but i don't know, how to control specific button.
also try add button into adapter. failed..
also search question in google but my eng skill bad. failed
add my code.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:paddingTop="10dp"
android:text="match day(weekend)"
android:textSize="28dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:background="#2d000000"
android:layout_width="match_parent"
android:layout_height="3dp">
</LinearLayout>
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/m"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:paddingLeft="10dp"
android:id="#+id/d"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/yoil"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/time"
android:textSize="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/vsTeam"
android:textSize="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/league"
android:paddingLeft="10dp"
android:textSize="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/공갈"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button_youtube"
android:text="다시보기"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"/>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/m"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:paddingLeft="10dp"
android:id="#+id/d"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/yoil"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/time"
android:textSize="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/vsTeam"
android:textSize="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/league"
android:paddingLeft="10dp"
android:textSize="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/공갈"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button_youtube"
android:text="다시보기"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"/>
</LinearLayout>
</LinearLayout>
</b>
adapter
class MlistViewAdapter extends BaseAdapter {
// Declare Variables
Context mContext;
LayoutInflater inflater;
private List<MatchInfomation> matchinformationlist = null;
private ArrayList<MatchInfomation> arraylist;
public MlistViewAdapter(Context context,
List<MatchInfomation> matchinformationlist) {
mContext = context;
this.matchinformationlist = matchinformationlist;
inflater = LayoutInflater.from(mContext);
this.arraylist = new ArrayList<MatchInfomation>();
this.arraylist.addAll(matchinformationlist);
}
public class ViewHolder {
TextView m;
TextView d;
TextView yoil;
TextView vsTeam;
TextView time;
TextView league;
}
#Override
public int getCount() {
return matchinformationlist.size();
}
#Override
public MatchInfomation getItem(int position) {
return matchinformationlist.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.activity_match_list, null);
button_youtube.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_SEARCH);
intent.setPackage("com.google.android.youtube");
intent.putExtra("query", "Android");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
}
});
// Locate the TextViews in listview_item.xml
holder.m = (TextView) view.findViewById(R.id.m);
holder.d = (TextView) view.findViewById(R.id.d);
holder.yoil = (TextView) view.findViewById(R.id.yoil);
holder.vsTeam = (TextView) view.findViewById(R.id.vsTeam);
holder.time = (TextView) view.findViewById(R.id.time);
holder.league = (TextView) view.findViewById(R.id.league);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// Set the results into TextViews
holder.m.setText(matchinformationlist.get(position).getm());
holder.d.setText(matchinformationlist.get(position).getd());
holder.yoil.setText(matchinformationlist.get(position).getyoil());
holder.vsTeam.setText(matchinformationlist.get(position).getvsTeam());
holder.time.setText(matchinformationlist.get(position).gettime());
holder.league.setText(matchinformationlist.get(position).getleague());
return view;
}
}
If you want to adjust a specific row, you can use the position parameter in your getView() method in adapter. For instance;
if(position==55){
holder.m.setVisibility(View.GONE);
}
I'm not sure i understand your question.
If i'm right you juste want your button to be invisble for specific row. To do so add the specific line at the end of your getView method
yourButton.setVisibility(View.INVISIBLE)
If you want to hide the entire row, the best way will probably be to change your adapter to diplay only row with content.
before setting the values to the view check the condition whatever you want like:-
if condition is true then set your view visible
else set your view invisible
if(true){
button.setVisibility(View.VISIBLE);
}else{
button.setVisibility(View.GONE);
}

OnClickListner not called on the whole View

On an Android application I am developing i got this situation.
In a Fragment there is a ListView that contains a list of customers (stored in instances of the Customer_InputOld class) using a customized ArrayAdapter (named ArrayAdapter_ClientiInput). The single row contains a ViewFlipper with two TextViews and seven nearly identical TextViews.
I put a ClickListner and a TouchListner in the Adapter, so on the click (and the touch) of a line the application make something. This works, except for the fact that these events are not triggered on the whole row.
Only the rough 80% of the row is "clickable", while the last textView does not respond to the listner.
Sincerely I don't know what code to post cause I have no idea where the problem could be: I tried to toast the View.getWidth() on the Click of the View itself, but actually the width is 1280 px, the entire device's width.
The single row is inflated with this customer_row.xml
<?xml version="1.0" encoding="utf-8"?>
<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:background="#fff"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
tools:context=".MyActivity">
<ViewFlipper
android:id="#+id/capitalViewFlipper"
android:layout_width="75dp"
android:layout_height="75dp">
<TextView
android:id="#+id/customerLogo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ABCDEF"
android:gravity="center"
android:text="D"
android:textColor="#fff"
android:textSize="28pt" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FBBB"
android:gravity="center"
android:src="#drawable/ic_selected_done" />
</ViewFlipper>
<TextView
android:id="#+id/ragioneSocialeViewList"
android:layout_width="400dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/capitalViewFlipper"
android:layout_gravity="center"
android:layout_toRightOf="#+id/capitalViewFlipper"
android:gravity="center_vertical"
android:padding="10dip"
android:singleLine="true"
android:text="Dummy Customer"
android:textColor="#000"
android:textSize="24dp" />
<TextView
android:id="#+id/canaleViewList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/capitalViewFlipper"
android:layout_alignLeft="#id/ragioneSocialeViewList"
android:layout_gravity="center"
android:layout_marginBottom="-7dp"
android:gravity="center"
android:padding="10dip"
android:text="Dummy type"
android:textColor="#000"
android:textSize="20dp" />
<TextView
android:id="#+id/sollecitiViewList"
android:layout_width="125dp"
android:layout_height="75dp"
android:layout_alignBottom="#+id/capitalViewFlipper"
android:layout_alignTop="#+id/capitalViewFlipper"
android:layout_gravity="center"
android:layout_toRightOf="#+id/ragioneSocialeViewList"
android:gravity="center"
android:padding="10dip"
android:text="2"
android:textColor="#000"
android:textSize="22dp" />
<TextView
android:id="#+id/tipoViewList"
android:layout_width="125dp"
android:layout_height="75dp"
android:layout_alignBottom="#+id/capitalViewFlipper"
android:layout_alignTop="#+id/capitalViewFlipper"
android:layout_toRightOf="#+id/sollecitiViewList"
android:gravity="center"
android:padding="10dip"
android:text="F/S"
android:textColor="#000"
android:textSize="22dp" />
<TextView
android:id="#+id/emailViewList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/telefonoViewList"
android:layout_alignRight="#+id/telefonoViewList"
android:layout_alignTop="#+id/capitalViewFlipper"
android:gravity="center"
android:padding="10dip"
android:text="dummymail#gmail.com"
android:textAlignment="center"
android:textColor="#000"
android:textSize="22dp" />
<TextView
android:id="#+id/telefonoViewList"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/capitalViewFlipper"
android:layout_gravity="center"
android:layout_marginBottom="-7dp"
android:layout_toRightOf="#+id/tipoViewList"
android:gravity="center"
android:padding="10dip"
android:text="tel. 035424344"
android:textColor="#000"
android:textSize="22dp" />
<TextView
android:id="#+id/ultimoOrdineViewList"
android:layout_width="match_parent"
android:layout_height="75dp"
android:layout_alignBottom="#+id/capitalViewFlipper"
android:layout_alignTop="#+id/capitalViewFlipper"
android:layout_gravity="center"
android:layout_toRightOf="#+id/emailViewList"
android:capitalize="words"
android:gravity="center_vertical|right"
android:padding="10dip"
android:singleLine="true"
android:text="21/12/2012"
android:textColor="#000"
android:textSize="22dp" />
</RelativeLayout>
While the Adapter Code is this:
public class ArrayAdapter_ClientiInput extends ArrayAdapter<CustomerInputOld> {
public EventLog.Event ImageTouched;
//public ListView customerListView;
private ArrayList<CustomerInputOld> list;
private Context context;
//this custom adapter receives an ArrayList of RowData objects.
//RowData is my class that represents the data for a single row and could be anything.
public ArrayAdapter_ClientiInput(Context context, int textViewResourceId, ArrayList<CustomerInputOld> rowDataList) {
//populate the local list with data.
super(context, textViewResourceId, rowDataList);
this.list = new ArrayList<CustomerInputOld>();
this.list.addAll(rowDataList);
this.context = context;
}
public View getView(final int position, View convertView, ViewGroup parent) {
//creating the ViewHolder we defined earlier.
ViewHolder_CustomerRow holder = new ViewHolder_CustomerRow();
//creating LayoutInflater for inflating the row layout.
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//inflating the row layout we defined earlier.
convertView = inflater.inflate(R.layout.customers_row, null);
//setting the views into the ViewHolder.
holder.title = (TextView) convertView.findViewById(R.id.ragioneSocialeViewList);
holder.capital = (TextView) convertView.findViewById(R.id.customerLogo);
holder.flipper = (ViewFlipper) convertView.findViewById(R.id.capitalViewFlipper);
//holder.line = (RelativeLayout) convertView.findViewById(R.id.customer_whole_row);
holder.canale = (TextView) convertView.findViewById(R.id.canaleViewList);
holder.solleciti = (TextView) convertView.findViewById(R.id.sollecitiViewList);
holder.email = (TextView) convertView.findViewById(R.id.emailViewList);
holder.tipo = (TextView) convertView.findViewById(R.id.tipoViewList);
holder.numero = (TextView) convertView.findViewById(R.id.telefonoViewList);
holder.ultimoOrdine = (TextView) convertView.findViewById(R.id.ultimoOrdineViewList);
//define an onClickListener for the CheckBox.
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(getContext(),Integer.toString(v.getWidth()),Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getContext(), CustomerDetail.class);
intent.putExtra("name", list.get(position).getCompany());
getContext().startActivity(intent);
}
});
convertView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent e) {
if (e.getAction() == android.view.MotionEvent.ACTION_DOWN) {
v.setBackgroundColor(0xFAAA);
} else if ( e.getAction() == android.view.MotionEvent.ACTION_UP ||
e.getAction() == android.view.MotionEvent.ACTION_CANCEL) {
isSelected(position, v);
}
//Ritorno false per permettere anche al OnClickListener di agire..
return false;
}
});
holder.flipper.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CustomerListFragment.onRowIconClicked(position, (RelativeLayout) v.getParent(), (ViewFlipper) v, context);
}
});
//setting data into the the ViewHolder.
holder.title.setText(list.get(position).getCompany());
holder.capital.setText(list.get(position).getCompany().substring(0, 1).toUpperCase());
holder.capital.setBackgroundColor(list.get(position).getIconColor());
holder.canale.setText(list.get(position).getChannel());
holder.solleciti.setText(list.get(position).getReminders());
if (Integer.parseInt(list.get(position).getReminders()) >= 3)
holder.solleciti.setTextColor(0xffff0000);
holder.email.setText(list.get(position).getEmail());
holder.numero.setText("tel. " + list.get(position).number);
holder.ultimoOrdine.setText(new SimpleDateFormat("c d MMM ''yy").format(list.get(position).getLastOrderDate()));
if (list.get(position).getInvoiceCustomer() == null) {
if (list.get(position).getDeliveryCustomer() == null) {
holder.tipo.setText("F/S");
} else {
holder.tipo.setText("F");
}
} else {
holder.tipo.setText("S");
}
//modifiche nel caso in cui sia selezionato
if ( isSelected(position,convertView) ) {
holder.flipper.showNext();
}
//return the row view.
return convertView;
}
private boolean isSelected(int position, View v){
if (position == CustomerListFragment.customerSelected) {
v.setBackgroundColor(0xFFE6C86F);
return true;
} else {
v.setBackgroundColor(0xFFFFFFFF);
return false;
}
}
}
Solution by OP.
In the last TextView I put android:capitalize="words", and that is an EditText's attribute, not Textview's! So when I clicked on the last "20%" of screen - coincidentally, that TextBox- tha app thinks that the click was on the TextView and not the line!

Android - Get text from inflated layout Listview

I have a listview in which I inflate a layout with multiple textviews and buttons. I understand to get the text from a view that was clicked is ((Textview)view.... However I am trying to get the text from the specific textview that is located in the layout in which the user clicked. I have tried using OnItemClick but when I use this the item must be focused before the any of the buttons functions work. I resorted to and prefer using onClickListeners in the getView method of my custom adapter. So simply put, how do I click a Button and get the text that is in TextView that is located in the appropriate inflated layout list view item, given that since each inflated layout is considered as one list item?
Here are pictures to clarify what i am looking for. Both layouts are members of a listview.
I want to click the button with the date on it and get the text from the textview in the middle of the layout. However when I click the button with the date on it, I can only get the text from the textview in the middle of the layout of the last child. If "My Party" is the first child in the listview and "3303 going away service..." is the second child, when I click the date button the code in my custom adapter returns the text from the last loaded text in the view which will be "3303 going away service". What I am trying to do is when I click the date button on "My party", get the text "My party". Like wise with the second child.
Here is the getView() in my custom adapter.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
viewHolder = new ViewHolder();
positionHolder = position;
Log.i("Position", "" + position);
if(convertView == null) {
try {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.post_layout, parent, false);
postLayout = convertView;
viewHolder.unameTV = (TextView) postLayout.findViewById(R.id.postUnameTv);
viewHolder.unameTV.setText(viewContent.get(index));
viewHolder.unameTV.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Starting new intent
Intent in = new Intent(getActivity(),
Profile.class);
// sending pid to next activity
String username =((TextView)view).getText().toString();
in.putExtra("username", username);
// starting new activity and expecting some response back
startActivityForResult(in, 100);
}
});
viewHolder.fillSpace = (TextView)postLayout.findViewById(R.id.posthelpSpace);
viewHolder.fillSpace.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
viewHolder.unameTV.performClick();
}
});
viewHolder.image = (ImageView) postLayout.findViewById(R.id.postProfPic);
DisplayImageOptions options = initiateDisplayImageOptions();
viewHolder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
viewHolder.unameTV.performClick();
}
});
ImageLoader imageloader = ImageLoader.getInstance();
initImageLoader(getActivity());
imageloader.displayImage(viewContent.get(index + 1), viewHolder.image, options);
viewHolder.addToCalendarButton = (TextView) postLayout.findViewById(R.id.addToCalendarButton);
viewHolder.addToCalendarButton.setText(viewContent.get(index + 2));
viewHolder.addToCalendarButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Calendar cal = new GregorianCalendar();
cal.setTime(new Date());
cal.add(Calendar.MONTH, 2);
long time = cal.getTime().getTime();
Uri.Builder builder =
CalendarContract.CONTENT_URI.buildUpon();
builder.appendPath("time");
builder.appendPath(Long.toString(time));
Intent intent =
new Intent(Intent.ACTION_INSERT, CalendarContract.Events.CONTENT_URI);
title = testText.getText().toString();
Log.i("Title", "" + title);
intent.putExtra("title", title); // **NOT WORKING**
startActivity(intent);
}
});
viewHolder.eventTitle = (TextView) postLayout.findViewById(R.id.postTitleTV);
viewHolder.eventTitle.setText(viewContent.get(index + 3));
viewHolder.eventTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
title = ((TextView)view).getText().toString();
Log.i("TITLE", "" + title);
}
});
testText = viewHolder.eventTitle;
viewHolder.eventImage = (ImageView) postLayout.findViewById(R.id.eventImage);
imageloader.displayImage(viewContent.get(index + 4), viewHolder.eventImage, options);
viewHolder.likesTV = (TextView) postLayout.findViewById(R.id.likesTV);
viewHolder.likesTV.setText("" + viewContent.get(index + 5));
viewHolder.planToAttendTV = (TextView) postLayout.findViewById(R.id.planToAttendTV);
viewHolder.planToAttendTV.setText(viewContent.get(index + 6));
viewHolder.addressTV = (TextView) postLayout.findViewById(R.id.postLocationTV);
viewHolder.addressTV.setText("" + viewContent.get(index + 7));
index = index + 8;
}
catch (IndexOutOfBoundsException ie)
{
ie.printStackTrace();
}
}
else
{
viewHolder = (ViewHolder) postLayout.getTag();
}
return postLayout;
}
UPDATE
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:background="#drawable/fill_back"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="400dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="#+id/relativeLayout"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/postUnameTv"
android:layout_alignParentTop="true"
android:layout_marginTop="30dp"
android:gravity="center|center_vertical|center_horizontal"
android:textColor="#ff518eff"
android:textSize="12dp"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/postProfPic"
android:text="Joshua" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Public"
android:id="#+id/postProfileIcon"
android:background="#drawable/publicicon"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="25dp"
android:layout_marginRight="70dp"
/>
<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/postProfPic"
android:layout_below="#+id/postUnameTv"
android:layout_centerHorizontal="true"
/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="330dp"
android:id="#+id/eventImage"
android:layout_alignTop="#+id/space"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
<Space
android:layout_width="20px"
android:layout_height="20px"
android:layout_alignBottom="#+id/postProfPic"
android:layout_centerHorizontal="true"
android:layout_marginBottom="18dp"
android:id="#+id/space" />
<Space
android:layout_width="20px"
android:layout_height="20px"
android:layout_above="#+id/postProfPic"
android:layout_centerHorizontal="true"
android:id="#+id/space2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/postTitleTV"
android:layout_below="#+id/postProfPic"
android:layout_centerHorizontal="true"
android:layout_marginTop="117dp"
android:gravity="center|center_vertical|center_horizontal"
android:textSize="28dp"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton3"
android:background="#drawable/details_button"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="15dp"
android:layout_marginBottom="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/postLocationTV"
android:layout_below="#+id/postProfPic"
android:layout_alignParentStart="true"
android:drawableLeft="#drawable/locate_button"
android:drawablePadding="5dp"
android:gravity="clip_horizontal"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:textSize="12dp"
android:singleLine="true"
android:layout_alignEnd="#+id/space"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/addToCalendarButton"
android:textSize="12dp"
android:layout_alignBottom="#+id/postLocationTV"
android:drawableLeft="#drawable/add_to_cal_button"
android:drawablePadding="5dp"
android:gravity="bottom"
android:layout_alignEnd="#+id/imageButton3"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="2 hours ago"
android:id="#+id/textView2"
android:textSize="8dp"
android:gravity="center_vertical|center_horizontal"
android:paddingRight="10dp"
android:textColor="#ff828084"
android:layout_above="#+id/eventImage"
android:layout_toRightOf="#+id/postProfPic"
android:layout_alignParentEnd="true"
android:layout_marginBottom="3dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/posthelpSpace"
android:layout_alignBottom="#+id/space"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/postProfPic"
android:layout_alignTop="#+id/postProfPic" />
</RelativeLayout>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/likeButton"
android:background="#drawable/like_button_unsel"
android:layout_below="#+id/relativeLayout"
android:layout_alignParentStart="true"
android:layout_marginLeft="15dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/likesTV"
android:textColor="#ffe11100"
android:layout_alignBottom="#+id/likeButton"
android:layout_toRightOf="#+id/likeButton"
android:layout_alignTop="#+id/likeButton"
android:gravity="center|center_vertical|center_horizontal"
android:paddingLeft="5dp"
android:paddingTop="5dp"
/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="0.3dp"
android:id="#+id/imageView"
android:layout_below="#+id/likeButton"
android:layout_alignParentStart="true"
android:layout_marginTop="100dp"
android:background="#drawable/divider" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/planToAttendTV"
android:layout_below="#+id/likesTV"
android:paddingLeft="5dp"
android:layout_marginTop="10dp"
android:layout_alignParentEnd="true"
android:gravity="center"
/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="30dp"
android:layout_height="30dp"
android:text="Yes"
android:id="#+id/button"
android:textSize="10dp"
android:layout_toRightOf="#+id/textView3"
android:background="#drawable/border_circular"
android:layout_marginLeft="2dp"
android:layout_below="#+id/planToAttendTV"
android:layout_marginTop="5dp"
/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="30dp"
android:layout_height="30dp"
android:text="No"
android:id="#+id/button2"
android:textSize="10dp"
android:layout_alignTop="#+id/button"
android:layout_toRightOf="#+id/button"
android:background="#drawable/border_circular"
android:layout_marginLeft="8dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Are you attending?"
android:id="#+id/textView3"
android:gravity="center"
android:layout_alignBottom="#+id/button"
android:layout_toRightOf="#+id/likeButton"
android:layout_marginBottom="7dp"
android:layout_marginLeft="45dp"
android:layout_marginRight="4dp"
/>
I would recommend making the child its own layout as a list item. I would then create a custom list view adapter which would make your layout much simpler as well as being able to get the text from the specific item clicked without having to worry about repeated code.
Allright I guess I figured out what you are trying to get. Correct me If I'm wrong.
Normally, the idea of using custom adapter is based on List of Objects. You need to store all the information you have in an Object and create an ArrayList of those objects.
Then, whichever child you click in the list view, you will get the clicked child's index. This index is also the index of Object in your ArrayList.
After you know which index is clicked, then you can simply get the Object from your ArrayList with this index.
e.g If the 1st list item is clicked, the position you will get is "0", then you will need to use yourArrayList.get(0).getTheDataYouWant()

Textview with long text pushes out other views in GridLayout despite ellipsize=end

My problem is very similar to How to get a layout where one text can grow and ellipsize, but not gobble up the other elements on the layout, but read on below why I can't use TableLayouts as proposed there.
I'm trying to create a listview row that basically looks like this:
| TextView | View 1 | View 2 |
All views contain variable width elements. The TextView has ellipsize="end" set. View 1 should align left of the TextView, while View 2 should align to the right of the screen. So, normally, there would be whitespace between View 1 and View 2. As the text in the TextView grows longer, the TextView should grow, pushing View 1 to the right until there is no more whitespace left. Then, ellipsize should kick in, cutting of the text in TextView and appending an ellipsis ("...") at the end.
So, the result should look something like this:
+----------------------------------------+
| short text [view1] [view2] |
+----------------------------------------+
| long text with ell ... [view1] [view2] |
+----------------------------------------+
I've tried:
TableLayouts, but they seem to make scrolling extremely slow on some devices.
RelativeLayouts, but I either had overlapping views, or view1 or view2 disappeared completely.
GridLayouts, but the TextView always grows until it takes up the whole width of the screen, thus pushing view1 and view2 out of the screen.
This is the GridLayout I tried:
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_gravity="left|fill_horizontal"
android:ellipsize="end"
android:singleLine="true"
android:text="Long text to demonstrate problem with TextView in GridLayout taking up too much space despite ellipsis" />
<TextView
android:layout_gravity="left"
android:text="(view1)" />
<TextView
android:layout_gravity="right"
android:text="(view2)" />
</GridLayout>
View 1 and View 2 are not really TextViews, I just used them in the example to simplify things.
Is there any way to achieve this without using TableLayouts?
EDIT:
As requested, here is my attempt at solving this with a RelativeLayout. The TextView takes up the full width of the screen in this case, so neither view1 nor view2 are visible.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/rl0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:ellipsize="end"
android:singleLine="true"
android:text="Long text to demonstrate problem with TextView in GridLayout taking up too much space despite ellipsis" />
<TextView
android:id="#+id/rl1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/rl0"
android:layout_marginLeft="10dp"
android:text="(view1)" />
<TextView
android:id="#+id/rl2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/rl1"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:text="(view2)" />
</RelativeLayout>
I seem to have found a potential solution to prevent a TextView in GridLayout from growing unboundedly and pushing out other views. Not sure if this has been documented before.
You need to use fill layout_gravity and set an arbitrary layout_width or width on the long TextView in need of ellipsizing.
android:layout_gravity="fill"
android:layout_width="1dp"
Works for both GridLayout and android.support.v7.widget.GridLayout
I'm a big fan of LinearLayouts, so here's my suggestion using those:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:singleLine="true"
android:text="Long text to demonstrate problem with TextView in GridLayout taking up too much space despite ellipsis" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="(view1)" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="(view2)" />
</LinearLayout>
I will suggest you to play with layout_weight property of your widget
Example:
<LinearLayout 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"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="10">
<LinearLayout
android:id="#+id/ll_twoViewContainer"
android:layout_weight="8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/rl0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:ellipsize="end"
android:singleLine="true"
android:text="Long text" />
<TextView
android:id="#+id/rl1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#id/rl0"
android:layout_weight="1"
android:minWidth="120dp"
android:text="(view1)" />
</LinearLayout>
<TextView
android:id="#+id/rl2"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/rl1"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:text="(view2)" />
</LinearLayout>
</LinearLayout>
finally your layout will look like as follow:
+----------------------------------------+
| short text [view1] [view2] |
+----------------------------------------+
| long text with ell ... [view1] [view2] |
+----------------------------------------+
I think you should create custom layout for your purpose. I don't know how to do this using only default layouts/view and make it work for all cases.
The trick which worked for me was to use maxWidth to restrict the width of the first view. You need to do it with Java, here is the basic logic:
firstView.setMaxWidth(parentView.getWidth() - view2.getWidth() - view1.getWidth() - padding * 2);
Not pretty, but it works.
I think there's just a small issue on the layout that could be solved, anchoring the view3 to the right and start from there to force the view to have a delimited area (hence being able to properly set the ellipse):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/rl3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="(view2)" />
<TextView
android:id="#+id/rl2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/rl3"
android:text="(view1)" />
<TextView
android:id="#+id/rl1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/rl2"
android:text="Long text to demonstrate problem with TextView in GridLayout taking up too much space despite ellipsis" />
</RelativeLayout>
Hope this helps...
Regards!
Try this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"
android:text="Long text to demonstrate problem with TextView in GridLayout taking up too much space despite ellipsis"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="(view1)"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="(view2)"/>
</LinearLayout>
</LinearLayout>
Currently, all views are centered. You can change android:gravity property to meet your needs. For example, you may want to align view1 right and view2 left in which case last two LinearLayouts would look something like (with 5dp margin on the right and left respectively):
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center|right">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginRight="5dp"
android:text="(view1)"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center|left">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginLeft="5dp"
android:text="(view2)"/>
</LinearLayout>
I find my solution for the case number 2 (the one with a long text):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="3" >
<TextView
android:id="#+id/rl0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:ellipsize="end"
android:singleLine="true"
android:text="Long text to demonstrate problem with TextView in GridLayout taking up too much space despite ellipsis" />
<TextView
android:id="#+id/rl1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:text="(view1)" />
<TextView
android:id="#+id/rl2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:text="(view2)" />
</LinearLayout>
The real problem is case one, and i didn't try a lot of things for this. I hope it helps (and if i have more spare time, i will try to achieve first one!).
If the views on the right get pushed over by the text by design, you might as well use a ListView instead of a GridView.
You would just need to make the base of the list item layout a RelativeLayout, and set rules like this:
You can set the two views on the right to alignParentRight (using android:layout_alignParentLeft="true"), but make sure the first view stays to the left of the second so it will push itself to the left as the views stretch out.
You can make the TextView on the left align to the left, but stay to the left of the first view (using android:layout_toLeftOf="#+id/viewId") so it won't overlap with the views.
Try using Layout Weight
<TableRow
android:id="#+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/tableRow2"
android:layout_alignParentBottom="true"
android:gravity="center"
android:weightSum="10"
android:background="#android:color/black" >
<TextView
android:id="#+id/txtInningsTotal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:text="0"
android:textColor="#android:color/white" />
<TextView
android:id="#+id/txtTeamOneTotal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2.5"
android:gravity="center"
android:text="0"
android:textColor="#android:color/white" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2" />
<TextView
android:id="#+id/txtTeamTwoTotal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2.5"
android:gravity="center"
android:text="0"
android:textColor="#android:color/white" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2" />
<TextView
android:id="#+id/txtGrandTotal"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:gravity="center"
android:text="0"
android:textColor="#android:color/white" />
</TableRow>
Here i have taken table row in which there is layout weight sum which is of 10 means that it is 100% width of its parent. and in all its child views i have set width to 0Dp and given weight to 1 or 2. so that it will take up to that percent of total 10. so the layout will be adjusted accordingly screen and also there will be no issue of overlapping.
If i have understood you correctly then this is the answer you wanted.
Hope it Helps!
First, you must layout [view 2] to parent Right;
Again, you reference the reference to the last two layout!
<Relativelayout android:layout_width="match_parent"
android:layout_height="wrap_content"
<TextView
android:id="#+id/view2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeft="#id/view2"
android:gravity="left">
<TextView
android:id="#+id/shortORlongtTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/view1"
android:ellipsize="end"
android:maxLines="1"
android:textSize="18dp"/>
<TextView
android:id="#+id/view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
I had the same problem with the grid layout. what i did is given a fixed width for the text view and also given layout_columnWeight property for each text view then the issue was fixed ,hope it helps ...
<TextView
android:id="#+id/txtName"
style="#style/MyDetailTitle"
android:layout_width="#dimen/detail_length"
android:layout_height="wrap_content"
android:text="#string/app_name"
app:layout_column="3"
app:layout_columnWeight="1"
app:layout_gravity="start"
app:layout_row="1" />
GridLayout is like the other things on Android : flawed by design.
You will need a custom Layout, the following example will allow you to layout things like:
[ label | short text | very long label | short text ]
[ long label | very very very | label | very long text ]
[ | long text | | ]
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
public class TwoColumsGridLayout extends ViewGroup {
private final List<List<View>> rows;
private int rowCount = 0;
private int firstColumWidth;
private int secondColumWidth;
private int thirdColumWidth;
private int fourthColumnWidth;
private final List<Integer> rowHeights = new ArrayList<>();
private final List<List<Integer>> cellHeights = new ArrayList<>();
private final List<Integer> firstCellsWidths = new ArrayList<>(4);
private final List<Integer> thirdCellsWidths = new ArrayList<>(4);
public TwoColumsGridLayout(Context context, int rowCount) {
super(context);
rows = new ArrayList<>(rowCount);
}
public void add(Context ctx, TextView l1, View t1, TextView l2, View t2) {
final List<View> row = new ArrayList<>(4);
row.add(l1);
row.add(t1);
row.add(l2);
row.add(t2);
rows.add(row);
this.addView(l1);
this.addView(t1);
if (l2 != null)
this.addView(l2);
if (t2 != null)
this.addView(t2);
this.rowCount++;
}
public int getRowCount() {
return rowCount;
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int curLeft = 0;
int curBottom;
int curRight;
int curTop = 0;
int i = 0;
for (List<View> row : rows) {
final int rowHeight = this.rowHeights.get(i);
final List<Integer> rowCellHeights = this.cellHeights.get(i);
final View v0 = row.get(0);
curLeft = 0;
curRight = curLeft + this.firstColumWidth;
if (v0 != null) {
curBottom = curTop + rowCellHeights.get(0);
// Right align
v0.layout(curLeft + this.firstColumWidth - this.firstCellsWidths.get(i), curTop + 7, curRight, curBottom + 7);
}
//
final View v1 = row.get(1);
curLeft += this.firstColumWidth;
curRight = curLeft + this.secondColumWidth;
if (v1 != null) {
curBottom = curTop + rowCellHeights.get(1);
v1.layout(curLeft, curTop, curRight, curBottom);
}
//
final View v2 = row.get(2);
curLeft += this.secondColumWidth;
curRight = curLeft + this.thirdColumWidth;
if (v2 != null) {
curBottom = curTop + rowCellHeights.get(2);
// Right align
v2.layout(curLeft + this.thirdColumWidth - this.thirdCellsWidths.get(i), curTop + 7, curRight, curBottom + 7);
}
//
final View v3 = row.get(3);
curLeft += this.thirdColumWidth;
curRight = curLeft + this.fourthColumnWidth;
if (v3 != null) {
curBottom = curTop + rowCellHeights.get(3);
v3.layout(curLeft, curTop, curRight, curBottom);
}
curTop += rowHeight;
i++;
}
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
// Compute first column width
firstColumWidth = 0;
for (List<View> row : rows) {
final View v = row.get(0);
if (v != null) {
v.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
measureChild(v, widthMeasureSpec, heightMeasureSpec);
final int w = v.getMeasuredWidth();
if (firstColumWidth < w) {
firstColumWidth = w;
}
}
}
// Compute third column width
thirdColumWidth = 0;
for (List<View> row : rows) {
final View v = row.get(2);
if (v != null) {
v.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
measureChild(v, widthMeasureSpec, heightMeasureSpec);
final int w = v.getMeasuredWidth();
if (thirdColumWidth < w) {
thirdColumWidth = w;
}
}
}
secondColumWidth = (parentWidth - firstColumWidth - thirdColumWidth) / 2;
fourthColumnWidth = parentWidth - firstColumWidth - secondColumWidth - thirdColumWidth;
// Clear
this.rowHeights.clear();
this.cellHeights.clear();
this.firstCellsWidths.clear();
this.thirdCellsWidths.clear();
// Compute heights
int height = 0;
for (List<View> row : rows) {
final ArrayList<Integer> rowCellHeights = new ArrayList<>(4);
cellHeights.add(rowCellHeights);
int rowHeight = 0;
// First column
final View v0 = row.get(0);
if (v0 != null) {
int h = v0.getMeasuredHeight();
this.firstCellsWidths.add(v0.getMeasuredWidth());
rowCellHeights.add(h);
if (rowHeight < h) {
rowHeight = h;
}
} else {
this.firstCellsWidths.add(0);
}
// Second column
final View v1 = row.get(1);
if (v1 != null) {
v1.setLayoutParams(new ViewGroup.LayoutParams(secondColumWidth, LayoutParams.WRAP_CONTENT));
measureChild(v1, widthMeasureSpec, heightMeasureSpec);
int h = v1.getMeasuredHeight();
rowCellHeights.add(h);
if (rowHeight < h) {
rowHeight = h;
}
}
// Third column
final View v2 = row.get(2);
if (v2 != null) {
int h = v2.getMeasuredHeight();
this.thirdCellsWidths.add(v2.getMeasuredWidth());
rowCellHeights.add(h);
if (rowHeight < h) {
rowHeight = h;
}
} else {
this.thirdCellsWidths.add(0);
}
// Fourth column
final View v3 = row.get(3);
if (v3 != null) {
v3.setLayoutParams(new ViewGroup.LayoutParams(fourthColumnWidth, LayoutParams.WRAP_CONTENT));
measureChild(v3, widthMeasureSpec, heightMeasureSpec);
int h = v3.getMeasuredHeight();
rowCellHeights.add(h);
if (rowHeight < h) {
rowHeight = h;
}
}
height += rowHeight;
this.rowHeights.add(rowHeight);
}
setMeasuredDimension(parentWidth, height);
}
}
Have fun.
TableLayout will give expected behavior. May cause performance issue as question's author mention, but works great with simple layout. If the row is repeatable and scrollable, consider use gridview instead
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:shrinkColumns="0"
>
<TableRow>
<TextView/>
<View1/>
<View2/>
</TableRow>
</TableLayout>

Categories

Resources