RecyclerView click and replace image from another activity - java

I have some doubts about how to solve the doubt about how to click on a RecyclerView and change image from another activity, such as MainActivity.
In the image you can see what I'm trying to do. I have a total of 6 images, 3 Small images and another 3 Large images.
In the footer of MainActivity I have the ReciclerView that loads the 3 small images, I want that when they click for example to the image_Small_2/item_Small_2, that replaces the image that is in the center by the image_Large_2/item_Large_2.
I don't know if I'm explaining myself well, I leave you a screenshot and the code to see if you can give me a hand. Thanks in advance. P.S. I use the glide library to load the images.
My Adapter RecyclerView
public class AdaptadorX extends RecyclerView.Adapter<AdaptadorX.ViewHolder> {
private ArrayList<Items> itemsLi;
private Context context;
public static class ViewHolder extends RecyclerView.ViewHolder{
public ImageView idSrcImagen;
public ViewHolder(#NonNull View itemView) {
super(itemView);
idSrcImagen = itemView.findViewById(R.id.idImagen);
}
}
public AdaptadorX(ArrayList<Items> itemsListado, Context context_L){
itemsLi = itemsListado;
context = context_L;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_item, parent, false);
ViewHolder content = new ViewHolder(view);
return content;
}
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, final int position) {
final Items contarItems = itemsLi.get(position);
Glide.with(context).load(contarItems.getxNombre_imagen()).into(holder.idSrcImagen);
holder.idSrcImagen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("Mensaje_AdaptadorX.java", "Mi Posicion FOTO es: "+String.valueOf(position));
}
});
}
#Override
public int getItemCount() {
Log.d("Mensaje_Size_Tamaño", String.valueOf(itemsLi.size()));
return itemsLi.size();
}
}
Class Items
public class Items {
private String xNombre_imagen;
public Items (String nombre_imagen_M){
xNombre_imagen = nombre_imagen_M;
}
public String getxNombre_imagen() {
return xNombre_imagen;
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
public ImageView idImgHead;
private ArrayList<Items> items;
private RecyclerView idRecyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
idRecyclerView = findViewById(R.id.idRecyclerView);
idImgHead = findViewById(R.id.idImgHead);
Glide.with(this).load("https://www.midominio.com/Imagen_GRANDE_head_01.jpg").into(idImgHead); //IMAGE BIG
listadoXhead();
}
private void listadoXhead() {
ArrayList<Items> items = new ArrayList<>();
items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_01.jpg")); //IMAGE Small
items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_02.jpg")); //IMAGE Small
items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_03.jpg")); //IMAGE Small
idRecyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(MainActivity.this);
((LinearLayoutManager) layoutManager).setOrientation(RecyclerView.HORIZONTAL);
adapter = new AdaptadorX(items, MainActivity.this);
idRecyclerView.setLayoutManager(layoutManager);
idRecyclerView.setAdapter(adapter);
}
}
activity_main
<androidx.constraintlayout.widget.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"
tools:context=".MainActivity">
<ImageView
android:id="#+id/idImgHead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:scaleType="fitCenter"
app:layout_constraintBottom_toTopOf="#+id/linearLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#mipmap/ic_launcher" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/idRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
activity_item
<?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">
<ImageView
android:id="#+id/idImagen"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/ic_launcher_foreground"
android:onClick="accionBoton"/>
</LinearLayout>
Example Image Here

