How to change layout height automatically with word wrap? - android

As you see, height sets normally when there's only one text string, but all crashes, when it's wrapped:
Here's my XML:
<?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:background="?android:attr/selectableItemBackground"
android:focusable="true"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingStart="15dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingEnd="15dp"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/title"
android:textSize="16sp"
android:textStyle="bold"
android:layout_marginTop="10dp"
/>
<TextView
android:id="#+id/descr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
</LinearLayout>
<CheckBox
android:id="#+id/checkbox"
android:theme="#style/checkBoxStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_marginTop="7dp"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
/>
</LinearLayout>
Here's my adapter:
class AppsAdapter extends RecyclerView.Adapter<AppsAdapter.ViewHolder> {
private Activity activity;
private ArrayList<Item> items;
private int inflater;
private OnItemCheckListener onItemClick;
AppsAdapter (Activity activity, ArrayList<Item> items, int inflater, OnItemCheckListener onItemCheckListener) {
this.activity = activity;
this.items = items;
this.inflater = inflater;
this.onItemClick = onItemCheckListener;
}
static class ViewHolder extends RecyclerView.ViewHolder {
private ImageView icon;
private TextView title, descr, version;
private CheckBox checkbox;
private ViewHolder (View view) {
super (view);
icon = (ImageView) view.findViewById (R.id.icon);
title = (TextView) view.findViewById (R.id.title);
descr = (TextView) view.findViewById (R.id.descr);
version = (TextView) view.findViewById (R.id.version);
checkbox = (CheckBox) view.findViewById (R.id.checkbox);
}
}
#Override
public ViewHolder onCreateViewHolder (ViewGroup parent, int viewType) {
return new ViewHolder (LayoutInflater.from (parent.getContext ()).inflate (inflater, parent, false));
}
#Override
public void onBindViewHolder (final ViewHolder holder, int position) {
Item item = items.get (position);
if (holder.icon != null)
holder.icon.setImageDrawable (item.icon);
holder.title.setText (item.getItem (0));
holder.descr.setText (item.getItem (1));
// Other actions...
}
}
So, is it really to do it? I don't want to use a standart Android Settings activity, because I want to support KitKat (4.4), but its default themes are awful(
Or perhaps it'll be a solution with TableLayout and so on?
Thanks in advance!

You using your parent layout height was using like this android:layout_height="wrap_content"
User your height android:layout_width="match_parent"
definitely you got o/p

Change the xml
<?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:background="?android:attr/selectableItemBackground"
android:focusable="true"
android:gravity="center"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingTop="5dp">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/colorPrimary"
android:textSize="16sp"
android:text="abababababababababbabababababababab"
android:textStyle="bold" />
<TextView
android:id="#+id/descr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="abababababababababbababababababababsdhsjdhsadsahdkjsahdsadsadhsadhsauidhsauidhsauihsauihsaduisahduisahduisahduisahdusaidhsauidhsauidhsauiidhsauidhsauidhsauidhsauidhsaudhsauidhsadiusahduisahdusahu"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:layout_marginTop="7dp"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:theme="#style/checkBoxStyle" />
</LinearLayout>

Related

How to get id of a spinner in main activity from a recycler view?

