I have a problem with item height and gravity in a horizontal recyclerview as you can see in the picture: RecyclerView item height and gravity not working
How to set the item height to same height as recyclerciew and the gravity at the bottom of recyclerview ?
Thank you for your help.
My code for Activity_Drill_Graph.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Drill_Graph_Activity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/toolbar_drill_graph"
layout="#layout/toolbar_drill_graph" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/margin_5dp"
android:background="#color/grey"
android:gravity="bottom"
android:orientation="horizontal"
android:weightSum="100">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:background="#color/lightgrey"
android:orientation="vertical"
android:padding="#dimen/padding_5dp">
<TextView
android:id="#+id/textView10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_10"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_9"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_8"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_7"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_6"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_5"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_4"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_3"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_2"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_1"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_0"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textViewDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/date"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="18sp" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerViewDrillGraph"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:layout_margin="#dimen/margin_5dp"
android:background="#color/white"
android:orientation="horizontal"
android:padding="#dimen/padding_5dp"
android:scrollbars="horizontal" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
item code: drill_graph_single_column.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"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:background="#color/lightgrey"
android:gravity="bottom"
android:orientation="vertical"
android:padding="#dimen/padding_1dp"
android:weightSum="110">
<Button
android:id="#+id/btnSkill"
style="#style/btnStyleScoreGrey"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="50"
android:text="#string/skill"
android:textAllCaps="false"
android:textSize="16sp" />
<Button
android:id="#+id/btnDate"
style="#style/btnStyleDrillGraphGrey"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="#dimen/margin_1dp"
android:layout_weight="10"
android:text="#string/date"
android:textAllCaps="false"
android:textSize="12sp" />
</LinearLayout>
My code for RecyclerView Adapter:
package org.poolshot.poolshotacademy;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import java.util.List;
public class My_DrillGraph_RecyclerView_Adapter extends RecyclerView.Adapter<My_DrillGraph_RecyclerView_Adapter.MyHolder> {
SharedPreferences defaultPreferencesSettings;
private Context myRCAContext;
private List<String> myDrillGraphDates;
private List<Integer> myDrillGraphSkill;
// constructor
public My_DrillGraph_RecyclerView_Adapter(Context applicationContext, List<String> mDrillGraphDates, List<Integer> mDrillGraphSkill) {
this.myRCAContext = applicationContext;
this.myDrillGraphDates = mDrillGraphDates;
this.myDrillGraphSkill = mDrillGraphSkill;
}
#NonNull
#Override
public MyHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int position) {
View layout = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.drill_graph_single_column,null);
MyHolder myHolder = new MyHolder(layout);
return myHolder;
}
#Override
public void onBindViewHolder(#NonNull final MyHolder viewHolder, final int position) {
String skill = myDrillGraphSkill.get(position).toString();
// set color for buttons
viewHolder.btnDate.setBackground(myRCAContext.getResources().getDrawable(R.drawable.custom_btn_grey));
viewHolder.btnSkill.setTextColor(myRCAContext.getResources().getColor(R.color.white));
// set text for buttons
viewHolder.btnDate.setText(myDrillGraphDates.get(position));
viewHolder.btnSkill.setText(skill);
}
#Override
public int getItemCount() {
return myDrillGraphDates.size();
}
public static class MyHolder extends RecyclerView.ViewHolder {
Button btnDate;
Button btnSkill;
public MyHolder(View itemView) {
super(itemView);
btnDate = itemView.findViewById(R.id.btnDate);
btnSkill = itemView.findViewById(R.id.btnSkill);
}
}
}
This is my solution :
I changed the code of the class MyHolder as :
public static class MyHolder extends RecyclerView.ViewHolder {
Button btnDate;
Button btnSkill;
public MyHolder(View itemView) {
super(itemView);
btnDate = itemView.findViewById(R.id.btnDate);
btnSkill = itemView.findViewById(R.id.btnSkill);
LinearLayout.LayoutParams layoutParams =
new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.gravity = Gravity.BOTTOM;
itemView.setLayoutParams(layoutParams);
}
}
Please see below screenshot for different results on tablet and on phones.
activity.xml
<RelativeLayout 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=".MainActivity"
android:id="#+id/frameLayout1"
android:weightSum="4"
android:padding="0dp"
android:orientation="vertical">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop"
android:src="#drawable/crossword_background"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
android:orientation="vertical"
android:id="#+id/frameLayout"
android:weightSum="4"
>
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#android:color/transparent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tvTitle"
style="#style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_width="100dp"
android:layout_height="24dp"
android:layout_marginStart="4dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="4dp"
android:layout_marginBottom="4dp"
android:background="#drawable/count_frame"
android:fontFamily="#font/roboto"
android:gravity="center"
android:textColor="#5A0FC8"
android:textSize="13sp"
android:textStyle="bold"
tools:text="4" />
<TextView
android:id="#+id/menu_item_score"
style="#style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_width="100dp"
android:layout_height="24dp"
android:layout_margin="4dp"
android:background="#drawable/count_frame"
android:gravity="right"
android:layout_alignParentRight="true"
android:textColor="#5A0FC8"
android:textSize="13sp"
android:textStyle="bold"
android:textAlignment="center"
tools:text="4" />
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
<LinearLayout
android:id="#+id/rightLayout"
android:layout_width="40dp"
android:layout_height="fill_parent"
android:layout_alignParentEnd="true"
android:layout_below="#id/horizontal_divider"
android:orientation="vertical" >
<LinearLayout
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="match_parent"
android:weightSum="4"
android:orientation="vertical" >
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_action_360"
android:background="#null"/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_action_360"
android:background="#null"/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_action_360"
android:background="#null"/>
<ImageButton
android:id="#+id/ShowHint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_action_360"
android:background="#null"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/leftLayout"
android:layout_width="40dp"
android:layout_height="fill_parent"
android:layout_alignParentStart="true"
android:layout_below="#id/horizontal_divider"
android:orientation="vertical" >
<LinearLayout
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="match_parent"
android:weightSum="4"
android:orientation="vertical" >
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_action_360"
android:background="#null"/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_action_360"
android:background="#null"/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_action_360"
android:background="#null"/>
<ImageButton
android:id="#+id/ShuffleButtons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_action_360"
android:background="#null"/>
</LinearLayout>
</LinearLayout>
<View
android:id="#+id/horizontal_divider"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_centerVertical="true" />
<RelativeLayout
android:id="#+id/firstLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
android:layout_centerInParent="true"
android:layout_margin="0dp"
android:layout_above="#+id/horizontal_divider"
android:layout_below="#id/toolbar"
android:gravity="center">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tblLayout"
android:gravity="center"
android:padding="0dip"
android:shrinkColumns="*"
android:layout_margin="0dip"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true">
</TableLayout>
</RelativeLayout >
<LinearLayout
android:id="#+id/secondLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/horizontal_divider"
android:layout_toStartOf="#id/rightLayout"
android:layout_toEndOf="#id/leftLayout"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
>
</FrameLayout>
</LinearLayout>
</RelativeLayout>
background_ic_btn_bonus.xml
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/ic_btn_bonus"
android:tileMode="disabled" android:gravity="center" >
</bitmap>
background_ic_btn_default.xml
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/ic_btn_default"
android:tileMode="disabled" android:gravity="center" >
</bitmap>
Corresponding Java Code
String[] iLayoutMap = getTableLayout();
/*
iLayoutMap contains split rows removing 2
2,0,0,0,0,0,0,2,0,0,0,0,0,0,2,0,0,1,1,1,0,2,0,0,0,1,0,0,2,0,0,0,0,0,0,2,0,0,0,0,0,0*/
TableLayout tableLayout = (TableLayout) findViewById(R.id.tblLayout);
for(int i=1;i<iLayoutMap.length;i++){
TableRow NewRow1 =new TableRow(this);
NewRow1.setPadding(0, 0, 0, 0);
NewRow1.setGravity(Gravity.CENTER);
NewRow1.setHorizontalGravity(Gravity.CENTER);
NewRow1.setVerticalGravity(Gravity.CENTER);
String [] items = iLayoutMap[i].split("\\s*,\\s*");
NewRow1.setWeightSum(items.length-1);
for(int j = 0; j < items.length;j++) {
if(items[j].equalsIgnoreCase("0")){
Button btnAdd = new Button(context);
btnAdd.setGravity(Gravity.CENTER);
btnAdd.setTextSize(25);
btnAdd.setTextColor(Color.WHITE);
//btnAdd.setPadding(10 ,10,10,10);
//btnAdd.setBackgroundResource(R.drawable.ic_btn_default);
btnAdd.setBackgroundResource(R.drawable.background_ic_btn_default);
btnAdd.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT ,1));
NewRow1.addView(btnAdd);
}else if(items[j].equalsIgnoreCase("1")){
//https://stackoverflow.com/questions/3404582/adding-text-to-imageview-in-android
Button btnAdd = new Button(context);
String strId = Integer.toString(i) + Integer.toString(j-1);
//btnAdd.setWidth(iObjectWidth);
//btnAdd.setHeight(iObjectHeight);
btnAdd.setId(Integer.valueOf(strId));
btnAdd.setGravity(Gravity.CENTER);
btnAdd.setTextSize(25);
btnAdd.setTextColor(Color.WHITE);
//btnAdd.setPadding(10 ,10,10,10);
//btnAdd.setBackgroundResource(R.drawable.ic_btn_default);
btnAdd.setBackgroundResource(R.drawable.background_ic_btn_bonus);
btnAdd.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT,1 ));
NewRow1.addView(btnAdd);
}
}
NewRow1.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT,items.length-1));
tableLayout.addView(NewRow1);
}
Function
public String[] getTableLayout() {
if (lvlinfo != null) {
return lvlinfo.getLayout().split("2");
}
//Return this as default in case of failure
String arr = "2,0,0,0,0,0,0,2,0,0,0,0,0,0,2,0,0,1,1,1,0,2,0,0,0,1,0,0,2,0,0,0,0,0,0,2,0,0,0,0,0,0";
//String arr = "2,0,0,0,0,0,0,2,0,0,0,0,0,0,2,0,0,1,1,1,0,2,0,0,0,1,0,0,2,0,0,0,0,0,0,2,0,0,0,0,0,0";
return arr.split("2");
}
Expected Result ( Working correctly on Tablet with Android 4.4.2 with below code)
Actual Results ( Tested on multiple phones with OS >= 8.1.0)
I Have tried your code but unable to reproduce this output
For me your code is working fine check the output
Result in Oreo
Result in android pie
Result in android Q
as per my opinion you should use
You should use RecyclerView with GridLayoutManager
Follow this sample code
Add one recyclerview in your activity layout xml file
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/firstLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
android:gravity="center">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="wrap_content"
android:id="#+id/gridRecyclerView"
android:layout_centerInParent="true"
android:layout_height="wrap_content"/>
</RelativeLayout>
Now inside your activity set GridLayoutManager to your RecyclerView
GridLayoutManager (Context context,
int spanCount)
Creates a vertical GridLayoutManager
Parameters
context Context: Current context, will be used to access resources.
spanCount int: The number of columns in the grid
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
public class JavaActivity extends AppCompatActivity {
RecyclerView myRecyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_java);
myRecyclerView = findViewById(R.id.gridRecyclerView);
GridLayoutManager gridLayoutManager = new GridLayoutManager(JavaActivity.this, 6);
myRecyclerView.setLayoutManager(gridLayoutManager);
myRecyclerView.setAdapter(new MyAdapter(this));
}
}
Create one adapter class RecyclerView like this
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private Context context;
public MyAdapter(Context context) {
this.context = context;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.row_list_item, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
if (position%2==0){
holder.imgBanner.setBackgroundColor(Color.BLUE);
}else {
holder.imgBanner.setBackgroundColor(Color.GREEN);
holder.imgBanner.setImageResource(R.drawable.red_heart);
}
}
#Override
public int getItemCount() {
return 36;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
ImageView imgBanner;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
imgBanner = itemView.findViewById(R.id.imgBanner);
}
}
}
Create one layout file for RecyclerView item
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="2dp"
android:orientation="vertical">
<ImageView
android:id="#+id/imgBanner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/app_name"
/>
</RelativeLayout>
Try with a padding like this:
<RelativeLayout
android:id="#+id/firstLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="someDp" ->>>> Padding
android:paddingRight="someDp" ->>>> Padding
android:layout_above="#+id/horizontal_divider"
android:layout_below="#id/toolbar"
android:gravity="center">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tblLayout"
android:gravity="center"
android:shrinkColumns="*"
android:padding="0dip"
android:layout_margin="0dip"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"></TableLayout>
</RelativeLayout >
I have edit button on toolbar on click this button i want to show new view that is cross button inside every row of listview. is this possible and if yes how ? Thanks in advance. Below is layout files:
main layout:
<?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"
android:orientation="vertical"
android:background="#color/backgroundColor"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:background="#color/TabColor"
android:weightSum="10"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_weight="1"
android:onClick="back2"
android:layout_gravity="center"
app:srcCompat="#drawable/back_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_weight="7"
android:paddingLeft="#dimen/margin_small"
android:text="List OF Hotels"
android:textSize="#dimen/text_title"
android:textColor="#color/WhiteColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<FrameLayout
android:layout_weight="2"
android:background="#drawable/transparent_bg"
android:layout_marginRight="#dimen/margin_large"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/edit_jobs"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_marginBottom="#dimen/margin_small"
android:layout_marginTop="#dimen/margin_small"
android:text="Edit"
android:textColor="#color/WhiteColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:visibility="invisible"
android:id="#+id/done_editing"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_marginBottom="#dimen/margin_small"
android:layout_marginTop="#dimen/margin_small"
android:text="Done"
android:textColor="#color/WhiteColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
<ListView
android:id="#+id/listview1"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
listview row layout where i have to show cross image:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_marginTop="#dimen/margin_large"
android:layout_marginLeft="#dimen/margin_large"
android:layout_marginRight="#dimen/margin_large"
android:background="#drawable/border"
android:padding="#dimen/margin_large"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:weightSum="10"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text=""
android:layout_gravity="center"
android:layout_weight="9"
android:textColor="#color/TabColor"
android:singleLine="true"
android:textSize="#dimen/text_large"
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<FrameLayout
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:gravity="right"
android:id="#+id/arrow"
android:src="#drawable/rightarrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:visibility="invisible"
android:id="#+id/cross"
android:gravity="right"
android:src="#drawable/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:weightSum="10"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text=""
android:layout_weight="5"
android:textColor="#color/blackColor"
android:textSize="#dimen/text_medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_weight="5"
android:text=""
android:gravity="right"
android:textColor="#color/blackColor"
android:textSize="#dimen/text_medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Adapter class:
public class MyAdapter extends ArrayAdapter<String> {
private final Context context;
private ArrayList<String> titlelist,datalist;
public MyAdapter(Context context, ArrayList<String> title, ArrayList<String> data) {
super(context, R.layout.listview_row, title);
titlelist= new ArrayList<>();
datalist= new ArrayList<>();
this.context=context;
this.titlelist=title;
this.datalist=data;
}
private static class ViewHolder {
TextView t1,t2;
}
#Override
public int getCount() {
return titlelist.size();
}
#Override
public String getItem(int position) {
return titlelist.get(position);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.listview_row, null);
holder = new ViewHolder();
holder.t1 = (TextView) convertView.findViewById(R.id.title);
holder.t2 = (TextView) convertView.findViewById(R.id.data);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.t1.setText(titlelist.get(position));
holder.t2.setText(datalist.get(position));
return convertView;
}
}
you have to add this functionality in your adapter,
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//ur other codes.....
//
//
if(crossButtonPress){
holder.imageView.setImageResource(R.drawable.cross);
}else{
holder.imageView.setImageResource(R.drawable.default_ic);
}
}
In addition, in you actual button's onClick,
crossButtonPress = true;
adapter.notifyDatasetChanged();
I am currently working with an Android App using RecyclerView and CardView .I am showing lists of UserPosts(Contains text and Image both) inside RecyclerView . The UserPost item has been created using CardView .
Edit
The problem I am facing here is onCreateViewHolder method is being called only half of the times the actual count returned by getItemCount method even I scrolls till end of the list .E.g I have a list of 10 posts then onCreateViewHolder calls for 5 times only . It should call 10 times (when I scroll till end of the list) first and then created views will be recycled . But it is not happening actually . I have visited many Stackoverflow posts but no luck .
Here is code UserPostAdapter.java :
package com.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.model.UserPostsListItem;
import com.musomeet.R;
import com.squareup.picasso.Picasso;
import java.util.List;
import de.hdodenhof.circleimageview.CircleImageView;
/**
* Created by SI_Android on 10/21/2015 .
*
*/
public class UserPostAdapter extends RecyclerView.Adapter<UserPostAdapter.UserPostViewHolder> {
private List<UserPostsListItem> userPostList;
Context conetxt ;
LayoutInflater inflater ;
public UserPostAdapter(){
}
public UserPostAdapter(Context conetxt, List<UserPostsListItem> postlist) {
this.userPostList = postlist;
this.conetxt = conetxt ;
inflater = LayoutInflater.from(this.conetxt);
}
#Override
public int getItemCount() {
//int size = 0 ;
//if( userPostList != null )
//size = userPostList.size();
return userPostList.size();
}
#Override
public void onBindViewHolder(UserPostViewHolder videoViewHolder, int position) {
UserPostsListItem item = userPostList.get(position);
videoViewHolder.userName.setText(item.getName());
videoViewHolder.userLocation.setText(item.getCity()+", "+item.getCountry());
videoViewHolder.timeAgo.setText(item.getAgo());
videoViewHolder.likes.setText(item.getNo_of_likes());
videoViewHolder.comments.setText(item.getNo_of_comments());
videoViewHolder.userMessage.setText(item.getContent());
if(!TextUtils.isEmpty(item.getPhoto())) {
Picasso.with(conetxt).load(item.getPhoto()).into(videoViewHolder.circleImageView);
}
String url = item.getAttachment_url() ;
if (!TextUtils.isEmpty(url)) {
if ((url.endsWith(".png") || url.endsWith(".jpg"))) {
//videoViewHolder.attachment.setVisibility(View.VISIBLE);
Picasso.with(conetxt).load(url).into(videoViewHolder.attachment);
}
}
}
#Override
public UserPostViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = inflater.inflate(R.layout.userposts_listitem_textmessage, viewGroup, false);
return new UserPostViewHolder(itemView);
}
public static class UserPostViewHolder extends RecyclerView.ViewHolder {
TextView userName, userLocation, timeAgo, likes, comments, userMessage;
ImageView attachment;
CircleImageView circleImageView;
public UserPostViewHolder(View v){
super(v);
attachment = (ImageView) v.findViewById(R.id.attachment);
circleImageView = (CircleImageView) v.findViewById(R.id.circleView);
userMessage = (TextView) v.findViewById(R.id.massage);
userName = (TextView) v.findViewById(R.id.name);
userLocation = (TextView) v.findViewById(R.id.location);
timeAgo = (TextView) v.findViewById(R.id.time);
likes = (TextView) v.findViewById(R.id.likespoints);
comments = (TextView) v.findViewById(R.id.commentspoints);
}
}
}
Here is the userposts_listitem_textmessage.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardView="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/userpopstcardview"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
cardView:cardElevation="5dp"
cardView:cardCornerRadius="10dp"
cardView:cardBackgroundColor="#fff"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="0dp">
<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">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:src="#drawable/contact"
android:layout_margin="10dp"
android:id="#+id/circleView"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
android:layout_marginLeft="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:text="Jasmine"
android:textSize="20sp"
android:textStyle="bold"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#929292"
android:text="Los Angles, California"
android:textSize="14sp"
android:textStyle="normal"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#929292"
android:text="-"
android:textSize="14sp"
android:textStyle="normal"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#929292"
android:text="2 hours ago"
android:textSize="14sp"
android:textStyle="normal"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/userListContentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
android:orientation="vertical">
<TextView
android:id="#+id/massage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/recet_text"
android:textColor="#000"
android:textSize="15sp"
android:padding="10dp"
android:scrollbars="vertical"/>
<ImageView
android:id="#+id/attachment"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="10dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:tint="#00000000"
android:src="#drawable/contact"
android:visibility="visible"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/likespoints"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:textColor="#929292"
android:textSize="14sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Likes"
android:textColor="#929292"
android:textSize="14sp"
android:layout_marginLeft="5dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="30dp">
<TextView
android:id="#+id/commentspoints"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:textColor="#929292"
android:textSize="14sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Comments"
android:textColor="#929292"
android:textSize="14sp"
android:layout_marginLeft="5dp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_height="1dp"
android:layout_width="match_parent">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#eeeeee"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:weightSum="3">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="#+id/like"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Like"
android:gravity="center"
android:layout_gravity="center"
android:textSize="14sp"
android:textColor="#929292"
android:padding="10dp"
android:clickable="true"
android:background="#drawable/textview_effect"/>
</LinearLayout>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="1dp">
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#eeeeee"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="#+id/comment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Comment"
android:gravity="center"
android:layout_gravity="center"
android:textSize="14sp"
android:textColor="#929292"
android:padding="10dp"
android:clickable="true"
android:background="#drawable/textview_effect"/>
</LinearLayout>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="1dp">
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#eeeeee"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="#+id/share"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Share"
android:gravity="center"
android:layout_gravity="center"
android:textSize="14sp"
android:textColor="#929292"
android:padding="10dp"
android:clickable="true"
android:background="#drawable/textview_effect"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
What you see is an expected behaviour. RecylerView won't create as many view as adapter's getItemCount() returns. It create view as many as it's necessary for rendering a view.
For instance, if your adapter contains 100 elements, but only 5 are displayed at at time, system will create 5 views (it may create 1 or 2 more view as "buffer") and reuse these views when user scrolls.
When a View is recycled, it doesn't get "reset" to its original state, so it and its child Views still have the attributes that were set on them the last time the ViewHolder was passed into onBindViewHolder(). Since you are currently only setting the image when your if conditions are true, the ImageView is not updated if either of those conditions is false, and it will still have the image set on it previously. You simply need to set the ImageView's Bitmap to null when those conditions are not true. Also, your two if statements can be combined:
#Override
public void onBindViewHolder(UserPostViewHolder videoViewHolder, int position) {
UserPostsListItem item = userPostList.get(position);
...
String url = item.getAttachment_url() ;
if (!TextUtils.isEmpty(url) && (url.endsWith(".png") || url.endsWith(".jpg"))) {
Picasso.with(conetxt).load(url).into(videoViewHolder.attachment);
}
else {
videoViewHolder.attachment.setImageBitmap(null);
}
}
While designing the UI for an android app I faced the following problem. I wanted to align the item in the ListView of the drawer to the bottom. I am new to android development. Any help will be appreciated.
This is my drawer code
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E0E0E0" >
<!-- Your main screen -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:text="Location: "
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/tvLocInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Rajeev Chowk, Block A, Connaught Place"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:text="Time: "
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/tvTimeInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="9:30 PM"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.79"
android:orientation="vertical" >
<ImageView
android:id="#+id/ivMap"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:layout_weight="0.85"
android:scaleType="fitXY"
android:src="#drawable/map" />
<Button
android:id="#+id/btnPanic"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:background="#drawable/red_button"
android:text="Panic Button"
android:textColor="#FFFFFF" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:layout_weight="0.010"
android:background="#drawable/top_border" >
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="#+id/tvDeviceStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Device Status: Working"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
android:id="#+id/tvDeviceStatus1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvDeviceStatus"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:text="Battery: 91%(~11 months)"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
<!-- left drawer -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<ListView
android:id="#+id/left_drawer"
android:layout_width="380dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#E0E0E0"
android:choiceMode="singleChoice"
android:dividerHeight="1dp" />
</android.support.v4.widget.DrawerLayout>
These are the three xml file that I inflate in the ListView
1) profile_view.xml
<ImageView
android:id="#+id/ivProfile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:src="#drawable/sneha_lata" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sneha Lata"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit Profile"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="" />
<ImageView
android:id="#+id/ivForward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/forward" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
2) emergency_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="20dp"
android:paddingTop="20dp" >
<TextView
android:id="#+id/tvManage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:text="Manage Emergency Contacts"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/ivForward"
android:layout_toRightOf="#id/tvManage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/forward" />
<TextView
android:id="#+id/tvManageText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tvManage"
android:layout_alignParentLeft="true"
android:layout_alignRight="#id/tvManage"
android:layout_marginLeft="40dp"
android:fontFamily="sans-serif-light"
android:text="#string/tvManageText"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
3) logout view
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/logout_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/btnLogout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/red_button"
android:text="Log Out"
android:textColor="#FFFFFF" />
</RelativeLayout>
Even if I used android:layout_alignParentBottom="true" in my logout_view.xml still it does not align at the bottom in the drawer but it does in logout_view.xml
This is the code for the DrawerAdapter
package com.majhe.android.adapter;
import com.majhe.android.LoginActivity;
import com.majhe.android.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebView.FindListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
public class DrawerAdapter extends ArrayAdapter<String> implements OnClickListener {
Context context;
Button btnLogout;
public DrawerAdapter(Context context) {
super(context, android.R.layout.simple_list_item_1, new String[] {
"Profile View", "Emergency View", "Logout View" });
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
switch (position) {
case 0:
convertView = inflater.inflate(R.layout.profile_view, null, true);
return convertView;
case 1:
convertView = inflater.inflate(R.layout.emergency_view, null,true);
return convertView;
case 2:
convertView = inflater.inflate(R.layout.logout_view, null, true);
btnLogout=(Button)convertView.findViewById(R.id.btnLogout);
btnLogout.setOnClickListener(this);
return convertView;
}
return super.getView(position, convertView, parent);
}
#Override
public void onClick(View v) {
context.startActivity(new Intent(context,LoginActivity.class));
((Activity)context).finish();
}
}
I think you dont need to use ListView at all if I am right? replace your listview with something like this might help
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#fff"
android:orientation="vertical"
android:paddingEnd="10dp" android:gravity="bottom">
<include layout="#layout/profile_view"/>
<include layout="#layout/emergency_view"/>
<include layout="#layout/logout_view"/>
</LinearLayout>
set layout_height 0 dp and layout_weight to 1 of the view to fill the remaining space ( most probably emergency_view ) so that your logot_view will alwyas stick to bottom