Android GridView add header from adapter? - android

I managed to update the header View of adapters. listview, headerView (from listview), gridview within a single screen. the problem is due to listview and headerView display the same item, I want to hide the list view so as not to appear. how can i do that?
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
final Context gridContext = rootView.getContext();
gridView = (GridViewWithHeaderAndFooter) rootView.findViewById(R.id.grid);
this.inflaterGeneral = inflater;
headerView = inflaterGeneral.inflate(R.layout.cell_fragment_home_header_list, null);
listViewHeader = (ListView) headerView.findViewById(R.id.listViewHeader);
rootView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
return rootView;
}
My Imageadapter
class ImageAdapterHeader extends BaseAdapter {
private Context context;
private final String[] mobileValues;
private TextView t;
public ImageAdapterHeader(Context context, String[] mobileValues) {
this.context = context;
this.mobileValues = mobileValues;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//View v = convertView;
ViewHolder holder;
final Item i = itemVideo.get(position);
if (i != null) {
final VideoDetail ei = (VideoDetail) i;
//begin
if (convertView == null) {
convertView = inflater.inflate(R.layout.cell_fragment_home_header, null);
holder = new ViewHolder();
holder.imgThumbnail = (ImageView) convertView.findViewById(R.id.imgThumbnail);
holder.txtDuration = (Button) convertView.findViewById(R.id.txtDuration);
holder.txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
holder.txtName = (TextView) convertView.findViewById(R.id.txtName);
holder.txtView = (TextView) convertView.findViewById(R.id.txtView);
holder.txtComment = (TextView) convertView.findViewById(R.id.txtComment);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtDuration.setText(ei.getDuration());
holder.txtTitle.setText(ei.getTitle());
holder.txtView.setText(ei.getViewCount());
holder.txtComment.setText(ei.getLikeCount());
Picasso.with(this.context)
.load(ei.getDetailPreview().replace(" ", "%20"))
.error(R.drawable.ic_launcher).into(holder.imgThumbnail);
holder.imgThumbnail.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getActivity().runOnUiThread(new Runnable() {
public void run() {
Intent intent = new Intent(getContext(), VideoPlayerActivity.class);
intent.putExtra("videoId", ei.getVideoId());
intent.putExtra("title", ei.getTitle());
intent.putExtra("artistName", ei.getArtistName());
intent.putExtra("view", ei.getViewCount());
intent.putExtra("like", ei.getLikeCount());
startActivity(intent);
}
});
}
});
} else {
System.out.println("Item Null");
}
//end
return convertView;
}
Fragment home
<?xml version="1.0" encoding="utf-8"?>
<in.srain.cube.views.GridViewWithHeaderAndFooter
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="2"
android:scrollbarStyle="outsideOverlay"
android:verticalScrollbarPosition="right"
android:scrollbars="vertical"
android:descendantFocusability="beforeDescendants"
android:background="#d9d8d8" >
</in.srain.cube.views.GridViewWithHeaderAndFooter>
cell Fragment home header list
<?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="wrap_content">
<ListView
android:id="#+id/listViewHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#d9d8d8"
android:scrollingCache="false" />
</LinearLayout>
Cell Fragment Home header
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="4dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1" >
<ImageView
android:id="#+id/imgThumbnail"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="#drawable/jen4d" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imgPlay"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginBottom="200dp"
android:background="#drawable/button_play"
android:scaleType="fitXY"
android:src="#drawable/button_play" />
</RelativeLayout>
<Button
android:id="#+id/txtDuration"
android:layout_width="50dp"
android:layout_height="20dp"
android:layout_alignBottom="#+id/xtName"
android:layout_alignLeft="#+id/imgThumbnail"
android:layout_alignRight="#+id/imgThumbnail"
android:layout_alignTop="#+id/play"
android:layout_gravity="bottom|right"
android:layout_marginBottom="65dp"
android:layout_marginRight="20dp"
android:background="#drawable/round"
android:gravity="center_horizontal|center_vertical"
android:text="3:20"
android:textColor="#FFFFFF"
android:textSize="15sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="60dp"
android:background="#drawable/transparan"
android:orientation="vertical"
android:layout_alignBottom="#+id/xtName"
android:layout_alignLeft="#+id/imgThumbnail"
android:layout_alignRight="#+id/imgThumbnail"
android:layout_alignTop="#+id/txtDuration"
android:layout_gravity="right|bottom">
<TextView
android:id="#+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:text="Klepek - Klepek Versi Dance &apos;Dangduters&apos;"
android:textColor="#ff8a65" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="0.3"
android:orientation="horizontal" >
<TextView
android:id="#+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:text="Janeta Janet"
android:textColor="#FFFFFF" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="0.5"
android:orientation="horizontal"
android:paddingBottom="25dp" >
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:src="#drawable/ic_remove_red_eye_white_24dp"
android:paddingLeft="10dp"
android:layout_marginLeft="10dp" />
<TextView
android:id="#+id/txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="10000"
android:textColor="#FFFFFF" />
<ImageView
android:layout_width="19dp"
android:layout_height="19dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="30dp"
android:src="#drawable/ic_chat_white_24dp" />
<TextView
android:id="#+id/txtComment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="10000"
android:textColor="#FFFFFF" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>