I have a recyclerview, I have designed a row which is inflated in the adapter,I want to get hold of the id of the spinner to perform some action with it which I am unable to do.
This is my ApolloActivity.java where I am trying to inflate a recylcer view, in onClick() of the fab i want the id of the spinner.
private List<ApolloPharmacyEntity> entity = null;
private RecyclerView rv;
private FloatingActionButton fab;
Spinner spinner;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setTitle("Apollo Pharmacy");
setContentView(R.layout.activity_apollo);
rv=(RecyclerView)findViewById(R.id.recyclerview2);
Log.i("TAG","recycler view"+rv);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Context context=view.getContext();
LayoutInflater inflater = null;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view= inflater.inflate(R.layout.apollopharmacy_listrow,null);
spinner = (Spinner)view.findViewById(R.id.spinner);//this is becoming null
String Text = spinner.getSelectedItem().toString();
Log.i("TAG","spinnervalue"+spinner);
Intent intent = new Intent(Apollo.this, Cart.class);
startActivity(intent);
}
});
initializeData();
initializeAdapter();
`
This is my adapter
public static class EntityViewHolder extends RecyclerView.ViewHolder {
TextView tabName;
TextView gobacktomenutv;
Spinner spinner;
String[] itemCount = { "1", "2", "3", "4","5","6","7","8","10"};
RelativeLayout addrl;
Dialog dialog;
EntityViewHolder( View itemView) {
super(itemView);
spinner = (Spinner)itemView.findViewById(R.id.spinner);
tabName = (TextView)itemView.findViewById(R.id.textView4);
ArrayAdapter aa = new ArrayAdapter(itemView.getContext(), android.R.layout.simple_spinner_dropdown_item, itemCount);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(aa);
}
}
List<ApolloPharmacyEntity> details;
ApolloPharmacyAdapter(List<ApolloPharmacyEntity> details){
this.details = details;
}
#Override
public ApolloPharmacyAdapter.EntityViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.apollopharmacy_listrow, viewGroup, false);
ApolloPharmacyAdapter.EntityViewHolder pvh = new ApolloPharmacyAdapter.EntityViewHolder(v);
return pvh;
}
#Override
public void onBindViewHolder(ApolloPharmacyAdapter.EntityViewHolder holder, int position) {
holder.tabName.setText(details.get(position).getTabName());
}
#Override
public int getItemCount() {
return details.size();
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
apollopharmacy_listrow.xml
<?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="wrap_content"
android:id="#+id/relativelayout3"
android:paddingLeft="20dp"
android:layout_below="#id/relativelayout2">
<TextView
android:text="ASPIRIN-250 mg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28sp"
android:textColor="#224e6d"
android:layout_marginTop="20dp"
android:textStyle="bold"
android:id="#+id/textView4" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/relativelayout3"
android:paddingTop="10dp"
android:paddingLeft="30dp"
>
<TextView
android:text="6 tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#224e6d"
android:textSize="26sp"
android:textStyle="bold"
android:id="#+id/textView6" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<View
android:layout_width="90dp"
android:layout_height="2dp"
android:background="#c0c0c0"
android:layout_marginTop="40dp"/>
</RelativeLayout>
<TextView
android:text="1 STRIP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#224e6d"
android:layout_marginTop="50dp"
android:textStyle="bold"
android:textSize="26sp"
android:id="#+id/textView7" />
<RelativeLayout
android:layout_width="100dp"
android:layout_height="45dp"
android:background="#drawable/add_rounded"
android:id="#+id/relativeLayout"
android:layout_marginBottom="15dp"
android:layout_alignBottom="#+id/textView7"
android:layout_alignParentEnd="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Transfer"
android:text="ADD"
android:textSize="20sp"
android:textColor="#FFFFFF"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/relativelayout3"
>
<TextView
android:text="₹"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:textSize="34sp"
android:textColor="#82BCB4"
android:layout_marginStart="200dp"
android:id="#+id/textView8" />
<TextView
android:text="26"
android:layout_width="wrap_content"
android:layout_height="80sp"
android:layout_marginTop="25dp"
android:textSize="50sp"
android:textColor="#224e6d"
android:textStyle="bold|normal"
android:layout_marginStart="230dp"
android:id="#+id/textView9" />
<Spinner
android:layout_width="55dp"
android:layout_height="55dp"
android:background="#drawable/spinner_outline"
android:id="#+id/spinner"
android:layout_marginEnd="30dp"
android:layout_alignBottom="#+id/textView8"
android:layout_alignEnd="#+id/textView8"
android:layout_marginBottom="9dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#drawable/ic_star_border_black_24px"
android:layout_marginTop="15dp"
android:layout_marginEnd="12dp"
android:id="#+id/imageView2"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</RelativeLayout>
In your ApolloActivity.java's onCreate() method you have not set your ApolloPharmacyAdapter to your RecyclerView. Hence the layout of apollopharmacy_listrow.xml is not inflated.
Please use rv.setAdapter() method in onCreate()
Also if there is no detail found for listrow RecyclerView will not inflate the listrow. Spinner cannot be used like this in onCreate() as there maybe more than one instances of Spinner depending on number of rows in RecyclerView.
You will have to specify which spinner do you want to refer to from you Activity.
I can help if you elaborate on what exactly do you want to achieve by sending a spinner item to next Activity.

ListView Items changed automatically in Fragment android

when I scroll the listview the items changed automatically, in other word an item override other item's view like : when I click on favorite button other items has been changed like this
example:
the item that I clicked
I didn't click on this item
here is my code :
CustomListAdapter.java :
public class CustomListAdapter extends BaseAdapter {
private Context context;
LayoutInflater inflater;
boolean isVoter;
public CustomListAdapter(Context c ) {
this.context = c;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null){
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.date_view,null);
viewHolder = new ViewHolder();
viewHolder.vote = (ImageButton) convertView.findViewById(R.id.vote);
}else {
viewHolder = (ViewHolder)convertView.getTag();
}
isVoter = newsItemArray.get(position).isVoter();
viewHolder.vote.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!Utility.isNetworkAvailable(context))
Toast.makeText(context, R.string.no_network, Toast.LENGTH_LONG).show();
else
voteBtn( position);
}});
return convertView;
}
public void voteBtn(int position){
if ( !isVoter ) {
viewHolder.vote.setImageResource(R.drawable.button_pressed);
}
else{
viewHolder.vote.setImageResource(R.drawable.button_normal);
}
isVoter = !isVoter;
}
}
private class ViewHolder{
ImageButton vote ;
}
#Override
public int getCount() {
return newsItemArray.size();
}
#Override
public Object getItem(int position) {
return newsItemArray.get(position).getTitle();
}
#Override
public long getItemId(int position) {
return position;
}
}
date_view.xml :
<?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:orientation="vertical"
android:background="#f2f2f2"
android:gravity="center"
android:elevation="8dp"
android:layout_margin="10dp">
<android.support.v7.widget.CardView
android:id="#+id/newsCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#fff"
android:layout_margin="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/newsimage1"
android:layout_width="45px"
android:layout_height="45px"
android:src="#mipmap/ic_launcher"
android:scaleType="centerCrop"
/>
<TextView
android:id="#+id/newsname"
android:layout_marginLeft="16dp"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fox News."
android:textColor="#000000"
android:textSize="20dp"
android:layout_gravity="center"
/>
<TextView
android:id="#+id/time"
android:layout_weight="0.6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2 day ago"
android:textColor="#color/listsub1"
android:textSize="14dp"
android:layout_gravity="center"
/>
<ImageView
android:id="#+id/more"
android:visibility="invisible"
android:layout_width="15dp"
android:layout_height="22dp"
android:src="#drawable/more"
android:layout_gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="12dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/sourceNews"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginRight="5dp"
>
<TextView
android:id="#+id/news"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Trump’s Plan for AmericanMade iPhonew Wold Be Disastrous. Trump’s Plan for AmericanMade iPhonew Wold Be Disastrous"
android:textSize="20dp"
android:textColor="#color/listtext"
android:lineSpacingExtra="3dp"
/>
<TextView
android:id="#+id/newssub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Why even a President Trump couldn’t make Apple manufacture iPhone in the state."
android:layout_marginTop="5dp"
android:textSize="13dp"
android:textColor="#color/listsub1"
android:lineSpacingExtra="3dp"
/>
<TextView
android:id="#+id/votes_num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="10dp"
android:textStyle="bold"
android:textColor="#f40000"
android:maxLines="1"
android:layout_marginTop="5dp"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<ImageButton
android:id="#+id/vote"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/star"
android:background="#00ffffff"
android:paddingLeft="5dp" />
<!--
<ImageButton
android:id="#+id/vote"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="#drawable/star"
android:paddingLeft="5dp" />
<Button
android:id="#+id/share"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Share"
android:textColor="#color/background_material_light"/>
<Button
android:id="#+id/comment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Comment"
android:textColor="#color/background_material_light"/>
-->
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
isVoter and viewHolder should not be global variables within the Adapter. Meaning, you are not actually changing whatever member is returned by newsItemArray.get(position).isVoter()
Try adding the position to the ViewHolder object. Then pass the ViewHolder object into the constructor of the viewHolder.vote.setOnClickListener. Within the OnClickListener you can then call
isVoter = newsItemArray.get(viewHolder.position).isVoter();
newsItemArray.get(viewHolder.position).setVoter(!isVoter);

I can use cardflip effect in a recyclerview item?

I want use the card flip effect (http://developer.android.com/intl/es/training/animation/cardflip.html) in each item of a recyclerview.
I have the 3 views.
item_cancion:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
item_cancion_front and item_cancion_back (Two xml with the same info for a proof)
<?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:padding="5dp"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgReproduciendo"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="#+id/txtArtista"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:maxWidth="30dp"
android:layout_marginRight="10dp" />
<TextView
android:id="#+id/txtTitulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:singleLine="true"
android:layout_toLeftOf="#+id/txtDuracion"
android:layout_toRightOf="#+id/imgReproduciendo"
android:layout_toEndOf="#+id/imgReproduciendo"
android:text="Titulo" />
<TextView
android:id="#+id/txtArtista"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dp"
android:singleLine="true"
android:maxWidth="190dp"
android:layout_below="#+id/txtTitulo"
android:layout_toRightOf="#+id/imgReproduciendo"
android:layout_toEndOf="#+id/imgReproduciendo"
android:text="Artista" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtAlbum"
android:singleLine="true"
android:textSize="14dp"
android:gravity="right"
android:layout_below="#+id/txtDuracion"
android:layout_alignRight="#+id/txtDuracion"
android:layout_alignEnd="#+id/txtDuracion"
android:layout_toRightOf="#+id/txtArtista"
android:layout_marginLeft="15dp"
android:text="Album" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtDuracion"
android:paddingLeft="25dp"
android:singleLine="true"
android:textSize="14dp"
android:textStyle="italic"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:text="Duracion" />
</RelativeLayout>
In the viewHolder adapter I have:
#Override
public AdaptadorCancionesActual.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cancion, parent, false);
actividad.getFragmentManager().beginTransaction().add(R.id.container, new CardFrontFragment()).commit();
ViewHolder vh = new ViewHolder(v, this);
return vh;
}
and the ViewHolder is:
public ViewHolder(View v, AdaptadorCancionesActual mAdapter) {
super(v);
this.mAdapter = mAdapter;
frameLayout = (RelativeLayout) v.findViewById(R.id.container);
txtCancion = (TextView) frameLayout.findViewById(R.id.txtTitulo);
txtArtista = (TextView) frameLayout.findViewById(R.id.txtArtista);
txtAlbum = (TextView) frameLayout.findViewById(R.id.txtAlbum);
txtFin = (TextView) frameLayout.findViewById(R.id.txtDuracion);
imgReproduciendo = (ImageView) frameLayout.findViewById(R.id.imgReproduciendo);
v.setOnClickListener(this);
}
#Override
public void onClick(View view) {
int position = getLayoutPosition();
Datos.getMusicSrv().setSong(position);
Datos.getMusicSrv().playSong();
mAdapter.flipCard();
//mAdapter.notifyDataSetChanged();
}
I have two question...
First, I can use this effect in the item of recyclerView?
And if Yes... Why the TextViews are null.

Button inside ListView not clickable

I want to be able to click on a button inside an item of a ListView. It should have a different effect from clicking the whole item. I realize there are several questions asked on stackoverflow, but none of the suggestions works for me.
The ListView is inside a Fragment.
Layout of the fragment:
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".EventFragment" >
<ListView
android:id="#+id/event_list"
android:background="#C0FFFFFF"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp" />
</RelativeLayout>
Layout of each list item:
<?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:orientation="vertical"
android:background="#C0101010">
<TextView
android:id="#+id/event_list_separator"
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="separator"
android:textColor="#android:color/white" />
<LinearLayout
android:id="#+id/event_list_element"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF"
android:padding="6dip" >
<ImageView
android:id="#+id/event_list_element_icon"
android:layout_width="26dip"
android:layout_height="60dip"
android:layout_marginRight="6dip"
android:contentDescription="TODO" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/event_list_element_firstLine"
android:layout_width="match_parent"
android:layout_height="25dip"
android:text="item_header"
android:textSize="18sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="35dip"
android:orientation="horizontal" >
<TextView
android:id="#+id/event_list_element_secondLine"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Description"
android:textSize="14sp" />
<Button
android:id="#+id/event_list_element_button_1"
android:layout_width="132dip"
android:layout_height="match_parent"
android:drawableLeft="#drawable/ic_button1"
android:text="Participate"
android:textStyle="bold"
android:textSize="14sp"
/>
<Button
android:id="#+id/event_list_element_button_2"
android:layout_width="110dip"
android:layout_height="match_parent"
android:ellipsize="marquee"
android:drawableLeft="#drawable/ic_button2"
android:singleLine="true"
android:text="No thanks"
android:textStyle="bold"
android:gravity="center_vertical"
android:textSize="14sp"
/>
<TextView
android:id="#+id/event_list_element_additional_text"
android:layout_width="100dip"
android:layout_height="match_parent"
android:ellipsize="marquee"
android:singleLine="true"
android:gravity="center_vertical"
android:text="sample"
android:textStyle="bold"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
My list adapter is:
public class EventAdapter extends ArrayAdapter<Event> {
static class ViewHolder {
TextView separator;
LinearLayout relativeLayout;
TextView eventHeader;
TextView eventDescription;
ImageView blueDot;
Button button1;
Button button2;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater)
_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.event_list_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.relativeLayout = (LinearLayout) convertView.findViewById(R.id.event_list_element);
viewHolder.blueDot = (ImageView) convertView.findViewById(R.id.event_list_element_icon);
viewHolder.eventHeader = (TextView) convertView.findViewById(R.id.event_list_element_firstLine);
viewHolder.eventDescription = (TextView) convertView.findViewById(R.id.event_list_element_secondLine);
viewHolder.button1 = (Button) convertView.findViewById(R.id.event_list_element_button1);
viewHolder.button2 = (Button) convertView.findViewById(R.id.event_list_element_button2);
viewHolder.separator = (TextView) convertView.findViewById(R.id.event_list_separator);
convertView.setTag(viewHolder);
} else{
viewHolder = (ViewHolder) convertView.getTag();
}
final Event item = getItem(position);
if (item != null) {
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(_context, "boo!", Toast.LENGTH_SHORT).show();
}
};
viewHolder.button1.setOnClickListener(listener);
}
return convertView;
}
}
The problem is that the two buttons are not clickable. What I tried to far:
ListView listView = (ListView) rootView.findViewById(R.id.event_list);
listView.setItemsCanFocus(true);
I also tried setting on the button:
android:focusable="true"
android:clickable="true"
I also experimented with android:descendantFocusability.
None of my tries made the buttons clickable.
Insert the attribute android:descendantFocusability="blocksDescendants" in the Parent Layout declaration of each list item.
The xml should be as follows:
<?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:orientation="vertical"
android:descendantFocusability="blocksDescendants"
android:background="#C0101010">
<TextView
android:id="#+id/event_list_separator"
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="separator"
android:textColor="#android:color/white" />
<LinearLayout
android:id="#+id/event_list_element"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF"
android:padding="6dip" >
<ImageView
android:id="#+id/event_list_element_icon"
android:layout_width="26dip"
android:layout_height="60dip"
android:layout_marginRight="6dip"
android:contentDescription="TODO" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/event_list_element_firstLine"
android:layout_width="match_parent"
android:layout_height="25dip"
android:text="item_header"
android:textSize="18sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="35dip"
android:orientation="horizontal" >
<TextView
android:id="#+id/event_list_element_secondLine"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Description"
android:textSize="14sp" />
<Button
android:id="#+id/event_list_element_button_1"
android:layout_width="132dip"
android:layout_height="match_parent"
android:drawableLeft="#drawable/ic_button1"
android:text="Participate"
android:textStyle="bold"
android:textSize="14sp"
/>
<Button
android:id="#+id/event_list_element_button_2"
android:layout_width="110dip"
android:layout_height="match_parent"
android:ellipsize="marquee"
android:drawableLeft="#drawable/ic_button2"
android:singleLine="true"
android:text="No thanks"
android:textStyle="bold"
android:gravity="center_vertical"
android:textSize="14sp"
/>
<TextView
android:id="#+id/event_list_element_additional_text"
android:layout_width="100dip"
android:layout_height="match_parent"
android:ellipsize="marquee"
android:singleLine="true"
android:gravity="center_vertical"
android:text="sample"
android:textStyle="bold"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
I found the solution! I wanted to update the list of events periodically (right now it's a thread running every x milliseconds, later I want to switch that to only update the event list when there is a change). Anyway, the code was (inside my main activity):
private BroadcastReceiver _bReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(RECEIVE_EVENT)) {
Bundle bundle = intent.getExtras();
Event event = (Event) bundle.get("event");
showEvent(event);
}
}
};
private void showEvent(final Event event){
final Context context = this;
runOnUiThread(new Runnable() {
public void run() {
final ListView listview = (ListView) findViewById(R.id.event_list);
EventAdapter adapter = new EventAdapter(context, id.event_list, getEventList());
listview.setAdapter(adapter);
}
});
}
Setting the adapter each time is certainly not the right approach. Once I changed that to only set the adapter once, it worked like suggested using android:descendantFocusability="blocksDescendants"

Resize the height of the ListView Row in Android?

I am working on an app using ListView to show some data which I get from server at Runtime.
I set the data in the list view dynamically. Below is my XML for ROW :
<LinearLayout
android:id="#+id/top_lilay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/top_header_relay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/timeline_header_bg_480" >
<TextView
android:id="#+id/bride_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="13dp"
android:text="STELLA"
android:textColor="#android:color/black"
android:textSize="50sp" />
<ImageView
android:id="#+id/and_IV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/bride_tv"
android:layout_centerHorizontal="true"
android:layout_marginTop="3dip"
android:background="#drawable/and" />
<TextView
android:id="#+id/groom_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/and_IV"
android:layout_centerHorizontal="true"
android:layout_marginTop="3dip"
android:text="JOSE"
android:textColor="#android:color/black"
android:textSize="50sp" />
<TextView
android:id="#+id/time_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/groom_tv"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dip"
android:text="85 DIAS 23 HOURAS 22 MINUS Y 30 SEG"
android:textColor="#android:color/white"
android:textSize="15sp" />
<TextView
android:id="#+id/text_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/time_tv"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dip"
android:layout_marginTop="2dip"
android:text="para nuestro gran dia"
android:textColor="#android:color/white"
android:textSize="15sp" />
</RelativeLayout>
<ImageView
android:id="#+id/bar_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/orange_bar_480" />
<ImageView
android:id="#+id/photo_IV"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:background="#drawable/post_photo" />
</LinearLayout>
<RelativeLayout
android:id="#+id/heading_relay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/top_lilay"
android:layout_marginBottom="10dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip" >
<ImageView
android:id="#+id/profile_pic_IV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dip"
android:background="#drawable/profile_pic" />
<TextView
android:id="#+id/username_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/profile_pic_IV"
android:text="LOREM IPSUM TITLE"
android:textColor="#android:color/black"
android:textSize="22sp" />
<TextView
android:id="#+id/posttime_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/username_tv"
android:layout_toRightOf="#id/profile_pic_IV"
android:text="Jose Q. a las 2:30 pm el 24 september 2013"
android:textColor="#android:color/black"
android:textSize="12sp" />
</RelativeLayout>
<LinearLayout
android:id="#+id/buttons_relay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/heading_relay"
android:layout_marginBottom="10dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:weightSum="3" >
<LinearLayout
android:id="#+id/like_lilay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<ImageView
android:id="#+id/like_IV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dip"
android:background="#drawable/like" />
<TextView
android:id="#+id/likes_count_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/likes_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/likes"
android:textColor="#android:color/black" />
</LinearLayout>
<LinearLayout
android:id="#+id/comment_lilay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<ImageView
android:id="#+id/comment_IV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dip"
android:background="#drawable/comment" />
<TextView
android:id="#+id/comment_count_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/comment_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/commments"
android:textColor="#android:color/black" />
</LinearLayout>
<LinearLayout
android:id="#+id/fav_lilay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<ImageView
android:id="#+id/fav_IV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dip"
android:background="#drawable/favorite" />
<TextView
android:id="#+id/fav_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/favorites"
android:textColor="#android:color/black" />
</LinearLayout>
</LinearLayout>
Just for the 1st ELEMENT I have to show the "top_header_relay" and the rest of the elements I have to change the visibility to GONE. In doing this, the height of the other Rows remains same as the 1st one which result a big Blank Space at the bottom of the Row.
Suggest me how can I resize the height of the other rows of the List at run time.
ADAPTER CLASS:
public class PostsListAdapter extends BaseAdapter implements OnClickListener{
ArrayList<String> itemList=null;
private Activity _mContext=null;
public LayoutInflater inflater=null;
private Context _Context = null;
private ArrayList<ViewHolder> listHolder=null;
private int[] imageList = {R.drawable.calendar_icon,
R.drawable.location_icon,R.drawable.fun_icon,
R.drawable.notification_icon,R.drawable.invitation_icon,
R.drawable.change_wedding,R.drawable.settings_icon,
R.drawable.logout_icon};
/**
* PostsListAdapter
* #param context
* #param itemList
*/
public PostsListAdapter(Activity context,ArrayList<String> itemList) {
super();
this._Context = context;
this._mContext = context;
this.itemList = itemList;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
listHolder = new ArrayList<PostsListAdapter.ViewHolder>();
}
public int getCount() {
return itemList.size();
}
public Object getItem(int position) {
return itemList.get(position);
}
public long getItemId(int position) {
return 0;
}
public static class ViewHolder{
private ImageView _postImageView=null;
private ImageView _profilePicImageView=null;
private TextView _usernameTitleTextView=null;
private TextView _timeTextView=null;
private TextView _likeCountTextView=null;
private TextView _commentsCountTitleTextView=null;
private LinearLayout _likeLinearLayout=null;
private LinearLayout _commentLinearLayout=null;
private LinearLayout _favLinearLayout=null;
private RelativeLayout _topheaderRelay=null;
private ImageView _barViewImageView=null;
}
/**
*
*/
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if(convertView==null){
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.post_list_element, null);
holder._postImageView=(ImageView)convertView.findViewById(R.id.photo_IV);
holder._postImageView.setOnClickListener(this);
holder._profilePicImageView=(ImageView)convertView.findViewById(R.id.profile_pic_IV);
holder._profilePicImageView.setOnClickListener(this);
holder._usernameTitleTextView = (TextView) convertView.findViewById(R.id.username_tv);
holder._usernameTitleTextView.setClickable(false);
holder._timeTextView = (TextView) convertView.findViewById(R.id.posttime_tv);
holder._timeTextView.setClickable(false);
holder._likeCountTextView = (TextView) convertView.findViewById(R.id.likes_count_tv);
holder._likeCountTextView.setClickable(false);
holder._commentsCountTitleTextView = (TextView) convertView.findViewById(R.id.comment_count_tv);
holder._commentsCountTitleTextView.setClickable(false);
holder._likeLinearLayout = (LinearLayout) convertView.findViewById(R.id.like_lilay);
holder._likeLinearLayout.setOnClickListener(this);
holder._commentLinearLayout = (LinearLayout) convertView.findViewById(R.id.comment_lilay);
holder._commentLinearLayout.setOnClickListener(this);
holder._favLinearLayout = (LinearLayout) convertView.findViewById(R.id.fav_lilay);
holder._favLinearLayout.setOnClickListener(this);
convertView.setTag(holder);
}
else
holder=(ViewHolder)convertView.getTag();
if(position!=0){
holder._topheaderRelay = (RelativeLayout) convertView.findViewById(R.id.top_header_relay);
holder._topheaderRelay.setVisibility(View.GONE);
holder._barViewImageView = (ImageView) convertView.findViewById(R.id.bar_view);
holder._barViewImageView.setVisibility(View.GONE);
}else{
holder._topheaderRelay = (RelativeLayout) convertView.findViewById(R.id.top_header_relay);
holder._topheaderRelay.setVisibility(View.VISIBLE);
holder._barViewImageView = (ImageView) convertView.findViewById(R.id.bar_view);
holder._barViewImageView.setVisibility(View.VISIBLE);
}
listHolder.add(holder);
return convertView;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.like_lilay:
break;
case R.id.comment_lilay:
break;
case R.id.fav_lilay:
break;
case R.id.profile_pic_IV:
break;
case R.id.photo_IV:
break;
default:
break;
}
}
}
When you populate the list, getview function is the best place to inflate the view and to set its layout attributes. You can hide the visibility of other rows based on the item position using setvisibility function

Categories

Resources