How to add a view to the bottom of android drawer - android

While designing the UI for an android app I faced the following problem. I wanted to align the item in the ListView of the drawer to the bottom. I am new to android development. Any help will be appreciated.
This is my drawer code
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E0E0E0" >
<!-- Your main screen -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:text="Location: "
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/tvLocInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Rajeev Chowk, Block A, Connaught Place"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:text="Time: "
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/tvTimeInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="9:30 PM"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.79"
android:orientation="vertical" >
<ImageView
android:id="#+id/ivMap"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:layout_weight="0.85"
android:scaleType="fitXY"
android:src="#drawable/map" />
<Button
android:id="#+id/btnPanic"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:background="#drawable/red_button"
android:text="Panic Button"
android:textColor="#FFFFFF" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:layout_weight="0.010"
android:background="#drawable/top_border" >
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="#+id/tvDeviceStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Device Status: Working"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
android:id="#+id/tvDeviceStatus1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvDeviceStatus"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:text="Battery: 91%(~11 months)"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
<!-- left drawer -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<ListView
android:id="#+id/left_drawer"
android:layout_width="380dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#E0E0E0"
android:choiceMode="singleChoice"
android:dividerHeight="1dp" />
</android.support.v4.widget.DrawerLayout>
These are the three xml file that I inflate in the ListView
1) profile_view.xml
<ImageView
android:id="#+id/ivProfile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:src="#drawable/sneha_lata" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sneha Lata"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit Profile"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="" />
<ImageView
android:id="#+id/ivForward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/forward" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
2) emergency_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="20dp"
android:paddingTop="20dp" >
<TextView
android:id="#+id/tvManage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:text="Manage Emergency Contacts"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/ivForward"
android:layout_toRightOf="#id/tvManage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/forward" />
<TextView
android:id="#+id/tvManageText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tvManage"
android:layout_alignParentLeft="true"
android:layout_alignRight="#id/tvManage"
android:layout_marginLeft="40dp"
android:fontFamily="sans-serif-light"
android:text="#string/tvManageText"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
3) logout view
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/logout_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/btnLogout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/red_button"
android:text="Log Out"
android:textColor="#FFFFFF" />
</RelativeLayout>
Even if I used android:layout_alignParentBottom="true" in my logout_view.xml still it does not align at the bottom in the drawer but it does in logout_view.xml
This is the code for the DrawerAdapter
package com.majhe.android.adapter;
import com.majhe.android.LoginActivity;
import com.majhe.android.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebView.FindListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
public class DrawerAdapter extends ArrayAdapter<String> implements OnClickListener {
Context context;
Button btnLogout;
public DrawerAdapter(Context context) {
super(context, android.R.layout.simple_list_item_1, new String[] {
"Profile View", "Emergency View", "Logout View" });
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
switch (position) {
case 0:
convertView = inflater.inflate(R.layout.profile_view, null, true);
return convertView;
case 1:
convertView = inflater.inflate(R.layout.emergency_view, null,true);
return convertView;
case 2:
convertView = inflater.inflate(R.layout.logout_view, null, true);
btnLogout=(Button)convertView.findViewById(R.id.btnLogout);
btnLogout.setOnClickListener(this);
return convertView;
}
return super.getView(position, convertView, parent);
}
#Override
public void onClick(View v) {
context.startActivity(new Intent(context,LoginActivity.class));
((Activity)context).finish();
}
}

I think you dont need to use ListView at all if I am right? replace your listview with something like this might help
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#fff"
android:orientation="vertical"
android:paddingEnd="10dp" android:gravity="bottom">
<include layout="#layout/profile_view"/>
<include layout="#layout/emergency_view"/>
<include layout="#layout/logout_view"/>
</LinearLayout>
set layout_height 0 dp and layout_weight to 1 of the view to fill the remaining space ( most probably emergency_view ) so that your logot_view will alwyas stick to bottom

Related

Recyclerview items unable scroll down.inside tabpane

