Clicking an ImageButton inside a ListView - android

I have a ListView. Each row is a custom layout having some textviews and 2 imagebuttons. Every tutorial/topic/documentation/etc. i tried, nothing worked on me, even thou testing the tutorials worked perfect on their own. So, i populate the ListView, than i'd like that by clicking an imagebutton on the row x to at least show me a toast or do something, like it should.
Below is the code (MainActivity.java).
package ro.pca.pizzarecipes;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import com.google.ads.AdRequest;
import com.google.ads.AdView;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity{
protected AdView adView;
public ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdRequest re = new AdRequest();
re.setGender(AdRequest.Gender.FEMALE);
adView = (AdView)findViewById(R.id.ad);
adView.setVisibility(AdView.VISIBLE);
adView.loadAd(re);
ArrayList<HashMap<String, String>> map = new ArrayList<HashMap<String, String>>();
HashMap mapItem1 = new HashMap();
mapItem1.put("Nume","Neapolitan style (alla Napoletana)");
mapItem1.put("Timp","30");
mapItem1.put("Calorii","");
mapItem1.put("Ingrediente","1 1/2 cups warm water...");
mapItem1.put("Preparare"," 1. In the bowl...");
mapItem1.put("Altele","Makes 8 servings.");
map.add(mapItem1);
HashMap mapItem2 = new HashMap();
mapItem2.put("Nume","Pan fried Hawaiian");
mapItem2.put("Timp","20");
mapItem2.put("Calorii","");
mapItem2.put("Ingrediente","Olive oil...");
mapItem2.put("Preparare"," Preheat...");
mapItem2.put("Altele","Makes 2 pizzas.");
map.add(mapItem2);
SimpleAdapter adapter = null;
adapter = new SimpleAdapter(this, map, R.layout.customlistlayout,
new String[]{"Nume", "Timp", "Calorii", "Ingrediente", "Preparare", "Altele"},
new int[]{R.id.textView1, R.id.textView2, R.id.textView4, R.id.textView7, R.id.textView9, R.id.textView10});
Collections.sort(map, new Comparator<Map<String, String>>() {
#Override
public int compare(Map<String, String> o1, Map<String, String> o2) {
String name1 = o1.get("Nume");
String name2 = o2.get("Nume");
if (name1 == null) return -1;
return name1.compareTo(name2);
}
});
setContentView(R.layout.activity_main);
lv = (ListView)findViewById(R.id.listView1);
lv.setChoiceMode(lv.CHOICE_MODE_MULTIPLE);
lv.setAdapter(adapter);
}
}
the main layout (activity_main.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<com.google.ads.AdView
android:id="#+id/ad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="a150d19cc5b81d8"
ads:loadAdOnCreate="true" />
</LinearLayout>
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayout1"
android:text="my fridge" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:divider="#C60202"
android:dividerHeight="3dip"
android:layout_below="#+id/button1" >
</ListView>
</RelativeLayout>
the row layout (customlistlayout.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="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/textView1"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="italic" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView2"
android:layout_alignBottom="#+id/textView2"
android:layout_toLeftOf="#+id/textView2"
android:text="time (min):"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/textView2"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="italic" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView4"
android:layout_alignBottom="#+id/textView4"
android:layout_toLeftOf="#+id/textView4"
android:text="calories:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView5"
android:text="ingredients"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold|italic" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView6"
android:layout_marginLeft="18dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="14sp" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView7"
android:text="preparation"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold|italic" />
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView7"
android:layout_below="#+id/textView8"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="14sp" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/imageButton1"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="#drawable/ic_launcher"
android:tag="2" />
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView9"
android:text="other info:"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold|italic" />
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView10"
android:layout_marginTop="20dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="#drawable/googletranslate"
android:tag="1" />
<TextView
android:id="#+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView9"
android:layout_below="#+id/textView11"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="15sp"
android:textStyle="italic" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp"
android:background="#layout/gradient"
android:text="nume"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30sp"
android:textStyle="bold" />
</RelativeLayout>
and a little gradient layout (gradient.xml)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#A0C2F2"
android:endColor="#D0E6F2"
android:angle="135" />
</shape>

You need extend the list adapter class, then in the getView method you can set the OnClickListener for each ImageButton.

Try this,
Instead of ImageButton use TextView . I had faced same issue in ListView Button click. It will work

I totally agree With #Dan but instead use BaseAdapter and on it getView set the Listener for the Image Button
//example
class Example extends BaseAdapter{
Context context;
public CardAdapt(Context context) {
this.context = context;
}
public int getCount() {
return //size of the list u have with the button
}
public Card getItem(int position) {
return //position of the button
}
public long getItemId(int position) {
return position;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(final int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
ViewHolder viewHolder;
Card card;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.//ur layout, null);
viewHolder.imgbtn (ImageButton) convertView.findViewById(R.id.//id for the imagebutton);
convertView.setTag(viewHolder);
}
viewHolder = (ViewHolder) convertView.getTag();
return convertView;
}
private class ViewHolder{
ImageButton imgbtn;
}
//the do the onclicklistner for the button and do what u want

According to this
the problem should be solved by adding this attribute the row layout (customlistlayout.xml)
...
<RelativeLayout
...
android:descendantFocusability="blocksDescendants" >

Related

Android custom array adapter getView is not called

this is my adapter, the getView method is not called even if the array list having elements
public class CarrierSammuryAdapter extends ArrayAdapter<CarrierSummary> {
private final Activity context;
private final ArrayList<CarrierSummary> ite;
private final int lay;
public CarrierSammuryAdapter(Activity context, ArrayList<CarrierSummary> menuLinkList,int layout) {
super(context,layout );
this.context = context;
this.ite = menuLinkList;
this.lay=layout;
}
// static to save the reference to the outer class and to avoid access to
// any members of the containing class
static class ViewHolder {
public TextView customer;
public TextView attempts;
public TextView successful;
public TextView minutes;
public TextView ASR;
public TextView ACD;
public TextView NER;
public TextView PDD;
}
#Override
public int getCount () {
return ite.size();
}
#Override
public long getItemId (int position) {
return position;
}
#Override
public CarrierSummary getItem (int position) {
return ite.get(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// ViewHolder will buffer the assess to the individual fields of the row layout
final ViewHolder holder;
// Recycle existing view if passed as parameter
// This will save memory and time on Android
// This only works if the base layout for all classes are the same
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();//this gives error !!
rowView=inflater.inflate(R.layout.carriersumamry_item,parent,false);
holder = new ViewHolder();
holder.customer=(TextView)rowView.findViewById(R.id.customer);
holder.attempts=(TextView)rowView.findViewById(R.id.attempts);
holder.successful=(TextView)rowView.findViewById(R.id.successful);
holder.minutes=(TextView)rowView.findViewById(R.id.minutes);
holder.ASR=(TextView)rowView.findViewById(R.id.asr);
holder.ACD=(TextView)rowView.findViewById(R.id.acd);
holder.NER=(TextView)rowView.findViewById(R.id.ner);
holder.PDD=(TextView)rowView.findViewById(R.id.pdd);
// ViewResizing.setListRowTextResizing(rowView, context);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
holder.customer.setText(ite.get(position).getCustomer());
holder.attempts.setText(ite.get(position).getAttempts());
holder.successful.setText(ite.get(position).getSuccessful());
holder.minutes.setText(ite.get(position).getMinutes());
holder.ASR.setText(ite.get(position).getASR());
holder.ACD.setText(ite.get(position).getACD());
holder.NER.setText(ite.get(position).getNER());
holder.PDD.setText(ite.get(position).getPDD());
return rowView;
}
}
and this is how i called it
carrierSummaryList =(ListView)findViewById(R.id.carrierSummary_listview);
CarrierSammuryAdapter adapter = new CarrierSammuryAdapter(CarrierSummaryActivity.this, carriersam,R.layout.carriersumamry_item);
carrierSummaryList.setAdapter(adapter);
I search a lot to solve this issue but no solution, getView method is never called.
this is my XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/pdd"
android:textColor="#color/grey"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ner"
android:textColor="#color/grey"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/acd"
android:textColor="#color/grey"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/asr"
android:textColor="#color/grey"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/minutes"
android:textColor="#color/grey"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/successful"
android:textColor="#color/grey"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/attempts"
android:textColor="#color/grey"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/customer"
android:textColor="#color/grey"
/>
</LinearLayout>
I had this exact same problem, to solve it, I simply changed the ArrayAdapter to BaseAdapter, implemented the overriden methods and it worked.
And as khuskal said, you should always use Context instead of Activity.
there are two mistakes..
first is change Activity to Contex in constructor
secondly change
rowView=inflater.inflate(R.layout.single_row,parent,false);
Hope this will help you
/**
* Constructor
*
* #param context The current context.
* #param resource The resource ID for a layout file containing a TextView to use when
* instantiating views.
*/
public ArrayAdapter(Context context, int resource) {
init(context, resource, 0, new ArrayList<T>());
}
while you are doing super(context,layout) , this is the constructor that gets called. So adapter data is set to empty list.
Like derek said, Instead you should use super(context,layout,menuLinkList);
/**
* Constructor
*
* #param context The current context.
* #param resource The resource ID for a layout file containing a TextView to use when
* instantiating views.
* #param objects The objects to represent in the ListView.
*/
public ArrayAdapter(Context context, int resource, List<T> objects) {
init(context, resource, 0, objects);
}
The problem was in my drawerLayout, i put the listView into RelativeLayout but when i change to LinearLayout its working, this is the code
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white"
android:orientation="vertical">
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/mainbar"
layout="#layout/second_topmain"/>
<ListView
android:id="#+id/carrierSummary_listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
></ListView>
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="0dp"
android:id="#+id/mainbar"
layout="#layout/bottom_bar"/>
</LinearLayout>
<LinearLayout
android:id="#+id/linearSlider"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="right"
android:background="#7e7e7e"
android:orientation ="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Carrier Type"
android:textColor="#android:color/white"
android:padding="5dp"
android:background="#color/darkgrey"
/>
<LinearLayout
android:padding="3dp"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:background="#color/darkgrey"
android:layout_height="wrap_content">
<Button
android:id="#+id/customerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/grey"
android:onClick="toggleClicked"
android:background="#drawable/toggleon"
android:text="Customer"
/>
<Button
android:id="#+id/SupplierButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="toggleClicked"
android:textColor="#color/grey"
android:background="#drawable/toggleoff"
android:text="Supplier"
/>
</LinearLayout>
<RelativeLayout
android:padding="3dp"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Carrier"
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true"
/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinnerCarrier"
android:layout_alignParentRight="true"
android:background="#android:color/transparent"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
<RelativeLayout
android:padding="3dp"
android:background="#color/darkgrey"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Top"
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true"
/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/SpinnerTop"
android:layout_alignParentRight="true"
android:background="#android:color/transparent"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
<RelativeLayout
android:padding="3dp"
android:layout_width="wrap_content"
android:onClick="showDateTimePickerFrom"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="From Date"
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/fromDate"
android:layout_alignParentRight="true"
android:background="#android:color/transparent"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
<RelativeLayout
android:padding="3dp"
android:background="#color/darkgrey"
android:layout_width="wrap_content"
android:onClick="showDateTimePickeTo"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="To Date"
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/toDate"
android:layout_alignParentRight="true"
android:background="#android:color/transparent"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
<RelativeLayout
android:padding="3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Group By Profile"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/byprofilecheck"
android:layout_alignParentRight="true"
android:background="#android:color/transparent"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>

Listview displays only one row from the list

I am new to android listviews and I am trying to populate a listview in android by a list of items coming from a webservice. I know that the list contains more than one record coming from the webservice but my custom listview displays only the first record from the list. I checked the size of the list and there is always more than one records but the listview is showing only one of them. My custom adapter is like following:
public class ListAdapter extends BaseAdapter {
Context ctx;
LayoutInflater lInflater;
ArrayList<LItem> lstItems;
ListAdapter(Context context, ArrayList<LItem> objects) {
ctx = context;
lstItems = objects;
lInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return lstItems.size();
}
#Override
public Object getItem(int position) {
return lstItems.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.list_style_task
, parent, false);
}
LItem p = getProduct(position);
((TextView) view.findViewById(R.id.tvMaterial )).setText(p.getMName() );
((TextView) view.findViewById(R.id.tvTask )).setText(p.getTName() );
((TextView) view.findViewById(R.id.tvBQuantity )).setText(p.getBQ() );
// CheckBox cbBuy = (CheckBox) view.findViewById(R.id.checkbox);
//cbBuy.setOnCheckedChangeListener(myCheckChangList);
// cbBuy.setTag(position);
// cbBuy.setChecked(p.selected);
return view;
}
LItem getProduct(int position)
{
return ((LItem) getItem(position));
}
ArrayList<LItem> getBox() {
ArrayList<LItem> box = new ArrayList<LItem>();
for (LItem p : lstItems) {
// if (p.selected)
// box.add(p);
}
return box;
}
OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
//getProduct((Integer) buttonView.getTag())= isChecked;
}
};
}
I am binding the listview as:
protected void onPostExecute(List<LItem> li ) {
super.onPostExecute(lstItm);
if(lstItm.size()>0) {
Adp=new ListAdapter(myactivity,lstItm);
lvTasks.setAdapter(Adp);
Log.d("Size---------",Integer.toString(lstItm.size()) );//here it writes more than one as size of the list.
}
}
My xml file for displaying lists is like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
>
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:focusable="false"
android:textColor="#FFF"
android:button="#drawable/custom_checkbox_design"
android:focusableInTouchMode="false"
android:text="" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Task:"
android:id="#+id/textView2"
android:layout_weight="1"
android:textColor="#FFF"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="task"
android:id="#+id/tvTask"
android:layout_weight="1"
android:textColor="#FFF"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="45dp"
android:layout_marginRight="20dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Material:"
android:id="#+id/textView1"
android:layout_weight="1"
android:textColor="#FFF"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="material"
android:id="#+id/tvMaterial"
android:layout_weight="1"
android:textColor="#FFF"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="45dp"
android:layout_marginRight="20dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Balanced Quantity:"
android:id="#+id/textView14"
android:layout_weight="1"
android:textColor="#FFF"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bquantity"
android:id="#+id/tvBQuantity"
android:layout_weight="1"
android:textColor="#FFF"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="45dp"
android:layout_marginRight="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Adjust Balanced Quantity:"
android:id="#+id/textView25"
android:layout_weight="1"
android:textColor="#FFF"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/tvAQuantity"
android:layout_weight="1"
android:textColor="#FFF"
/>
</LinearLayout>
</LinearLayout>
My xml for listview is like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="kanix.highrise.com.highrise.generate_material_requisition">
<!-- TODO: Update blank fragment layout -->
<ScrollView android:id="#+id/ScrlView" android:layout_width="fill_parent" android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lstTaskQuantity"
android:layout_weight="1" >
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:id="#+id/txtLddsdfabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="Req. From :"
android:layout_weight="1"
android:textColor="#fff" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="date"
android:id="#+id/etDate"
android:layout_weight=".5"
android:hint="DD/MM/YYYY"/>
<ImageButton
android:layout_width="35dp"
android:layout_height="35dp"
android:id="#+id/btnimgcal"
android:src="#drawable/calender"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:id="#+id/txtLddsdfadsbel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="For Days :"
android:layout_weight="1"
android:textColor="#fff" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:text="0"
android:id="#+id/editText"
android:layout_weight="1.69" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:id="#+id/txtLddsddfgfadsbel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="Quantity :"
android:layout_weight="1"
android:textColor="#fff" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:text="0"
android:id="#+id/etQuantity"
android:layout_weight="1.60" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="center"
android:orientation="vertical"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#0080FF"
android:background="#fff"
android:text="Generate Requisition"
android:id="#+id/btnSave" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Issue was with the scrollview which was createing the issue. I just removed
<ScrollView android:id="#+id/ScrlView" android:layout_width="fill_parent" android:layout_height="fill_parent" >
</ScrollView>
and it worked for me :)

