I'm implementing View-pager in List-view as row item .When run the application my images is not scrolling one by one in view pager. What is the wrong in my code. Can someone suggest me how to make it work or scrolling smoothly images in view pager.
Here is my code
public class Demo_Display extends Activity {
private String str[] = {"weklfjo","??2","??3"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView)findViewById(R.id.listview);
MyListAdapter adapter = new MyListAdapter(this,R.layout.row,str);
listView.setAdapter(adapter);
}
class MyListAdapter extends ArrayAdapter<String> {
private LayoutInflater inflater = null;
private static final float BUTTON_WIDTH_DP = 70f;
private int margin;
public MyListAdapter(Context context, int resource,String[] items) {
super(context, resource,items);
inflater = LayoutInflater.from(context);
float density = getContext().getResources().getDisplayMetrics().density;
int buttonWidthPX = (int) (BUTTON_WIDTH_DP * density + 0.5f);
WindowManager wm = (WindowManager)getContext().getSystemService(getContext().WINDOW_SERVICE);
Display dp = wm.getDefaultDisplay();
margin = dp.getWidth() - buttonWidthPX;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = inflater.inflate(R.layout.row,null);
}
ViewPager viewPager = (ViewPager)convertView.findViewById(R.id.Image_ViewPagers);
viewPager.setPageMargin(-margin);
// MyPagerAdapter adapter = new MyPagerAdapter(getContext(),getItem(position));
MyPagerAdapter adapter = new MyPagerAdapter(getContext());
viewPager.setAdapter(adapter);
return convertView;
}
}
class MyPagerAdapter extends PagerAdapter {
Context mContent;
LayoutInflater mLayoutInflater;
private int[] GalImages = new int[] {
R.drawable.flower1,
R.drawable.flower1
};
public MyPagerAdapter(Context context) {
super();
mContent = context;
mLayoutInflater = (LayoutInflater) mContent.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public Object instantiateItem(ViewGroup container, int position)
{
ImageView imageView = new ImageView(mContent);
int padding = mContent.getResources().getDimensionPixelSize(R.dimen.abc_button_padding_horizontal_material);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(GalImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
#Override
public int getCount() {
// return PAGE_NUM;
return GalImages.length;
}
#Override
public boolean isViewFromObject(View view, Object obj) {
//return view.equals(obj);
return view == ((ImageView) obj);
}
}
}
Here is my XML file
<?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="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/txt_InitialLetter"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/circle_shape"
android:gravity="center"
android:paddingBottom="5sp"
android:text="jsdygf" />
<TextView
android:id="#+id/txt_Desc"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal|left"
android:paddingLeft="5sp"
android:text="sdkjhfeorygdshgvfsjd" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:orientation="horizontal"
android:paddingLeft="5sp" >
<android.support.v4.view.ViewPager
android:id="#+id/Image_ViewPagers"
android:layout_width="fill_parent"
android:layout_height="190dp"
android:background="#android:color/white"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="4dp"
android:orientation="horizontal" >
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:contentDescription="#string/desc"
android:src="#drawable/reply_blue" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:contentDescription="#string/desc"
android:src="#drawable/retweet_blue" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:contentDescription="#string/desc"
android:src="#drawable/star" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:contentDescription="#string/desc"
android:src="#drawable/camera_blue" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:contentDescription="#string/desc"
android:src="#drawable/recorder_blue" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:contentDescription="#string/desc"
android:src="#drawable/follow_up" />
</LinearLayout>
</LinearLayout>
and List View xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
Related
I have something wrong with recyclerView. It doesn't show all items. For example, recyclerView has 12 items, but it shows like 10 with a half items. I can set paddingBottom for it, and it shows all items, but I don't think it is a good way. How can I fix it? I think maybe it becouse of my buttons above it.
acctivity_search_coin.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingBottom="50dp"
app:layout_constraintTop_toBottomOf="#+id/lianerForThreeBtn" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:background="#FFFFFF"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:textSize="15sp"
android:drawablePadding="6dp"
android:gravity="center_vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="#+id/lianerForThreeBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintTop_toBottomOf="#+id/button2">
<Button
android:id="#+id/btn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:layout_weight="1"/>
<Button
android:id="#+id/btn2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:layout_weight="1"/>
<Button
android:id="#+id/btn3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:layout_weight="1"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
activity_recycler_view_adapter_coin_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/recyclerImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:id="#+id/denominationTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginStart="20dp"
android:layout_marginEnd="8dp"
android:layout_toEndOf="#+id/secondIV"
android:textSize="20sp" />
<TextView
android:id="#+id/yearTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/denominationTV"
android:layout_alignStart="#+id/denominationTV"
android:layout_alignParentEnd="true"
android:layout_marginStart="0dp"
android:layout_marginTop="6dp"
android:layout_marginEnd="8dp"
android:textSize="20sp" />
<ImageView
android:id="#+id/firstIV"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentStart="true"
android:layout_marginStart="0dp"/>
<ImageView
android:id="#+id/secondIV"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginStart="4dp"
android:layout_toEndOf="#+id/firstIV"/>
</RelativeLayout>
RecyclerViewAdapterCoinList.java
public class RecyclerViewAdapterCoinList extends RecyclerView.Adapter<RecyclerViewAdapterCoinList.ViewHolder> {
private List<String> mYearList;
private List<String> mDenominationList;
private List<String> mImageList1;
private List<String> mImageList2;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
Context context;
RecyclerViewAdapterCoinList(Context context, List<String> denominationList,List<String> yearList, List<String> imageList1,List<String> imageList2) {
this.mInflater = LayoutInflater.from(context);
this.mYearList = yearList;
this.mDenominationList = denominationList;
this.mImageList1 = imageList1;
this.mImageList2 = imageList2;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.activity_recycler_view_adapter_coin_list, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
String year = mYearList.get(position);
String denomination = mDenominationList.get(position);
String imageString1 = mImageList1.get(position);
String imageString2 = mImageList2.get(position);
holder.denominationTV.setText(denomination);
holder.yearTV.setText(year);
try {
Glide.with(context).load(imageString1).into(holder.firstIV);
Glide.with(context).load(imageString2).into(holder.secondIV);
}catch (Exception e){e.printStackTrace();}
}
// total number of rows
#Override
public int getItemCount() {
return mDenominationList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView denominationTV;
TextView yearTV;
ImageView firstIV;
ImageView secondIV;
ViewHolder(View itemView) {
super(itemView);
denominationTV = itemView.findViewById(R.id.denominationTV);
yearTV = itemView.findViewById(R.id.yearTV);
firstIV = itemView.findViewById(R.id.firstIV);
secondIV = itemView.findViewById(R.id.secondIV);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
}
}
String getItem(int id) {
return mDenominationList.get(id);
}
void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
public interface ItemClickListener {
void onItemClick(View view, int position);
}
}
Try adding app:layout_constraintBottom_toBottomOf="parent" in the recycler view and also make the height of the recycler view to be 0dp (match_constraint).
I use fragment and I want to set listview in m fragment.I have error in this field.
I want to use ListView of Foldingcell, but I think I have an error in my adapter.
i can not find error.
any body can't help me?
what should I do?
this is my error:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.a700daneh.a700daneh.loanFragment.onCreateView(loanFragment.java:49)
this is my element for using in listView
public class Item {
String family1;
String cost1;
String family2;
String cost2;
String explain;
String code;
int Icon;
private View.OnClickListener requestBtnClickListener;
public Item() {
}
public Item(String family1, String cost1, String family2, String cost2, String explain, String code, int Icon) {
this.family1 = family1;
this.cost1 = cost1;
this.family2 = family2;
this.cost2 = cost2;
this.explain = explain;
this.code = code;
this.Icon = Icon;
//this.time = time;
}
public String getFamily1() {
return family1;
}
public void setFamily1(String family1) {
this.family1 = family1;
}
public String getCost1() {
return cost2;
}
public void setCost1(String cost2) {
this.cost2 = cost2;
}
public String getFamily2() {
return family2;
}
public void setFamily2() {
this.family2 = family2;
}
public String getCost2() {
return cost2;
}
public void setCost2() {
this.cost2 = cost2;
}
public String getExplain() {
return explain;
}
public void setExplain() {
this.explain = explain;
}
public String getCode() {
return code;
}
public void setCode() {
this.code = code;
}
public int getIcon() {
return Icon;
}
/**
* #return List of elements prepared for tests
*/
public static ArrayList<Item> getTestingList() {
ArrayList<Item> items = new ArrayList<>();
items.add(new Item("خانواده x:", "500,000 تومان", "خانواده x:", "500,000 تومان", "توضیحات:", "کد: 12345678", R.drawable.family));
items.add(new Item("خانواده x:", "500,000 تومان", "خانواده x:", "500,000 تومان", "توضیحات:", "کد: 12345678", R.drawable.family));
items.add(new Item("خانواده x:", "500,000 تومان", "خانواده x:", "500,000 تومان", "توضیحات:", "کد: 12345678", R.drawable.family));
items.add(new Item("خانواده x:", "500,000 تومان", "خانواده x:", "500,000 تومان", "توضیحات:", "کد: 12345678", R.drawable.family));
items.add(new Item("خانواده x:", "500,000 تومان", "خانواده x:", "500,000 تومان", "توضیحات:", "کد: 12345678", R.drawable.family));
return items;
}
}
this is my adapter called FoldingCellListAdapter
public class FoldingCellListAdapter extends ArrayAdapter<Item> {
private HashSet<Integer> unfoldedIndexes = new HashSet<>();
private View.OnClickListener defaultRequestBtnClickListener;
public FoldingCellListAdapter(Context context, List<Item> objects) {
super(context, 0, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// get item for selected view
Item item = getItem(position);
// if cell is exists - reuse it, if not - create the new one from resource
FoldingCell cell = (FoldingCell) convertView;
ViewHolder viewHolder;
if (cell == null) {
viewHolder = new ViewHolder();
LayoutInflater vi = LayoutInflater.from(getContext());
cell = (FoldingCell) vi.inflate(R.layout.cell, parent, false);
// binding view parts to view holder
viewHolder.family1 = (TextView) cell.findViewById(R.id.family1);
viewHolder.code = (TextView) cell.findViewById(R.id.code);
viewHolder.family2 = (TextView) cell.findViewById(R.id.family2);
viewHolder.cost2 = (TextView) cell.findViewById(R.id.cost2);
viewHolder.explain = (TextView) cell.findViewById(R.id.explain);
viewHolder.cost1 = (TextView) cell.findViewById(R.id.cost1);
viewHolder.payment = (TextView) cell.findViewById(R.id.pay);
viewHolder.familyImage1 = (ImageView) cell.findViewById(R.id.familyImage1);
viewHolder.pinkback = (ImageView) cell.findViewById(R.id.pinkback);
viewHolder.familyImage2 = (ImageView) cell.findViewById(R.id.familyImage2);
cell.setTag(viewHolder);
} else {
// for existing cell set valid valid state(without animation)
if (unfoldedIndexes.contains(position)) {
cell.unfold(true);
} else {
cell.fold(true);
}
viewHolder = (ViewHolder) cell.getTag();
}
// bind data from selected element to view through view holder
viewHolder.family1.setText(item.getFamily1());
//viewHolder.time.setText(item.getTime());
viewHolder.code.setText(item.getCode());
viewHolder.family2.setText(item.getFamily2());
viewHolder.cost2.setText(item.getCost2());
viewHolder.explain.setText(String.valueOf(item.getExplain()));
viewHolder.cost1.setText(item.getCost1());
viewHolder.familyImage1.setImageResource(item.getIcon());
viewHolder.familyImage2.setImageResource(item.getIcon());
viewHolder.pinkback.setImageResource(item.getIcon());
return cell;
}
// simple methods for register cell state changes
public void registerToggle(int position) {
if (unfoldedIndexes.contains(position))
registerFold(position);
else
registerUnfold(position);
}
public void registerFold(int position) {
unfoldedIndexes.remove(position);
}
public void registerUnfold(int position) {
unfoldedIndexes.add(position);
}
// View lookup cache
private static class ViewHolder {
TextView family1;
TextView payment;
TextView cost1;
TextView family2;
TextView cost2;
TextView explain;
TextView code;
ImageView familyImage1;
ImageView pinkback;
ImageView familyImage2;
}
}
this is my codes in loanFragment
public class loanFragment extends Fragment {
public loanFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.cell, container, false);
ListView lvlist = (ListView) view.findViewById(R.id.lvlist);
final ArrayList<Item> items = Item.getTestingList();
// create custom adapter that holds elements and their state (we need hold a id's of unfolded elements for reusable elements)
final FoldingCellListAdapter adapter = new FoldingCellListAdapter(getContext(), items);
// set elements to adapter
lvlist.setAdapter(adapter);
// set on click event listener to list view
lvlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
// toggle clicked cell state
((FoldingCell) view).toggle(false);
// register in adapter that state for selected cell is toggled
adapter.registerToggle(pos);
}
});
return view;
}
}
I had a layout called cell_content_layout for foldingcell and another layout called cell_title_layout and include these layouts in one layout called cell.
cell_title_content
<?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="horizontal"
android:baselineAligned="false"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:id="#+id/cell_title_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/cell_content_view"
android:layout_toRightOf="#+id/cell_content_view">
<FrameLayout
android:id="#+id/familyFrame"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#dd0f70">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp" />
<ImageView
android:id="#+id/familyImage1"
app:srcCompat="#drawable/family"
android:layout_width="100dp"
android:layout_height="109dp"
android:layout_alignParentBottom="true"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:visibility="visible" />
</FrameLayout>
<FrameLayout
android:id="#+id/textFrame1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d3d8dd">
<LinearLayout
android:id="#+id/textLinear1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/family1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="18dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="۱. خانواده x"
android:textSize="18dp" />
<TextView
android:id="#+id/cost1"
android:layout_width="match_parent"
android:layout_height="51dp"
android:layout_marginRight="18dp"
android:gravity="center_vertical"
android:text="۵۰۰,۰۰۰ تومان"
android:textSize="18dp" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp" />
</FrameLayout>
</LinearLayout>
</LinearLayout>
cell_content_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:id="#+id/cell_content_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#ffffff"
android:orientation="vertical"
>
<FrameLayout
android:id="#+id/textFrame2"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#d3d8dd">
<ImageView
android:id="#+id/pinkback"
android:layout_width="100dp"
android:layout_height="109dp"
android:layout_alignParentBottom="true"
android:layout_gravity="left"
android:layout_weight="1"
android:background="#dd0f70"
android:visibility="visible" />
<ImageView
android:id="#+id/familyImage2"
android:layout_width="100dp"
android:layout_height="109dp"
android:layout_alignParentBottom="true"
android:layout_gravity="left"
android:layout_weight="1"
android:visibility="visible"
app:srcCompat="#drawable/family" />
<LinearLayout
android:id="#+id/textLinear2"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical">
<TextView
android:id="#+id/family2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="18dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="۱. خانواده x"
android:textSize="18dp" />
<TextView
android:id="#+id/cost2"
android:layout_width="match_parent"
android:layout_height="51dp"
android:layout_marginRight="18dp"
android:gravity="center_vertical"
android:text="۵۰۰,۰۰۰ تومان"
android:textSize="18dp" />
</LinearLayout>
</FrameLayout>
<FrameLayout
android:id="#+id/containFrame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"></LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
</FrameLayout>
<FrameLayout
android:id="#+id/frameContent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="vertical">
<TextView
android:id="#+id/explain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginRight="18dp"
android:layout_marginTop="15dp"
android:layout_weight="1.00"
android:gravity="right"
android:text="توضیحات:" />
<TextView
android:id="#+id/code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="18dp"
android:layout_weight="0.99"
android:gravity="right"
android:text="کد: ۱۲۳۴۵۶۷۸۹" />
<Button
android:id="#+id/pay"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="#drawable/button_style2"
android:text="پرداخت"
android:textColor="#color/white"
android:textSize="16dp" />
</LinearLayout>
</FrameLayout>
<TextView
android:id="#+id/textView6"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>
cell
<?xml version="1.0" encoding="utf-8"?>
<com.ramotion.foldingcell.FoldingCell
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/folding_cell_main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:clipChildren="false"
android:clipToPadding="false">
<include
android:id="#+id/include"
layout="#layout/cell_content_layout"
android:visibility="gone" />
<include
layout="#layout/cell_title_layout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/include" />
</com.ramotion.foldingcell.FoldingCell>
It looks like your error is in the setAdapter not the onItemClickListener.
My guess is that ListView lvlist = (ListView) view.findViewById(R.id.lvlist); return null;
Check if "R.id.lvlist" is realy the id of your listView in the xml file.
Also, Check if "lvlist" is null after the findViewById line of code
I've been struggling to figure out this weird bug for a day and a half. Whenever I click on an EditText that is a child of an ExpandableListView, the screen gets obscured such that the user is unable to see what is being typed, as shown in the gif below: \n
https://giant.gfycat.com/GenuineUnluckyHyrax.mp4
I started digging into the Hierarchy View to see what could be doing this, here is a screenshot of it:
https://i.imgur.com/B4kkHv7.png
To me it looks like the Toolbar up top is extending downwards to cover the screen, which is odd because it doesnt do that on other activities that contain EditText widgets. I've checked that parent views are using "wrap_content" for height instead of "match_parent" as well, except for my DrawerLayout which must match_parent. Below is the layout code for the activity:
<!-- This DrawerLayout has two children at the root -->
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- This LinearLayout represents the contents of the screen -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- The ActionBar displayed at the top -->
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- The main content view where fragments are loaded -->
<FrameLayout
android:id="#+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/rlcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true">
<!--SideBar views -->
<RelativeLayout
android:layout_width="350dp"
android:layout_height="match_parent"
android:background="#color/sidebarColor">
<ExpandableListView
android:id="#+id/qwedit_sidebar_elv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:indicatorLeft="?
android:attr/expandableListPreferredItemIndicatorLeft"
android:divider="#color/colorPrimary"
android:dividerHeight="0.5dp">
</ExpandableListView>
</RelativeLayout>
<!--MainView views -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ExpandableListView
android:id="#+id/qwedit_main_elv"
android:layout_width="930dp"
android:layout_height="wrap_content"
android:indicatorLeft="?
android:attr/expandableListPreferredItemIndicatorLeft"
android:divider="#color/colorPrimary"
android:dividerHeight="0.5dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="25dp">
</ExpandableListView>
</RelativeLayout>
</RelativeLayout>
</FrameLayout>
</LinearLayout>
<!-- The navigation drawer that comes from the left -->
<!-- Note that `android:layout_gravity` needs to be set to 'start' -->
<android.support.design.widget.NavigationView
android:id="#+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"
android:layout_marginTop="25dp"
app:menu="#menu/drawer_view" />
</android.support.v4.widget.DrawerLayout>
Here is the EVL's group layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/qwedit_sidebar_elv_parent_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:padding="3dp"
android:layout_marginLeft="40dp"
android:gravity="center"
android:textSize="20sp"
android:text="Category"/>
<ImageView
android:id="#+id/qwedit_elv_parent_status"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="35dp"
android:src="#drawable/ic_warning"/>
<TextView
android:id="#+id/qwedit_elv_parent_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="75dp"
android:layout_gravity="left"
android:gravity="right"
android:textColor="#color/colorPrimary"
android:textSize="20sp"
android:text="Score"/>
</LinearLayout>
and the item layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/qwedit_elv_item_score_container"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/qwedit_main_badView"
android:layout_width="232.5dp"
android:layout_height="145.3dp"
android:background="#color/badScore">
<TextView
android:id="#+id/qwedit_main_badTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#color/white"
android:textSize="20sp"
android:text="Bad"/>
</LinearLayout>
<LinearLayout
android:id="#+id/qwedit_main_mediocreView"
android:layout_width="232.5dp"
android:layout_height="145.3dp"
android:background="#color/mediocreScore">
<TextView
android:id="#+id/qwedit_main_mediumTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#color/white"
android:textSize="20sp"
android:text="Mediocre"/>
</LinearLayout>
<LinearLayout
android:id="#+id/qwedit_main_goodView"
android:layout_width="232.5dp"
android:layout_height="145.3dp"
android:background="#color/goodScore">
<TextView
android:id="#+id/qwedit_main_goodTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#color/white"
android:textSize="20sp"
android:text="Good"/>
</LinearLayout>
<LinearLayout
android:id="#+id/qwedit_main_wcView"
android:layout_width="232.5dp"
android:layout_height="145.3dp"
android:background="#color/worldclassScore">
<TextView
android:id="#+id/qwedit_main_worldclassTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#color/white"
android:textSize="20sp"
android:text="World Class"/>
</LinearLayout>
</LinearLayout>
<EditText
android:layout_width="930dp"
android:layout_height="wrap_content"
android:layout_below="#+id/qwedit_elv_item_score_container"
android:focusable="true"
android:focusableInTouchMode="true"
android:hint="Comments"/>
</RelativeLayout>
I'm assuming that this is all due to the layouts, but here is the CustomExpandableListAdapter that I wrote for it just in case the answer lies in how the views are being inflated:
/**
* Created by Tadhg on 9/8/2017.
*/
public class QwEditMainCustomExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<String>> expandableListDetail;
//Values inits
private int categoryScore;
private int subCategoryScore;
private boolean completedCategory;
private boolean completedSubCategory;
// ChildView views
private View badScoreView;
private View mediocreScoreView;
private View goodScoreView;
private View worldclassScoreView;
TextView badScoreTextView;
TextView mediocreScoreTextView;
TextView goodScoreTextView;
TextView worldClassScoreTextView;
EditText questionCommentEditText;
public QwEditMainCustomExpandableListAdapter(Context context, List<String> expandableListTitle,
HashMap<String, List<String>> expandableListDetail) {
this.context = context;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
}
#Override
public Object getChild(int listPosition, int expandedListPosition) {
return this.expandableListDetail
.get(this.expandableListTitle.get(listPosition))
.get(expandedListPosition);
}
#Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}
#Override
public View getChildView(int listPosition, final int expandedListPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
final String expandedListText = (String) getChild(listPosition, expandedListPosition);
if(convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.qwedit_main_elv_item, null);
}
// ChildView views
badScoreView = convertView.findViewById(R.id.qwedit_main_badView);
mediocreScoreView = convertView.findViewById(R.id.qwedit_main_mediocreView);
goodScoreView = convertView.findViewById(R.id.qwedit_main_goodView);
worldclassScoreView = convertView.findViewById(R.id.qwedit_main_wcView);
badScoreTextView = (TextView) convertView.findViewById(R.id.qwedit_main_badTextView);
mediocreScoreTextView = (TextView) convertView.findViewById(R.id.qwedit_main_mediumTextView);
goodScoreTextView = (TextView) convertView.findViewById(R.id.qwedit_main_goodTextView);
worldClassScoreTextView = (TextView) convertView.findViewById(R.id.qwedit_main_worldclassTextView);
// TODO: TextView score subcategory setters
badScoreView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO: add logic to change score
subCategoryScore = 1;
badScoreView.setBackgroundColor(Color.parseColor("#eca9a7"));
mediocreScoreView.setBackgroundColor(Color.parseColor("#f0ad4e"));
goodScoreView.setBackgroundColor(Color.parseColor("#5cb85c"));
worldclassScoreView.setBackgroundColor(Color.parseColor("#0275d8"));
}
});
mediocreScoreView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO: add logic to change score
subCategoryScore = 2;
badScoreView.setBackgroundColor(Color.parseColor("#d9534f"));
mediocreScoreView.setBackgroundColor(Color.parseColor("#f7d6a6"));
goodScoreView.setBackgroundColor(Color.parseColor("#5cb85c"));
worldclassScoreView.setBackgroundColor(Color.parseColor("#0275d8"));
}
});
goodScoreView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO: add logic to change score
subCategoryScore = 3;
badScoreView.setBackgroundColor(Color.parseColor("#d9534f"));
mediocreScoreView.setBackgroundColor(Color.parseColor("#f0ad4e"));
goodScoreView.setBackgroundColor(Color.parseColor("#addbad"));
worldclassScoreView.setBackgroundColor(Color.parseColor("#0275d8"));
}
});
worldclassScoreView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO: add logic to change score
subCategoryScore = 4;
badScoreView.setBackgroundColor(Color.parseColor("#d9534f"));
mediocreScoreView.setBackgroundColor(Color.parseColor("#f0ad4e"));
goodScoreView.setBackgroundColor(Color.parseColor("#5cb85c"));
worldclassScoreView.setBackgroundColor(Color.parseColor("#80baeb"));
}
});
/**
* Use variables categoryScore and subCategoryScore to update scores
*/
return convertView;
}
#Override
public int getChildrenCount(int listPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition)).size();
}
#Override
public Object getGroup(int listPosition) {
return this.expandableListTitle.get(listPosition);
}
#Override
public int getGroupCount() {
return this.expandableListTitle.size();
}
#Override
public long getGroupId(int listPosition) {
return listPosition;
}
#Override
public View getGroupView(int listPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String listTitle = (String) getGroup(listPosition);
if(convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.qwedit_main_elv_group, null);
}
TextView listTitleTextView = (TextView) convertView
.findViewById(R.id.qwedit_main_elv_parent_title);
listTitleTextView.setText(listTitle);
return convertView;
}
#Override
public boolean hasStableIds() { return false; }
#Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) { return true; }
}
Any points or tips would be great. I think that this may be due to the number of views in my hierarchy (or at least a subissue of it), but I'm not sure and am relatively new to Android Development.
Thanks!
I solved the issue, simply adding android:windowSoftInputMode="adjustNothing|stateHidden" caused the ActionBar to not expand to 996px when the EditText gained focus
I've been spending a few hours on a problem and I still can't figure it out. The setOnClickListener in HotelOverviewFragment is not firing when I click an item in my ListView. However, the setOnClickListener does work from my custom adapter (NowArrayAdapter).
My question is: why the setOnClickListener not working in HotelOverviewFragment (Class where the ListView is shown)?
Here's a list of what I've tried:
Setting android:focusable="false", android:focusableInTouchMode="false", android:descendantFocusability="blocksDescendants" in the hotel_row_layout.xml and fragment_hotel_overview.xml.
Setting android:clickable on true and false. Both didn't work.
Changing from BaseAdapter implementation to arrayAdapter.
I tried different listeners in HotelOverviewFragment: setOnClickListener, setOnItemClickListener, setOnItemSelectedListener and setOnTouchListener. Unfortunately, none of those worked for me.
Here's my code:
Custom adapter
public class NowArrayAdapter extends ArrayAdapter<String> {
private Context context;
private ArrayList<String> values;
private Typeface typeface;
private static Hashtable fontCache = new Hashtable();
private LayoutInflater inflater;
private TextView item;
public NowArrayAdapter(Context context, ArrayList<String> commandsList) {
super(context, R.layout.hotel_row_layout, commandsList);
this.context = context;
values = new ArrayList<String>();
values.addAll(commandsList);
typeface = getTypeface(this.context, "fonts/Roboto-Light.ttf");
inflater = LayoutInflater.from(this.context);
}
static Typeface getTypeface(Context context, String font) {
Typeface typeface = (Typeface)fontCache.get(font);
if (typeface == null) {
typeface = Typeface.createFromAsset(context.getAssets(), font);
fontCache.put(font, typeface);
}
return typeface;
}
public View getView(int position, View convertView, ViewGroup parent) {
String myText = getItem(position);
if(convertView == null) {
convertView = inflater.inflate(R.layout.hotel_row_layout, parent, false);
item = (TextView) convertView.findViewById(R.id.maps_button);
item.setTypeface(typeface);
convertView.setTag(item);
} else {
item = (TextView) convertView.getTag();
}
item.setText(myText);
//myListItem.descText.setTextSize(14);
convertView.setOnClickListener(new View.OnClickListener() {
// WORKS!
#Override
public void onClick(View view) {
Log.d("click", "Don't look at me!");
}
});
return convertView;
}
}
Fragment
public class HotelOverviewFragment extends Fragment {
private static Hashtable fontCache = new Hashtable();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_hotel_overview, container, false);
ListView list = (ListView) v.findViewById(android.R.id.list);
// Set up listview and buttons
setUp(v, list);
// Inflate the layout for this fragment
return v;
}
static Typeface getTypeface(Context context, String font) {
Typeface typeface = (Typeface)fontCache.get(font);
if (typeface == null) {
typeface = Typeface.createFromAsset(context.getAssets(), font);
fontCache.put(font, typeface);
}
return typeface;
}
public void setUp(View v, ListView l){
TextView address = (TextView) v.findViewById(R.id.content);
TextView header = (TextView) v.findViewById(R.id.header);
TextView hotelName = (TextView) v.findViewById(R.id.hotelName);
Typeface typeface = getTypeface(getActivity(), "fonts/Roboto-Light.ttf");
address.setText("some street \nZipCode, City \nCountry \nEmail \nphoneNumber");
address.setTypeface(typeface);
header.setText("Hotel info");
header.setTypeface(typeface);
header.setTextSize(20);
hotelName.setText("Hotel name");
hotelName.setTypeface(typeface);
// Set up button
ArrayList<String> n = new ArrayList<String>();
n.add(0, "More info");
// Show button
NowArrayAdapter adapter = new NowArrayAdapter(getActivity(), n);
l.setAdapter(adapter);
// THIS IS THE STUFF I'VE BEEN TRYING
try {
l.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("click", "Success");
}
});
l.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("click", "Success");
}
});
l.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.d("click", "Success");
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
l.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("click", "Success");
return false;
}
});
}catch (Exception e){
Log.d("click", e + "");
}
}
}
Layout xml of HotelOverviewFragment
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.example"
android:background="#ffebebeb">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="fill_parent"
android:layout_height="200dp"
android:src="#drawable/banner"
android:id="#+id/hotelBanner"
android:layout_gravity="top"
android:adjustViewBounds="false"
android:scaleType="fitXY" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Hotelname"
android:id="#+id/hotelName"
android:gravity="center"
android:textSize="40dp"
android:layout_alignBottom="#+id/hotelBanner"
android:layout_alignParentRight="false"
android:layout_alignParentLeft="false"
android:textColor="#ffc4c4c4"
android:layout_marginBottom="5dp" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="0dp"
android:background="#drawable/header_card">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/header"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_weight="1"
android:textColor="#android:color/primary_text_light"
android:layout_height="wrap_content"
android:text="Header"
android:layout_toRightOf="#+id/headerImage"
android:layout_centerVertical="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/headerImage"
android:src="#drawable/ic_action_live_help"
android:layout_centerVertical="true" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:background="#drawable/content_card">
<TextView
android:id="#+id/content"
android:layout_gravity="left|center_vertical"
android:layout_width="fill_parent"
android:layout_weight="1"
android:textColor="#android:color/primary_text_light"
android:layout_height="wrap_content"
android:text="Content"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
The custom xml for a listview item
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:background="#drawable/content_card">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/maps_button"
android:layout_gravity="left|center_vertical"
android:layout_width="wrap_content"
android:layout_weight="1"
android:textColor="#android:color/primary_text_light"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true" />
<ImageView
android:src="#drawable/ic_action_arrow_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_alignParentTop="false"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="false"
android:layout_centerVertical="true"
/>
</RelativeLayout>
</LinearLayout>
Thanks in advance.
Thanks to Daniel Nugent's suggestion I got it working. Removing the convertView.setOnClickListener() is part of the answer. I think it was blocking the other listeners in the HotelOverviewFragment.
My next mistake was that I used setOnClickListener on a ListView for testing.
setOnClickListener should be used for buttons not ListViews.
So after removing setOnClickListener all the other listeners started working.
Thanks for your time.
I am new in android development and I need to modify my code to make it show the text with image function:
print_view xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#BFAF80">
<ListView
style="14dp"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:id="#+id/operations"
android:layout_below="#+id/search_view"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_below="#+id/operations"
android:layout_centerHorizontal="true"
android:src="#drawable/printer" />
list_row_item xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:padding="20dp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_weight="1"
android:src="#drawable/wifi"
android:layout_height="wrap_content"
android:id="#+id/imageForList" />
<TextView
android:layout_width="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_marginTop="35dp"
android:layout_height="wrap_content"
android:text="Nearby Wifi"
android:id="#+id/textForList" />
<ImageView
android:layout_width="match_parent"
android:layout_weight="1"
android:src="#drawable/bluetooth"
android:layout_height="wrap_content"
android:id="#+id/imageForList2" />
<TextView
android:layout_width="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_marginTop="35dp"
android:layout_height="wrap_content"
android:text="Nearby Blutooth"
android:id="#+id/textForList2" />
<ImageView
android:layout_width="match_parent"
android:layout_weight="1"
android:src="#drawable/usb"
android:layout_height="wrap_content"
android:id="#+id/imageForList3" />
<TextView
android:layout_width="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_marginTop="35dp"
android:layout_height="wrap_content"
android:text="Direct USB Connected"
android:id="#+id/textForList3" />
<ImageView
android:layout_width="match_parent"
android:layout_weight="1"
android:src="#drawable/cloud"
android:layout_height="wrap_content"
android:id="#+id/imageForList4" />
<TextView
android:layout_width="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_marginTop="35dp"
android:layout_height="wrap_content"
android:text="Google Cloud Print"
android:id="#+id/textForList4" />
</LinearLayout>
SettingsList java class:
public class SettingsList {
private int[] images;
private String[] items;
public SettingsList(int[] images, String[] items) {
this.images = images;
this.items = items;
}
public int[] getImages() {
return images;
}
public void setImages(int[] images) {
this.images = images;
}
public String[] getItems() {
return items;
}
public void setItems(String[] items) {
this.items = items;
}
}
SettingsAdapter java class :
public class SettingsAdapter extends ArrayAdapter<SettingsList> {
public SettingsAdapter(Context context, int resource, List<SettingsList> objects) {
super(context, resource, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderItems holder;
if(convertView == null){
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.list_row_item, parent, false);
holder = new ViewHolderItems();
holder.holderImage = (ImageView)convertView.findViewById(R.id.imageForList);
holder.holderImage = (ImageView)convertView.findViewById(R.id.imageForList2);
holder.holderImage = (ImageView)convertView.findViewById(R.id.imageForList3);
holder.holderImage = (ImageView)convertView.findViewById(R.id.imageForList4);
holder.holderText = (TextView)convertView.findViewById(R.id.textForList);
holder.holderText = (TextView)convertView.findViewById(R.id.textForList2);
holder.holderText = (TextView)convertView.findViewById(R.id.textForList3);
holder.holderText = (TextView)convertView.findViewById(R.id.textForList4);
convertView.setTag(holder);
}else{
holder = (ViewHolderItems)convertView.getTag();
}
SettingsList current = getItem(position);
holder.holderImage.setImageResource(current.getImages()[position]);
holder.holderText.setText(current.getItems()[position]);
return convertView;
}
static class ViewHolderItems{
ImageView holderImage;
TextView holderText;
}
}
PrintView mainactivity :
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class PrintView extends ActionBarActivity { private ListView lv;
private SettingsAdapter adapter;
private List<SettingsList> itemList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.print_view);
int[] imgs = new int[]
{
R.drawable.wifi,
R.drawable.bluetooth,
R.drawable.usb,
R.drawable.cloud,
};
String[] values = new String[]
{"Nearby Wifi",
"Nearby Blutooth",
"Direct USB Connected",
"Google Cloud Print"
};
lv = (ListView)findViewById(R.id.operations);
itemList = new ArrayList<>();
itemList.add(new SettingsList(imgs, values));
adapter = new SettingsAdapter(PrintView.this, R.layout.list_row_item, itemList);
lv.setAdapter(adapter);
}
}
Try this:
1) Create a res/layout/list_row_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:padding="20dp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_weight="1"
android:src="#mipmap/ic_launcher"
android:layout_height="wrap_content"
android:id="#+id/imageForList" />
<TextView
android:layout_width="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_marginTop="35dp"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textForList" />
</LinearLayout>
</LinearLayout>
2) Create your custom List item:
public class SettingsList {
private int image;
private String item;
public SettingsList(int image, String item) {
this.image = image;
this.item = item;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
}
}
3) Create your custom adapter
public class SettingsAdapter extends ArrayAdapter<SettingsList> {
public SettingsAdapter(Context context, int resource, List<SettingsList> objects) {
super(context, resource, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderItems holder;
if(convertView == null){
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.list_row_item, parent, false);
holder = new ViewHolderItems();
holder.holderImage = (ImageView)convertView.findViewById(R.id.imageForList);
holder.holderText = (TextView)convertView.findViewById(R.id.textForList);
convertView.setTag(holder);
}else{
holder = (ViewHolderItems)convertView.getTag();
}
SettingsList current = getItem(position);
holder.holderImage.setImageResource(current.getImage());
holder.holderText.setText(current.getItem());
return convertView;
}
static class ViewHolderItems{
ImageView holderImage;
TextView holderText;
}
}
4) In your Activity that you want the listview to be shown do this:
public class YourActivity extends ActionBarActivity {
private ListView lv;
private SettingsAdapter adapter;
private List<SettingsList> itemList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView)findViewById(R.id.operations);
itemList = new ArrayList<>();
itemList.add(new SettingsList(R.drawable.wifi, Nearby Wifi));
itemList.add(new SettingsList(R.drawable.bluetooth, Nearby Blutooth));
itemList.add(new SettingsList(R.drawable.usb, Direct USB Connected));
itemList.add(new SettingsList(R.drawable.cloud, Google Cloud Print));
adapter = new SettingsAdapter(YourActivity.this, R.layout.list_row_item, itemList);
lv.setAdapter(adapter);
}
}
5) The print_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#BFAF80">
<ListView
style="14dp"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:id="#+id/operations"
android:layout_below="#+id/search_view"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Hope it helps!!!