used tabpane take 120dp height in top.
mainxml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#E0E0E0"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.suranpc.recy.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="120dp"
android:scrollbars="vertical"
/>
</RelativeLayout>
cardview design xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="3dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:padding="16dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#ffffff"
android:orientation="horizontal"
android:padding="16dp"
>
<TextView
android:id="#+id/firstcardtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="TextView"
android:textColor="#color/colorFontblack"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="60dp"
android:background="#ffffff"
android:orientation="horizontal"
android:padding="4dp"
>
<TextView
android:id="#+id/firstcardtext2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="TextView :"
android:textAlignment="center"
android:textColor="#color/colorFontblack"
android:textSize="22dp"
android:textStyle="bold" />
<TextView
android:id="#+id/firstcardtext3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.91"
android:gravity="right"
android:text="TextView"
android:textAlignment="center"
android:textColor="#color/colorPrimaryDark"
android:textSize="22dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="120dp"
android:background="#ffffff"
android:orientation="horizontal"
android:padding="1dp"
>
<TextView
android:id="#+id/firstcardtext4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.92"
android:gravity="left"
android:text="TextView:"
android:textAlignment="center"
android:textColor="#color/colorFontblack"
android:textSize="22dp"
android:textStyle="bold" />
<TextView
android:id="#+id/firstcardtext5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.98"
android:gravity="right"
android:text="20000"
android:textAlignment="center"
android:textColor="#color/colorFontheader"
android:textSize="22dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="180dp"
android:background="#ffffff"
android:orientation="horizontal"
android:padding="1dp"
>
<TextView
android:id="#+id/firstcardtext6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="TextView:"
android:textAlignment="center"
android:textColor="#color/colorFontblack"
android:textSize="22dp"
android:textStyle="bold" />
<TextView
android:id="#+id/firstcardtext7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="TextView"
android:textAlignment="center"
android:textColor="#color/colorFontheader"
android:textSize="22dp"
android:textStyle="bold" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
recycler adapter code:
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class Readapter extends RecyclerView.Adapter<Readapter.ProductViewHolder> {
//this context we will use to inflate the layout
private Context mCtx;
//we are storing all the products in a list
private List<Front> productList;
//getting the context and product list with constructor
public Readapter(Context mCtx, List<Front> productList) {
this.mCtx = mCtx;
this.productList = productList;
}
#Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//inflating and returning our view holder
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.layoutte,parent,false);
return new ProductViewHolder(view);
}
#Override
public void onBindViewHolder(ProductViewHolder holder, int position) {
//getting the product of the specified position
Front product = productList.get(position);
//binding the data with the viewholder views
holder.textView1.setText(product.getbud());
holder.textView2.setText(product.getbudnum());
holder.textView3.setText(product.gettosp());
holder.textView4.setText(product.gettospnum());
holder.textView5.setText(product.getrem());
holder.textView6.setText(product.getremnum());
holder.textView7.setText(product.getremnums());
}
#Override
public int getItemCount() {
return productList.size();
}
class ProductViewHolder extends RecyclerView.ViewHolder {
TextView textView1, textView2, textView3, textView4, textView5,textView6,textView7;
public ProductViewHolder(View itemView) {
super(itemView);
textView1 = itemView.findViewById(R.id.firstcardtext);
textView2 = itemView.findViewById(R.id.firstcardtext2);
textView3 = itemView.findViewById(R.id.firstcardtext3);
textView4 = itemView.findViewById(R.id.firstcardtext4);
textView5 = itemView.findViewById(R.id.firstcardtext5);
textView6 = itemView.findViewById(R.id.firstcardtext6);
textView7 = itemView.findViewById(R.id.firstcardtext7);
}
}
}
Mainactivity code(add datas):
recyclerView=(RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
productList =new ArrayList<>();
productList.add(new
Front("tested1","tested","tested","tested","tested","tested","tested"));
productList.add(new
Front("tested2","tested","tested","tested","tested","tested","tested"));
productList.add(new
Front("tested3","tested","tested","tested","tested","tested","tested"));
productList.add(new Front("tested4","tested","tested","tested","tested","tested","tested"));
Readapter adapter=new Readapter(this,productList);
recyclerView.setAdapter(adapter);
only two cards are visible in screen.another two cards not visibled.fixed height datas only showed.(not scrolled).
only test1 and test2 datas visible in screen another test3 abd test4 not viewd and scrolled .
appbar xml code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.suranpc..MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
<android.support.design.widget.TabLayout
android:background="#color/colorTabbg"
app:tabSelectedTextColor="#color/colorFontwhite"
app:tabIndicatorColor="#color/colorFontwhite"
app:tabIndicatorHeight="4dp"
app:tabTextColor="#color/colorFontblack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
app:tabGravity="fill"
android:id="#+id/tbl_pages"/>
<!-- <tabbed pane" />-->
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/vp_pages"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_dialog_email" />
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
tabpane code in mainactivity
//the tabpane>
ViewPager vp_pages= (ViewPager) findViewById(R.id.vp_pages);
PagerAdapter pagerAdapter=new FragmentAdapter(getSupportFragmentManager());
vp_pages.setAdapter(pagerAdapter);
TabLayout tbl_pages= (TabLayout) findViewById(R.id.tbl_pages);
final int[]ICONS=new int[]{
R.drawable.ic_menu_camera, R.drawable.ic_menu_camera, R.drawable.ic_menu_camera, R.drawable.ic_menu_camera,
};
tbl_pages.setupWithViewPager(vp_pages);
tbl_pages.getTabAt(0).setIcon(ICONS[0]);
tbl_pages.getTabAt(1).setIcon(ICONS[1]);
tbl_pages.getTabAt(2).setIcon(ICONS[2]);
tbl_pages.getTabAt(3).setIcon(ICONS[3]);
//<tabpane>
Try this just Change this in your cardview design xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:padding="16dp"
app:cardCornerRadius="3dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#ffffff"
android:orientation="horizontal"
android:padding="16dp"
>
<TextView
android:id="#+id/firstcardtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="TextView"
android:textColor="#color/colorFontblack"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="60dp"
android:background="#ffffff"
android:orientation="horizontal"
android:padding="4dp"
>
<TextView
android:id="#+id/firstcardtext2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="TextView :"
android:textAlignment="center"
android:textColor="#color/colorFontblack"
android:textSize="22dp"
android:textStyle="bold" />
<TextView
android:id="#+id/firstcardtext3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.91"
android:gravity="right"
android:text="TextView"
android:textAlignment="center"
android:textColor="#color/colorPrimaryDark"
android:textSize="22dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="120dp"
android:background="#ffffff"
android:orientation="horizontal"
android:padding="1dp"
>
<TextView
android:id="#+id/firstcardtext4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.92"
android:gravity="left"
android:text="TextView:"
android:textAlignment="center"
android:textColor="#color/colorFontblack"
android:textSize="22dp"
android:textStyle="bold" />
<TextView
android:id="#+id/firstcardtext5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.98"
android:gravity="right"
android:text="20000"
android:textAlignment="center"
android:textColor="#color/colorFontheader"
android:textSize="22dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="180dp"
android:background="#ffffff"
android:orientation="horizontal"
android:padding="1dp"
>
<TextView
android:id="#+id/firstcardtext6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="TextView:"
android:textAlignment="center"
android:textColor="#color/colorFontblack"
android:textSize="22dp"
android:textStyle="bold" />
<TextView
android:id="#+id/firstcardtext7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="TextView"
android:textAlignment="center"
android:textColor="#color/colorFontheader"
android:textSize="22dp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
EDIT
<?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:background="#E0E0E0"
tools:context="com.example.suranpc.recy.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:layout_marginTop="120dp"
android:scrollbars="vertical" />
</LinearLayout>
Display content main xml behalf of tabpane fragment(home page xml).use fragment it works.

XML/Java - Creating Two OnClick Methods and RadioButton Writing to Specific Values

This is a Noob question. I am sorry if it has been answered. I have been searching but either could not find or understand the answer. Anyway, I am trying to write a simple weight and balance program for a fleet of aircraft but I'm not sure how to proceed.
I'm trying to figure out how to have the program write to a specific field while a certain radio button is checked.
Could anyone be of any assistance? Thank you!
XML
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_weight_and_balance"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.tropicaircharters.android.employee.weightAndBalance">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Aircraft Currently Selected:"
android:layout_weight="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/aircraft_selected"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="50dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="A" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="B" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="C" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="D" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="E" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="F" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/display_a"
android:text="#string/a_weight" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/display_b"
android:text="#string/b_weight" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/display_c"
android:text="#string/c_weight" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/display_d"
android:text="#string/d_weight" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/display_e"
android:text="#string/e_weight" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/display_f"
android:text="#string/f_weight" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Weight" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/weight_display"
android:text="#string/weight_display" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="CG" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/cg_display" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="25dp"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Last Weights Entered" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/last_weight"
android:id="#+id/display_last_entry_1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/last_weight2"
android:id="#+id/display_last_entry_2" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/last_weight3"
android:id="#+id/display_last_entry_3"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/last_weight4"
android:id="#+id/display_last_entry_4" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/last_weight5"
android:id="#+id/display_last_entry_5"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fuel Tanks"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<TextView
android:text="Main Tanks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView5"
android:layout_weight="1"/>
<TextView
android:text="Auxiliary Tanks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView4"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButton2"
android:layout_weight="1" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButton"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<TextView
android:text="0 gallons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView3"
android:layout_weight="1"/>
<TextView
android:text="0 gallons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView2"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<TextView
android:text="0 pounds"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView3"
android:layout_weight="1"/>
<TextView
android:text="0 pounds"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView2"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="30dp"
android:text="Weight in Section" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/radio_section">
<RadioButton
android:id="#+id/radio_section_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonClicked"
android:text="A" />
<RadioButton
android:id="#+id/radio_section_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonClicked"
android:text="B" />
<RadioButton
android:id="#+id/radio_section_c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonClicked"
android:text="C" />
<RadioButton
android:id="#+id/radio_section_d"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonClicked"
android:text="D" />
<RadioButton
android:id="#+id/radio_section_e"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonClicked"
android:text="E" />
<RadioButton
android:id="#+id/radio_section_f"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonClicked"
android:text="F" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/enter_weight_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:gravity="center"
android:inputType="number"
android:text="#string/enter_weight" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/add_weight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Add Weight"
android:onClick="add_weight"/>
<Button
android:text="Subtract Weight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/subtract_weight"
android:layout_weight="1"
android:onClick="subtract_weight" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/change_aircraft"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Change Aircraft"
android:onClick="change_aircraft"/>
<Button
android:text="Calculator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/calculator"
android:layout_weight="1"
android:onClick="startActivity(calculator)" />
</LinearLayout>
Java
package com.tropicaircharters.android.employee;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.TextView;
import android.view.View;
import static android.R.attr.onClick;
import static android.R.id.input;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.T;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.V;
import static com.tropicaircharters.android.employee.R.id.display_last_entry_1;
import static com.tropicaircharters.android.employee.R.id.subtract_weight;
import static com.tropicaircharters.android.employee.R.string.a_weight;
import static com.tropicaircharters.android.employee.R.string.weight_display;
public class weightAndBalance extends AppCompatActivity {
//These are weights for each section.
int main_tank_gallons = 0;
int main_tank_weight = main_tank_gallons*6;
int aux_tank_gallons = 0;
int aux_tank_weight = aux_tank_gallons*6;
EditText enter_weight_box;
TextView display_a, weight_display, display_last_weight, display_last_weight_2, display_last_weight_3, display_last_weight_4, display_last_weight_5;
Button add_weight, subtract_weight;
RadioButton radio_section_a, radio_section_b, radio_section_c, radio_section_d, radio_section_e, radio_section_f;
double enter_weight, total_weight, a_weight;
String current_section, last_shizzle, last_shizzle2, last_shizzle3, last_shizzle4, last_shizzle5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weight_and_balance);
enter_weight_box = (EditText) findViewById(R.id.enter_weight_box);
display_a = (TextView) findViewById(R.id.display_a);
weight_display = (TextView) findViewById(R.id.weight_display);
add_weight = (Button) findViewById(R.id.add_weight);
subtract_weight = (Button) findViewById(R.id.subtract_weight);
display_last_weight = (TextView) findViewById(R.id.display_last_entry_1);
display_last_weight_2 = (TextView) findViewById(R.id.display_last_entry_2);
display_last_weight_3 = (TextView) findViewById(R.id.display_last_entry_3);
display_last_weight_4 = (TextView) findViewById(R.id.display_last_entry_4);
display_last_weight_5 = (TextView) findViewById(R.id.display_last_entry_5);
//This displays adds the entered number to A, the Total,
// and creates the last weights entered log.
add_weight.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
enter_weight = Double.parseDouble(enter_weight_box.getText().toString());
a_weight = a_weight + enter_weight;
total_weight = total_weight + enter_weight;
last_shizzle5 = last_shizzle4;
last_shizzle4 = last_shizzle3;
last_shizzle3 = last_shizzle2;
last_shizzle2 = last_shizzle;
last_shizzle = "Added " + Double.toString(enter_weight) + " in A";
//These create displays for totals and the last entered log.
display_a.setText(Double.toString(a_weight));
weight_display.setText(Double.toString(total_weight));
display_last_weight.setText(last_shizzle);
display_last_weight_2.setText(last_shizzle2);
display_last_weight_3.setText(last_shizzle3);
display_last_weight_4.setText(last_shizzle4);
display_last_weight_5.setText(last_shizzle5);
enter_weight_box.setText("");
}
});
//This displays and subtracts the entered number to A, the Total,
// and creates the last weights entered log.
subtract_weight.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
enter_weight = Double.parseDouble(enter_weight_box.getText().toString());
a_weight = a_weight - enter_weight;
total_weight = total_weight - enter_weight;
last_shizzle5 = last_shizzle4;
last_shizzle4 = last_shizzle3;
last_shizzle3 = last_shizzle2;
last_shizzle2 = last_shizzle;
last_shizzle = "Subtracted " + Double.toString(enter_weight) + " in A";
//These create displays for totals and the last entered log.
display_a.setText(Double.toString(a_weight));
weight_display.setText(Double.toString(total_weight));
display_last_weight.setText("Subtracted "+Double.toString(enter_weight)+" in "+"A");
display_last_weight.setText(last_shizzle);
display_last_weight_2.setText(last_shizzle2);
display_last_weight_3.setText(last_shizzle3);
display_last_weight_4.setText(last_shizzle4);
display_last_weight_5.setText(last_shizzle5);
enter_weight_box.setText("");
}
});
}
}