Android RelativeLayout not center

I have a problem about TextView in RelativeLayout is not center, (only some objects)
First, I'm use ListAdapter for ListView.
public class ListAdapter extends ArrayAdapter<Menu> {
private LayoutInflater inflater;
public ListAdapter(Context c, ArrayList<Menu> o) {
super(c, 0, o);
inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(int position, View v, ViewGroup parent) {
View view = null;
if (v == null) {
view = inflater.inflate(R.layout.row, null);
} else {
view = v;
}
final Menu data = this.getItem(position);
TextView title = (TextView) view.findViewById(R.id.title);
ImageView background = (ImageView) view.findViewById(R.id.background);
ImageView icon = (ImageView) view.findViewById(R.id.icon);
TextView subtitle = (TextView) view.findViewById(R.id.subtitle);
subtitle.setText(data.getSubTitle());
title.setText(data.getTitle());
background.setImageDrawable(data.getBackground());
icon.setImageDrawable(data.getIcon());
return view;
}
}
and, My row.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="match_parent" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="75dip"
android:layout_height="82dip"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<ImageView
android:id="#+id/background"
android:layout_width="75dip"
android:layout_height="83dip"
android:src="#drawable/mediumorchid" />
<ImageView
android:id="#+id/icon"
android:layout_width="35dip"
android:layout_height="35dip"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_discard" />
</RelativeLayout>
<FrameLayout
android:id="#+id/fl1"
android:layout_width="75dip"
android:layout_height="80dip"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</FrameLayout>
<RelativeLayout
android:id="#+id/width"
android:layout_width="fill_parent"
android:layout_height="80dip"
android:layout_alignBottom="#+id/fl1"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/fl1"
android:layout_toRightOf="#+id/fl1"
android:background="#ecf0f1" >
<RelativeLayout
android:id="#+id/item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginBottom="10dip"
android:text="Pure"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_below="#+id/title"
android:layout_marginTop="5dip"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
</RelativeLayout>
<FrameLayout
android:id="#+id/frameLayout1"
android:layout_width="75dip"
android:layout_height="2dip"
android:layout_alignLeft="#+id/width"
android:layout_alignParentRight="true"
android:layout_below="#+id/width"
android:background="#drawable/cdivider" >
</FrameLayout>
This xml's Graphic Design
But in Device(example grouper) , i discover this problem.
In some object, (example see Help ~) Was centered properly. but, other objects, wasn't centered properly.
and i discover this problem another device(different resolution, size, Android version, density).
What can i do solve this problem??
Try this..
Add android:layout_centerHorizontal="true" for subtitle TextView
<TextView
android:id="#+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:layout_marginTop="5dip"
android:gravity="center_vertical"
android:layout_centerHorizontal="true"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
or this
<TextView
android:id="#+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_below="#+id/title"
android:layout_marginTop="5dip"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />

Android Keyboard appear after Tabbar

I have four tabs in my home screen and one of them takes user input,
I have a two problem one is when open tab activity then android keyboard automatically open
and another problem is keyboard coming after tabbar.
I already added android:windowSoftInputMode="adjustPan" in my menifetch file
I Share My code
My createchallan.xml code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="38dp"
android:text="Search" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/search"
android:layout_alignBottom="#+id/search"
android:layout_toRightOf="#+id/search"
android:background="#android:drawable/editbox_background"
android:layout_marginLeft="2dip"
android:singleLine="true"
android:ems="5" >
<requestFocus android:layout_width="wrap_content" />
</EditText>
<CheckBox
android:id="#+id/review"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/search"
android:layout_alignParentRight="true"
android:layout_marginTop="6dip"
android:layout_alignBaseline="#+id/search"
android:text="Review Item" />
<RelativeLayout
android:id="#+id/header"
android:layout_marginTop="10dip"
android:layout_below="#+id/review"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#E5E4E2"
android:orientation="horizontal" >
<TextView
android:id="#+id/txtItemcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item Code"
android:singleLine="true"
android:layout_marginLeft="3dip"
android:textStyle="bold"
/>
<TextView
android:id="#+id/txtItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="23dp"
android:layout_toRightOf="#+id/txtItemcode"
android:textStyle="bold"
android:text="Item" />
<TextView
android:id="#+id/txtItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="14dip"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:textStyle="bold"
android:text="Quantity" />
</RelativeLayout>
<LinearLayout
android:id="#+id/listlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header"
android:layout_above="#+id/lastbutton">
<ListView
android:id="#+id/createlist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:cacheColorHint="#00000000"
android:divider="#adb8c2"
android:dividerHeight="1dp"
android:scrollingCache="false"
android:smoothScrollbar="true"
>
</ListView>
</LinearLayout>
<RelativeLayout
android:layout_alignParentBottom="true"
android:id="#+id/lastbutton"
android:layout_marginBottom="8dip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/createcancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/createsavedraft"
android:background="#drawable/roundedbutton"
android:text=" Cancel " />
<Button
android:id="#+id/createsavedraft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/creatapprove"
android:background="#drawable/roundedbutton"
android:text=" Save Draft " />
<Button
android:id="#+id/creatapprove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/roundedbutton"
android:text=" Approve " />
</RelativeLayout>
my custom xml create_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/txtItemcode"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:text="323232"
android:singleLine="true"
android:layout_marginTop="9dip"
android:layout_marginLeft="5dip"
android:textStyle="bold"
android:focusable="false"
android:focusableInTouchMode="false"
/>
<TextView
android:id="#+id/txtItem"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_marginTop="9dip"
android:layout_marginLeft="25dp"
android:layout_toRightOf="#+id/txtItemcode"
android:layout_alignBaseline="#+id/txtItemcode"
android:text="5456455565456"
android:focusable="false"
android:focusableInTouchMode="false" />
<EditText
android:id="#+id/editcreateQuantity"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#android:drawable/editbox_background"
android:ems="10"
android:inputType="number"
android:layout_marginRight="5dip"
android:layout_alignBaseline="#+id/txtItemcode"
android:focusable="true"
android:singleLine="true" >
</EditText>
</RelativeLayout>
My java code
public class CreateChallan extends Activity {
ListView lstCreate;
String[] strmainItemCode;
String[] strItem;
String[] strQuantity;
Context context=this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.createchallan);
lstCreate= (ListView) findViewById(R.id.createlist);
lstCreate.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
strmainItemCode= new String[]{"55555551","255555","355555","455555","555555"};
strItem =new String[]{"A","B","C","D","F"};
strQuantity =new String[]{"100","200","30","400","500"};
CreateAdapter adapter= new CreateAdapter(this, strmainItemCode, strItem, strQuantity);
lstCreate.setAdapter(adapter);
lstCreate.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position1, long id) {
// TODO Auto-generated method stub
Toast.makeText(context, "Position", Toast.LENGTH_LONG).show();
}
});
}
// Create List Adapter
class CreateAdapter extends ArrayAdapter<String>
{
TextView txtItecode, txtItem;
EditText editQuantity;
String[] strItecode;
String[] strItem;
String[] strQuantity;
Context context;
CreateAdapter(Context context, String[] strItemcode, String[] strItem, String[] strQauntity)
{
super(context,R.layout.create_list_item,R.id.txtItemcode,strItemcode);
this.context= context;
this.strItecode= strItemcode;
this.strItem= strItem;
this.strQuantity= strQauntity;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View row;
row=mInflater.inflate(R.layout.create_list_item, parent,false);
txtItecode= (TextView) row.findViewById(R.id.txtItemcode);
txtItem =(TextView) row.findViewById(R.id.txtItem);
editQuantity = (EditText) row.findViewById(R.id.editcreateQuantity);
editQuantity.setSelected(false);
txtItecode.setText(strItecode[position]);
txtItem.setText(strItem[position]);
editQuantity.setText(strQuantity[position]);
txtItecode.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "click", Toast.LENGTH_LONG).show();
}
});
return row;
}
}
}
below code on you manifest in TabHostActivity or tab parent activity..
android:windowSoftInputMode="adjustPan"
Hi try below code on you manifest for activity
android:windowSoftInputMode="adjustPan"
hope this will work,Thanks