Try to use [HeaderGridView][1], it subclass of GridView. I hope it helps.

Related

How to set Visibility of any view inside listview row from button outside the listview

I have edit button on toolbar on click this button i want to show new view that is cross button inside every row of listview. is this possible and if yes how ? Thanks in advance. Below is layout files:
main layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="#color/backgroundColor"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:background="#color/TabColor"
android:weightSum="10"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_weight="1"
android:onClick="back2"
android:layout_gravity="center"
app:srcCompat="#drawable/back_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_weight="7"
android:paddingLeft="#dimen/margin_small"
android:text="List OF Hotels"
android:textSize="#dimen/text_title"
android:textColor="#color/WhiteColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<FrameLayout
android:layout_weight="2"
android:background="#drawable/transparent_bg"
android:layout_marginRight="#dimen/margin_large"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/edit_jobs"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_marginBottom="#dimen/margin_small"
android:layout_marginTop="#dimen/margin_small"
android:text="Edit"
android:textColor="#color/WhiteColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:visibility="invisible"
android:id="#+id/done_editing"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_marginBottom="#dimen/margin_small"
android:layout_marginTop="#dimen/margin_small"
android:text="Done"
android:textColor="#color/WhiteColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
<ListView
android:id="#+id/listview1"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
listview row layout where i have to show cross image:
<?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_marginTop="#dimen/margin_large"
android:layout_marginLeft="#dimen/margin_large"
android:layout_marginRight="#dimen/margin_large"
android:background="#drawable/border"
android:padding="#dimen/margin_large"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:weightSum="10"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text=""
android:layout_gravity="center"
android:layout_weight="9"
android:textColor="#color/TabColor"
android:singleLine="true"
android:textSize="#dimen/text_large"
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<FrameLayout
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:gravity="right"
android:id="#+id/arrow"
android:src="#drawable/rightarrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:visibility="invisible"
android:id="#+id/cross"
android:gravity="right"
android:src="#drawable/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:weightSum="10"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text=""
android:layout_weight="5"
android:textColor="#color/blackColor"
android:textSize="#dimen/text_medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_weight="5"
android:text=""
android:gravity="right"
android:textColor="#color/blackColor"
android:textSize="#dimen/text_medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Adapter class:
public class MyAdapter extends ArrayAdapter<String> {
private final Context context;
private ArrayList<String> titlelist,datalist;
public MyAdapter(Context context, ArrayList<String> title, ArrayList<String> data) {
super(context, R.layout.listview_row, title);
titlelist= new ArrayList<>();
datalist= new ArrayList<>();
this.context=context;
this.titlelist=title;
this.datalist=data;
}
private static class ViewHolder {
TextView t1,t2;
}
#Override
public int getCount() {
return titlelist.size();
}
#Override
public String getItem(int position) {
return titlelist.get(position);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.listview_row, null);
holder = new ViewHolder();
holder.t1 = (TextView) convertView.findViewById(R.id.title);
holder.t2 = (TextView) convertView.findViewById(R.id.data);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.t1.setText(titlelist.get(position));
holder.t2.setText(datalist.get(position));
return convertView;
}
}
you have to add this functionality in your adapter,
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//ur other codes.....
//
//
if(crossButtonPress){
holder.imageView.setImageResource(R.drawable.cross);
}else{
holder.imageView.setImageResource(R.drawable.default_ic);
}
}
In addition, in you actual button's onClick,
crossButtonPress = true;
adapter.notifyDatasetChanged();

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.