You need to implement an interface listener in your activity and pass it to your adapter. Add this to your adapter and pass it through the constructor
interface OnImageClickListener{
void onSelected(String url);
}
Your whole adapter should look like this.
public class AdaptadorX extends RecyclerView.Adapter<AdaptadorX.ViewHolder> {
private ArrayList<Items> itemsLi;
private Context context;
pruvate OnImageClickListener listener;
interface OnImageClickListener{
void onSelected(String url);
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public ImageView idSrcImagen;
public ViewHolder(#NonNull View itemView) {
super(itemView);
idSrcImagen = itemView.findViewById(R.id.idImagen);
}
}
public AdaptadorX(ArrayList<Items> itemsListado, Context context_L, OnImageClickListener listener){
itemsLi = itemsListado;
context = context_L;
this.listener = listener;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_item, parent, false);
ViewHolder content = new ViewHolder(view);
return content;
}
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, final int position) {
final Items contarItems = itemsLi.get(position);
Glide.with(context).load(contarItems.getxNombre_imagen()).into(holder.idSrcImagen);
holder.idSrcImagen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
listener.onSelected(contarItems.getxNombre_imagen())
}
});
}
#Override
public int getItemCount() {
Log.d("Mensaje_Size_Tamaño", String.valueOf(itemsLi.size()));
return itemsLi.size();
}
}
And in your Activity you implement this interface and pass it to the adapter.
public class MainActivity extends AppCompatActivity implements OnImageClickListener {
public ImageView idImgHead;
private ArrayList<Items> items;
private RecyclerView idRecyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
idRecyclerView = findViewById(R.id.idRecyclerView);
idImgHead = findViewById(R.id.idImgHead);
Glide.with(this).load("https://www.midominio.com/Imagen_GRANDE_head_01.jpg").into(idImgHead); //IMAGE BIG
listadoXhead();
}
#Override
public onSelected(String url) {
Glide.with(this).load(url).into(idImgHead);
}
private void listadoXhead() {
ArrayList<Items> items = new ArrayList<>();
items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_01.jpg")); //IMAGE Small
items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_02.jpg")); //IMAGE Small
items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_03.jpg")); //IMAGE Small
idRecyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(MainActivity.this);
((LinearLayoutManager) layoutManager).setOrientation(RecyclerView.HORIZONTAL);
adapter = new AdaptadorX(items, MainActivity.this, this);
idRecyclerView.setLayoutManager(layoutManager);
idRecyclerView.setAdapter(adapter);
}
}

Related

How to add a spinner to each item inside a recyclerview?

I am trying to add a spinner object inside each item of a recyclerview.
I think the problem is that in OnCreate the setContentView(R.layout.activity_modify_list2) is referring to the xml file that contains the recyclerview:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_modify_list2);
initView();
}
Because of this, the createSpinner() method is looking for the spinner in that file rather than the xml file for the individual recyclerview item, giving a null object reference error:
private Spinner CreateSpinner() {
Spinner staticSpinner = findViewById(R.id.spinner);
... }
Full java file for the page:
public class ModifyListActivity extends Activity {
private RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_modify_list2);
initView();
}
private void initView() {
recyclerView = (RecyclerView) findViewById(R.id.recyclerViewModifyList);
createList();
}
private void createList() {
ArrayList<ModifyListPageItem> items = new ArrayList<>();
ModifyListPageItem item;
item = new ModifyListPageItem();
item.setTitle("Resident Evil Games");
item.setDescription("Ranking all Resident Evil games");
items.add(item);
item = new ModifyListPageItem();
item.setRank("1.");
item.setSpinner(CreateSpinner());
items.add(item);
item = new ModifyListPageItem();
item.setRank("2.");
item.setSpinner(CreateSpinner());
items.add(item);
item = new ModifyListPageItem();
item.setRank("3.");
item.setSpinner(CreateSpinner());
items.add(item);
// set adapter
ModifyListPageAdapter adapter = new ModifyListPageAdapter(this, items);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
private Spinner CreateSpinner() {
Spinner staticSpinner = findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner
ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter
.createFromResource(this, R.array.resident_evil_games,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
staticAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner.setAdapter(staticAdapter);
return staticSpinner;
}
}
Full Java file for adapter
public class ModifyListPageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static int TYPE_HEADER = 1;
private static int TYPE_ITEM = 2;
private Context context;
private ArrayList<ModifyListPageItem> items;
public ModifyListPageAdapter(Context context, ArrayList<ModifyListPageItem> items) {
this.context = context;
this.items = items;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view;
if (viewType == TYPE_HEADER) {
view = LayoutInflater.from(context).inflate(R.layout.item_modlist_header, parent, false);
return new HeaderViewHolder(view);
}
else {
view = LayoutInflater.from(context).inflate(R.layout.item_modlist_object, parent, false);
return new ItemViewHolder(view);
}
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
if (getItemViewType(position) == TYPE_HEADER)
((HeaderViewHolder)holder).SetHeaderDetails(items.get(position));
else
((ItemViewHolder)holder).SetItemDetails(items.get(position));
}
#Override
public int getItemCount() {
return items.size();
}
#Override
public int getItemViewType(int position) {
if (TextUtils.isEmpty(items.get(position).getRank())) {
return TYPE_HEADER;
}
else
return TYPE_ITEM;
}
class HeaderViewHolder extends RecyclerView.ViewHolder {
private TextView txtTitle;
private TextView txtDescription;
public HeaderViewHolder(#NonNull View itemView) {
super(itemView);
txtTitle = itemView.findViewById(R.id.txtModListTitle);
txtDescription = itemView.findViewById(R.id.txtModListDescription);
}
private void SetHeaderDetails(ModifyListPageItem item) {
txtTitle.setText(item.getTitle());
txtDescription.setText(item.getDescription());
}
}
class ItemViewHolder extends RecyclerView.ViewHolder {
private TextView txtRank;
private Spinner spinner;
public ItemViewHolder(#NonNull View itemView) {
super(itemView);
txtRank = itemView.findViewById(R.id.modlist_rank);
spinner = itemView.findViewById(R.id.spinner);
}
private void SetItemDetails(ModifyListPageItem item) {
txtRank.setText(item.getRank());
spinner.setOnItemClickListener(spinner.getOnItemClickListener());
}
}
}
XML file for the page layout
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ModifyListActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/bottom_navigation"
app:itemBackground="#color/colorPrimary"
app:itemTextColor="#drawable/selector"
app:itemIconTint="#drawable/selector"
app:menu="#menu/menu_navigation"
android:layout_alignParentBottom="true"
android:background="#color/colorPrimary"/>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recyclerViewModifyList"/>
</RelativeLayout>
XML File for the recyclerview item layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:padding="8dp"
tools:context=".MainActivity">
<TextView
android:id="#+id/modlist_rank"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="1."
android:textColor="#000000"
android:textSize="38dp" />
<Spinner
android:id="#+id/spinner"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp" />
</LinearLayout>