onCreateViewHolder is being called only half of the times the actual count returned by getItemCount - Android

I am currently working with an Android App using RecyclerView and CardView .I am showing lists of UserPosts(Contains text and Image both) inside RecyclerView . The UserPost item has been created using CardView .
Edit
The problem I am facing here is onCreateViewHolder method is being called only half of the times the actual count returned by getItemCount method even I scrolls till end of the list .E.g I have a list of 10 posts then onCreateViewHolder calls for 5 times only . It should call 10 times (when I scroll till end of the list) first and then created views will be recycled . But it is not happening actually . I have visited many Stackoverflow posts but no luck .
Here is code UserPostAdapter.java :
package com.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.model.UserPostsListItem;
import com.musomeet.R;
import com.squareup.picasso.Picasso;
import java.util.List;
import de.hdodenhof.circleimageview.CircleImageView;
/**
* Created by SI_Android on 10/21/2015 .
*
*/
public class UserPostAdapter extends RecyclerView.Adapter<UserPostAdapter.UserPostViewHolder> {
private List<UserPostsListItem> userPostList;
Context conetxt ;
LayoutInflater inflater ;
public UserPostAdapter(){
}
public UserPostAdapter(Context conetxt, List<UserPostsListItem> postlist) {
this.userPostList = postlist;
this.conetxt = conetxt ;
inflater = LayoutInflater.from(this.conetxt);
}
#Override
public int getItemCount() {
//int size = 0 ;
//if( userPostList != null )
//size = userPostList.size();
return userPostList.size();
}
#Override
public void onBindViewHolder(UserPostViewHolder videoViewHolder, int position) {
UserPostsListItem item = userPostList.get(position);
videoViewHolder.userName.setText(item.getName());
videoViewHolder.userLocation.setText(item.getCity()+", "+item.getCountry());
videoViewHolder.timeAgo.setText(item.getAgo());
videoViewHolder.likes.setText(item.getNo_of_likes());
videoViewHolder.comments.setText(item.getNo_of_comments());
videoViewHolder.userMessage.setText(item.getContent());
if(!TextUtils.isEmpty(item.getPhoto())) {
Picasso.with(conetxt).load(item.getPhoto()).into(videoViewHolder.circleImageView);
}
String url = item.getAttachment_url() ;
if (!TextUtils.isEmpty(url)) {
if ((url.endsWith(".png") || url.endsWith(".jpg"))) {
//videoViewHolder.attachment.setVisibility(View.VISIBLE);
Picasso.with(conetxt).load(url).into(videoViewHolder.attachment);
}
}
}
#Override
public UserPostViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = inflater.inflate(R.layout.userposts_listitem_textmessage, viewGroup, false);
return new UserPostViewHolder(itemView);
}
public static class UserPostViewHolder extends RecyclerView.ViewHolder {
TextView userName, userLocation, timeAgo, likes, comments, userMessage;
ImageView attachment;
CircleImageView circleImageView;
public UserPostViewHolder(View v){
super(v);
attachment = (ImageView) v.findViewById(R.id.attachment);
circleImageView = (CircleImageView) v.findViewById(R.id.circleView);
userMessage = (TextView) v.findViewById(R.id.massage);
userName = (TextView) v.findViewById(R.id.name);
userLocation = (TextView) v.findViewById(R.id.location);
timeAgo = (TextView) v.findViewById(R.id.time);
likes = (TextView) v.findViewById(R.id.likespoints);
comments = (TextView) v.findViewById(R.id.commentspoints);
}
}
}
Here is the userposts_listitem_textmessage.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardView="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/userpopstcardview"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
cardView:cardElevation="5dp"
cardView:cardCornerRadius="10dp"
cardView:cardBackgroundColor="#fff"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:src="#drawable/contact"
android:layout_margin="10dp"
android:id="#+id/circleView"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
android:layout_marginLeft="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:text="Jasmine"
android:textSize="20sp"
android:textStyle="bold"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#929292"
android:text="Los Angles, California"
android:textSize="14sp"
android:textStyle="normal"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#929292"
android:text="-"
android:textSize="14sp"
android:textStyle="normal"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#929292"
android:text="2 hours ago"
android:textSize="14sp"
android:textStyle="normal"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/userListContentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
android:orientation="vertical">
<TextView
android:id="#+id/massage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/recet_text"
android:textColor="#000"
android:textSize="15sp"
android:padding="10dp"
android:scrollbars="vertical"/>
<ImageView
android:id="#+id/attachment"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="10dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:tint="#00000000"
android:src="#drawable/contact"
android:visibility="visible"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/likespoints"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:textColor="#929292"
android:textSize="14sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Likes"
android:textColor="#929292"
android:textSize="14sp"
android:layout_marginLeft="5dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="30dp">
<TextView
android:id="#+id/commentspoints"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:textColor="#929292"
android:textSize="14sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Comments"
android:textColor="#929292"
android:textSize="14sp"
android:layout_marginLeft="5dp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_height="1dp"
android:layout_width="match_parent">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#eeeeee"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:weightSum="3">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="#+id/like"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Like"
android:gravity="center"
android:layout_gravity="center"
android:textSize="14sp"
android:textColor="#929292"
android:padding="10dp"
android:clickable="true"
android:background="#drawable/textview_effect"/>
</LinearLayout>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="1dp">
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#eeeeee"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="#+id/comment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Comment"
android:gravity="center"
android:layout_gravity="center"
android:textSize="14sp"
android:textColor="#929292"
android:padding="10dp"
android:clickable="true"
android:background="#drawable/textview_effect"/>
</LinearLayout>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="1dp">
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#eeeeee"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="#+id/share"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Share"
android:gravity="center"
android:layout_gravity="center"
android:textSize="14sp"
android:textColor="#929292"
android:padding="10dp"
android:clickable="true"
android:background="#drawable/textview_effect"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
What you see is an expected behaviour. RecylerView won't create as many view as adapter's getItemCount() returns. It create view as many as it's necessary for rendering a view.
For instance, if your adapter contains 100 elements, but only 5 are displayed at at time, system will create 5 views (it may create 1 or 2 more view as "buffer") and reuse these views when user scrolls.
When a View is recycled, it doesn't get "reset" to its original state, so it and its child Views still have the attributes that were set on them the last time the ViewHolder was passed into onBindViewHolder(). Since you are currently only setting the image when your if conditions are true, the ImageView is not updated if either of those conditions is false, and it will still have the image set on it previously. You simply need to set the ImageView's Bitmap to null when those conditions are not true. Also, your two if statements can be combined:
#Override
public void onBindViewHolder(UserPostViewHolder videoViewHolder, int position) {
UserPostsListItem item = userPostList.get(position);
...
String url = item.getAttachment_url() ;
if (!TextUtils.isEmpty(url) && (url.endsWith(".png") || url.endsWith(".jpg"))) {
Picasso.with(conetxt).load(url).into(videoViewHolder.attachment);
}
else {
videoViewHolder.attachment.setImageBitmap(null);
}
}