how to set adapter leftcall image and text and right call only text in android

This is my code for adapter class:
public class ChatAdapter extends ArrayAdapter<Chat> {
private final Context context;
private final ArrayList<Chat> values;
ImageLoader imageloader;
Datamodel dm;
public ChatAdapter(Context context, ArrayList<Chat> values) {
super(context, R.layout.list_row_layout_odd, values);
// TODO Auto-generated constructor stub
this.context = context;
this.values = values;
imageloader = new ImageLoader(context);
}
public void addMessage(Chat chat) {
values.add(chat);
this.notifyDataSetChanged();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row_layout_odd,
parent, false);
RoundedImageView userImg = (RoundedImageView) convertView.findViewById(R.id.user_img);
RelativeLayout root = (RelativeLayout) convertView
.findViewById(R.id.even_container);
TextView tv = (TextView) convertView.findViewById(R.id.text);
RoundedImageView oddImg = (RoundedImageView) convertView
.findViewById(R.id.odd_bubble);
ImageView leftimageicon=(ImageView)root.findViewById(R.id.left);
ImageView rightimageicon=(ImageView)root.findViewById(R.id.right);
Typeface fontArial = Typeface.createFromAsset(context.getAssets(),
"fonts/ARIAL.TTF");
SharedPreferences prefs = context.getSharedPreferences(
AppConstants.LOGIN_PREFS, Context.MODE_PRIVATE);
String K = prefs.getString("Member_id", "");
Chat chat = values.get(position);
String t = chat.getRecieverID();
tv.setText(chat.getMessage());
tv.setTypeface(fontArial);
AQuery aq = new AQuery(context);
if (chat.getSenderID().equals(prefs.getString("Member_id", ""))) {
root.setBackgroundColor(Color.parseColor("#07000000"));
tv.setTextColor(Color.parseColor("#636363"));
tv.setBackgroundColor(Color.parseColor("#00ad9a"));
leftimageicon.setVisibility(View.GONE);
rightimageicon.setVisibility(View.VISIBLE);
oddImg.setVisibility(View.GONE);
} else {
root.setBackgroundColor(Color.parseColor("#07000000"));
tv.setTextColor(Color.parseColor("#bdbdbd"));
tv.setBackgroundColor(Color.parseColor("#ffffff"));
oddImg.setVisibility(View.VISIBLE);
aq.id(oddImg).image(
"http://api.lociiapp.com/TransientStorage/" + K + ".jpg");
leftimageicon.setVisibility(View.VISIBLE);
rightimageicon.setVisibility(View.GONE);
oddImg.setVisibility(View.VISIBLE);
}
return convertView;
}
}
list_row_layout_odd.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/even_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<com.lociiapp.utils.RoundedImageView
android:id="#+id/odd_bubble"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_alignParentLeft="true"
android:layout_margin="5dip"
android:visibility="visible" />
<LinearLayout
android:id="#+id/shareRow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_toRightOf="#+id/odd_bubble"
android:background="#07000000"
android:orientation="horizontal"
android:weightSum="1" >
<ImageView
android:id="#+id/left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginTop="15dip"
android:layout_weight=".10"
android:src="#drawable/callout_left" />
<TextView
android:id="#+id/text"
android:layout_width="155dip"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:layout_marginTop="15dip"
android:layout_toLeftOf="#+id/left"
android:textColor="#636363"
android:textSize="20sp" />
<ImageView
android:id="#+id/right"
android:layout_width="wrap_content"![enter image description here][1]
android:layout_height="wrap_content"
android:layout_marginRight="15dip"
android:layout_marginTop="15dip"
android:layout_toLeftOf="#+id/text"
android:layout_weight=".10"
android:src="#drawable/callout_right" />
</LinearLayout>
</RelativeLayout>
i am able to display chatlistview But there is problem i am unable to set item according to screen i am trying to set left and right textview but right textview not going to design . i have to make adapter so that i can sperate send and receiver textview and image view according to given screen .and below is desire screen :
below is my current screen : respectively
i dont know where am doing mistake please tell me and suggest me .
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/even_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1" >
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.10" >
<com.lociiapp.utils.RoundedImageView
android:id="#+id/odd_bubble"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_margin="5dip"
android:visibility="visible" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.90" >
<LinearLayout
android:id="#+id/shareRow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.9"
android:background="#07000000"
android:orientation="horizontal" >
<ImageView
android:id="#+id/left"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".10"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:layout_marginTop="15dip"
android:layout_weight="0.5"
android:textColor="#636363"
android:textSize="20sp" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>

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"