My recycleview isn't working. Can you help me?

I really need some help. I'm new in android studio. I'm having a problem about my recyclerview, it wont show some items but there's no errors.
Adapter:
public class ClassAdapter extends RecyclerView.Adapter<ClassAdapter.ClassViewHolder> {
private ArrayList<ExampleClass> qClassList;
public static class ClassViewHolder extends RecyclerView.ViewHolder {
public TextView code,name,year;
public ClassViewHolder(View view){
super(view);
code = view.findViewById(R.id.code);
name = view.findViewById(R.id.name);
year = view.findViewById(R.id.year);
}
}
public ClassAdapter(ArrayList<ExampleClass> classList){
qClassList = classList;
}
#NonNull
#Override
public ClassViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_classes, viewGroup, false);
ClassViewHolder cvh = new ClassViewHolder(v);
return cvh;
}
#Override
public void onBindViewHolder(ClassViewHolder classViewHolder, int i)
{
ExampleClass exampleClass = qClassList.get(i);
classViewHolder.code.setText(exampleClass.getqCode());
classViewHolder.name.setText(exampleClass.getqName());
classViewHolder.year.setText(exampleClass.getqYear());
}
#Override
public int getItemCount() {
return qClassList.size();
}
}
}
ClassActivity:
public class ClassActivity extends AppCompatActivity {
private ArrayList<ExampleClass> exampleClassArrayList;
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_classes);
ArrayList<ExampleClass> exampleClasses = new ArrayList<>();
exampleClasses.add(new ExampleClass("CANS103","PROGRAMMING 1","BSIT-3"));
createClassList();
buildRecycleView();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Click action
Intent intent = new Intent(ClassActivity.this, CreateClassActivity.class);
startActivity(intent);
}
});
}
public void createClassList(){
exampleClassArrayList = new ArrayList<>();
}
public void buildRecycleView(){
recyclerView = findViewById(R.id.recyclerView);
layoutManager = new LinearLayoutManager(this);
adapter = new ClassAdapter(exampleClassArrayList);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
}
activity_class.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"
android:padding="4dp"
android:scrollbars="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="0dp" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/recyclerView"
android:layout_alignParentEnd="true"
android:layout_marginEnd="27dp"
android:layout_marginBottom="32dp"
android:src="#android:drawable/ic_input_add" />
</RelativeLayout>
I've already search some problems similar to mine but it didn't work. I was wondering if someone can tell me whats the mistake I've done here. I would really appreciate it. Sorry for the inconvience.
Chaneg this Lines of code:
ArrayList<ExampleClass> exampleClasses = new ArrayList<>(); // remove this line No need
createClassList(); //call this method
exampleClassArrayList.add(new ExampleClass("CANS103","PROGRAMMING 1","BSIT-3"));
After this call
buildRecycleView()
You are adding in different list (exampleClasses ) and using different list for adapter (exampleClassArrayList) .
In createClassList() method create a new instance from your list, while you added a new item in onCreate list, remove call createClassList() worked correctly

Custom ListView with image, text and add item button