how to expand and collapse the textView which is having EditText as its child

i am new to android..In my xml file i have used three textviews,
inside that i have used three edittext for every textview..
what i want is when i press a particular textview it has to expand to get their
child(edittexts)..and when i again press the textview should collapse back.
Now in my code when i press textview it expands,but when i press again its not collapsing.
Below is my code..any help would be appreciated...thanks in advance..
Java file
CategoryPage.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class CategoryPage extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category_page);
TextView btnProfile = (TextView) findViewById(R.id.btnprofile);
TextView btnSettings = (TextView) findViewById(R.id.btncitizen);
TextView btnPrivacy = (TextView) findViewById(R.id.btnprivacy);
View panelProfile = findViewById(R.id.panelProfile);
panelProfile.setVisibility(View.GONE);
View panelSettings = findViewById(R.id.panelSettings);
panelSettings.setVisibility(View.GONE);
View panelPrivacy = findViewById(R.id.panelPrivacy);
panelPrivacy.setVisibility(View.GONE);
btnProfile.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// DO STUFF
View panelProfile = findViewById(R.id.panelProfile);
panelProfile.setVisibility(View.VISIBLE);
View panelSettings = findViewById(R.id.panelSettings);
panelSettings.setVisibility(View.GONE);
View panelPrivacy = findViewById(R.id.panelPrivacy);
panelPrivacy.setVisibility(View.GONE);
}
});
btnSettings.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// DO STUFF
View panelProfile = findViewById(R.id.panelProfile);
panelProfile.setVisibility(View.GONE);
View panelSettings = findViewById(R.id.panelSettings);
panelSettings.setVisibility(View.VISIBLE);
View panelPrivacy = findViewById(R.id.panelPrivacy);
panelPrivacy.setVisibility(View.GONE);
}
});
btnPrivacy.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// DO STUFF
View panelProfile = findViewById(R.id.panelProfile);
panelProfile.setVisibility(View.GONE);
View panelSettings = findViewById(R.id.panelSettings);
panelSettings.setVisibility(View.GONE);
View panelPrivacy = findViewById(R.id.panelPrivacy);
panelPrivacy.setVisibility(View.VISIBLE);
}
});
}
}
XML File
activity_category_page.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff">
<LinearLayout android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="5dip"
android:paddingBottom="5dip">
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="0dp"
android:src="#drawable/miiskylogo" />
</LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/header" >
<LinearLayout
android:id="#+id/root_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFFFF"
android:layout_weight="1"
android:layout_marginTop="10dp"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:orientation="vertical">
<TextView
android:id="#+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:text="#string/Text"
android:textSize="20dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textview1"
android:text="#string/Text2"
android:layout_marginBottom="10dp"
android:textSize="13dp"
/>
<TextView
android:id="#+id/btnprofile"
android:layout_width="match_parent"
android:layout_height="35dp"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:background="#drawable/verifyedit"
android:text="Profile Vault"
android:textColor="#000"
android:textSize="15dp" />
<LinearLayout
android:id="#+id/panelProfile"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFFFFFFF">
<LinearLayout
android:id="#+id/panelProfile1"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginTop="17dp"
android:layout_gravity="center_horizontal"
android:background="#FFFFFFFF"
android:orientation="horizontal" >
<EditText
android:id="#+id/salutation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.81"
android:background="#drawable/verifyedit"
android:ems="10"
android:paddingLeft="10dp"
android:hint="Salutation" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:id="#+id/panelProfile2"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginTop="8dp"
android:layout_gravity="center_horizontal"
android:background="#FFFFFFFF"
android:orientation="horizontal" >
<EditText
android:id="#+id/firstname"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.81"
android:background="#drawable/verifyedit"
android:ems="10"
android:paddingLeft="10dp"
android:hint="First Name" />
</LinearLayout>
<LinearLayout
android:id="#+id/panelProfile3"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginTop="8dp"
android:layout_gravity="center_horizontal"
android:background="#FFFFFFFF"
android:orientation="horizontal" >
<EditText
android:id="#+id/middlename"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.81"
android:background="#drawable/verifyedit"
android:ems="10"
android:paddingLeft="10dp"
android:hint="Middle Name" />
</LinearLayout>
<LinearLayout
android:id="#+id/panelProfile4"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginTop="8dp"
android:layout_gravity="center_horizontal"
android:background="#FFFFFFFF"
android:orientation="horizontal" >
<EditText
android:id="#+id/lastname"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.81"
android:background="#drawable/verifyedit"
android:ems="10"
android:paddingLeft="10dp"
android:hint="Last Name" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="#+id/btncitizen"
android:layout_width="match_parent"
android:layout_height="35dp"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:layout_marginTop="15dp"
android:background="#drawable/verifyedit"
android:text="Citizenship/Registration"
android:textColor="#000"
android:textSize="15dp" />
<LinearLayout
android:id="#+id/panelSettings"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFFFFFFF">
<LinearLayout
android:id="#+id/panelSettings1"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginTop="17dp"
android:layout_gravity="center_horizontal"
android:background="#FFFFFFFF"
android:orientation="horizontal" >
<EditText
android:id="#+id/currentaddress"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.81"
android:background="#drawable/verifyedit"
android:ems="10"
android:paddingLeft="10dp"
android:hint="Current address" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:id="#+id/panelSettings2"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginTop="8dp"
android:layout_gravity="center_horizontal"
android:background="#FFFFFFFF"
android:orientation="horizontal" >
<EditText
android:id="#+id/houseno"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.81"
android:background="#drawable/verifyedit"
android:ems="10"
android:paddingLeft="10dp"
android:hint="House no" />
</LinearLayout>
<LinearLayout
android:id="#+id/panelSettings3"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginTop="8dp"
android:layout_gravity="center_horizontal"
android:background="#FFFFFFFF"
android:orientation="horizontal" >
<EditText
android:id="#+id/roadno"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.81"
android:background="#drawable/verifyedit"
android:ems="10"
android:paddingLeft="10dp"
android:hint="Road no" />
</LinearLayout>
<LinearLayout
android:id="#+id/panelSettings4"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginTop="8dp"
android:layout_gravity="center_horizontal"
android:background="#FFFFFFFF"
android:orientation="horizontal" >
<EditText
android:id="#+id/location"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.81"
android:background="#drawable/verifyedit"
android:ems="10"
android:paddingLeft="10dp"
android:hint="Location" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="#+id/btnprivacy"
android:layout_width="match_parent"
android:layout_height="35dp"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:layout_marginTop="15dp"
android:background="#drawable/verifyedit"
android:text="Banking"
android:textColor="#000"
android:textSize="15dp" />
<LinearLayout
android:id="#+id/panelPrivacy"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFFFFFFF">
<LinearLayout
android:id="#+id/panelPrivacy1"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginTop="17dp"
android:layout_gravity="center_horizontal"
android:background="#FFFFFFFF"
android:orientation="horizontal" >
<EditText
android:id="#+id/bankacc"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.81"
android:background="#drawable/verifyedit"
android:ems="10"
android:paddingLeft="10dp"
android:hint="Bank account no" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:id="#+id/panelPrivacy2"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginTop="8dp"
android:layout_gravity="center_horizontal"
android:background="#FFFFFFFF"
android:orientation="horizontal" >
<EditText
android:id="#+id/acctype"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.81"
android:background="#drawable/verifyedit"
android:ems="10"
android:paddingLeft="10dp"
android:hint="Account Type" />
</LinearLayout>
<LinearLayout
android:id="#+id/panelPrivacy3"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginTop="8dp"
android:layout_gravity="center_horizontal"
android:background="#FFFFFFFF"
android:orientation="horizontal" >
<EditText
android:id="#+id/bankcredit"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.81"
android:background="#drawable/verifyedit"
android:ems="10"
android:paddingLeft="10dp"
android:hint="Bank Credit Card No" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
check if view is visible or not onClick of TextView:
if(panelProfile.getVisibility() == View.VISIBLE) {
panelProfile.setVisiblity(View.GONE);
} else {
panelProfile.setVisiblity(View.VISIBLE);
}
Note: You are declaring below code multiple times, instead just do it only once.
View panelProfile = findViewById(R.id.panelProfile);
same with,
View panelSettings = findViewById(R.id.panelSettings);
View panelPrivacy = findViewById(R.id.panelPrivacy);

How to remove list view item in list view android?

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....

Categories

Resources