I am building an app where i have to use both gridview and listview in same activity but the problem is gridview items are clickable but not the listview this is my xml of activity...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
android:orientation="vertical" >
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tableRow2"
android:layout_alignRight="#+id/tableRow1"
android:layout_below="#+id/tableRow2"
android:layout_marginTop="39dp"
android:background="#drawable/border"
android:numColumns="3" >
</GridView>
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="23dp" >
<Button
android:id="#+id/button1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginRight="180dp"
android:text="Run Background" />
<Button
android:id="#+id/button2"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:text="Add To Cart" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tableRow1"
android:layout_below="#+id/tableRow1"
android:layout_marginTop="25dp" >
<Button
android:id="#+id/button3"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Edit Cart" />
<EditText
android:id="#+id/editText1"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/tableRow2"
android:layout_marginLeft="200dp"
android:layout_marginRight="20dp"
android:layout_toRightOf="#+id/tableRow2"
android:background="#layout/editext_background"
android:ems="10"
android:hint="Quantity" />
</TableRow>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tableRow1"
android:layout_marginLeft="22dp" >
<TextView
android:id="#+id/tv_slno"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Item Name"
android:textColor="#000" />
<TextView
android:id="#+id/tv_name"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Quantity"
android:textColor="#000" />
<TextView
android:id="#+id/tv_phone"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Price"
android:textColor="#000" />
</LinearLayout>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/linearLayout1"
android:layout_alignTop="#+id/tableRow1"
android:layout_marginRight="105dp"
android:text="Bill"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/tableRow2"
android:layout_toLeftOf="#+id/gridView1" >
</ListView>
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button4"
android:layout_alignBottom="#+id/button4"
android:layout_alignRight="#+id/linearLayout1"
android:text="Tender" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearLayout1"
android:layout_alignParentBottom="true"
android:layout_marginBottom="22dp"
android:text="Delete" />
please help me how to make listview item and row clickable ......
Have you tried android:clickable="true"?
like:
<TableRow
android:id="#+id/tableRow1"
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="23dp" >
You need to determine the height of the grid view first. Use this tutorial.
http://www.devexchanges.info/2015/03/combining-gridview-and-listview-in-one.html
Firstly, you only need to put 2 RecyclerViews object to your activity/fragment layout. For better scroll later, please wrap theme in a NestedScrollView like this:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#ffffe0"
tools:context="info.devexchanges.gridlistrecyclerview.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:text="#string/os"
android:textStyle="bold" />
<android.support.v7.widget.RecyclerView
android:id="#+id/grid"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="#string/corporation"
android:textStyle="bold" />
<android.support.v7.widget.RecyclerView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
Of course, you always need to creating layout for each RecyclerView item. In this every item is a CardView:
item_list.xml
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="5dp"
android:contentDescription="#string/app_name" />
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toRightOf="#+id/image"
android:gravity="center" />
</RelativeLayout>
</android.support.v7.widget.CardView>
item_grid.xml
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
app:cardCornerRadius="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<ImageView
android:id="#+id/image"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:contentDescription="#null" />
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/image"
android:layout_marginTop="5dp"
android:background="#color/colorPrimaryDark"
android:gravity="center"
android:padding="5dp"
android:textColor="#ffffff"
android:textStyle="bold" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Now, we must create 2 adapter classes for 2 RecyclerViews based on RecyclerView.Adapter:
ListViewAdapter.java
import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
import info.devexchanges.gridlistrecyclerview.R;
import info.devexchanges.gridlistrecyclerview.RecyclerViewItem;
public class ListViewAdapter extends RecyclerView.Adapter<ListViewAdapter.ViewHolder> {
private Activity activity;
private List<RecyclerViewItem> items;
public ListViewAdapter(Activity activity, List<RecyclerViewItem> items) {
this.activity = activity;
this.items = items;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = activity.getLayoutInflater();
View view = inflater.inflate(R.layout.item_list, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
viewHolder.imageView.setImageResource(items.get(position).getDrawableId());
viewHolder.textView.setText(items.get(position).getName());
}
#Override
public int getItemCount() {
return items.size();
}
/**
* View holder to display each RecylerView item
*/
protected class ViewHolder extends RecyclerView.ViewHolder {
private ImageView imageView;
private TextView textView;
public ViewHolder(View view) {
super(view);
imageView = (ImageView) view.findViewById(R.id.image);
textView = (TextView)view.findViewById(R.id.text);
}
}
}
GridViewAdapter.java
import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
import info.devexchanges.gridlistrecyclerview.R;
import info.devexchanges.gridlistrecyclerview.RecyclerViewItem;
public class GridViewAdapter extends RecyclerView.Adapter<GridViewAdapter.ViewHolder> {
private List<RecyclerViewItem> items;
private Activity activity;
public GridViewAdapter(Activity activity, List<RecyclerViewItem> items) {
this.activity = activity;
this.items = items;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
LayoutInflater inflater = activity.getLayoutInflater();
View view = inflater.inflate(R.layout.item_grid, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(GridViewAdapter.ViewHolder viewHolder, int position) {
viewHolder.imageView.setImageResource(items.get(position).getDrawableId());
viewHolder.textView.setText(items.get(position).getName());
}
#Override
public int getItemCount() {
return items.size();
}
/**
* View holder to display each RecylerView item
*/
protected class ViewHolder extends RecyclerView.ViewHolder {
private ImageView imageView;
private TextView textView;
public ViewHolder(View view) {
super(view);
textView = (TextView)view.findViewById(R.id.text);
imageView = (ImageView) view.findViewById(R.id.image);
}
}
}
There is no special point in your activity or fragment programmatically code, locating all xml elements from layout file, create LayoutManager for RecyclerViews, initializing adapters and attaching them,...This is full code for my main activity:
MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import info.devexchanges.gridlistrecyclerview.adapter.GridViewAdapter;
import info.devexchanges.gridlistrecyclerview.adapter.ListViewAdapter;
public class MainActivity extends AppCompatActivity {
private RecyclerView listView;
private RecyclerView gridView;
private ListViewAdapter listViewAdapter;
private GridViewAdapter gridViewAdapter;
private ArrayList<RecyclerViewItem> corporations;
private ArrayList<RecyclerViewItem> operatingSystems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (RecyclerView) findViewById(R.id.list);
gridView = (RecyclerView) findViewById(R.id.grid);
setDummyData();
listView.setHasFixedSize(true);
gridView.setHasFixedSize(true);
//set layout manager and adapter for "ListView"
LinearLayoutManager horizontalManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
listView.setLayoutManager(horizontalManager);
listViewAdapter = new ListViewAdapter(this, corporations);
listView.setAdapter(listViewAdapter);
//set layout manager and adapter for "GridView"
GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
gridView.setLayoutManager(layoutManager);
gridViewAdapter = new GridViewAdapter(this, operatingSystems);
gridView.setAdapter(gridViewAdapter);
}
private void setDummyData() {
corporations = new ArrayList<>();
corporations.add(new RecyclerViewItem(R.drawable.microsoft, "Microsoft"));
corporations.add(new RecyclerViewItem(R.drawable.apple, "Apple"));
corporations.add(new RecyclerViewItem(R.drawable.google, "Google"));
corporations.add(new RecyclerViewItem(R.drawable.oracle, "Oracle"));
corporations.add(new RecyclerViewItem(R.drawable.yahoo, "Yahoo"));
corporations.add(new RecyclerViewItem(R.drawable.mozilla, "Mozilla"));
operatingSystems = new ArrayList<>();
operatingSystems.add(new RecyclerViewItem(R.drawable.bbos, "BlackBerry OS"));
operatingSystems.add(new RecyclerViewItem(R.drawable.ios, "iOS"));
operatingSystems.add(new RecyclerViewItem(R.drawable.tizen, "Tizen"));
operatingSystems.add(new RecyclerViewItem(R.drawable.android, "Android"));
operatingSystems.add(new RecyclerViewItem(R.drawable.symbian, "Symbian"));
operatingSystems.add(new RecyclerViewItem(R.drawable.firefox_os, "Firefox OS"));
operatingSystems.add(new RecyclerViewItem(R.drawable.wp_os, "Windows Phone OS"));
}
}
And this is the POJO class of this project:
RecyclerViewItem.java
public class RecyclerViewItem {
private int drawableId;
private String name;
public RecyclerViewItem(int drawableId, String name) {
this.drawableId = drawableId;
this.name = name;
}
public int getDrawableId() {
return drawableId;
}
public String getName() {
return name;
}
}
NOTE: Never forget to put RecyclerView and CardView dependencies to your app-level build.gradle file:
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.android.support:cardview-v7:27.1.1'
Related
I have a layout with CoordinatorLayout, AppBarLayout and RecyclerView. I am facing scroll issue when add RecyclerView inside AppBarLayout, RecyclerView never scroll while AppBarLayout is expanded or collapsed
Here is my whole project, Where is the issue?
MainActivity
package com.example.coordinatorlayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView rvItems;
private RecyclerView.LayoutManager layoutManager;
private ItemAdapter itemAdapter;
private ItemModel itemModel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rvItems = findViewById(R.id.rv_products);
invalidateView( setListItems());
}
private List<ItemModel> setListItems() {
List<ItemModel> itemsList = new ArrayList<>();
itemModel = new ItemModel();
for(int i = 0; i <= 100; i++) {
itemModel = new ItemModel();
itemModel.setName("Name_" + i);
itemModel.setPrice("10");
itemModel.setStock(i + 2);
itemsList.add(i, itemModel);
}
return itemsList;
}
private void invalidateView(List<ItemModel> items) {
layoutManager = new GridLayoutManager(this, 2);
rvItems.setLayoutManager(layoutManager);
rvItems.setNestedScrollingEnabled(true);
itemAdapter = new ItemAdapter(items);
rvItems.setAdapter(itemAdapter);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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:background="#color/colorAccent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<ImageView
android:id="#+id/iv_logo"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:scaleType="fitCenter"
android:src="#mipmap/ic_launcher" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_products"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/round_top_corners_rectangle_gray"
tools:listitem="#layout/item_view" />
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
ItemModel
package com.example.coordinatorlayout;
import android.os.Parcel;
import android.os.Parcelable;
public class ItemModel implements Parcelable {
private String name;
private String price;
private int stock;
public String getName() {
return name;
}
public void setName(String iName) {
this.name = iName;
}
public String getPrice() {
return price;
}
public void setPrice(String iPrice) {
this.price = iPrice;
}
public int getStock() {
return stock;
}
public void setStock(int iStock) {
this.stock = iStock;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.name);
dest.writeString(this.price);
dest.writeInt(this.stock);
}
public ItemModel() {
}
protected ItemModel(Parcel in) {
this.name = in.readString();
this.price = in.readString();
this.stock = in.readInt();
}
public static final Creator<ItemModel> CREATOR = new Creator<ItemModel>() {
#Override
public ItemModel createFromParcel(Parcel source) {
return new ItemModel(source);
}
#Override
public ItemModel[] newArray(int size) {
return new ItemModel[size];
}
};
}
ItemAdapter
package com.example.coordinatorlayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.MyViewHolder> {
private List<ItemModel> itemsList;
public ItemAdapter(List<ItemModel> list) {
this.itemsList = list;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_view, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
ItemModel product = itemsList.get(position);
holder.bindData(product);
}
public void setData(List<ItemModel> list) {
this.itemsList = list;
notifyDataSetChanged();
}
#Override
public int getItemCount() {
return itemsList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView tvQuantity;
TextView tvName;
TextView tvPrice;
MyViewHolder(View view) {
super(view);
tvQuantity = view.findViewById(R.id.tv_quantity);
tvName = view.findViewById(R.id.tv_name);
tvPrice = view.findViewById(R.id.tv_price);
}
void bindData(ItemModel item) {
tvQuantity.setText(String.valueOf(item.getStock()));
tvName.setText(item.getName());
tvPrice.setText(item.getPrice());
}
}
}
item_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="#+id/layout_main_item_container_product"
android:layout_width="140dp"
android:layout_height="180dp"
android:layout_margin="5dp"
android:background="#android:color/white">
<TextView
android:id="#+id/tv_quantity_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:paddingStart="15dp"
android:paddingLeft="15dp"
android:text="Quantity"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_toEndOf="#+id/tv_quantity_title"
android:layout_toRightOf="#+id/tv_quantity_title"
tools:text="5" />
<TextView
android:id="#+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_quantity_title"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:paddingStart="15dp"
android:paddingLeft="15dp"
android:visibility="visible"
tools:text="Product" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/tv_price_currency"
android:layout_below="#+id/tv_name"
android:layout_centerHorizontal="true"
android:layout_marginStart="8dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="8dp"
app:srcCompat="#mipmap/ic_launcher" />
<TextView
android:id="#+id/tv_price_currency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="5dp"
android:paddingStart="15dp"
android:paddingLeft="15dp"
android:paddingEnd="20dp"
android:paddingRight="20dp"
tools:text="$" />
<TextView
android:id="#+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="5dp"
android:layout_toEndOf="#+id/tv_price_currency"
android:layout_toRightOf="#+id/tv_price_currency"
android:paddingStart="30dp"
android:paddingLeft="30dp"
tools:text="5" />
round_top_corners_rectangle_gray.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:padding="10dp">
<solid android:color="#F9F9F9" />
<corners
android:bottomRightRadius="0dp"
android:bottomLeftRadius="0dp"
android:topLeftRadius="33dp"
android:topRightRadius="33dp" />
Put RecyclerView outside of AppBarLayout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorAccent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/layout_sticky_header_store_details"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:text="Name" />
</RelativeLayout>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="#+id/layout_header"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:fitsSystemWindows="true">
<RelativeLayout
android:id="#+id/layout_scrollable_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#201A98E5"
android:paddingStart="10dp"
android:paddingLeft="10dp"
android:paddingEnd="20dp"
android:paddingRight="20dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="#+id/iv_logo"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:scaleType="fitCenter"
android:src="#mipmap/ic_launcher" />
<TextView
android:id="#+id/tv_name_label_scrollable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="#+id/iv_logo"
android:layout_toLeftOf="#+id/iv_logo"
android:text="Name: "
android:textStyle="bold" />
<TextView
android:id="#+id/tv_name_scrollable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_toStartOf="#+id/tv_name_label_scrollable"
android:layout_toLeftOf="#+id/tv_name_label_scrollable"
android:paddingBottom="5dp"
android:text="Name" />
<TextView
android:id="#+id/tv_quantity_label_scrollable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_name_label_scrollable"
android:layout_toStartOf="#+id/iv_logo"
android:layout_toLeftOf="#+id/iv_logo"
android:text="Quantity: "
android:textStyle="bold" />
<TextView
android:id="#+id/tv_quantity_scrollable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_name_label_scrollable"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_toStartOf="#+id/tv_quantity_label_scrollable"
android:layout_toLeftOf="#+id/tv_quantity_label_scrollable"
android:text="22" />
<TextView
android:id="#+id/tv_call_label_scrollable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_quantity_label_scrollable"
android:layout_marginBottom="10dp"
android:layout_toStartOf="#+id/iv_logo"
android:layout_toLeftOf="#+id/iv_logo"
android:text="Call us"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_call_scrollable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_quantity_label_scrollable"
android:layout_centerHorizontal="true"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_toStartOf="#+id/tv_call_label_scrollable"
android:layout_toLeftOf="#+id/tv_call_label_scrollable"
android:text="0123456789" />
<TextView
android:id="#+id/tv_description_scrollable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_call_label_scrollable"
android:layout_toStartOf="#+id/iv_logo"
android:layout_toLeftOf="#+id/iv_logo"
android:maxLines="2"
android:paddingBottom="16dp"
android:scrollbars="vertical"
android:text="Description description description description description description description description description description description" />
</RelativeLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_products"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/round_top_corners_rectangle_gray"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:listitem="#layout/item_view" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</LinearLayout>
UPDATE: You need to use the RecyclerView inside the Coordinatorlayout and outside of the AppBarLayout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="250dp"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<ImageView
android:id="#+id/iv_logo"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:src="#mipmap/ic_launcher" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_products"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:listitem="#layout/prof_item" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
The first layout you've added to the question has many issues. First, CoordintorLayout should be the root of the layout.
Second, if you're trying to add some content at the top of the layout, you'll need to use the RelativeLayout inside the AppBarLayout or using CollapsingToolbarLayout inside the AppBarLayout.
Another problem is that you've added:
app:layout_behavior="#string/appbar_scrolling_view_behavior"
To the RecyclerView which is only used outside of AppBarLayout to notify the AppBarLayout when scroll events occur on this particular view. So remove this attribute.
I want to know how to develop a custom dropdown icons Menu with GridView options from Menu.
This is my code for custom down icons
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#c2ec97"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="viralandroid.com.androidxmluserinterfacetutorial.MainActivity">
<Button
android:id="#+id/dropdown_custom_icon_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#5bace6"
android:drawableRight="#android:drawable/arrow_down_float"
android:onClick="verticalDropDownIconMenu"
android:padding="16dp"
android:text="DropDown\t"
android:textColor="#eee" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/dropdown_custom_icon_menu"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android Custom Vertical Dropdown Menu IconsAndroid Custom Vertical. Break line after Icon in Menu Item
Android.
Dropdown Menu IconsAndroid Custom Vertical Dropdown. popup menu with icon in android example.
Android Custom Vertical Dropdown Menu Icons. Android Custom Vertical Dropdown Menu Icons" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Android Custom Vertical Dropdown Menu Icons" />
</LinearLayout>
<LinearLayout
android:id="#+id/vertical_dropdown_icon_menu_items"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/dropdown_custom_icon_menu"
android:background="#333"
android:orientation="vertical"
android:padding="3dp"
android:visibility="invisible">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="26dp"
android:paddingRight="26dp"
android:paddingTop="5dp"
android:src="#drawable/ic_action_send"
android:text="Android Vertical Custom DropDown Menu" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:background="#drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="26dp"
android:paddingRight="26dp"
android:paddingTop="5dp"
android:src="#drawable/ic_action_attach"
android:text="Android Vertical Custom DropDown Menu" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:background="#drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="26dp"
android:paddingRight="26dp"
android:paddingTop="5dp"
android:src="#drawable/ic_action_mail"
android:text="Android Vertical Custom DropDown Menu" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:background="#drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="26dp"
android:paddingRight="26dp"
android:paddingTop="5dp"
android:src="#drawable/ic_action_refresh"
android:text="Android Vertical Custom DropDown Menu" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:background="#drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="26dp"
android:paddingRight="26dp"
android:paddingTop="5dp"
android:src="#drawable/ic_action_attach"
android:text="Android Vertical Custom DropDown Menu" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:background="#drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="26dp"
android:paddingRight="26dp"
android:paddingTop="5dp"
android:src="#drawable/ic_action_mail"
android:text="Android Vertical Custom DropDown Menu" />
</LinearLayout>
</RelativeLayout>
This is what i have :
This is what i want :
You need to use the spinner and that will lead to use an adapter and a couple of layouts.
First in your activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Spinner
android:id="#+id/sSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" />
</RelativeLayout>
then create a new layout under /res/layout folder and just name it anything like custom_spinner_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/myImageView"
android:layout_width="70dp"
android:layout_height="70dp"
android:padding="10dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:text="test"
android:textColor="#000" />
</LinearLayout>
then in you MainActivity.java you need to do your code:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
String[] carNames={"Chevy","Dodge","Mazda","Honda","BMW","Toyota"};
int icons[] = {R.drawable.Chevy, R.drawable.Dodge, R.drawable.Mazda, R.drawable.Honda, R.drawable.BMW, R.drawable.Toyota};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spin = (Spinner) findViewById(R.id.sSpinner);
spin.setOnItemSelectedListener(this);
CustomAdapter customAdapter=new CustomAdapter(getApplicationContext(),icons,carNames);
spin.setAdapter(customAdapter);
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {
//your code goes here///
Toast.makeText(getApplicationContext(), carNames[position], Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// something happens here
}
}
You will need to create a new java class file and name it CustomAdapter.java and thats gonna be extended fro BaseAdapter:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomAdapter extends BaseAdapter {
Context context;
int icons[];
String[] carNames;
LayoutInflater inflter;
public CustomAdapter(Context applicationContext, int[] icons, String[] carNames) {
this.context = applicationContext;
this.icons = icons;
this.carNames = carNames;
inflter = (LayoutInflater.from(applicationContext));
}
#Override
public int getCount() {
return icons.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.custom_spinner_layout, null);
ImageView icon = (ImageView) view.findViewById(R.id.imageView);
TextView names = (TextView) view.findViewById(R.id.textView);
icon.setImageResource(icons[i]);
names.setText(carNames[i]);
return view;
}
}
you can get the working code in :
https://github.com/edgebasis/spinnerExample
I have a spinner in my app where you can select categories and sub categories.
It was working perfect in the past but since i did alot of modifications in the app, the spinner does not scroll anymore, not just one spinner, but all spinners in this layout (Drawer menu).
Here's the XML for the right menu drawer that contains the spinner:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
xmlns:rsb="http://schemas.android.com/apk/res-auto"
android:id="#+id/rightadvancedsearch"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/Grey"
android:gravity="center"
tools:context="com.chno.v1.Home">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="50dp"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="16dp">
<RelativeLayout
android:id="#+id/AllCat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="false"
android:paddingBottom="16dp"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/query"
android:layout_width="match_parent"
android:layout_height="52dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="20dp"
android:layout_weight="1"
android:backgroundTint="#color/Blue"
android:drawableLeft="#android:drawable/ic_menu_search"
android:ems="10"
android:hint="Search"
android:inputType="textPersonName"
android:paddingLeft="5dp"
android:textColor="#color/Black"
android:textColorHint="#color/GreyDark" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="6dp"
android:text="#string/Category"
android:textColor="#color/Blue"/>
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="40dp"
android:textColor="#color/Blue" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/city"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/AllCat"
android:paddingBottom="16dp"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="6dp"
android:text="#string/Location"
android:textColor="#color/Blue" />
<TextView
android:id="#+id/textView28"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/SelectCity" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<!--
<CheckBox
android:id="#+id/nearby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/Nearby" />-->
<CheckBox
android:id="#+id/checkNearby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/Nearby" />
<TextView
android:id="#+id/currentray"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="#string/defaultRay"
android:textAlignment="textEnd"
android:visibility="gone" />
</LinearLayout>
<com.yahoo.mobile.client.android.util.rangeseekbar.RangeSeekBar
android:id="#+id/rangeSeekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:visibility="gone"
rsb:absoluteMaxValue="100"
rsb:absoluteMinValue="1"
rsb:singleThumb="true" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/includeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/city"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp">
</LinearLayout>
</RelativeLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:background="#color/BgLight">
<RelativeLayout
android:id="#+id/reset"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#color/GreyLight"
android:padding="10dp"
android:gravity="center">
<TextView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/Black"
android:layout_marginRight="12dp"
android:text="Ré-initialiser"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/search"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#color/Orange"
android:padding="10dp"
android:gravity="center">
<TextView
android:id="#+id/imageView3"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#color/White"
android:textStyle="bold"
android:text="Recherche"/>
</RelativeLayout>
</LinearLayout>
This is the spinner adapter:
package com.chno.v1;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* List view
*/
public class SpinnerAdapter extends ArrayAdapter<ItemDate> {
private int groupid;
private Activity context;
private ArrayList<ItemDate> list;
private LayoutInflater inflater;
public SpinnerAdapter(Activity context, int groupid, int id, ArrayList<ItemDate> list) {
super(context,id, list);
this.list = list;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.groupid = groupid;
}
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = inflater.inflate(groupid,parent,false);
ImageView imageView = (ImageView)itemView.findViewById(R.id.img);
ItemDate item = list.get(position);
if(item != null) {
if(item.getType() != null) {
if (item.getType().equals("sub_category")) {
imageView.setVisibility(View.GONE);
} else {
imageView.setImageResource(item.getImageId());
}
} else {
imageView.setImageResource(item.getImageId());
}
} else {
imageView.setImageResource(item.getImageId());
}
imageView.setImageResource(item.getImageId());
TextView textView = (TextView)itemView.findViewById(R.id.txt);
textView.setText(item.getText());
return itemView;
}
public View getDropDownView(int position, View convertView, ViewGroup
parent) {
return getView(position,convertView,parent);
}
}
The thing is i can select items from the spinner but i cannot scroll it down for more items.
Here's the code that populates the spinner:
JSONObject result = config.getCategories();
Iterator<String> iter = result.keys();
while (iter.hasNext()) {
String key = iter.next();
try {
JSONObject cat = result.getJSONObject(key);
String category = cat.getString("n");
int drawable = helper.getCategoryDrawable(Integer.parseInt(key));
Log.e("" + category, "" + key);
list.add(new ItemDate("category", category, Integer.parseInt(key), drawable));
JSONObject subs = cat.getJSONObject("l");
Iterator<String> it2 = subs.keys();
while (it2.hasNext()) {
String sub_key = it2.next();
JSONObject sub_category = subs.getJSONObject(sub_key);
String name = sub_category.getString("n");
Log.e("FFROM LIST", "" + name);
list.add(new ItemDate("sub_category", name, Integer.parseInt(sub_key), Integer.parseInt(key), R.drawable.tool_icon_filter));
}
} catch (JSONException e) {
Log.e("JSONException", e.getMessage());
e.printStackTrace();
}
}
Spinner sp=(Spinner)findViewById(R.id.spinner1);
SpinnerAdapter adapter=new SpinnerAdapter((Activity) context, R.layout.sub_select_item, R.id.txt, list);
sp.setAdapter(adapter);
sp.setFocusable(true);
sp.setFocusableInTouchMode(true);
How can i solve this? Thanks.
I want to take the reference when i click on an item in this recyclerview then go to another activity with this reference there is override functions in listview but recylclerview didn't have the same function
card.xml :
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="#+id/card_view"
android:layout_width="wrap_content"
android:layout_height="119dp"
android:layout_gravity="center"
android:elevation="3dp"
card_view:cardUseCompatPadding="true"
card_view:cardElevation="4dp"
card_view:cardCornerRadius="1dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="112dp">
<TextView
android:text="ID Article : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txt_idArticle1"
android:textStyle="normal|bold"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#android:color/black" />
<TextView
android:text="Reference : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txt_reference1"
android:textStyle="normal|bold"
android:layout_above="#+id/txt_des"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="5dp"
android:textColor="#android:color/background_dark" />
<TextView
android:text="Prix : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txt_des1"
android:textStyle="normal|bold"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="-51dp"
android:layout_below="#+id/txt_des"
android:textColor="#android:color/background_dark" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txt_des"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/txt_prix1"
android:layout_toEndOf="#+id/txt_prix1" />
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txt_prix"
android:layout_alignBaseline="#+id/txt_des1"
android:layout_alignBottom="#+id/txt_des1"
android:layout_toRightOf="#+id/txt_des1"
android:layout_toEndOf="#+id/txt_des1" />
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txt_idArticle"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/txt_idArticle1"
android:layout_toEndOf="#+id/txt_idArticle1" />
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txt_reference"
android:layout_alignBaseline="#+id/txt_reference1"
android:layout_alignBottom="#+id/txt_reference1"
android:layout_toRightOf="#+id/txt_reference1"
android:layout_toEndOf="#+id/txt_reference1" />
<TextView
android:text="Designation : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txt_prix1"
android:textStyle="normal|bold"
android:textColor="#android:color/black"
android:layout_below="#+id/txt_reference1"
android:layout_alignParentLeft="true"
android:layout_marginBottom="5dp"
android:layout_alignParentStart="true" />
</RelativeLayout>
</android.support.v7.widget.CardView> </LinearLayout>
avticity_recherche_art.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:article_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_recherche_art"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.bacha.pfe.activity.recherche_art">
<TextView
android:text="Recherche Article :"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:textSize="24sp"
android:textStyle="normal|bold"
android:textAlignment="center"
android:textColor="?attr/colorPrimary"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:text="rechercher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnrechArt"
android:layout_marginRight="20dp"
android:layout_marginEnd="20dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignTop="#+id/eTextRechArt" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/eTextRechArt"
android:layout_below="#+id/textView"
android:layout_toLeftOf="#+id/btnrechArt"
android:layout_toStartOf="#+id/btnrechArt" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
article_view:cardUseCompatPadding="true"
article_view:cardElevation="5dp"
article_view:cardCornerRadius="5dp"
android:scrollbars="vertical"
android:layout_marginTop="18dp"
android:layout_below="#+id/btnrechArt"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
recherche_art.java:
package com.example.bacha.pfe.activity;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.example.bacha.pfe.R;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class recherche_art extends AppCompatActivity {
private RecyclerView recyclerView ;
private GridLayoutManager gridLayoutManager;
private ArticleAdapter adapter ;
private List<Article> data_list ;
private String recherche_article;
private Button btnrechArt ;
private EditText arech;
int d=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recherche_art);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
btnrechArt = (Button) findViewById(R.id.btnrechArt);
btnrechArt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
arech= (EditText) findViewById(R.id.eTextRechArt);
data_list = new ArrayList<>();
recherche_article=arech.getText().toString();
load_article_from_server(0);
gridLayoutManager = new GridLayoutManager(recherche_art.this,1);
recyclerView.setLayoutManager(gridLayoutManager);
adapter = new ArticleAdapter(recherche_art.this,data_list);
recyclerView.setAdapter(adapter);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if(gridLayoutManager.findLastCompletelyVisibleItemPosition() == data_list.size()-1){
d=data_list.size();
Log.d("PFE", String.valueOf(d));
load_article_from_server(d);
}
}
});
}
});
}
private void load_article_from_server(final int id) {
AsyncTask<Integer, Void, Void> task = new AsyncTask<Integer,Void, Void>() {
#Override
protected Void doInBackground(Integer... Params) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("http://10.0.2.2/slim/article/"+recherche_article+"/"+id).build();
try {
Response response = client.newCall(request).execute();
JSONArray array = new JSONArray(response.body().string());
for(int i=0;i<array.length();i++){
JSONObject object = array.getJSONObject(i);
Article data = new Article(/*dd,*/object.getString("id_Article"),object.getString("Reference"),object.getString("Designation"),object.getString("PVTTC"));
data_list.add(data);
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
System.out.print("End of content");
}
return null ;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
adapter.notifyDataSetChanged();
}
};
task.execute(id);
}
}
You have to implement a listener in the holder that you are using in the ArticleAdapter.
Look this: RecyclerView onClick
I'm trying to make a Toast message whenever I click an item from the list, For some reason when I click the item nothing happens,
I hope you can help me, thanks.
ProductList.java
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.util.SparseBooleanArray;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.support.v4.app.FragmentManager;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ProductList extends AppCompatActivity {
private List<myProductsView> myProducts_types = new ArrayList<myProductsView>();
ArrayAdapter<myProductsView> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pop_productlist);
//Button btnDel = (Button) findViewById(R.id.btnDel);
//adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice,list);
populateProductsList();
populateListView();
}
private void populateProductsList() {
myProducts_types.add(new myProductsView("aaa", "aaa", 1111, 12.90, R.drawable.cereal, 1));
myProducts_types.add(new myProductsView("aaa", "aaaaaa ", 1112, 10.90, R.drawable.cereal, 2));
myProducts_types.add(new myProductsView("aaa", "aaa", 1112, 30.00, R.drawable.cereal, 1));
myProducts_types.add(new myProductsView("aaa", "aaa", 1112, 20.00, R.drawable.cereal, 3));
}
private void populateListView() {
adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.product_list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
System.out.println("BLALBALBLALBLABLAL");
Toast.makeText(ProductList.this, "BLA", Toast.LENGTH_SHORT);
}
});
}
public void StartCalck(View view){
Intent intent = new Intent(ProductList.this, SplitBuying.class);
startActivity(intent);
}
public class MyListAdapter extends ArrayAdapter<myProductsView>{
public MyListAdapter(){
super(ProductList.this, R.layout.pop_productlist, myProducts_types);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//make sure we have a view to work with(may have been given null
View itemView = convertView;
if(itemView == null){
itemView = getLayoutInflater().inflate(R.layout.product_item_view, parent, false);
}
//we need to populate the list
//find the product to work with
myProductsView currentProduct = myProducts_types.get(position);
//fill the view
CheckBox checkBox = (CheckBox)itemView.findViewById(R.id.product_checkBox);
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(ProductList.this,"BLA",Toast.LENGTH_SHORT);
}
});
TextView productname = (TextView) itemView.findViewById(R.id.product_name);
productname.setText(currentProduct.getProductName());
EditText quantity = (EditText)itemView.findViewById(R.id.edit_text);
quantity.setText(String.valueOf(currentProduct.getQuantity()));
return itemView;
}
}
}
product_item_view.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:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="7dp"
android:layout_weight="1">
<Button
android:id="#+id/btn_minus"
android:layout_width="35dp"
android:layout_height="40dp"
android:text="-" />
<EditText
android:id="#+id/edit_text"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:inputType="number"
android:gravity="center"
android:focusable="false"
android:text="0" />
<Button
android:id="#+id/btn_plus"
android:layout_width="35dp"
android:layout_height="40dp"
android:text="+" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Text"
android:textSize="15dp"
android:textIsSelectable="true"
android:gravity="center"
android:id="#+id/product_name"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/product_checkBox"
android:layout_toStartOf="#+id/product_checkBox" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="5dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/product_checkBox" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
pop_productlist.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"
android:background="#drawable/ightwall"
android:id="#+id/drawerlayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="הרשימה שלי"
android:textAlignment="center"
android:textSize="30dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="#+id/product_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:choiceMode="multipleChoice"
android:layout_below="#+id/ChooseStore"
>
</ListView>
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/CalckButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/calckButton"
android:onClick="StartCalck"
android:layout_gravity="center" />
<Button
android:id="#+id/btnDel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="#string/lblBtnDel"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#D5D4D4"
android:layout_gravity="bottom">
<ImageView
android:id="#+id/mylist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/createlist_mylist"
android:adjustViewBounds="true"
android:maxHeight="90dp"
android:maxWidth="90dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
<ImageView
android:id="#+id/freeadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/createlist_freetext"
android:adjustViewBounds="true"
android:maxHeight="90dp"
android:maxWidth="90dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
<ImageView
android:id="#+id/favproductadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/createlist_favproduct"
android:adjustViewBounds="true"
android:maxHeight="90dp"
android:maxWidth="90dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
</LinearLayout>
</LinearLayout>
You have to call .show() on your toast.
change
Toast.makeText(ProductList.this, "BLA", Toast.LENGTH_SHORT);
to
Toast.makeText(ProductList.this, "BLA", Toast.LENGTH_SHORT).show();