I am quite new to Android and can't achieve this, been searching all day.
Layout I'm trying to create.
I have created the custom xml layout, I have found ways to add items on create, but I need the list to be empty and than when the button is pressed to add from a list.
This is the layout:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<ImageView
android:id="#+id/info_img_view"
android:layout_width="30dp"
android:layout_height="30dp" />
<TextView
android:id="#+id/info_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<TextView
android:id="#+id/info_time_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="right"
/>
</TableRow>
I have a ListView in the main activity:
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
How do I go about this? I would appreciate it if someone can point me to a resource where I can learn what the code is actually doing, not the tutorials I find where I just copy and paste...
Thanks!
Edit, a bit more explanation of what I'm trying to achieve
I have 6 buttons. When a button is pressed it should add a list item with two textviews, and one image out of total three images.
So for instance if Button1 is pressed: Add list item > "Text one" "Text one" "imageTwoOfThree".
Than, if Button2 is pressed: Add list item on top > "Text two" "Text two" "imageTwoOfThree"
And so on... The text is hardcoded.
Here use this:
I have created a list with dummydata you can change the text and Image according to you
First create a class Data:
public class Data {
private String name,price;
private int imageId;
public Data(){}
public Data(String name,String price,int imageId){
this.name = name;
this.price = price;
this.imageId = imageId;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
}
Then create a ListView Adapter to handle your data:
public class ListViewAdaptor extends RecyclerView.Adapter<ListViewAdaptor.MyViewHolder> {
private List<Data> mDataList;
public class MyViewHolder extends RecyclerView.ViewHolder{
public TextView name,price;
public ImageView imageView;
public MyViewHolder(View view){
super(view);
name = (TextView) view.findViewById(R.id.name);
price= (TextView) view.findViewById(R.id.price);
imageView = (ImageView) view.findViewById(R.id.image);
}
}
public ListViewAdaptor(List<Data> dataList){
this.mDataList = dataList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_view_item, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Data data = mDataList.get(position);
holder.name.setText(data.getName());
holder.price.setText(data.getPrice());
holder.imageView.setImageResource(data.getImageId());
}
#Override
public int getItemCount() {
return mDataList.size();
}
}
layout for your list view items name it list_view_item:
<?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="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_launcher"
android:id="#+id/image"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/name"
android:text="name"
android:gravity="center"
android:textSize="26sp"
android:layout_weight="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/price"
android:text="price"
android:gravity="center"
android:textSize="26sp"
android:layout_weight="1"/>
</LinearLayout>
Then from your activity where you want to add listView add recyclerView in layout:
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/recycler_view">
</android.support.v7.widget.RecyclerView>
Then use this recyclerview like this:
//I have called it from my MainActivity you can use it in whatever activity you'll like
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private ListViewAdaptor mAdapter;
private List<Data> mDataList = new ArrayList<>();
private static final String TAG = "MainActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mAdapter = new ListViewAdaptor(mDataList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setAdapter(mAdapter);
prepareList();
}
public void prepareList(){
Data data = new Data("Item1","Price1",R.drawable.star);
mDataList.add(data);
data = new Data("Item2","Price2",R.drawable.star);
mDataList.add(data);
data = new Data("Item3","Price3",R.drawable.star);
mDataList.add(data);
data = new Data("Item4","Price4",R.drawable.star);
mDataList.add(data);
data = new Data("Item5","Price5",R.drawable.star);
mDataList.add(data);
}
}
Hope this helps!!!
You will find lots of online resources for this. But let me put it in a brief way.
Assuming you want to store 2 textviews in one single row.
1. For each layout of the row, you will need to make a custom layout xml file and design your layout there.
Next, make a class which stores the data and returns the data through getters.
class Category {
private String categoryName;
private String categoryImageURL;
Category (String categoryName, String categoryImageUrl) {
this.categoryName = categoryName;
this.categoryImageURL = categoryImageUrl;
}
String getCategoryName () {
return categoryName;
}
String getCategoryImageURL () {
return categoryImageURL;
}
}
Now make a custom arrayadapter which will link the data and the layout.
Extend the arrayadapter class with the class you defined above.
private class categoryArrayAdapter extends ArrayAdapter<Category> {
private Context context;
private List<Category> categories;
public categoryArrayAdapter (Context context, int resource, ArrayList<Category> objects) {
super(context, resource, objects);
this.context = context;
this.categories = objects;
}
public View getView (int position, View convertView, ViewGroup parent) {
Category category = categories.get(position);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.category_row_view, null);
TextView categoryListRowText = (TextView) view.findViewById(R.id.categoryListRowText);
TextView categoryListRowImage = (TextView) view.findViewById(R.id.categoryListRowImage);
categoryListRowText.setText(category.getCategoryName());
categoryListRowImage.setText(category.getCategoryImageURL());
return view;
}
}
Finally link the adapter to your listview
ArrayAdapter<Category> adapter = new categoryArrayAdapter(this, 0, categories);
categoryListView.setAdapter(adapter);
Hope this answered you question.

