This is the individual item's layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#DDE6EC"
tools:context="com.example.personal.newsfeeder.MainActivity">
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/section_recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/tools"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginTop="16dp"
android:id="#+id/card_view"
android:layout_gravity="center"
card_view:cardCornerRadius="8dp"
card_view:cardElevation="10dp"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/avatar_section"
android:layout_width="match_parent"
android:layout_height="72dp"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#drawable/circular_image"
android:id="#+id/avatar_image"
/>
<TextView
android:id="#+id/avatar_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/avatar_image"
android:layout_marginLeft="4dp"
tools:text="Title"
android:textStyle="bold"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#616161"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/avatar_name"
android:layout_toRightOf="#id/avatar_image"
android:layout_marginLeft="4dp"
tools:text="Subhead"
android:id="#+id/avatar_subhead"
android:textAppearance="?android:textAppearanceSmall"
android:textColor="#616161"/>
</RelativeLayout>
<ImageView
android:layout_height="wrap_content"
android:layout_width="match_parent"
tools:src="#drawable/placeholder"
android:id="#+id/image_view"
android:scaleType="centerCrop"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:text="Keep Moving"
android:paddingLeft="16dp"
android:textAppearance="?android:textAppearanceMedium"
android:textAllCaps="true"
android:textStyle="bold"
android:id="#+id/the_title"
/>f
<TextView
android:id="#+id/the_three_lines"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="3"
android:padding="16dp"
tools:text="#string/description"
android:ellipsize="end"
android:lineSpacingMultiplier="1.2"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/heart_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/heart"
android:padding="8dp"
/>
<ImageView
android:id="#+id/bookmark_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/bookmark"
android:padding="8dp"
/>
</LinearLayout>
</LinearLayout>
enter code here
</android.support.v7.widget.CardView>
</LinearLayout>
This is the recycler's Adapter code
#Override
public void onBindViewHolder(ArticleViewHolder holder, int position) {
holder.mAvatarView.setText(mArticles.get(position).getmAvatarInitial());
holder.mAvatarNameView.setText(mArticles.get(position).getmAvatarName());
holder.mAvatarSubView.setText(mArticles.get(position).getmAvatarSub());
mRowIndex++;
if (position == 0) {
holder.horizontalList.setLayoutManager(new LinearLayoutManager(holder.context, LinearLayoutManager.HORIZONTAL, false));
holder.horizontalAdapter = new SectionRVAdapter(holder.context);
holder.horizontalList.setAdapter(holder.horizontalAdapter);
holder.horizontalAdapter.setDataset(holder.sections);
}
String imageUrl = mArticles.get(position).getmImageURL();
Picasso.with(mContext)
.load(imageUrl)
.placeholder(R.drawable.placeholder)
.error(R.drawable.error_img)
.into(holder.mImageView);
holder.mTheTitleView.setText(mArticles.get(position).getmTheTitle());
holder.mTheThreeLinesView.setText(mArticles.get(position).getmTheThreeLines());
}
Now i want a horizontal recycler view over the vertical recycler view. But at present the horizontal recycler view is repeated after every 5 cardviews.
I am checking if position is equal to 0 in my onBindViewHolder method but still why is it created multiple times?
I hope i made my question clear.
holder.setIsRecyclable(false);
Add this line in public void onBindViewHolder(ArticleViewHolder holder, int position) {}
I hope this will help you
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);
enter image description here
In Activty one i have RecyclerView with to View :
HEADER_VIEW
ITEM_VIEW
Header_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="?attr/actionBarSize"
android:background="#FFF7F7F7">
<TextView
android:id="#+id/tv_group_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="16dp"
android:layout_weight="1"
android:text="TextView"
android:textColor="#F50057"
android:textSize="20sp" />
<TextView
android:id="#+id/more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:layout_weight="1"
android:text="More..." />
</RelativeLayout>
Item_view.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:id="#+id/ll_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/cv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:elevation="3dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:src="#mipmap/ic_launcher" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_below="#id/imageView"
android:background="#f5fff2">
<TextView
android:id="#+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:text="name"
android:textAlignment="center"
android:textColor="#FF20C200"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/new_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/old_price"
android:layout_marginRight="5dp"
android:layout_marginStart="5dp"
android:layout_toEndOf="#+id/old_price"
android:text="480.00 DA"
android:textColor="#FF65C2FD"
android:textStyle="bold" />
<TextView
android:id="#+id/old_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/tv_name"
android:layout_marginBottom="10dp"
android:layout_marginRight="5dp"
android:layout_marginStart="5dp"
android:text="250.00 DA"
android:textAlignment="center"
android:textColor="#F50057" />
<ImageButton
android:id="#+id/overflow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignTop="#+id/new_price"
android:layout_marginEnd="5dp"
android:layout_marginLeft="5dp"
android:background="?attr/selectableItemBackgroundBorderless"
app:srcCompat="#drawable/ic_more_vert_black_24dp" />
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
I created the Adapter and it Work Fine .
My problem is :
I need when I click on more TextView In HEADER_VIEW an New Activity Start
with Specific Itemes Of Stection.
My try :
In Adapter Exactly in onBindViewHolder
#Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
//Context context = mContextWeakReference.get();
if (mContext == null) {
return;
}
if (SECTION_VIEW == getItemViewType(position)) {
final SectionViewHolder sectionViewHolder =(SectionViewHolder)holder;
GroupTitleModel sectionItem = ((GroupTitleModel) mUsersAndSectionList.get(position));
sectionViewHolder.title.setText(sectionItem.title);
sectionViewHolder.more.setText(sectionItem.moreSection);
sectionViewHolder.more.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View view) {
// I TRY TO DO IT HERE BUT NO THINGS
}
} );
return;
}
My first thought is that you are bringing yourself into trouble by doing it this way.
You separate the Header view and its Content view, so the problem appear when the Header has no idea which content to continue to next step. If you don't want to refactor than it's ok, there will be a workaround. But my suggestion is that you combine the header and the content into 1 ViewHolder, keep the title, more text, and items inside 1 Model class too. They need to go with each other so they know how to handle with interaction
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.
In my application i have one custom list view. whenever i click the close image list item, the list item layout must be removed with space. i used visibility:gone attribute, the list item layout deleted but the space is visible. I am using listview in scroll view ,,that was also not working....please help me out.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.recharge_listitem, null);
TextView accountnumber = (TextView) convertView
.findViewById(R.id.accountnumber);
TextView servicename = (TextView) convertView
.findViewById(R.id.servicetypname);
TextView planamount = (TextView) convertView
.findViewById(R.id.rechargeamount);
final ImageView close = (ImageView) convertView
.findViewById(R.id.crosse_close_img);
final LinearLayout layoutrecharge=(LinearLayout)convertView.findViewById(R.id.rechargelay);
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
layoutrecharge.setVisibility(View.GONE);
}
});
accountnumber.setText("Rs."+bpData.get(position).getAccountNumber());
servicename.setText(bpData.get(position).getServicename());
planamount.setText(bpData.get(position).getRechargeamount());
return convertView;
}
}
xml:
<?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"
android:background="#drawable/background">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#1e365a">
<TextView
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:text="PAYMENT"
android:textColor="#ffffff"
android:textSize="20dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="10dp"
android:text="Your Recharges"
android:textColor="#1e365a"
android:textSize="20dp" />
</RelativeLayout>
<View
android:layout_width="fill_parent"
android:layout_height="4dp"
android:background="#drawable/dottedline_view" />
<ListView
android:id="#+id/recharge_lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone">
</ListView>
<View
android:layout_width="fill_parent"
android:layout_height="0.8dp"
android:layout_marginTop="12dp"
android:background="#1e365a" />
<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="25dp"
android:layout_weight="1"
android:layout_margin="10dp"
android:text="Amount to be Paid"
android:textColor="#1e365a"
android:textSize="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_weight="1"
android:layout_margin="10dp"
android:text="Rs.130"
android:textColor="#1e365a"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:weightSum="100" >
<EditText
android:id="#+id/ev_b_dob"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_weight="50"
android:hint="Enter coupn code"
android:inputType="text" >
<requestFocus />
</EditText>
<Button
android:id="#+id/bt_apply"
style="#style/payment_button_style"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginLeft="-5dp"
android:layout_weight="20"
android:gravity="center"
android:text="Apply" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:layout_marginLeft="28dp"
android:layout_marginRight="30dp">
<CheckBox
android:id="#+id/cb_reload_cash"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Pay using Reload Cash"
android:textColor="#1e365a"
android:textSize="20dp"
android:layout_marginLeft="12dp"/>
</LinearLayout>
<Button
android:id="#+id/atmdebit_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="28dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:background="#F93249"
android:gravity="center"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:text="ATM/DEBIT/CREDIT"
android:textColor="#ffffff"
android:textSize="20dp"/>
<Button
android:id="#+id/netbankng_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="28dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:background="#F93249"
android:gravity="center"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:text="NET BANKING"
android:textColor="#ffffff"
android:textSize="20dp"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
please help me.
custom listview item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rechargelay">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:id="#+id/accountnumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8008505106" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="(" />
<TextView
android:id="#+id/servicetypname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Vodafone" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=")" />
</LinearLayout>
<TextView
android:id="#+id/rechargeamount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RS.30"
android:layout_weight="1" />
<ImageView
android:id="#+id/crosse_close_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/close"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
Set View.GONE for View is not right way to remove row from ListView. to remove row remove select row item from Adapter data-source and call notify data change method of adapter to populate changes:
close.setTag(position);
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int deletePos=Integer.parseInt(v.getTag().toString());
bpData.remove(deletePos);
notifyDataSetChanged();
}
});
Set visibility View.GONE of SCROLL VIEW or other parent of list view....