how to add admob as a another layout in my app?

i am trying to add admob as header.but having error. is it possible to add a header in listview.
admob.xml
<com.google.ads.AdView android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="AD_UNIT_ID"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"/>
list.xml
<?xml version="1.0" encoding="utf-8"?>
android:layout_height="wrap_content"
android:background="#drawable/selector"
android:padding="5dp" >
<ImageView
android:id="#+id/logo"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_marginLeft="5dp"
android:duplicateParentState="true"
android:layout_weight="10"
android:src="#drawable/play" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="40.00"
android:orientation="vertical" >
<TextView
android:id="#+id/Tittle"
android:layout_width="match_parent"
android:layout_height="27dp"
android:text="#+id/Tittle"
android:duplicateParentState="true"
android:layout_gravity="center_horizontal"
android:textSize="18sp" >
</TextView>
<TextView
android:id="#+id/Descriprion"
android:layout_width="match_parent"
android:layout_height="18dp"
android:duplicateParentState="true"
android:text="#+id/Descriprion"
android:layout_gravity="center_horizontal"
android:textSize="16sp" >
</TextView>
</LinearLayout>
<ImageView
android:id="#+id/options"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:layout_weight="9.52"
android:duplicateParentState="false"
android:background="#drawable/selector"
android:src="#drawable/options" />
combined.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/home_root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include layout="#layout/admob"/>
<include layout="#layout/list"/>
</LinearLayout>
and in constructor
public MobileArrayAdapter(final Context context, String[] values, MediaPlayer mp2) {
super(context, R.layout.combined, values);
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
this.values = values;
and in getview
public View getView(int position, View rowView, ViewGroup parent) {
ViewHolder holder =null;
if (rowView == null) {
rowView = inflater.inflate(R.layout.list_mobile, null);
holder = new ViewHolder();
holder.text = (TextView) rowView.findViewById(R.id.Tittle);
holder.descrip=(TextView)rowView.findViewById(R.id.Descriprion);
holder. imageView= (ImageView) rowView
.findViewById(R.id.logo);
holder.options = (ImageView)rowView.findViewById(R.id.options)
rowView.setTag(holder);
}else {
holder= (ViewHolder) rowView.getTag();
AdView adView = new AdView((Activity) getContext(), AdSize.BANNER, "a1524d14f8dfc1b" );
AdRequest adrequest = new AdRequest();
adrequest.addTestDevice(AdRequest.TEST_EMULATOR);
// Lookup your LinearLayout assuming it's been given
// the attribute android:id="#+id/mainLayout"
LinearLayout layout1 = (LinearLayout)findViewById(R.id.webView1);
// Add the adView to it
layout1.addView(adView);
// Add the adView to it
layout1.addView(adView);
adView.loadAd(adrequest);
}
holder. imageView.setTag(position);
holder.options.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
mQuickAction.show(arg0);
}
});
error logcat
try this, In your combined.xml add layout width and height.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/home_root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include layout="#layout/admob"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<include layout="#layout/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Hi you can follow this (https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals#android) step by step process to add admob in android project.

Categories

Resources