I created a Recyclerview in my main_activity and I wanted to use this cardview in a partial activity... I put extras variables and data to the new activity but any thing not showing in new activity...
this is my partial xml code (hotel_page.xml) :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/card_view1"
android:layout_width="match_parent"
android:layout_height="288dp"
android:layout_margin="#dimen/card_margin"
android:elevation="3dp"
card_view:cardCornerRadius="0dp"
android:gravity="top">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/card"
android:focusable="true"
android:contextClickable="true"
android:gravity="top"
android:layout_alignParentBottom="true">
</RelativeLayout>
</android.support.v7.widget.CardView>
<TextView
android:id="#+id/title1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="25dp"
android:paddingTop="10dp"
android:textColor="#color/cardview_dark_background"
android:textSize="15dp"
android:layout_above="#+id/count1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:id="#+id/thumbnail1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:scaleType="fitXY"
android:contextClickable="true"
android:layout_alignTop="#+id/title1" />
<TextView
android:id="#+id/count1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="25dp"
android:textSize="12dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</LinearLayout>
This is My HotelPageList.java :
package ir.homa;
public class HotelPageList {
private String name;
private int numOfRooms;
private int thumbnail;
public HotelPageList() {
}
public HotelPageList(String name, int numOfRooms, int thumbnail) {
this.name = name;
this.numOfRooms = numOfRooms;
this.thumbnail = thumbnail;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumOfRooms() {
return numOfRooms;
}
public void setNumOfRooms(int numOfRooms) {
this.numOfRooms = numOfRooms;
}
public int getThumbnail() {
return thumbnail;
}
public void setThumbnail(int thumbnail) {
this.thumbnail = thumbnail;
}
}
This is HotelPageAdapter.java :
package ir.homa;
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.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.List;
/**
* Created by SMQ on 7/20/2016.
*/
public class HotelPageAdapter extends RecyclerView.Adapter<HotelPageAdapter.MyViewHolder> {
private Context mContext;
private List<HotelPageList> hotelPage;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title1, count1;
public ImageView thumbnail1, overflow1;
public MyViewHolder(View view) {
super(view);
title1 = (TextView) view.findViewById(R.id.title1);
count1 = (TextView) view.findViewById(R.id.count1);
thumbnail1 = (ImageView) view.findViewById(R.id.thumbnail1);
overflow1 = (ImageView) view.findViewById(R.id.overflow);
}
}
public HotelPageAdapter(Context mContext, List<HotelPageList> hotelPage) {
this.mContext = mContext;
this.hotelPage = hotelPage;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.hotel_page, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
final HotelPageList hotel = hotelPage.get(position);
holder.title1.setText(hotel.getName());
holder.count1.setText(hotel.getNumOfRooms() + " اتاق");
// loading hotel cover using Glide library
Glide.with(mContext).load(hotel.getThumbnail()).into(holder.thumbnail1);
}
#Override
public int getItemCount() {return hotelPage.size();}
}
and this is result:
Where is problem ?
Try this way
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="288dp">
<android.support.v7.widget.CardView
android:id="#+id/card_view1"
android:layout_width="match_parent"
android:layout_height="288dp"
android:elevation="3dp"
card_view:cardCornerRadius="0dp"
android:gravity="top">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/card"
android:focusable="true"
android:contextClickable="true"
android:gravity="top"
android:layout_alignParentBottom="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView2"
android:src="#drawable/ic_launcher"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
<TextView
android:id="#+id/title1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="25dp"
android:paddingTop="10dp"
android:textColor="#color/cardview_dark_background"
android:textSize="15dp"
android:text="Title"
android:layout_above="#+id/count1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:id="#+id/thumbnail1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:src="#drawable/ic_launcher"
android:contextClickable="true"
android:layout_alignTop="#+id/title1" />
<TextView
android:id="#+id/count1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="25dp"
android:textSize="12dp"
android:text="sub title"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</LinearLayout>
Related
I wanna ask how can I sum all patty price and display in the total ? The total is in activity_main.xml layout while all the product price is in view_product.xml .
Screenshot of app
As you can see the grey color text is per patty price , and the right side black text is price after multiply the quantity of patty. I want all the black text to sum up and show at below total.
MainActivity.java
package com.tankarjian.it212n.a2910assingment;
import android.app.Activity;
import android.os.Bundle;
import android.widget.BaseAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends Activity {
private Products products;
private BaseAdapter productsAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
products = createInitialProductList();
productsAdapter = new ProductsAdapter(products, productClickListener, getLayoutInflater());
ListView productsListView = (ListView) findViewById(R.id.products_list);
productsListView.setAdapter(productsAdapter);
}
private Products createInitialProductList() {
return new Products(new ArrayList<>(Arrays.asList(
new Product("Chicken Patty", 0, 4.00),
new Product("Chicken Special Patty", 0, 5.00),
new Product("Imported Lamb Patty", 0, 8.00),
new Product("Imported Lamb Special Patty", 0, 10.00)
)));
}
private final ProductClickListener productClickListener = new ProductClickListener() {
#Override
public void onMinusClick(Product product) {
products.removeOneFrom(product);
productsAdapter.notifyDataSetChanged();
}
#Override
public void onPlusClick(Product product) {
products.addOneTo(product);
productsAdapter.notifyDataSetChanged();
}
};
private static class Products implements ProductDataSet {
private final List<Product> productList;
Products(List<Product> productList) {
this.productList = productList;
}
#Override
public int size() {
return productList.size();
}
#Override
public Product get(int position) {
return productList.get(position);
}
#Override
public long getId(int position) {
return position;
}
public void removeOneFrom(Product product) {
int i = productList.indexOf(product);
if (i == -1) {
throw new IndexOutOfBoundsException();
}
Product updatedProduct = new Product(product.name, (product.quantity - 1), product.getPrice());
productList.remove(product);
productList.add(i, updatedProduct);
}
public void addOneTo(Product product) {
int i = productList.indexOf(product);
if (i == -1) {
throw new IndexOutOfBoundsException();
}
Product updatedProduct = new Product(product.name, (product.quantity + 1), product.getPrice());
productList.remove(product);
productList.add(i, updatedProduct);
}
}
}
ProductsAdapter.java
package com.tankarjian.it212n.a2910assingment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Locale;
class ProductsAdapter extends BaseAdapter {
private final ProductDataSet productDataSet;
private final ProductClickListener productClickListener;
private final LayoutInflater layoutInflater;
ProductsAdapter(ProductDataSet productDataSet, ProductClickListener productClickListener, LayoutInflater layoutInflater) {
this.productDataSet = productDataSet;
this.productClickListener = productClickListener;
this.layoutInflater = layoutInflater;
}
#Override
public int getCount() {
return productDataSet.size();
}
#Override
public Product getItem(int position) {
return productDataSet.get(position);
}
#Override
public long getItemId(int position) {
return productDataSet.getId(position);
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = createView(parent);
view.setTag(ViewHolder.from(view));
}
Product Products =(Product) getItem(position);
ImageView image = (ImageView) view.findViewById(R.id.imageBurgerView);
image.setImageResource(R.drawable.burger);
TextView text = (TextView) view.findViewById(R.id.singlePrice);
String price = String.format(Locale.ENGLISH, "%.2f", Products.getPrice());
text.setText(price);
TextView text1 = (TextView) view.findViewById(R.id.totalPrice);
String totalPrice = String.format(Locale.ENGLISH, "%.2f", (Products.getPrice() * Products.quantity));
text1.setText(totalPrice);
Product product = productDataSet.get(position);
ViewHolder viewHolder = (ViewHolder) view.getTag();
update(viewHolder, product);
return view;
}
private View createView(ViewGroup parent) {
return layoutInflater.inflate(R.layout.view_product, parent, false);
}
private void update(ViewHolder viewHolder, final Product product) {
viewHolder.name.setText(product.name);
viewHolder.quantity.setText(String.valueOf(product.quantity));
viewHolder.minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
productClickListener.onMinusClick(product);
}
});
viewHolder.plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
productClickListener.onPlusClick(product);
}
});
}
private static final class ViewHolder {
final TextView name;
final TextView quantity;
final View minus;
final View plus;
static ViewHolder from(View view) {
return new ViewHolder(
((TextView) view.findViewById(R.id.product_name)),
((TextView) view.findViewById(R.id.product_quantity)),
view.findViewById(R.id.product_minus),
view.findViewById(R.id.product_plus)
);
}
private ViewHolder(TextView name, TextView quantity, View minus, View plus) {
this.name = name;
this.quantity = quantity;
this.minus = minus;
this.plus = plus;
}
}
}
Product.java
package com.tankarjian.it212n.a2910assingment;
class Product {
final String name;
final int quantity;
private double price;
Product(String name, int quantity, Double price) {
this.name = name;
this.quantity = quantity;
this.price = price;
}
public double getPrice() {return price;}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:layout_marginStart="15dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="16dp"
tools:context=".MainActivity">
<ListView
android:id="#+id/products_list"
android:layout_width="0dp"
android:layout_height="512dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Switch
android:id="#+id/switchMember"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:text="Member"
app:layout_constraintEnd_toEndOf="#+id/products_list"
app:layout_constraintTop_toBottomOf="#+id/products_list" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_marginStart="16dp"
android:text="Tax "
android:textColor="#2A2F49"
android:textSize="19sp"
android:textStyle="bold|italic"
app:layout_constraintBottom_toTopOf="#+id/textView3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/products_list"
app:layout_constraintVertical_bias="0.92" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_marginBottom="68dp"
android:text="Total "
android:textColor="#2A2F49"
android:textSize="19sp"
android:textStyle="bold|italic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/totalTax"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:text="0"
android:textColor="#2A2F49"
android:textSize="19sp"
android:textStyle="bold|italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="#+id/textView2"
app:layout_constraintTop_toTopOf="#+id/textView7" />
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:text="RM"
android:textColor="#2A2F49"
android:textSize="19sp"
android:textStyle="bold|italic"
app:layout_constraintEnd_toStartOf="#+id/totalTax"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="#+id/textView7"
app:layout_constraintTop_toTopOf="#+id/textView7" />
<TextView
android:id="#+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:text="RM"
android:textColor="#2A2F49"
android:textSize="19sp"
android:textStyle="bold|italic"
app:layout_constraintEnd_toStartOf="#+id/finalTotalPrice"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="#+id/textView8"
app:layout_constraintTop_toTopOf="#+id/textView8" />
<TextView
android:id="#+id/finalTotalPrice"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:text="0"
android:textColor="#2A2F49"
android:textSize="19sp"
android:textStyle="bold|italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="#+id/textView3"
app:layout_constraintTop_toTopOf="#+id/textView8" />
<TextView
android:id="#+id/textView7"
android:layout_width="210dp"
android:layout_height="25dp"
android:layout_marginStart="20dp"
android:text="--------------------------------"
android:textColor="#2A2F49"
android:textSize="19sp"
android:textStyle="bold|italic"
app:layout_constraintEnd_toStartOf="#+id/totalTax"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#+id/textView2"
app:layout_constraintTop_toTopOf="#+id/textView2" />
<TextView
android:id="#+id/textView8"
android:layout_width="210dp"
android:layout_height="26dp"
android:layout_marginStart="20dp"
android:text="--------------------------------"
android:textColor="#2A2F49"
android:textSize="19sp"
android:textStyle="bold|italic"
app:layout_constraintEnd_toStartOf="#+id/finalTotalPrice"
app:layout_constraintHorizontal_bias="0.08"
app:layout_constraintStart_toEndOf="#+id/textView3"
app:layout_constraintTop_toTopOf="#+id/textView3" />
</androidx.constraintlayout.widget.ConstraintLayout>
view_product.xml
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/product_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#FF5722"
android:textSize="20sp"
android:textStyle="bold|italic" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageBurgerView"
android:layout_width="70dp"
android:layout_height="60dp"
android:layout_marginStart="10dp"
android:contentDescription="#string/todo"
app:srcCompat="#drawable/burger" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:text="#string/rm"
android:textAlignment="textEnd"
android:textColor="#878BA3"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/singlePrice"
android:layout_width="108dp"
android:layout_height="wrap_content"
android:layout_marginStart="3dp"
android:layout_weight="1"
android:text="#string/textview"
android:textColor="#878BA3"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/rm"
android:textAlignment="textEnd"
android:textColor="#272E53"
android:textSize="16sp"
android:textStyle="bold|italic" />
<TextView
android:id="#+id/totalPrice"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/textview"
android:textAlignment="textStart"
android:textColor="#272E53"
android:textSize="16sp"
android:textStyle="bold|italic" />
</LinearLayout>
<LinearLayout
android:layout_width="116dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/product_minus"
android:layout_width="30dp"
android:layout_height="40dp"
android:text="-"
tools:ignore="TouchTargetSizeCheck" />
<TextView
android:id="#+id/product_quantity"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="20"
android:textAlignment="center"
android:textSize="16sp"
android:textStyle="bold" />
<Button
android:id="#+id/product_plus"
android:layout_width="30dp"
android:layout_height="40dp"
android:text="+"
tools:ignore="TouchTargetSizeCheck" />
</LinearLayout>
</LinearLayout>
In view_product.xml you are only displaying the price, but that's not where the actual data is located so your question shouldn't be that the prices are in view_product.xml and you want to show in activity_main.xml, but your question should be -
Where is the data located
How to make the necessary calculations
Where to show it. .
Your data regarding the prices are in products object. So, in order to calculate the total you just need to add one more method which calculates the total
private double calculateTotal(ProductDataSet productDataSet) {
double totalPrice = 0.0;
for(int i=0; i<productDataSet.size(); i++) {
totalPrice += productDataSet.get(i).getPrice();
}
return totalPrice;
}
The result of this you can store in a new value or show directly in the textView itself.
((TextView)findViewById(R.id.finalTotalPrice)).setText(calculateTotal(products).toString());
This you can call anywhere after createInitialProductList(). I hope this helps and makes it clear for you.
I'm writing a coursework project and I need to create a custom material card.
But when I inherit a material card, the styles disappear. I have tried all the solutions I found on the Internet. I will be grateful for any help.
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Case aCase = new Case();
aCase.setId(183928);
aCase.setStatus("Status");
aCase.setDescription("fermentum dui faucibus in ornare quam viverra orci sagittis eu");
CaseView caseView = findViewById(R.id.caseView);
caseView.setCase(aCase);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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">
<com.google.android.material.card.MaterialCardView
android:id="#+id/card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<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:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title"
android:textAppearance="?attr/textAppearanceHeadline6" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="secondary_text"
android:textAppearance="?attr/textAppearanceBody2"
android:textColor="?android:attr/textColorSecondary" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="supporting_text"
android:textAppearance="?attr/textAppearanceBody2"
android:textColor="?android:attr/textColorSecondary" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="horizontal">
<com.google.android.material.button.MaterialButton
style="?attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="action_1" />
<com.google.android.material.button.MaterialButton
style="?attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="action_2" />
</LinearLayout>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
<com.barmatograf.interpolith.view.CaseView
android:id="#+id/caseView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
screenshot https://i.stack.imgur.com/htRq0.png
model class for custom card
package com.barmatograf.interpolith.model;
public class Case {
private Integer id;
private String description;
private String status;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
CaseView.java
package com.barmatograf.interpolith.view;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
import com.barmatograf.interpolith.R;
import com.barmatograf.interpolith.model.Case;
import com.google.android.material.card.MaterialCardView;
public class CaseView extends MaterialCardView {
private Case aCase;
private TextView number;
private TextView status;
private TextView description;
public CaseView(Context context) {
this(context, null);
}
public CaseView(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.materialCardViewStyle);
}
public CaseView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
inflate(context, R.layout.case_view_layout, this);
number = findViewById(R.id.case_number);
status = findViewById(R.id.case_status);
description = findViewById(R.id.case_description);
}
public void setCase(Case aCase) {
this.aCase = aCase;
if (aCase != null) {
if (aCase.getId() != null)
number.setText(String.valueOf(aCase.getId()));
if (aCase.getStatus() != null)
status.setText(aCase.getStatus());
if (aCase.getDescription() != null) {
String descriptionText = aCase.getDescription();
int length = Math.min(descriptionText.length(), 100);
descriptionText = descriptionText.substring(0, length);
descriptionText = descriptionText.trim();
descriptionText = descriptionText + "...";
description.setText(descriptionText);
}
}
}
public Case getCase() {
return aCase;
}
}
case_view_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="horizontal">
<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:orientation="horizontal"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title"
android:textAppearance="?attr/textAppearanceHeadline6" />
<TextView
android:id="#+id/case_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?attr/textAppearanceHeadline6" />
</LinearLayout>
<TextView
android:id="#+id/case_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textAppearance="?attr/textAppearanceBody2"
android:textColor="?android:attr/textColorSecondary" />
<TextView
android:id="#+id/case_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:textAppearance="?attr/textAppearanceBody2"
android:textColor="?android:attr/textColorSecondary" />
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
styles
<style name="AppTheme" parent="Theme.MaterialComponents">
<item name="materialCardViewStyle">#style/Widget.MaterialComponents.CardView</item>
</style>
note that your CaseView IS a MaterialCardView (extends), but you have also another MaterialCardView as a root of your inflated XML. this is another View without style set (no xml style attr), in fact your CaseView contains one child - another MaterialCardView.
imho it should respect "global" setting from AppTheme, but still worth fix this double-CardView case - use <merge tag instead of root MaterialCardView in XML. inflate method will add all childs placed inside <merge> tag to CaseView
I want to add shared element transition to items in a recyclerview which will opened on another fragment, but nothing works.
The only view that i want to share is an ImageView.
ImageViews and Textviews inside recyclerview items are loaded using Glide for images and firebase database methods for texts. In the detailed fragment, the bitmap of recyclerview item is passed and only the text is downloaded again.
the method which opens the new fragment from recyclerview items is :
homeUserAnnouncesAdapter.setOnCardAnnounceClickedListener(new OnCardAnnounceClickedListener() {
#Override
public void onCardClicked(CardRentAnnounce card, ImageView imageView, int poz) {
Log.v("TRANSITIONTEST", "CLICKED FROM HOME FRAGMENT --- RENT");
Log.v("TRANSITIONTEST", "in onClick: " + imageView.getTransitionName());
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
OpenRentAnnounceFragment newFragment = OpenRentAnnounceFragment.newInstanceWithImage(card, bitmap, poz);
newFragment.setSharedElementEnterTransition(new DetailsTransition());
newFragment.setEnterTransition(new Fade());
newFragment.setExitTransition(new Fade());
newFragment.setSharedElementReturnTransition(new DetailsTransition());
getActivity().getSupportFragmentManager()
.beginTransaction()
.addSharedElement(imageView, imageView.getTransitionName())
.replace(R.id.content_main_without_toolbar, newFragment)
.addToBackStack(null)
.commit();
}
});
the DetailsTransition class is :
public class DetailsTransition extends TransitionSet{
public DetailsTransition() {
init();
}
/**
* This constructor allows us to use this transition in XML
*/
public DetailsTransition(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
setOrdering(ORDERING_TOGETHER);
addTransition(new ChangeBounds())
.addTransition(new ChangeTransform())
.addTransition(new ChangeImageTransform());
}
}
i'm assigning different transition names for every view, on adapter:
#Override
public void onBindViewHolder(final HomeUserAnnouncesViewHolder holder, int position) {
final CardRentAnnounce cardRentAnnounce = cardRentAnnounceArrayList.get(position);
holder.updateUI(cardRentAnnounce);
ViewCompat.setTransitionName(holder.rentImage, String.valueOf(position) + "_rent");
MaterialRippleLayout mlr = (MaterialRippleLayout)holder.itemView.findViewById(R.id.search_home_ripple);
mlr.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(listener != null){
listener.onCardClicked(cardRentAnnounce, holder.rentImage, holder.getAdapterPosition());
}
}
});
}
the id of image in recyclerview item is identically with the image in fragment
i have followed this tutorial to create the transition, even cloned the project to my pc, run on emulator and trying to do exactly like in the working project but still not working.
how i set the image and imageTransition name in new fragment :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_open_rent_announce, container, false);
rootView = view;
image = (ImageView)view.findViewById(R.id.detailed_image);
image.setImageBitmap(currentImageBitmap);
image.setTransitionName(String.valueOf(pozition) + "_rent");
Log.v("TRANSITIONTEST", "in onCreateView: " + image.getTransitionName());
initViews(view);
setViews();
return view;
}
Fragment SharedElementsTransition tutorial
both images on recyclerview and detailed fragment are loaded with Glide, but i've tried with local images and still no succes.
What did i'm missing? ask for more code if needed.
Thanks.
the full adapter :
package com.minimalart.studentlife.adapters;
import android.content.Context;
import android.net.Uri;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.balysv.materialripple.MaterialRippleLayout;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
import com.firebase.ui.storage.images.FirebaseImageLoader;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.minimalart.studentlife.R;
import com.minimalart.studentlife.interfaces.OnCardAnnounceClickedListener;
import com.minimalart.studentlife.interfaces.OnImageReadyListener;
import com.minimalart.studentlife.models.CardRentAnnounce;
import java.util.ArrayList;
/**
* Created by ytgab on 23.01.2017.
*/
public class HomeUserAnnouncesAdapter extends RecyclerView.Adapter<HomeUserAnnouncesAdapter.HomeUserAnnouncesViewHolder>{
public OnCardAnnounceClickedListener listener;
public OnImageReadyListener imageListener;
private ArrayList<CardRentAnnounce> cardRentAnnounceArrayList;
private Context context;
public void setOnCardAnnounceClickedListener(OnCardAnnounceClickedListener listener){
this.listener = listener;
}
public void setOnImageReadyListener(OnImageReadyListener imageListener) {
this.imageListener = imageListener;
}
public HomeUserAnnouncesAdapter(ArrayList<CardRentAnnounce> cardRentAnnounceArrayList, Context context) {
this.cardRentAnnounceArrayList = cardRentAnnounceArrayList;
this.context = context;
}
#Override
public HomeUserAnnouncesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View announceCard = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_home_user_announces, parent, false);
return new HomeUserAnnouncesAdapter.HomeUserAnnouncesViewHolder(announceCard);
}
#Override
public void onBindViewHolder(final HomeUserAnnouncesViewHolder holder, int position) {
final CardRentAnnounce cardRentAnnounce = cardRentAnnounceArrayList.get(position);
holder.updateUI(cardRentAnnounce);
ViewCompat.setTransitionName(holder.rentImage, String.valueOf(position) + "_rent");
MaterialRippleLayout mlr = (MaterialRippleLayout)holder.itemView.findViewById(R.id.search_home_ripple);
mlr.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(listener != null){
listener.onCardClicked(cardRentAnnounce, holder.rentImage, holder.getAdapterPosition());
}
}
});
}
public void loadNewData(ArrayList<CardRentAnnounce> list){
cardRentAnnounceArrayList = list;
}
#Override
public int getItemCount() {
return cardRentAnnounceArrayList.size();
}
public class HomeUserAnnouncesViewHolder extends RecyclerView.ViewHolder{
private TextView rentTitle;
private TextView rentRooms;
private TextView rentPrice;
private TextView rentLocation;
private ImageView rentImage;
private Uri uri;
StorageReference storageReference;
private static final String REF_RENT_IMAGES = "rent-images";
public HomeUserAnnouncesViewHolder(View itemView) {
super(itemView);
rentTitle = (TextView)itemView.findViewById(R.id.card_home_user_announces_title);
rentImage = (ImageView)itemView.findViewById(R.id.detailed_image);
}
public void updateUI(CardRentAnnounce cardRentAnnounce){
getImageURL(cardRentAnnounce.getAnnounceID());
rentTitle.setText(cardRentAnnounce.getTitle());
}
public void getImageURL(String announceID){
FirebaseStorage firebaseStorage = FirebaseStorage.getInstance();
storageReference = firebaseStorage.getReference().child(REF_RENT_IMAGES).child(announceID);
/*rentImage.setImageDrawable(context.getResources().getDrawable(R.drawable.apartment_inside, context.getTheme()));*/
Glide.with(context).using(new FirebaseImageLoader()).load(storageReference).into(new GlideDrawableImageViewTarget(rentImage){
#Override
public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) {
super.onResourceReady(resource, animation);
Log.v("TRANSITIONTEST", "Image ready");
if(imageListener != null)
imageListener.onImageReady();
}
});
}
public ImageView getRentImage(){
return rentImage;
}
public Uri getUri() {
return uri;
}
public void setUri(Uri uri) {
this.uri = uri;
}
}
}
recyclerview card layout :
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card="http://schemas.android.com/apk/res-auto"
android:layout_width="100dp"
android:layout_height="match_parent"
android:id="#+id/card_home_user_announces"
card:cardElevation="#dimen/card_elevation"
card:cardCornerRadius="#dimen/card_radius">
<com.balysv.materialripple.MaterialRippleLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/search_home_ripple">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="120dp"
android:layout_height="120dp"
android:src="#drawable/apartment_inside"
android:id="#+id/detailed_image"
android:scaleType="centerCrop"
android:adjustViewBounds="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:ellipsize="marquee"
android:id="#+id/card_home_user_announces_title"
android:text="Titlu"
android:textColor="#color/textPrincipal"
android:maxLines="1"
android:lines="1"
android:textSize="16sp"
android:textStyle="normal"/>
</LinearLayout>
</com.balysv.materialripple.MaterialRippleLayout>
</android.support.v7.widget.CardView>
detailed fragment layout :
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.minimalart.studentlife.fragments.OpenRentAnnounceFragment">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/background_light"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="#+id/rent_appbar"
android:layout_width="match_parent"
android:layout_height="300dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/rent_detailed_collapsing"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp"
android:descendantFocusability="blocksDescendants">
<ImageView
android:id="#+id/detailed_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
android:src="#drawable/apartment_inside"
app:layout_collapseMode="parallax"
android:transitionName="rentTransition"/>
<android.support.v7.widget.Toolbar
android:id="#+id/detailed_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin"
app:title="Titlu">
</android.support.v7.widget.Toolbar>
<ImageButton
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="left"
android:layout_marginLeft="#dimen/standard_padding"
android:layout_marginTop="#dimen/standard_padding"
android:src="#drawable/ic_arrow_back"
android:background="#color/full_alpha"
android:id="#+id/detailed_back_btn"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
app:behavior_overlapTop="30dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/standard_padding"
android:layout_marginLeft="#dimen/standard_padding"
android:layout_marginRight="#dimen/standard_padding">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/detailed_description"
android:textColor="#color/textSecondary"
android:textSize="16sp"
android:text="Descriere: "
android:padding="#dimen/activity_horizontal_margin" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/standard_padding"
android:layout_marginLeft="#dimen/standard_padding"
android:layout_marginRight="#dimen/standard_padding">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/detailed_rooms"
android:textColor="#color/textSecondary"
android:textSize="16sp"
android:text="Număr camere: "
android:padding="#dimen/activity_horizontal_margin" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/standard_padding"
android:layout_marginLeft="#dimen/standard_padding"
android:layout_marginRight="#dimen/standard_padding">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/detailed_location"
android:textColor="#color/textSecondary"
android:textSize="16sp"
android:text="Locatie: "
android:padding="#dimen/activity_horizontal_margin" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/standard_padding"
android:layout_marginLeft="#dimen/standard_padding"
android:layout_marginRight="#dimen/standard_padding">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/detailed_price"
android:textColor="#color/textSecondary"
android:textSize="16sp"
android:text="Pret: "
android:padding="#dimen/activity_horizontal_margin" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/standard_padding"
android:layout_marginLeft="#dimen/standard_padding"
android:layout_marginRight="#dimen/standard_padding"
android:layout_marginBottom="#dimen/standard_padding">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/detailed_discount"
android:textColor="#color/textSecondary"
android:textSize="16sp"
android:text="Discount studenti"
android:padding="#dimen/activity_horizontal_margin" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/standard_padding"
android:layout_marginRight="#dimen/standard_padding"
android:layout_marginBottom="#dimen/standard_padding">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/detailed_seller"
android:textSize="16sp"
android:text="#string/open_rent_seller"
android:padding="#dimen/activity_horizontal_margin" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/detailed_contact_user_email"
android:background="#color/full_alpha"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:text="#string/open_rent_email"
android:textStyle="bold"
android:textColor="#color/textSecondary"
android:layout_weight="1">
</Button>
<Button
android:id="#+id/detailed_contact_user_phone"
android:background="#color/full_alpha"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:text="#string/open_rent_phone"
android:textStyle="bold"
android:textColor="#color/textSecondary"
android:layout_weight="1">
</Button>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_margin="#dimen/activity_horizontal_margin"
android:id="#+id/rent_detailed_fab"
app:fabSize="normal"
android:src="#drawable/ic_favorite"
android:layout_gravity="bottom|end" />
</android.support.design.widget.CoordinatorLayout>
</FrameLayout>
when make edit to array list by popup windows or remove item by long click
it just update and remove last value
arraylist and adapter and listview not updated together
this is my code:
CustomListViewAapter.java
package com.zoom.plumbingcalculation;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
public class CustomListViewAdapter extends BaseAdapter{
public static final String colFrom="From";
public static final String colTo="To";
public static final String colCFM="CFM";
public static final String colDia="Dia";
public static final String colLength="Length";
public static final String colTotLoss="Loss";
public static final String colDP="DP";
public ArrayList<HashMap<String, String>> list;
Activity activity;
TextView txtFrom;
TextView txtTo;
TextView txtCFM;
TextView txtDia;
TextView txtLength;
TextView txtTotLoss;
TextView txtDP;
public CustomListViewAdapter(Activity activity, ArrayList<HashMap<String, String>> list){
super();
this.activity=activity;
this.list=list;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=activity.getLayoutInflater();
if(convertView == null){
convertView=inflater.inflate(R.layout.custom_list_view, null);
txtFrom=(TextView) convertView.findViewById(R.id.From);
txtTo=(TextView) convertView.findViewById(R.id.To);
txtCFM=(TextView) convertView.findViewById(R.id.CFM);
txtDia=(TextView) convertView.findViewById(R.id.Dia);
txtLength=(TextView) convertView.findViewById(R.id.Length);
txtTotLoss=(TextView) convertView.findViewById(R.id.TotLoss);
txtDP=(TextView) convertView.findViewById(R.id.DP);
}
HashMap<String, String> map=list.get(position);
txtFrom.setText(map.get(colFrom));
txtTo.setText(map.get(colTo));
txtCFM.setText(map.get(colCFM));
txtDia.setText(map.get(colDia));
txtLength.setText(map.get(colLength));
txtTotLoss.setText(map.get(colTotLoss));
txtDP.setText(map.get(colDP));
return convertView;
}
}
Activity.java
package com.zoom.plumbingcalculation;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import static com.zoom.plumbingcalculation.CustomListViewAdapter.colCFM;
import static com.zoom.plumbingcalculation.CustomListViewAdapter.colDP;
import static com.zoom.plumbingcalculation.CustomListViewAdapter.colDia;
import static com.zoom.plumbingcalculation.CustomListViewAdapter.colFrom;
import static com.zoom.plumbingcalculation.CustomListViewAdapter.colLength;
import static com.zoom.plumbingcalculation.CustomListViewAdapter.colTo;
import static com.zoom.plumbingcalculation.CustomListViewAdapter.colTotLoss;
public class ps_ss_compressed_air extends Activity {
//Compressed Air Variable
private ImageButton CAButAdd;
private ListView CALvDataTable;
private ArrayList<HashMap<String, String>> CALvDataTableListItems;
private CustomListViewAdapter CALvDataTableAdapter;
//Compressed Air Popup Add Variable
private LayoutInflater layoutAddInflater;
private View popupAddView;
private PopupWindow popupAddWindow;
private Button CAPAButAdd;
private Button CAPAButCancel;
private Spinner CAPASpDia;
private EditText CAPATxtFrom;
private EditText CAPATxtTo;
private EditText CAPATxtCFM;
private EditText CAPATxtLength;
//Compressed Air Popup Edit Variable
private LayoutInflater layoutEditInflater;
private View popupEditView;
private PopupWindow popupEditWindow;
private Button CAPEButEdit;
private Button CAPEButCancel;
private Spinner CAPESpDia;
private EditText CAPETxtFrom;
private EditText CAPETxtTo;
private EditText CAPETxtCFM;
private EditText CAPETxtLength;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ps_ss_compressed_air);
//Compressed Air Variable
CAButAdd = (ImageButton) findViewById(R.id.CAButAdd);
CALvDataTable =(ListView) findViewById(R.id.CALvDataTable);
CALvDataTableListItems= new ArrayList<HashMap<String,String>>();
CALvDataTableAdapter=new CustomListViewAdapter(this, CALvDataTableListItems);
CALvDataTable.setAdapter(CALvDataTableAdapter);
CAButAdd.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
layoutAddInflater = (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
popupAddView = layoutAddInflater.inflate(R.layout.ps_ss_compressed_air_popup_add, null);
//popupView.setAlpha(0.5F);
popupAddView.setAlpha(1);
popupAddWindow = new PopupWindow(
popupAddView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
CAPAButAdd = (Button)popupAddView.findViewById(R.id.CAPAButAdd);
CAPAButCancel = (Button)popupAddView.findViewById(R.id.CAPAButCancel);
CAPASpDia = (Spinner) popupAddView.findViewById(R.id.CAPASpDia);
CAPATxtFrom = (EditText) popupAddView.findViewById(R.id.CAPATxtFrom);
CAPATxtTo = (EditText) popupAddView.findViewById(R.id.CAPATxtTo);
CAPATxtCFM = (EditText) popupAddView.findViewById(R.id.CAPATxtCFM);
CAPATxtLength = (EditText) popupAddView.findViewById(R.id.CAPATxtLength);
CAPAButAdd.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
//Add--------------------------------------------------------------------------------------------
HashMap<String,String> temp = new HashMap<>();
temp.put(colFrom, CAPATxtFrom.getText().toString());
temp.put(colTo, CAPATxtTo.getText().toString());
temp.put(colCFM, CAPATxtCFM.getText().toString());
temp.put(colDia, CAPASpDia.getSelectedItem().toString());
temp.put(colLength, CAPATxtLength.getText().toString());
temp.put(colTotLoss, "");
temp.put(colDP, "");
CALvDataTableListItems.add(temp);
CALvDataTableAdapter.notifyDataSetChanged();
//-----------------------------------------------------------------------------------------------
popupAddWindow.dismiss();
}});
CAPAButCancel.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
popupAddWindow.dismiss();
}});
//popupWindow.showAsDropDown(CAButAdd, 50, 50);
popupAddWindow.showAtLocation(popupAddView, Gravity.CENTER,0,0);
popupAddWindow.setFocusable(true);
popupAddWindow.update();
}});
CALvDataTable.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id)
{
layoutEditInflater = (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
popupEditView = layoutEditInflater.inflate(R.layout.ps_ss_compressed_air_popup_edit, null);
popupEditView.setAlpha(1);
popupEditWindow = new PopupWindow(
popupEditView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
CAPEButEdit = (Button)popupEditView.findViewById(R.id.CAPEButEdit);
CAPEButCancel = (Button)popupEditView.findViewById(R.id.CAPEButCancel);
CAPESpDia = (Spinner) popupEditView.findViewById(R.id.CAPESpDia);
CAPETxtFrom = (EditText) popupEditView.findViewById(R.id.CAPETxtFrom);
CAPETxtTo = (EditText) popupEditView.findViewById(R.id.CAPETxtTo);
CAPETxtCFM = (EditText) popupEditView.findViewById(R.id.CAPETxtCFM);
CAPETxtLength = (EditText) popupEditView.findViewById(R.id.CAPETxtLength);
HashMap<String, String> temp=CALvDataTableListItems.get(position);
CAPETxtFrom.setText(temp.get(colFrom));
CAPETxtTo.setText(temp.get(colTo));
CAPETxtCFM.setText(temp.get(colCFM));
CAPESpDia.setSelection(((ArrayAdapter<String>)CAPESpDia.getAdapter()).getPosition(temp.get(colDia)));
CAPETxtLength.setText(temp.get(colLength));
CAPEButEdit.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
//Edit-------------------------------------------------------------------------------------------
HashMap<String,String> temp= CALvDataTableListItems.get(position);
temp.put(colFrom, CAPETxtFrom.getText().toString());
temp.put(colTo, CAPETxtTo.getText().toString());
temp.put(colCFM, CAPETxtCFM.getText().toString());
temp.put(colDia, CAPESpDia.getSelectedItem().toString());
temp.put(colLength, CAPETxtLength.getText().toString());
temp.put(colTotLoss, "");
temp.put(colDP, "");
CALvDataTableListItems.set(position,temp);
CALvDataTableAdapter.notifyDataSetChanged();
//-----------------------------------------------------------------------------------------------
popupEditWindow.dismiss();
}});
CAPEButCancel.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
popupEditWindow.dismiss();
}});
//popupWindow.showAsDropDown(CAButEdit, 50, 50);
popupEditWindow.showAtLocation(popupEditView, Gravity.CENTER,0,0);
popupEditWindow.setFocusable(true);
popupEditWindow.update();
//Toast.makeText(ps_ss_compressed_air.this, Integer.toString(position+1) + " Clicked", Toast.LENGTH_SHORT).show();
}
});
CALvDataTable.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
{
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
CALvDataTableListItems.remove(position);
CALvDataTableAdapter.notifyDataSetChanged();
Toast.makeText(ps_ss_compressed_air.this,"Removed", Toast.LENGTH_SHORT).show();
return true;
}
});
}
}
custom_list_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:orientation="horizontal"
android:divider="#000000"
android:weightSum="7">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/From"
android:layout_weight="1"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/To"
android:layout_weight="1"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/CFM"
android:layout_weight="1" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/Dia"
android:layout_weight="1"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/Length"
android:layout_weight="1"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/TotLoss"
android:layout_weight="1"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/DP"
android:layout_weight="1"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000" />
</LinearLayout>
Activity.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.AppBarLayout
android:id="#+id/CAappbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/appbar_padding_top"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/CAtoolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:logo="#mipmap/ic_launcher"
app:title="#string/app_name">
</android.support.v7.widget.Toolbar>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:paddingStart="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="14sp"
android:text="#string/Compressed_Air_Name"
android:id="#+id/CALabCAName"
android:layout_weight="7"/>
<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/CAButAdd"
android:layout_weight="1"
android:background="#drawable/ps_ss_compressed_air_but_add"/>
</LinearLayout>
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_below="#+id/CAappbar"
android:id="#+id/linearLayoutCAHeader">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:divider="#000000">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/Nodes_Lab"
android:id="#+id/CALabNodes"
android:textStyle="bold"
android:layout_weight="1"/>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#000000" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_gravity="center"
android:weightSum="2"
android:layout_weight="1">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/Nodes_From_Lab"
android:id="#+id/CALabFrom"
android:textStyle="bold"
android:layout_weight="1"/>
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/Nodes_To_Lab"
android:id="#+id/CALabTo"
android:textStyle="bold"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/CFM_Lab"
android:id="#+id/CALabCFM"
android:textStyle="bold"
android:layout_weight="1"/>
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/Diameter_Lab"
android:id="#+id/CALabDia"
android:textStyle="bold"
android:layout_weight="1"/>
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/Length_Lab"
android:id="#+id/CALabLength"
android:textStyle="bold"
android:layout_weight="1"/>
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/Total_Loss_Lab"
android:gravity="center"
android:id="#+id/CALabTotLoss"
android:textStyle="bold"
android:layout_weight="1"/>
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/Pressure_Drop_Lab"
android:id="#+id/CALabDP"
android:textStyle="bold"
android:layout_weight="1"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#000000" />
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/linearLayoutCAHeader"
android:id="#+id/CALvDataTable"
android:longClickable="true"/>
</RelativeLayout>
I am trying to add a RecyclerView with GridLayoutManagerto my app. Everything works as expected, only problem is it seems the columns are left justified instead of center justified. Any ideas? Thanks in advance!
Here is an image showing how it looks:
Here is the single 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="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="?android:selectableItemBackgroundBorderless"
android:clickable="true"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:id="#+id/iconSpot"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center_horizontal"
android:src="#drawable/ic_launcher"
tools:ignore="MissingPrefix" />
<TextView
android:id="#+id/textLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Dummy Text"
android:textSize="16sp" />
</LinearLayout>
And here is the full activity 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"
android:id="#+id/totalScreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignTop="#+id/fab"
android:layout_gravity="bottom|center"
android:gravity="bottom|center_vertical">
<TextView
android:id="#+id/sheetTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#424242"
android:padding="12dp"
android:text="Dummy Title"
android:textSize="18sp" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignTop="#+id/sheetTitle"
android:layout_marginEnd="20dp"
android:layout_marginTop="-28dp"
android:visibility="gone"
app:fabSize="normal" />
<android.support.v7.widget.RecyclerView
android:id="#+id/gridScreen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/sheetTitle"
android:background="#424242"
android:gravity="bottom|center"
android:layout_gravity = "center"
android:orientation="vertical"
android:paddingBottom="24dp" />
</RelativeLayout>
Ok! I figured it out, it ended up being the fact that I needed to make the item layout width set as match_parent instead of wrap_content. So that makes it look like this:
<?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_gravity="center_horizontal"
android:layout_margin="10dp"
android:background="?android:selectableItemBackgroundBorderless"
android:clickable="true"
android:orientation="vertical">
<ImageView
android:id="#+id/iconSpot"
android:layout_width="44dp"
android:layout_height="44dp"
android:layout_gravity="center_horizontal"
android:src="#drawable/ic_launcher"
tools:ignore="MissingPrefix" />
<TextView
android:id="#+id/textLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Dummy Text"
android:textSize="16sp" />
</LinearLayout>
It is hard to fix with just the layout file since the item layout needs to be inflated and attached to the main layout in order to see the result.
So I decided to recreate it. I made modification to your layout file.
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="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#color/colorPrimary"
android:layout_margin="2dp"
android:clickable="true"
android:orientation="vertical">
<ImageView
android:id="#+id/iconSpot"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center_horizontal"
android:src="#android:drawable/ic_delete"
tools:ignore="MissingPrefix" />
<TextView
android:id="#+id/textLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Dummy Text"
android:textSize="16sp" />
</LinearLayout>
Main activity 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"
android:id="#+id/totalScreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignTop="#+id/fab"
android:layout_gravity="bottom|center"
android:gravity="bottom|center_vertical">
<TextView
android:id="#+id/sheetTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#424242"
android:padding="12dp"
android:text="Dummy Title"
android:textSize="18sp" />
<android.support.v7.widget.RecyclerView
android:id="#+id/gridScreen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/sheetTitle"
android:background="#424242"
android:gravity="bottom|center"
android:layout_gravity = "center"
android:clipToPadding="false"
android:orientation="vertical"
android:paddingBottom="24dp" />
</RelativeLayout>
Main Activity class. This class (GridSpacingItemDecoration) used for item margin is copied from here
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private GridLayoutManager layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = new GridLayoutManager(MainActivity.this, 2);
RecyclerView rView = (RecyclerView)findViewById(R.id.gridScreen);
rView.setHasFixedSize(true);
rView.setLayoutManager(layout);
int spanCount = 2;
int spacing = 50;
boolean includeEdge = true;
rView.addItemDecoration(new GridSpacingItemDecoration(spanCount, spacing, includeEdge));
RecyclerViewAdapter rcAdapter = new RecyclerViewAdapter(MainActivity.this, getAllItemList());
rView.setAdapter(rcAdapter);
}
private List<ItemObject> getAllItemList(){
List<ItemObject> allItems = new ArrayList<ItemObject>();
allItems.add(new ItemObject(android.R.drawable.ic_delete, "United"));
allItems.add(new ItemObject(android.R.drawable.ic_delete, "United"));
allItems.add(new ItemObject(android.R.drawable.ic_delete, "United"));
allItems.add(new ItemObject(android.R.drawable.ic_delete, "United"));
allItems.add(new ItemObject(android.R.drawable.ic_delete, "United"));
allItems.add(new ItemObject(android.R.drawable.ic_delete, "United"));
return allItems;
}
}
RecycleView Adapter Class
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolders> {
private List<ItemObject> itemList;
private Context context;
public RecyclerViewAdapter(Context context, List<ItemObject> itemList) {
this.itemList = itemList;
this.context = context;
}
#Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list, null);
RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView);
return rcv;
}
#Override
public void onBindViewHolder(RecyclerViewHolders holder, int position) {
holder.displayedImage.setImageResource(itemList.get(position).getImage());
holder.textTitle.setText(itemList.get(position).getTitle());
}
#Override
public int getItemCount() {
return this.itemList.size();
}
}
ViewHolder class
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class RecyclerViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener{
public ImageView displayedImage;
public TextView textTitle;
public RecyclerViewHolders(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
displayedImage = (ImageView)itemView.findViewById(R.id.iconSpot);
textTitle = (TextView)itemView.findViewById(R.id.textLabel);
}
#Override
public void onClick(View view) {
}
}
ObjectEntity class
public class ItemObject {
private int image;
private String title;
public ItemObject(int image, String title) {
this.image = image;
this.title = title;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
The Result is below. Try and see if it works for you.