How to make card style menu in android

I want to add card style in my app like this
i use in my app mysql database so i need to make like this cards and put my data from database in it now i use ListView with this code
public void listAllItme() {
ListAdapter lA = new listAdapter(listitems);
listView.setAdapter(lA);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent open = new Intent(R_arabic.this, rewaya_show.class);
open.putExtra("name", listitems.get(position).name);
open.putExtra("url", listitems.get(position).url);
open.putExtra("img", listitems.get(position).img);
open.putExtra("num", listitems.get(position).num);
startActivity(open);
}
}
});
}
class listAdapter extends BaseAdapter {
ArrayList<listitem_gib> lista = new ArrayList<listitem_gib>();
public listAdapter(ArrayList<listitem_gib> lista) {
this.lista = lista;
}
#Override
public int getCount() {
return lista.size();
}
#Override
public Object getItem(int position) {
return lista.get(position).name;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = getLayoutInflater();
View view = layoutInflater.inflate(R.layout.row_item_gib, null);
TextView name = (TextView) view.findViewById(R.id.textView_gib);
ImageView img = (ImageView) view.findViewById(R.id.imageView_gib);
TextView num = (TextView) view.findViewById(R.id.textView_gib2);
TextView size = (TextView) view.findViewById(R.id.textView_gib3);
name.setText(lista.get(position).name);
num.setText(lista.get(position).num);
size.setText(lista.get(position).size);
Picasso.with(R_arabic.this).load("http://grassyhat.com/android/image/" + lista.get(position).img).into(img);
return view;
}
}
first i want to know how i can make like this card style
second how i can use this code with card menu not listview
sorry im new in android and sorry for my bad english
What do you mean by card menu? because the example in the image is a recyclerview with a cardview item, you can achieve this by doing something like this
This will be your activity
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
//Just your list of objects, in your case the list that comes from the db
List<Items> itemsList = new ArrayList<>();
CardAdapter adapter = new CardAdapter(this, itemsList);
//RecyclerView needs a layout manager in order to display data so here we create one
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
//Here we set the layout manager and the adapter to the listview
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
Inside the layout file you just have to place the recyclerview like this
<RelativeLayout
android:id="#+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="jsondh.myapplication.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Then your adapter will be something like this
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.CardViewHolder> {
private List<Items> itemsList;
private Activity activity;
public CardAdapter(Activity activity, List<Items> items){
this.activity = activity;
this.itemsList = items;
}
#Override
public CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = activity.getLayoutInflater().inflate(R.layout.cardview_layout, parent, false);
return new CardViewHolder(itemView);
}
#Override
public void onBindViewHolder(CardViewHolder holder, int position) {
//Here you bind your views with the data from each object from the list
}
#Override
public int getItemCount() {
return itemsList.size();
}
public class CardViewHolder extends RecyclerView.ViewHolder {
public ImageView bookImage;
public TextView bookLabel01, bookLabel02;
public CardViewHolder(View itemView) {
super(itemView);
bookImage = (ImageView)itemView.findViewById(R.id.image);
bookLabel01 = (TextView)itemView.findViewById(R.id.label01);
bookLabel02 = (TextView)itemView.findViewById(R.id.label02);
}
}
And the last one will be the layout from each item on the list, like this
<RelativeLayout
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="match_parent"
android:layout_margin="10dp">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardElevation="15dp"
app:cardBackgroundColor="#3369Ed">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/image"
android:layout_width="150dp"
android:layout_height="130dp"/>
<TextView
android:id="#+id/label01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Label"
android:layout_gravity="right"
android:padding="5dp"
android:textColor="#ffffff"/>
<TextView
android:id="#+id/label02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LongerLabel"
android:layout_gravity="right"
android:padding="5dp"
android:textColor="#ffffff"/>
</LinearLayout>
</android.support.v7.widget.CardView>
You also have to add this to your gradle file:
compile 'com.android.support:recyclerview-v7:25.0.0'
compile 'com.android.support:cardview-v7:25.0.0'
Hope it helps!
It's pretty simple. You will have to use a RecyclerView with GridLayoutManager and add a cardView to it.
Then, use an Adapter and ViewHolder to feed the data.
I suggest you to check this out:
https://developer.android.com/training/material/lists-cards.html

