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

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.

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();

Android GridView add header from adapter?

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.

Android List Item with Image View not clickable

I have an List Item with ImageView's, TextView's. Issue is List Item as whole is not clickable, it's only when I click on any of the ImageView it's clickable. I have set android:descendantFocusability="blocksDescendants" for root and below for each child
android:focusable="false"
android:focusableInTouchMode="false"
Still only ImageViews are clickable. Below is my list item xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://splashurl.com/m22ydvb
android:id="#+id/itemdisplaylist_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:minHeight="#dimen/loclist_item_minumum_height"
android:orientation="horizontal"
android:weightSum="1" >
<ImageView
android:id="#+id/listitem_pic"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.2"
android:contentDescription="#null"
android:focusable="false"
android:focusableInTouchMode="false" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.7"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical"
android:weightSum="1" >
<TextView
android:id="#+id/listitem_name"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"
android:focusable="false"
android:focusableInTouchMode="false"
android:textColor="#color/loclistitem_text"
android:textIsSelectable="true" >
</TextView>
<TextView
android:id="#+id/listitem_address"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:focusable="false"
android:focusableInTouchMode="false"
android:textIsSelectable="true" >
</TextView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal" >
<TextView
android:id="#+id/listitem_text2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:focusable="false"
android:focusableInTouchMode="false"
android:textColor="#color/loclistitem_text"
android:textIsSelectable="true" />
<ImageView
android:id="#+id/listitem_prop1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:contentDescription="#null"
android:focusable="false"
android:focusableInTouchMode="false" />
<ImageView
android:id="#+id/listitem_prop2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:contentDescription="#null"
android:focusable="false"
android:focusableInTouchMode="false" />
<ImageView
android:id="#+id/listitem_prop3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:contentDescription="#null"
android:focusable="false"
android:focusableInTouchMode="false" />
</LinearLayout>
</LinearLayout>
<ImageView
android:id="#+id/listitem_img3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:contentDescription="#null"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"/>
</LinearLayout>
I am not sure where I am going wrong but struggling for last 6 hours.
Custom Adapter Code(Partial Coe):
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_listitem, parent,false);
holder.locName = (TextView) convertView
.findViewById(R.id.listitem_name);
holder.locDistance = (TextView) convertView
.findViewById(R.id.listitem_text2);
holder.locRating = (ImageView) convertView
.findViewById(R.id.listitem_img3);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
convertView.setOnClickListener(this);
holder.locName.setText(data.get(position).getLocationName());
return convertView;
Please advise.
Solution
Finally I was able to do this by using Relative Layout instead of Linear Layout in my list item layout.
Add the click listener when you inflate the view only. Try this:
convertView = inflater.inflate(R.layout.list_listitem, null);
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
convertView.setTag(holder);
Try using this on your Activity class:
listView.setItemsCanFocus(true);
or
listView.setItemsCanFocus(false);
which is useful for you.
I know its late but may be someone finds it helpful. In order to make the whole item as clickable instead of different items in a row use this
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});

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"

Memory issue with items in BaseAdapter

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

Categories

Resources