i have a gridview to show images with button, but i have a problem, it only shows the first row, and i send it data for 3 rows..
This is my layout code:
<?xml version="1.0" encoding="utf-8"?><!-- To make screen scroll in vertical direction -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bootstrapthumbnail="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:background="#drawable/fondodroid2"
android:orientation="vertical">
<!-- Main layout -->
<ScrollView
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fefdff"
android:layout_marginLeft="#dimen/login_activity_horizontal_margin"
android:layout_marginRight="#dimen/login_activity_horizontal_margin"
android:layout_marginTop="#dimen/login_activity_vertical_margin"
android:padding="#dimen/login_activity_horizontal_margin"
android:id="#+id/linearLayout">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="true"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:layout_marginBottom="#dimen/login_activity_vertical_margin"
android:weightSum="1">
<ImageView
android:layout_width="#dimen/login_activity_logo"
android:layout_height="#dimen/login_activity_logo"
android:id="#+id/imageView"
android:src="#drawable/logo"
android:contentDescription="Icon GG"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/files"
android:id="#+id/textView"
android:textSize="#dimen/login_activity_text"
android:textColor="#android:color/black"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/imageView"
android:layout_marginLeft="10dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:id="#+id/relativeLayout">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:background="#686868"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:text="#string/sign_string"
android:textSize="#dimen/login_activity_vertical_margin"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="#+id/linearSign"
android:layout_centerInParent="true"
android:background="#cccccc"
android:visibility="gone">
<ImageView
android:id="#+id/sign_thumb"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_margin="3dp"></ImageView>
<LinearLayout
android:orientation="horizontal"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_below="#+id/sign_thumb"
android:layout_alignEnd="#+id/sign_thumb">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Eliminar"
android:id="#+id/deleteButton"
android:background="#cc6b67"
android:textStyle="bold"
android:layout_marginBottom="8dp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:id="#+id/relativeLayout2"
android:layout_marginTop="16dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:background="#686868"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:text="#string/images"
android:textSize="#dimen/login_activity_vertical_margin"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/linearLayout2">
<GridView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/gridImages"
android:numColumns="2"
android:layerType="hardware"
android:verticalSpacing="1dp"
android:horizontalSpacing="1dp" />
</LinearLayout>
</LinearLayout>
<ImageView
android:layout_width="#dimen/login_activity_logo_h"
android:layout_height="#dimen/login_activity_logo_v"
android:id="#+id/imageView2"
android:src="#drawable/logo"
android:layout_below="#+id/linearLayout"
android:layout_alignRight="#+id/linearLayout"
android:layout_marginTop="#dimen/login_activity_vertical_margin"
android:layout_marginBottom="#dimen/login_activity_vertical_margin"
android:contentDescription="#string/logo" />
</RelativeLayout>
</ScrollView>
</LinearLayout>
In the adapter i dont have any strangle, but if you need i copy it here..I try with fill_parent wrap_content etc but nothing works..
Edit: the adapter code:
public class GridAdapter extends BaseAdapter {
private LayoutInflater inflater;
private Activity activity;
private List<String> images = new ArrayList<String>();
private OnDeleteItem mListener;
public interface OnDeleteItem {
public void onDeleteItemClick(String path);
}
public GridAdapter(Activity inyectActivity, List<String> listImages) {
activity = inyectActivity;
images = listImages;
inflater = LayoutInflater.from(activity);
mListener = (ReviewFilesActivity) activity;
}
public void notifyDataSetChanged(List<String> dataImages) {
images = dataImages;
super.notifyDataSetChanged();
}
public View getView(final int position, View convertView, ViewGroup parent) {
GridViewHolder vHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.grid_view, null);
vHolder = new GridViewHolder(convertView, mListener);
vHolder.image.setImageBitmap(ImagesUtilities.decodeSampledBitmapFromResource(images.get(position), 400, 400));
vHolder.path = images.get(position);
vHolder.delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mListener.onDeleteItemClick(images.get(position));
}
});
} else {
vHolder = (GridViewHolder) convertView.getTag();
}
return convertView;
}
#Override
public int getCount() {
return images.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
static class GridViewHolder {
public GridViewHolder(View view, OnDeleteItem listener) {
ButterKnife.inject(this, view);
}
#InjectView(R.id.grid_item_image)
ImageView image;
#InjectView(R.id.deleteButton)
Button delete;
String path;
public String getPath() {
return path;
}
}
}
Edit2:
I did more simply the XML, but the rows dont show.
<?xml version="1.0" encoding="utf-8"?><!-- To make screen scroll in vertical direction -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:background="#drawable/fondodroid2"
android:orientation="vertical">
<!-- Main layout -->
<ScrollView
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fefdff"
android:layout_marginLeft="#dimen/login_activity_horizontal_margin"
android:layout_marginRight="#dimen/login_activity_horizontal_margin"
android:layout_marginTop="#dimen/login_activity_vertical_margin"
android:padding="#dimen/login_activity_horizontal_margin"
android:id="#+id/linearLayout">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="true"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:layout_marginBottom="#dimen/login_activity_vertical_margin"
android:weightSum="1">
<ImageView
android:layout_width="#dimen/login_activity_logo"
android:layout_height="#dimen/login_activity_logo"
android:id="#+id/imageView"
android:src="#drawable/logo"
android:contentDescription="Icon GG"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/files"
android:id="#+id/textView"
android:textSize="#dimen/login_activity_text"
android:textColor="#android:color/black"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/imageView"
android:layout_marginLeft="10dp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/relativeLayout"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:text="#string/sign_string"
android:textSize="#dimen/login_activity_vertical_margin"
android:textStyle="bold"
android:background="#686868"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:gravity="center"
android:id="#+id/textView3" />
<ImageView
android:id="#+id/sign_thumb"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_alignParentEnd="false"
android:visibility="gone" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Eliminar"
android:id="#+id/deleteButton"
android:background="#cc6b67"
android:textStyle="bold"
android:layout_alignBottom="#+id/textView3"
android:layout_alignParentStart="true"
android:visibility="gone" />
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:id="#+id/relativeLayout2"
android:layout_marginTop="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textColor="#android:color/white"
android:text="#string/images"
android:textSize="#dimen/login_activity_vertical_margin"
android:textStyle="bold"
android:background="#686868"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:gravity="center"
android:id="#+id/textView2" />
<GridView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="#+id/gridImages"
android:numColumns="2"
android:verticalSpacing="1dp"
android:horizontalSpacing="1dp"
android:layout_alignParentLeft="false"
android:layout_marginLeft="0dp"
android:layout_below="#+id/textView2" />
</RelativeLayout>
</LinearLayout>
<ImageView
android:layout_width="#dimen/login_activity_logo_h"
android:layout_height="#dimen/login_activity_logo_v"
android:id="#+id/imageView2"
android:src="#drawable/logo"
android:layout_below="#+id/linearLayout"
android:layout_alignRight="#+id/linearLayout"
android:layout_marginTop="#dimen/login_activity_vertical_margin"
android:layout_marginBottom="#dimen/login_activity_vertical_margin"
android:contentDescription="#string/logo" />
</RelativeLayout>
</ScrollView>
</LinearLayout>
Here is some simplified example to achieve needed layout
<?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="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_icon" />
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/sign_thumb"
android:layout_width="match_parent"
android:layout_height="100dp"
android:src="#drawable/ic_icon" />
<Button
android:id="#+id/deleteButton"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<GridView
android:id="#+id/gridImages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="2" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/ic_icon" />
</LinearLayout>
Related
I have a ListView, where i changed appearence of row, but listview have size of one row, instead of fullscreen.
and my scrollview is working but listview is not working.
activity_graph_view.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_graph_table_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="00dp"
android:background="#color/background"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
</LinearLayout>
</android.support.design.widget.AppBarLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/appbar"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">
<LinearLayout
android:id="#+id/layoutTableOne"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp"
android:visibility="visible">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Table View"
android:textSize="#dimen/subheading"
android:textColor="#color/subheading"
android:textAllCaps="false" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/lv_spirometer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#color/mdtp_button_selected"
android:dividerHeight="1dp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:visibility="visible"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.9"
android:padding="10dp"
android:text="Graph View"
android:textAllCaps="false"
android:textColor="#color/subheading"
android:textSize="#dimen/subheading" />
</LinearLayout>
<com.jjoe64.graphview.GraphView
android:id="#+id/grapfinal"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp"
android:weightSum="1">
<LinearLayout
android:id="#+id/grphtextMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/grphtextColor"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_marginTop="3dp"
android:orientation="vertical" />
<TextView
android:id="#+id/grphtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Hello"
android:textColor="#color/subheading"
android:textSize="14dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/grphtextMain2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/grphtextColor2"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_marginTop="3dp"
android:background="#color/red_btn_bg_color"
android:orientation="vertical" />
<TextView
android:id="#+id/grphtext2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Hello"
android:textColor="#color/subheading"
android:textSize="14dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:visibility="visible"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.9"
android:padding="10dp"
android:text="Graph View"
android:textAllCaps="false"
android:textColor="#color/subheading"
android:textSize="#dimen/subheading" />
</LinearLayout>
<com.jjoe64.graphview.GraphView
android:id="#+id/grapfinal1"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp"
android:weightSum="1">
<LinearLayout
android:id="#+id/grphtextMain1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/grphtextColor1"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_marginTop="3dp"
android:background="#color/red_btn_bg_color"
android:orientation="vertical" />
<TextView
android:id="#+id/grphtext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Hello"
android:textColor="#color/subheading"
android:textSize="14dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>
</RelativeLayout>
spirometer_item.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="match_parent"
android:orientation="vertical">
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:cardBackgroundColor="#fff4f4f3"
app:cardElevation="10dp"
app:cardPreventCornerOverlap="true"
card_view:cardCornerRadius="8dp">
<LinearLayout
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/tv_date"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_marginLeft="10dp"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/tv_fvc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FVC 3.15 L" />
<TextView
android:id="#+id/tv_fev1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FEV1 2.44 L" />
<TextView
android:id="#+id/tv_pef"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PEF 3.74 L/s" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
CustomAdapter1.java
public class CustomAdapter1 extends BaseAdapter {
ArrayList<GetUserSpirometer> arrayList;
public CustomAdapter1(ArrayList<GetUserSpirometer> arrayList){
this.arrayList=arrayList;
Log.e("arraylist length",""+arrayList.size());
}
#Override
public int getCount() {
return arrayList.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater= getLayoutInflater();
ViewHolder1 holder=new ViewHolder1();
convertView = inflater.inflate(R.layout.spirometer_item, parent, false);
holder.tv_date=(TextView)convertView.findViewById(R.id.tv_date);
holder.tv_fvc=(TextView)convertView.findViewById(R.id.tv_fvc);
holder.tv_fev1=(TextView)convertView.findViewById(R.id.tv_fev1);
holder.tv_pef=(TextView)convertView.findViewById(R.id.tv_pef);
String[] arr = getDate(Long.parseLong(arrayList.get(position).get_date()), "MMM dd, yyy/hh:mm a").split("/");
holder.tv_date.setText(arr[0] + "\n" + arr[1]);
holder.tv_fvc.setText(arrayList.get(position).get_userfvc());
holder.tv_fev1.setText(arrayList.get(position).get_userfev1());
holder.tv_pef.setText(arrayList.get(position).get_userpef());
convertView.setTag(holder);
return convertView;
}
}
Items are not scrolling.
how to solve this. please help.
thanks in advance.
Then you need to change itemView's xml file ,
android:layout_height="match_parent"
instead of like ,
android:layout_height="wrap_content"
Simple change in spirometer_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
....Your design code
</LinearLayout>
I was having problem with the same from such a long time. Then I found a solution that worked for me.
Add a ListViewHelper java class. Here below is code for ListViewHelper.java
package com.molescope;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
public class ListViewHelper {
public static void getListViewSize(ListView listView){
ListAdapter adapter = listView.getAdapter();
if(adapter!=null){
int totalHeight = 0;
//setting list adapter in loop tp get final size
for (int i=0; i<adapter.getCount(); i++){
View listItem = adapter.getView(i, null, listView);
listItem.measure(0,0);
totalHeight += listItem.getMeasuredHeight();
}
//setting listview items in adapter
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() *
(adapter.getCount()-1));
listView.setLayoutParams(params);
}else{
return;
}
}
}
And after adding this java file, in your code wherever you are setting adapter to listview, right after that line add the code below:
ListView myList=(ListView) findViewById(R.id.listView);
myList.setAdapter(new ArrayAdapter<String>.
(this,android.R.layout.simple_list_item_1, listview_array));
ListViewHelper.getListViewSize(myList);
how can I make a layout that looks like this in android using list view.
I am making a tracking system for many buses. If the bus crossed the location the circle will turn green else it is red.
I just want a vertical line which has length depending on the number of stops for the bus.
the line should be passing through the center of the circles.
Here is the listBusStops.xml file
`
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_marginBottom="30dp">
<ImageView
android:id="#+id/ivstatus"
android:layout_width="30dp"
android:layout_height="30dp"/>
<TextView
android:layout_marginStart="5dp"
android:gravity="center_vertical"
android:id="#+id/tvname"
android:layout_width="wrap_content"
android:layout_height="30dp"
/>
</TableRow>
</RelativeLayout>
`
here the custom_list class which I am using
`
class Custom_list extends ArrayAdapter {
private final Activity context;
private final Integer imageid;
ArrayList<String> list;
ArrayList<String> status;
public Custom_list(Activity context, ArrayList<String> list, ArrayList<String> status, Integer imageid) {
super(context, R.layout.list_bus_stops,Track_any_bus.stop_name);
this.context=context;
this.list = list;
this.imageid=imageid;
this.status = status;
}
public View getView (int position, View view, ViewGroup parent)
{
LayoutInflater inflater = context.getLayoutInflater();
View rowview = inflater.inflate(R.layout.list_bus_stops,null,true);// ALL LAYOUT RESOURSE OF list_bus_stops SAVED TO THIS VIEW(rowview)
TextView textView = rowview.findViewById(R.id.tvname);//
ImageView imageView = rowview.findViewById(R.id.ivstatus);
textView.setText(Track_any_bus.stop_name.get(position));
if(Track_any_bus.status.get(position).equalsIgnoreCase("yes")) {
imageView.setImageResource(R.drawable.circle_green);
}
else {
imageView.setImageResource(R.drawable.circle_red);
}
return rowview;
}
`
thank you for your time.
Here is the demo working code plz have a look i hope it will help you
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.widget.NestedScrollView
android:id="#+id/nested_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:scrollingCache="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="18dp"
android:layout_marginStart="18dp"
android:gravity="center_horizontal"
android:orientation="vertical">
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:background="#D3D3D3" />
<ImageView
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_marginTop="44dp"
android:tint="#000000"
app:srcCompat="#drawable/shape_round_solid" />
</RelativeLayout>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="1dp"
app:cardCornerRadius="5dp"
app:cardElevation="12dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:background="#ffff"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:background="#800000"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_height="1dp"></LinearLayout>
<TextView
android:id="#+id/noteTitle"
android:textColor="#ffffff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:background="#000000 "
android:textSize="20dp" />
<LinearLayout
android:layout_width="match_parent"
android:background="#800000"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_height="1dp"></LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:textSize="14dp"
android:textColor="#000080"
android:text="Text-Preview"/>
<TextView
android:id="#+id/textPreview"
android:textSize="14dp"
android:textColor="#000080"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="5dp"
android:text="Sample Text"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_height="wrap_content"
android:textSize="14dp"
android:textColor="#000080"
android:layout_marginLeft="18dp"
android:layout_width="120dp"
android:text="Last Modified"/>
<TextView
android:id="#+id/lastModified"
android:textColor="#000080"
android:layout_width="wrap_content"
android:textSize="14dp"
android:layout_marginLeft="50dp"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="5dp"
android:text="12/4/2018"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
I'm building an Android app.
In my first activity I want to create a list of button like this:
So I have build this code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#e3e3e3"
android:gravity="center_horizontal"
android:baselineAligned="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4"
android:orientation="horizontal"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginTop="15dp"
>
<ImageButton
android:id="#+id/ButtonArticoli"
android:layout_width="150px"
android:layout_height="150px"
android:onClick="visualizzaArticoli"
android:scaleType="fitCenter"
android:src="#drawable/articoli"
/>
<TextView
android:id="#+id/textViewArticoli"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/articoli"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200px"
android:orientation="horizontal"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginTop="15dp"
>
<ImageButton
android:id="#+id/ImageButtonCreateOrder"
android:layout_width="150px"
android:layout_height="150px"
android:onClick="creaOrdine"
android:src="#drawable/order"/>
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/ordini"
android:gravity="center" />
</LinearLayout>
<!-- Prima Riga -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200px"
android:orientation="horizontal"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginTop="15dp"
>
<ImageButton
android:id="#+id/ImageButtonClienti"
android:layout_width="150px"
android:layout_height="150px"
android:onClick="viewClienti"
android:scaleType="fitCenter"
android:src="#drawable/customer"/>
<TextView
android:id="#+id/textView23"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/cliente"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200px"
android:orientation="horizontal"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginTop="15dp"
>
<ImageButton
android:id="#+id/ImageButtonAlignDatabase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="allineaDatase"
android:scaleType="fitCenter"
android:src="#drawable/sincronizza"/>
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/allinea_database"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200px"
android:orientation="horizontal"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginTop="15dp"
>
<ImageButton
android:id="#+id/ImageButtonSetting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setting"
android:scaleType="fitCenter"
android:src="#drawable/setting"/>
<TextView
android:id="#+id/TextView01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/impostazioni"
android:gravity="center" />
</LinearLayout>
</LinearLayout>
But the result of my app when I try yo start my application is this:
Can we help me?
Use this code if you have a fixed size of elements.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="#F1C983">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
card_view:cardBackgroundColor="#D9EE9A">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:background="#null"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sample Text"
android:layout_gravity="center_vertical"
android:layout_marginLeft="50dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
card_view:cardBackgroundColor="#D9EE9A">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:background="#null"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sample Text"
android:layout_gravity="center_vertical"
android:layout_marginLeft="50dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
card_view:cardBackgroundColor="#D9EE9A">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:background="#null"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sample Text"
android:layout_gravity="center_vertical"
android:layout_marginLeft="50dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
Use compile 'com.android.support:cardview-v7:25.0.0' to get the CardView.
this is the sample output.
use ListView or RecyclerView with your custom list item which will contain image buttons and custom adapter
Instead of px use dp it´s much better!
If you want a fixed list, your XML is correct, but if you want a dynamic list, you should use ListView or RecycleView , as the others users said.
The weight in your LinearLayout is wrong, weight should be used when you desires your screen to fit in every screen, no matter the size ( and you will need to "distribute" your weigth throught the children´s).
To make your screen similar to the first image, you need to set a background in your second LinearLayout
Okey this a recyclerview for what you want,
public class MainPageAdapter extends RecyclerView.Adapter<MainPageAdapter.ViewHolder> {
private #DrawableRes int[] iconDrawables;
private String[] texts;
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.img_lockIcon.setImageResource(iconDrawables[position]);
holder.txt_whatever.setText(texts[position]);
}
#Override
public int getItemCount() {
return iconDrawables != null ? iconDrawables.length : 0;
}
public void setData(#DrawableRes int[] iconDrawables, String[] texts){
this.iconDrawables = iconDrawables;
this.texts = texts;
this.notifyDataSetChanged();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
ImageView img_icon;
TextView txt_whatever;
ImageView img_lockIcon;
public ViewHolder(View itemView) {
super(itemView);
img_icon = itemView.findViewById(R.id.item_img_icon);
txt_whatever = itemView.findViewById(R.id.item_txt_whatever);
img_lockIcon = itemView.findViewById(R.id.item_img_lockIcon);
}
}
}
this is R.layout.item,
<?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:layout_margin="8dp"
android:padding="8dp"
android:background="#color/colorAccent"
xmlns:tools="http://schemas.android.com/tools">
<ImageView
android:id="#+id/item_img_icon"
android:layout_width="36dp"
android:layout_height="36dp"
tools:src="#mipmap/ic_launcher"/>
<TextView
android:id="#+id/item_txt_whatever"
android:layout_width="0dp"
android:layout_height="36dp"
android:layout_weight="1"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:textColor="#android:color/white"
tools:text="YouTube"/>
<ImageView
android:id="#+id/item_img_lockIcon"
android:layout_width="40dp"
android:layout_height="40dp"
tools:src="#mipmap/ic_launcher"/>
</LinearLayout>
and below is MainActivity layout,
<FrameLayout 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.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="0dp"
app:cardElevation="0dp"
app:cardBackgroundColor="#color/colorPrimary"
android:layout_margin="8dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="#layout/item"/>
</android.support.v7.widget.CardView>
</FrameLayout>
and below is MainActivity,
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
MainPageAdapter adapter = new MainPageAdapter();
recyclerView.setAdapter(adapter);
//Set your data
//adapter.setData();
}
}
You can change color as you want and also notify that ı used two different arrays for adapter but you can use pojo classes also. Hope this helps you.
i have an activity which is having about 4 fragment and fragment have a recyclerview and in each recyclerview item there is a button.. i have a textfield in activity and i want to show the no. of button clicked.
thats my activity.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="3"
tools:context="com.example.khaalijeb.newlistview_module.PromoCodeActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.4"
android:background="#f9f9f9"
android:elevation="5dp">
<ImageView
android:id="#+id/back"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:src="#drawable/greyback" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/back"
android:text="Fancy Some Deals ?"
android:textSize="16dp"
android:textStyle="bold" />
<Button
android:id="#+id/skip"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:background="#drawable/roundedcorneredittext"
android:text="SKIP"
android:textColor="#0277bd"
android:textSize="12dp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.55"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabIndicatorColor="#E12728"
app:tabMode="scrollable"
app:tabSelectedTextColor="#E12728"
app:tabTextAppearance="#style/TextAppearance.Design.Tab" />
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ffffff" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.05"
android:background="#f9f9f9"
android:orientation="vertical"
android:weightSum="2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="vertical"
android:weightSum="2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.2"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#727272"
/>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/TextLabel">
<EditText
android:id="#+id/spass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:hint="Promo Code"
android:inputType="textPassword"
android:paddingLeft="10dp"
android:textColor="#color/material_blue_grey_800"
android:textSize="14dp" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.5"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#727272" />
<Button
android:id="#+id/apply"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:background="#drawable/roundedcorneredittext"
android:text="APPLY"
android:textColor="#0277bd"
android:textSize="12dp" />
<Button
android:id="#+id/applied"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:background="#drawable/roundedcorneredittext"
android:text="APPLIED"
android:textColor="#0277bd"
android:textSize="12dp"
android:visibility="invisible"
/>
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.8">
<TextView
android:id="#+id/credittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textSize="12dp"
android:text="Credited To Jeb No." />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/credittext"
android:layout_toRightOf="#+id/credittext"
android:text="C100"
android:textSize="12dp"
/>
<TextView
android:id="#+id/amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/credittext"
android:layout_marginRight="16dp"
android:text="Rs.100"
android:textSize="12dp"
/>
<TextView
android:id="#+id/pickdeal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/credittext"
android:layout_margin="5dp"
android:text="Haven't Picked Any Deals Yet"
android:textColor="#78909C"
android:textSize="12dp" />
<TextView
android:id="#+id/dealamount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/amount"
android:text="Rs.50"
android:layout_marginRight="16dp"
android:layout_alignTop="#+id/pickdeal"
android:textSize="12dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/dealamount"
android:text="+"
android:layout_alignTop="#+id/dealamount"
android:layout_marginRight="2dp"
android:textSize="12dp"
/>
<TextView
android:id="#+id/discount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/pickdeal"
android:text="Discount"
android:layout_alignLeft="#+id/pickdeal"
android:textColor="#F44336"
android:textSize="12dp"
android:layout_marginTop="5dp"
/>
<TextView
android:id="#+id/discountedamount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Rs.50"
android:textColor="#F44336"
android:layout_alignTop="#+id/discount"
android:layout_marginRight="16dp"
android:textSize="12dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/discountedamount"
android:layout_alignTop="#+id/discountedamount"
android:text="-"
android:textColor="#F44336"
android:layout_marginRight="2dp"
/>
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.5">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0277bd"
android:text="PROCEED TO PAY" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
recyelrview item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/llContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical"
android:padding="5dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
card_view:cardBackgroundColor="#f9f9f9"
card_view:cardCornerRadius="1dp"
card_view:cardElevation="5dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:orientation="vertical"
android:weightSum="2"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.7">
<ImageView
android:id="#+id/icon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:src="#drawable/nike" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:orientation="horizontal"
android:weightSum="2">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5">
<TextView
android:id="#+id/brandname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="hello"
android:textAllCaps="true"
android:textColor="#000000"
android:textSize="14dp"
android:typeface="sans" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/brandname"
android:layout_below="#+id/brandname"
android:layout_marginTop="10dp"
android:text="Get 50% off on Apparels"
android:textSize="12dp" />
<TextView
android:id="#+id/seemore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/brandname"
android:layout_below="#+id/title"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:text="See More"
android:textSize="12dp" />
<ImageView
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_alignTop="#+id/seemore"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/seemore"
android:src="#drawable/sendgrey" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.5">
<Button
android:id="#+id/free"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_centerInParent="true"
android:layout_marginBottom="16dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="16dp"
android:background="#drawable/roundedcorneredittext"
android:text="Free"
android:textColor="#0277bd"
android:textSize="12dp"
/>
<Button
android:id="#+id/taken"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_centerInParent="true"
android:layout_marginBottom="16dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="16dp"
android:background="#drawable/roundedcorner1"
android:text="Great!"
android:textColor="#ffffff"
android:textSize="12dp"
android:visibility="invisible"
/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Adapter.java
public class PromoCodeRecyclerViewAdapter extends RecyclerView.Adapter<PromoCodeRecyclerViewAdapter.MyViewholder> {
private ClickListener clickListener;
private LayoutInflater inflator;
private Context mcontext;
Typeface font;
public Button b1;
public ImageButton b2;
List<promocodedata> data = Collections.emptyList();
public PromoCodeRecyclerViewAdapter(Context context, List<promocodedata> y, Typeface font) {
inflator = LayoutInflater.from(context);
this.data = y;
this.mcontext = context;
this.font = font;
}
public void setClickListener(ClickListener clickListener) {
this.clickListener = clickListener;
}
#Override
public MyViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflator.inflate(R.layout.promocode_item_layout, parent, false);
MyViewholder holder = new MyViewholder(view);
return holder;
}
#Override
public void onBindViewHolder(final MyViewholder holder, int position) {
promocodedata current = data.get(position);
holder.brandicon.setImageResource(current.brandicon);
holder.brandname.setText(current.brandname);
holder.title.setText(current.title);
holder.free.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.taken.setVisibility(View.VISIBLE);
holder.free.setVisibility(View.INVISIBLE);
}
});
holder.taken.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.taken.setVisibility(View.INVISIBLE);
holder.free.setVisibility(View.VISIBLE);
}
});
}
#Override
public int getItemCount() {
return data.size();
}
public interface ClickListener {
void itemClicked(View v, int position);
}
class MyViewholder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView brandicon;
TextView brandname;
TextView title;
Button free;
Button taken;
public MyViewholder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
brandicon = (ImageView) itemView.findViewById(R.id.icon);
brandname = (TextView) itemView.findViewById(R.id.brandname);
title = (TextView) itemView.findViewById(R.id.title);
free = (Button)itemView.findViewById(R.id.free);
taken = (Button)itemView.findViewById(R.id.taken);
}
#Override
public void onClick(View v) {
if (clickListener != null) {
clickListener.itemClicked(v, getPosition());
}
}
}
}
You need to create your onitemclick because RecyclerView does not have a OnItemClick like Listview, i have an example in Github check it.
My goal is below image
And i have below codes
row_right.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/me"
android:layout_gravity="right"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:layout_marginLeft="5dp"
android:layout_marginRight="15dp"
android:layout_gravity="right"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:textSize="15sp"
android:textColor="#000000"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="15dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/dataAndTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:textSize="15sp"
android:textColor="#001199"
android:text="gggggggghyjkljgfdgjlkhfdhklggg" />
</LinearLayout>
</LinearLayout>
row_left.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:background="#drawable/you"
android:layout_gravity="left"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:layout_marginLeft="5dp"
android:layout_marginRight="15dp"
android:layout_gravity="right"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:textSize="15sp"
android:textColor="#000000"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="15dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/dataAndTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:textSize="15sp"
android:textColor="#001199"
android:text="gggggggggg" />
</LinearLayout>
</LinearLayout>
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#FFFFFF" >
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_weight="1"
android:scrollbars="vertical"
android:divider="#null"
android:dividerHeight="0dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/content"
android:layout_width="253dp"
android:layout_height="wrap_content"
android:autoText="false"
android:capitalize="none"
android:ems="10"
android:scrollHorizontally="true"
android:singleLine="true"
android:textSize="16sp"
android:hint="Enter text"
>
</EditText>
<Button
android:id="#+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send" />
</LinearLayout>
</LinearLayout>
but my result become
How i can Fix it?
UPDATE1
my listAdapter.java
public class ListAdapter1 extends BaseAdapter {
private LayoutInflater myInflater;
private List<SmsInformation> list;
public ListAdapter1(Context context) {
myInflater = LayoutInflater.from(context);
}
public void setData(List<SmsInformation> list2) {
this.list = list2;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
if (list.get(position).getTypeOfSms().equals("send"))
convertView = myInflater.inflate(R.layout.raw_left, null);
else
convertView = myInflater.inflate(R.layout.raw_right, null);
holder = new ViewHolder();
holder.message = (TextView) convertView.findViewById(R.id.message);
holder.dateAndTime = (TextView) convertView.findViewById(R.id.dataAndTime);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.message.setText(list.get(position).getMessageContent());
holder.dateAndTime.setText(list.get(position).geTime()+list.get(position).getDate());
return convertView;
}
static class ViewHolder {
TextView message;
TextView dateAndTime;
}
}
Changes made:
~ in row_right.xml & row_left.xml, layout_width attribute of the parent LinearLayout should be set to wrap_content instead of match_parent
~ in main.xml, layout_width attribute of ListView should be set to match_parent. not wrap_content
Try the following code:
~ row_right.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/me"
android:layout_alignParentRight="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="5"
android:layout_marginLeft="5dp"
android:layout_marginRight="15dp"
android:layout_gravity="right"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:textSize="15sp"
android:textColor="#000000"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="15dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/dataAndTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:textSize="15sp"
android:textColor="#001199"
android:text="gggggggghyjkljgfdgjlkhfdhklggg" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
~ row_left.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" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/you"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="5"
android:layout_marginLeft="5dp"
android:layout_marginRight="15dp"
android:layout_gravity="right"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:textSize="15sp"
android:textColor="#000000"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="15dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/dataAndTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:textSize="15sp"
android:textColor="#001199"
android:text="gggggggggg" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
~ main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#FFFFFF" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:scrollbars="vertical"
android:divider="#null"
android:dividerHeight="0dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/content"
android:layout_width="253dp"
android:layout_height="wrap_content"
android:autoText="false"
android:capitalize="none"
android:ems="10"
android:scrollHorizontally="true"
android:singleLine="true"
android:textSize="16sp"
android:hint="Enter text"
>
</EditText>
<Button
android:id="#+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send" />
</LinearLayout>
</LinearLayout>
Just a guess. in main.xml change width of your ListView to match_parent.
Also add appropriate weight to the layout containing EditText and button. Now the whole space will be assigned to the listView.