How to Store EditText Into ListView Item With Buttons

I am just starting with android, so I don't have much to show of what I have so far...
I am trying to make a to-do list. I have seen tutorials on how to do this with a menu for input and have the tasks displayed in a ListView, however I would like the user to input the task into an EditText instead. I was using this (which doesn't use a ListView) to just have a button click make a new TextView underneath the EditText, however I couldn't add buttons next to the TextView which would remove it (when the user finishes a task).
Using a ListView seems much easier for this, but how can I have the EditText input go to a ListView with all the tasks and buttons to remove them (permanently, not just set visibility to 0)? I would also like the tasks to be stored, so the user can close the app and return with the tasks still there.
Any ideas? I haven't made much progress.
I recommend you to use RecyclerView instead of Listview. compile this in your Gradle
compile 'com.levelupstudio:expandable-recyclerview:1.0.1'
Here is the mainActivity.java
public class MainActivity extends ActionBarActivity implements
RecyclerViewAdapter.OnItemClickListener{
private RecyclerView myRecyclerView;
private LinearLayoutManager linearLayoutManager;
private RecyclerViewAdapter myRecyclerViewAdapter;
EditText nameField;
Button btnAdd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myRecyclerView = (RecyclerView)findViewById(R.id.myrecyclerview);
linearLayoutManager =
new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
/*
linearLayoutManager =
new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
*/
myRecyclerViewAdapter = new RecyclerViewAdapter(this);
myRecyclerViewAdapter.setOnItemClickListener(this);
myRecyclerView.setAdapter(myRecyclerViewAdapter);
myRecyclerView.setLayoutManager(linearLayoutManager);
nameField = (EditText)findViewById(R.id.namefield);
btnAdd = (Button)findViewById(R.id.addbutton);
btnAdd.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
String newName = nameField.getText().toString();
Context context= getApplicationContext();
myRecyclerViewAdapter.add(0,newName);
Toast.makeText(context,"You added" +newName.toUpperCase()+ "in your view",Toast.LENGTH_LONG).show();
}
});
}
#Override
public void onItemClick(RecyclerViewAdapter.ItemHolder item, int position) {
Toast.makeText(this,
"Remove " + position + " : " + item.getItemName(),
Toast.LENGTH_SHORT).show();
}}
And AdapterClass for Recycler
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ItemHolder> {
private List<String> itemsName;
private OnItemClickListener onItemClickListener;
private LayoutInflater layoutInflater;
public RecyclerViewAdapter(Context context){
layoutInflater = LayoutInflater.from(context);
itemsName = new ArrayList<String>();
}
#Override
public RecyclerViewAdapter.ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = layoutInflater.inflate(R.layout.list_item, parent, false);
return new ItemHolder(itemView, this);
}
#Override
public void onBindViewHolder(RecyclerViewAdapter.ItemHolder holder, int position) {
holder.setItemName(itemsName.get(position));
}
#Override
public int getItemCount() {
return itemsName.size();
}
public void setOnItemClickListener(OnItemClickListener listener){
onItemClickListener = listener;
}
public OnItemClickListener getOnItemClickListener(){
return onItemClickListener;
}
public interface OnItemClickListener{
public void onItemClick(ItemHolder item, int position);
}
public void add(int location, String iName){
itemsName.add(location, iName);
notifyItemInserted(location);
}
public static class ItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private RecyclerViewAdapter parent;
TextView textItemName;
public ItemHolder(View itemView, RecyclerViewAdapter parent) {
super(itemView);
itemView.setOnClickListener(this);
this.parent = parent;
textItemName = (TextView) itemView.findViewById(R.id.item_name);
}
public void setItemName(CharSequence name){
textItemName.setText(name);
}
public CharSequence getItemName(){
return textItemName.getText();
}
#Override
public void onClick(View v) {
final OnItemClickListener listener = parent.getOnItemClickListener();
if(listener != null){
listener.onItemClick(this, getPosition());
}
}
}}
and your xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/namefield"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/addbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/myrecyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
list_xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="16dp"
android:background="#android:color/holo_blue_light"
android:orientation="vertical">
<TextView
android:id="#+id/item_name"
android:layout_centerVertical="true"
android:layout_margin="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>

Categories