Reputation: 53
Hello I want to make a list of cardView based on how many strings i have in the InfoGenBibliotheque class. I don't really know how to create a list of cardViews based on items count. If someone could be of help I would appreciate it. I have read that i have to create a cardview layout file to create a list from it. //edited so I managed to make it using a listView and BaseAdapter. But when I execute the application the margins are no where to be found.
InfoGenBibliotheque class:
public class InfoGenBibliotheque {
private String SalutaionsAnglais [] = {
"Certainly!",
"Good afternoon.",
"Good evening sir.",
"Good Luck.",
"Good morning."
};
private String SalutaionsFrancais [] = {
"Certainement!",
"Bon après-midi.",
"Bonsoir Monsieur.",
"Bonne chance.",
"Bonjour."
};
}
my XML activity_info_generales file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".ui.InfoGeneralesActivity">
<ListView
android:id="@+id/listView"
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"
app:layout_constraintTop_toTopOf="parent"
android:layout_margin="10dp">
</ListView>
</android.support.constraint.ConstraintLayout>
my cardView xml file
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
android:id="@+id/expressionCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:gravity="center"
app:cardPreventCornerOverlap="false"
app:cardCornerRadius="8dp"
android:padding="-4dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:weightSum="2"
android:orientation="vertical"
android:gravity="center"
android:padding="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/colorPrimaryDark">
<TextView
android:id="@+id/expressionFrancais"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/colorAccent"
android:text="Najb Mohammed"
android:textSize="24dp"
android:textAlignment="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/backgroundColor">
<TextView
android:id="@+id/expressionAnglais"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Najb mohammed"
android:textSize="24dp"
android:textAlignment="center"/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
Adapter java file:
public class ExpressionAdapter extends BaseAdapter {
private Context context;
private ArrayList<Expression> expressions;
public ExpressionAdapter(Context context, ArrayList<Expression> expressions) {
this.context = context;
this.expressions = expressions;
}
@Override
public int getCount() {
return expressions.size();
}
@Override
public Object getItem(int position) {
return expressions.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = View.inflate(context, R.layout.expressions_card_view,null);
}
TextView expressionFrancais = convertView.findViewById(R.id.expressionFrancais);
TextView expressionAnglais = convertView.findViewById(R.id.expressionAnglais);
Expression expression = expressions.get(position);
expressionFrancais.setText(expression.getExpressionFrancais());
expressionAnglais.setText(expression.getExpressionAnglais());
return convertView;
}
}
populating a list of expressions:
public class ListExpression {
public static ArrayList<Expression> getExpressionList(){
ArrayList<Expression> expressionList = new ArrayList<>();
InfoGenBibliotheque infoGenBibliotheque = new InfoGenBibliotheque();
for(int i = 0;i < infoGenBibliotheque.getSalutaionsAnglais().length;i++){
expressionList.add(new Expression(i,infoGenBibliotheque.getSalutationFrancais(i),infoGenBibliotheque.getSalutationAnglais(i)));
}
return expressionList;
}
}
Expression class
public class Expression {
private int idExpression;
private String expressionFrancais;
private String expressionAnglais;
public Expression(int idExpression, String expressionFrancais, String expressionAnglais) {
this.idExpression = idExpression;
this.expressionFrancais = expressionFrancais;
this.expressionAnglais = expressionAnglais;
}
public int getIdExpression() {
return idExpression;
}
public void setIdExpression(int idExpression) {
this.idExpression = idExpression;
}
public String getExpressionFrancais() {
return expressionFrancais;
}
public void setExpressionFrancais(String expressionFrancais) {
this.expressionFrancais = expressionFrancais;
}
public String getExpressionAnglais() {
return expressionAnglais;
}
public void setExpressionAnglais(String expressionAnglais) {
this.expressionAnglais = expressionAnglais;
}
}
Upvotes: 2
Views: 1621
Reputation: 13019
Just using the CardView
as root element makes the margins disappear:
Putting the CardView
inside a FrameLayout
gives you the margins:
(But I personally don't think that the horizontal divider lines look good with the cards. If you want them to be invisible, take a look at this post or use a RecyclerView
instead of a ListView
. )
xml for the second approach:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="@+id/expressionCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
app:cardCornerRadius="8dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:weightSum="2"
android:orientation="vertical"
android:gravity="center"
android:padding="4dp">
<!-- skipped inner Views here -->
</LinearLayout>
</android.support.v7.widget.CardView>
</FrameLayout>
Upvotes: 1