How to show Toast in a class extended by BaseAdapter Get View Method

I am having problem of showing Toast Message when i click a button within a list View.
The problem is that i a custom class extended by BaseAdapter i have a method named GetView
I wanna show a toast Message from that particular message
Following is my Listview Row Xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:context=".HomeActivity" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="#+id/tableRow_laugh"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:ignore="UselessParent" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<TableRow
android:id="#+id/tableRow_Header_laugh"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#818085" >
<RelativeLayout
android:id="#+id/relative_header"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#818085" >
<ImageView
android:id="#+id/UserIconPic_laugh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_weight="0.4"
android:src="#drawable/ic_launcher"
app:ignore="ObsoleteLayoutParam,ContentDescription" />
<Button
android:id="#+id/btn_Vote_laugh"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_weight=".4"
android:text="VOTE"
app:ignore="ObsoleteLayoutParam,HardcodedText" />
<TextView
android:id="#+id/txt_number_of_vote_laugh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btn_Vote_laugh"
android:layout_alignBottom="#+id/btn_Vote_laugh"
android:layout_toRightOf="#+id/UserIconPic_laugh"
android:layout_weight=".4"
android:textAppearance="?android:attr/textAppearanceMedium"
app:ignore="ObsoleteLayoutParam,HardcodedText" />
</RelativeLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow_MainText_laugh"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/txt_MainText_laugh"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/whatweare_about_main"
android:textAppearance="?android:attr/textAppearanceMedium"
app:ignore="HardcodedText" />
</TableRow>
<TableRow
android:id="#+id/tableRow_Footer_laugh"
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="#+id/relative_footer"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#bababa" >
<Button
android:id="#+id/btn_Share_laugh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Share"
app:ignore="HardcodedText" />
<ImageButton
android:id="#+id/btn_facebook_laugh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/btn_twitter_laugh"
android:src="#drawable/pic2"
app:ignore="ContentDescription" />
<ImageButton
android:id="#+id/btn_twitter_laugh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/btn_facebook_laugh"
android:src="#drawable/pic3"
app:ignore="ContentDescription" />
</RelativeLayout>
</TableRow>
</TableLayout>
</RelativeLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:ignore="UselessParent" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<TableRow
android:id="#+id/tableRow_header_light"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/RelativeLayout02"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#818085" >
<ImageView
android:id="#+id/UserIconPic_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_weight="0.4"
android:src="#drawable/ic_launcher"
app:ignore="ObsoleteLayoutParam,ContentDescription" />
<Button
android:id="#+id/btn_Vote_light"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_weight=".4"
android:text="VOTE"
app:ignore="ObsoleteLayoutParam,HardcodedText" />
<TextView
android:id="#+id/txt_vote_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btn_Vote_light"
android:layout_alignBottom="#+id/btn_Vote_light"
android:layout_toRightOf="#+id/UserIconPic_light"
android:layout_weight=".4"
android:textAppearance="?android:attr/textAppearanceMedium"
app:ignore="ObsoleteLayoutParam,HardcodedText" />
</RelativeLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow_content_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:maxLength="300"
android:text="#string/Main_laugh"
android:textAppearance="?android:attr/textAppearanceSmall"
app:ignore="HardcodedText" />
<ImageView
android:id="#+id/light_image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
app:ignore="ContentDescription" />
</FrameLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow_Footer_light"
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="#+id/RelativeLayout01"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#bababa" >
<Button
android:id="#+id/Btn_Share_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Share"
app:ignore="HardcodedText" />
<ImageButton
android:id="#+id/btn_facebook_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/btn_twitter_light"
android:src="#drawable/pic2"
app:ignore="ContentDescription" />
<ImageButton
android:id="#+id/btn_twitter_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/btn_facebook_light"
android:src="#drawable/pic3"
app:ignore="ContentDescription" />
</RelativeLayout>
</TableRow>
</TableLayout>
</RelativeLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow_fun"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:ignore="UselessParent" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<TableRow
android:id="#+id/tableRow_header_fun"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/RelativeLayout04"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#818085" >
<ImageView
android:id="#+id/UserIconPic_fun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_weight="0.4"
android:src="#drawable/ic_launcher"
app:ignore="ObsoleteLayoutParam,ContentDescription" />
<Button
android:id="#+id/btn_vote_fun"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_weight=".4"
android:text="VOTE"
app:ignore="ObsoleteLayoutParam,HardcodedText" />
<TextView
android:id="#+id/txt_vote_number_fun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btn_vote_fun"
android:layout_alignBottom="#+id/btn_vote_fun"
android:layout_toRightOf="#+id/UserIconPic_fun"
android:layout_weight=".4"
android:textAppearance="? android:attr/textAppearanceMedium"
app:ignore="ObsoleteLayoutParam,HardcodedText" />
</RelativeLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow_Content_fun"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/relativelayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/image_fun"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:baselineAlignBottom="true"
android:cropToPadding="true"
app:ignore="ContentDescription" />
<TextView
android:id="#+id/myImageViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/image_fun"
android:layout_alignLeft="#+id/image_fun"
android:layout_alignRight="#+id/image_fun"
android:layout_alignTop="#+id/image_fun"
android:layout_margin="1dp"
android:gravity="center"
android:text="#string/main_light"
android:textColor="#000000" />
</RelativeLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow_Footer_fun"
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="#+id/RelativeLayout03"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#bababa" >
<Button
android:id="#+id/Btn_share_fun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Share"
app:ignore="HardcodedText" />
<ImageButton
android:id="#+id/btn_facebook_fun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/btn_twitter_fun"
android:src="#drawable/pic2"
app:ignore="ContentDescription" />
<ImageButton
android:id="#+id/btn_twitter_fun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/btn_facebook_fun"
android:src="#drawable/pic3"
app:ignore="ContentDescription" />
</RelativeLayout>
</TableRow>
</TableLayout>
</RelativeLayout>
</TableRow>
</TableLayout>
</ScrollView>
My CustomeAdapter Class code
package com.example.laysapp.ContentListView;
import java.util.ArrayList;
import java.util.List;
import com.example.laysapp.R;
import com.example.laysapp.RegisteredUser.LighterShare;
import android.app.Application;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class ContentItemAdapter extends BaseAdapter {
private final List<Content_Items> items;
public ContentItemAdapter(final Context context, final int itemResId,
final ArrayList<Content_Items> items2) {
this.items = items2;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#SuppressWarnings("null")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final Content_Items item = this.items.get(position);
View itemView = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) parent.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemView = inflater.inflate(R.layout.contentlistrow, null);
} else {
itemView = convertView;
}
// Set the text of the Laugh Row Start
TextView txtLaugh_Vote = (TextView) itemView
.findViewById(R.id.txt_number_of_vote_laugh);
txtLaugh_Vote.setText(item.getLaughNo_Vote());
TextView txtlaugh_Content = (TextView) itemView
.findViewById(R.id.txt_MainText_laugh);
txtlaugh_Content.setText(item.getLaughContent());
ImageView imgView_Laugh_UserPic = (ImageView) itemView
.findViewById(R.id.UserIconPic_laugh);
imgView_Laugh_UserPic.setImageBitmap(item.getLaughtUserPic());
// Set the text of the Laugh Row End
// Setting of Light Row Start
TextView txtLight_Vote = (TextView) itemView
.findViewById(R.id.txt_vote_light);
txtLight_Vote.setText(item.getLaughNo_Vote());
TextView txtlight_Content = (TextView) itemView
.findViewById(R.id.textView1);
txtlight_Content.setText(item.getLaughContent());
ImageView imgView_Light_UserPic = (ImageView) itemView
.findViewById(R.id.UserIconPic_light);
imgView_Light_UserPic.setImageBitmap((item.getLightStoriesUserPic()));
ImageView imgView_LightContent_Image = null;
// Check weather ContentImage is Available For Light
String Check = item.getIsLightContentAvaiable();
if (Check == "yes") {
imgView_LightContent_Image = (ImageView) imgView_LightContent_Image
.findViewById(R.id.light_image);
imgView_LightContent_Image.setImageBitmap(item
.getLightStoriesContentImagePic());
}
// Setting of Light Row End
// Set the text of the Funto Row Start
TextView txtfunto_Vote = (TextView) itemView
.findViewById(R.id.txt_vote_number_fun);
txtfunto_Vote.setText(item.getLaughNo_Vote());
TextView txtfunto_Content = (TextView) itemView
.findViewById(R.id.myImageViewText);
txtfunto_Content.setText(item.getLaughContent());
// Set the text of the Funto Row End
ImageView imgView_funto_UserPic = (ImageView) itemView
.findViewById(R.id.UserIconPic_fun);
imgView_funto_UserPic.setImageBitmap(item.getfuntoUserPic());
ImageView imgView_funto_ContentPic = (ImageView) itemView
.findViewById(R.id.image_fun);
imgView_funto_ContentPic.setImageBitmap(item.getfuntoContentImagePic());
Button MainVote_Laugh = null;
MainVote_Laugh = (Button) MainVote_Laugh
.findViewById(R.id.btn_Vote_laugh);
MainVote_Laugh.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplication(), "Clicked Laugh Vote", Toast.LENGTH_SHORT).Show();
}
});
return itemView;
}
}
Replace this line:
Toast.makeText(getApplication(), "Clicked Laugh Vote", Toast.LENGTH_SHORT).show();
with this line:
Toast.makeText(v.getContext(), "Clicked Laugh Vote", Toast.LENGTH_SHORT).show();
You have got a context right in the constructor. create a class level Context variable say ctx. inside constructor write
this.ctx = context;
finally replace getApplication() in
Toast.makeText(getApplication(), "Clicked Laugh Vote", Toast.LENGTH_SHORT).Show();
with ctx.
You can also use parent.getContext(), full code of toast is like below
Toast.makeText(parent.getContext(), "view clicked", Toast.LENGTH_SHORT).show();
As you switch from your MainActivity context changes, you have to use your AdapterClass > ViewHolderClass > ViewHolder's constructor argument to get context for adapter.
Here is an example.
public MyViewHolder(final View itemView) {
super(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(itemView.getContext(),"Hi",Toast.LENGTH_LONG);
}
});
}
otherwise you can also pass context to a constructor argument for any other class not having activity.
example.
public class MyClass {
Context c;
public MyClass(Context context) {
c = context;
}}

Categories

Resources