I am working with com.beardedhen.androidbootstrap.BootstrapDropDown and I don't understand how to get the value of the selected item from the dropdown
dropdownresource.xml
`<resources>
<string name="app_name">GTFirstPage</string>
<string name="navigation_drawer_open">Open navigation drawer</string>
<string name="navigation_drawer_close">Close navigation drawer</string>
<string name="action_settings">Settings</string>
<array name="bootstrap_dropdown_example_data">
<item>Family Trip</item>
<item>Camping</item>
<item>Business Trip</item>
<item>{dropdown_separator}</item>
<item>Custom Trip</item>
</array>
</resources>`
plan.xml
<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"
tools:context="com.example.hp.gtfirstpage.plantrip">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<AutoCompleteTextView
android:id="#+id/autotv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left|center_horizontal|top"
android:layout_marginTop="25dp"
android:background="#f3f3f3"
android:hint="Where do you want to go?"
android:textSize="30sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="107dp"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="#+id/textView2"
android:layout_width="176dp"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text="Destination"
android:textSize="30sp" />
<TextView
android:id="#+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:background="#f3f3f3"
android:ems="10"
android:textSize="20sp"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="107dp"
android:orientation="horizontal">
<TextView
android:id="#+id/textView11"
android:layout_width="176dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="0.00"
android:text="Category"
android:textSize="30sp"/>
<com.beardedhen.androidbootstrap.BootstrapDropDown
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
app:bootstrapBrand="regular"
app:bootstrapExpandDirection="down"
app:bootstrapSize="md"
app:bootstrapText="Select Category"
app:dropdownResource="#array/bootstrap_dropdown_example_data"
app:roundedCorners="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="107dp"
android:orientation="vertical">
<TextView
android:id="#+id/textView13"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Date"
android:textSize="30sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="85dp"
android:orientation="horizontal">
<TextView
android:id="#+id/tv4"
android:layout_width="165dp"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="20sp"
android:background="#f3f3f3"
android:inputType="date" />
<TextView
android:id="#+id/textView14"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:text="to"
android:textSize="30sp" />
<TextView
android:id="#+id/tv5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="20sp"
android:background="#f3f3f3"
android:inputType="date" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="107dp"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<Button
android:id="#+id/bt1"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="BACK" />
<Button
android:id="#+id/bt2"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="DONE" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
plantrip.java
package com.example.hp.gtfirstpage;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.TextView;
import android.widget.Toast;
import com.beardedhen.androidbootstrap.BootstrapDropDown;
public class plantrip extends Fragment implements
AdapterView.OnItemSelectedListener, DatePickerDialog.OnDateSetListener
{
BootstrapDropDown spin;
String city,category,dest;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View root=null;
root= inflater.inflate(R.layout.fragment_plantrip, container, false);
spin = (BootstrapDropDown) root.findViewById(R.id.spinner);
spin.setOnDropDownItemClickListener(new
BootstrapDropDown.OnDropDownItemClickListener() {
#Override
public void onItemClick(ViewGroup parent, View v, int id) {
// category=spin.getResources().getResourceName(spin.getId());
}
});
return root;
}
}
I am new to android so if there is any mistake please explain properly.
Please tell me how to retrieve items from dropdown list.
Thank You!!!
BootstrapDropDown has a method called getDropdownData();
This method returns a String[] array containing the items in your BootstapDropDown. Use int id as an index for your array. The 'int id' gets passed to you as a parameter of the method:
public void onItemClick(ViewGroup parent, View v, int id) {...}
I believe you can grab the selected item by adding this line to your code:
String[] dropDownItems = spin.getDropdownData();
String item = dropDownItems[id];
spin.setOnDropDownItemClickListener(new
BootstrapDropDown.OnDropDownItemClickListener() {
#Override
public void onItemClick(ViewGroup parent, View v, int id) {
// category=spin.getResources().getResourceName(spin.getId());
String[] dropDownItems = spin.getDropdownData();
String item = dropDownItems[id];
}
});
// made a couple of edits to fix a mistake
// I hope this helped!
Related
I have a problem with item height and gravity in a horizontal recyclerview as you can see in the picture: RecyclerView item height and gravity not working
How to set the item height to same height as recyclerciew and the gravity at the bottom of recyclerview ?
Thank you for your help.
My code for Activity_Drill_Graph.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Drill_Graph_Activity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/toolbar_drill_graph"
layout="#layout/toolbar_drill_graph" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/margin_5dp"
android:background="#color/grey"
android:gravity="bottom"
android:orientation="horizontal"
android:weightSum="100">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:background="#color/lightgrey"
android:orientation="vertical"
android:padding="#dimen/padding_5dp">
<TextView
android:id="#+id/textView10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_10"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_9"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_8"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_7"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_6"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_5"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_4"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_3"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_2"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_1"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_0"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textViewDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/date"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="18sp" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerViewDrillGraph"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:layout_margin="#dimen/margin_5dp"
android:background="#color/white"
android:orientation="horizontal"
android:padding="#dimen/padding_5dp"
android:scrollbars="horizontal" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
item code: drill_graph_single_column.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:background="#color/lightgrey"
android:gravity="bottom"
android:orientation="vertical"
android:padding="#dimen/padding_1dp"
android:weightSum="110">
<Button
android:id="#+id/btnSkill"
style="#style/btnStyleScoreGrey"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="50"
android:text="#string/skill"
android:textAllCaps="false"
android:textSize="16sp" />
<Button
android:id="#+id/btnDate"
style="#style/btnStyleDrillGraphGrey"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="#dimen/margin_1dp"
android:layout_weight="10"
android:text="#string/date"
android:textAllCaps="false"
android:textSize="12sp" />
</LinearLayout>
My code for RecyclerView Adapter:
package org.poolshot.poolshotacademy;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import java.util.List;
public class My_DrillGraph_RecyclerView_Adapter extends RecyclerView.Adapter<My_DrillGraph_RecyclerView_Adapter.MyHolder> {
SharedPreferences defaultPreferencesSettings;
private Context myRCAContext;
private List<String> myDrillGraphDates;
private List<Integer> myDrillGraphSkill;
// constructor
public My_DrillGraph_RecyclerView_Adapter(Context applicationContext, List<String> mDrillGraphDates, List<Integer> mDrillGraphSkill) {
this.myRCAContext = applicationContext;
this.myDrillGraphDates = mDrillGraphDates;
this.myDrillGraphSkill = mDrillGraphSkill;
}
#NonNull
#Override
public MyHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int position) {
View layout = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.drill_graph_single_column,null);
MyHolder myHolder = new MyHolder(layout);
return myHolder;
}
#Override
public void onBindViewHolder(#NonNull final MyHolder viewHolder, final int position) {
String skill = myDrillGraphSkill.get(position).toString();
// set color for buttons
viewHolder.btnDate.setBackground(myRCAContext.getResources().getDrawable(R.drawable.custom_btn_grey));
viewHolder.btnSkill.setTextColor(myRCAContext.getResources().getColor(R.color.white));
// set text for buttons
viewHolder.btnDate.setText(myDrillGraphDates.get(position));
viewHolder.btnSkill.setText(skill);
}
#Override
public int getItemCount() {
return myDrillGraphDates.size();
}
public static class MyHolder extends RecyclerView.ViewHolder {
Button btnDate;
Button btnSkill;
public MyHolder(View itemView) {
super(itemView);
btnDate = itemView.findViewById(R.id.btnDate);
btnSkill = itemView.findViewById(R.id.btnSkill);
}
}
}
This is my solution :
I changed the code of the class MyHolder as :
public static class MyHolder extends RecyclerView.ViewHolder {
Button btnDate;
Button btnSkill;
public MyHolder(View itemView) {
super(itemView);
btnDate = itemView.findViewById(R.id.btnDate);
btnSkill = itemView.findViewById(R.id.btnSkill);
LinearLayout.LayoutParams layoutParams =
new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.gravity = Gravity.BOTTOM;
itemView.setLayoutParams(layoutParams);
}
}
i am trying to set my json into my Recyclerview, where i can looking with recycler view, but the json is shown in just one row. How can fix this?
This is my code :
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(PenawaranDetail.this, LinearLayoutManager.HORIZONTAL, true));
recyclerView.setHasFixedSize(true);
This is my Adapter Code :
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
import android.widget.ScrollView;
import android.widget.TextView;
/**
* Created by Programx4 on 1/21/2017.
*/
public class AdapterBarangPenawaran extends RecyclerView.Adapter <AdapterBarangPenawaran.ViewHolder> {
private Context context;
private List<DataBarangPenawaran> data;
public AdapterBarangPenawaran(Context context, List<DataBarangPenawaran>data){
this.context = context;
this.data = data;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View Itemview = LayoutInflater.from(parent.getContext()).inflate(R.layout.daftarbarangpenawaran, parent, false);
return new ViewHolder(Itemview);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.tvnama.setText(data.get(position).getBarang());
holder.tvjumlah.setText(String.valueOf(data.get(position).getJumlah()));
holder.tvsup1.setText(String.valueOf(data.get(position).getHarga1()));
holder.tvsup2.setText(String.valueOf(data.get(position).getHarga2()));
holder.tvsup3.setText(String.valueOf(data.get(position).getHarga3()));
}
#Override
public int getItemCount() {
return data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView tvnama, tvjumlah, tvsup1, tvsup2, tvsup3, tvtotal;
public ViewHolder(View Itemview){
super(Itemview);
tvjumlah = (TextView)Itemview.findViewById(R.id.jumlah);
tvnama = (TextView)Itemview.findViewById(R.id.namabar);
tvsup1 = (TextView)Itemview.findViewById(R.id.sup1);
tvsup2 = (TextView)Itemview.findViewById(R.id.sup2);
tvsup3 = (TextView)Itemview.findViewById(R.id.sup3);
tvtotal = (TextView)Itemview.findViewById(R.id.total);
}
}
}
and this is my xml layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#383735"
android:baselineAligned="false">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp">
<TextView
android:layout_width="130dp"
android:layout_height="wrap_content"
android:id="#+id/koded"
android:textColor="#ffffff"
android:text="Kode: "
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/koded"
android:textColor="#ffffff"
android:id="#+id/isikoded"/>
<TextView
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_below="#id/isikoded"
android:id="#+id/tglaju"
android:textColor="#ffffff"
android:text="Tgl Input: "
android:layout_marginTop="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/tglaju"
android:layout_below="#+id/isikoded"
android:id="#+id/isitglaju"
android:textColor="#ffffff"
android:layout_marginTop="10dp"/>
<TextView
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_below="#id/isitglaju"
android:id="#+id/jpegawai"
android:textColor="#ffffff"
android:text="Pegawai: "
android:layout_marginTop="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/jpegawai"
android:layout_below="#+id/isitglaju"
android:id="#+id/isipegawai"
android:textColor="#ffffff"
android:layout_marginTop="10dp"/>
<TextView
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_below="#id/isipegawai"
android:id="#+id/jdivisi"
android:textColor="#ffffff"
android:text="Divisi: "
android:layout_marginTop="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/jdivisi"
android:layout_below="#+id/isipegawai"
android:id="#+id/isidivisi"
android:textColor="#ffffff"
android:layout_marginTop="10dp"/>
<TextView
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_below="#id/isidivisi"
android:id="#+id/tglbth"
android:textColor="#ffffff"
android:text="Tgl Penawaran: "
android:layout_marginTop="10dp"
/>
<TextView android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/tglbth"
android:layout_below="#+id/isidivisi"
android:id="#+id/isitglbutuh"
android:textColor="#ffffff"/>
<TextView
android:layout_width="130dp"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:id="#+id/supd"
android:layout_marginTop="10dp"
android:text="No Penawaran: "
android:layout_below="#+id/tglbth"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/isitglbutuh"
android:layout_toRightOf="#id/supd"
android:id="#+id/isinomor"
android:textColor="#ffffff"
android:layout_marginTop="10dp"/>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/isinomor"
android:id="#+id/tablejdl"
android:weightSum="7"
android:layout_marginTop="20dp"
>
<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:weightSum="7"
android:gravity="center_horizontal">
<TextView
android:textColor="#ffffff"
android:layout_marginTop="10dp"
android:layout_width="0dp"
android:textSize="11sp"
android:layout_height="wrap_content"
android:text="Barang"
android:layout_weight="2"
android:id="#+id/kodebar"
/>
<TextView
android:textColor="#ffffff"
android:layout_marginTop="10dp"
android:layout_width="0dp"
android:textSize="11sp"
android:layout_height="wrap_content"
android:text="Jumlah"
android:layout_weight="1"
android:id="#+id/jumlah"
/>
<TextView
android:textColor="#ffffff"
android:layout_marginTop="10dp"
android:layout_width="0dp"
android:textSize="11sp"
android:layout_height="wrap_content"
android:text="Sup1"
android:layout_weight="1"
android:id="#+id/namabar"/>
<TextView
android:textColor="#ffffff"
android:layout_marginTop="10dp"
android:layout_width="0dp"
android:textSize="11sp"
android:layout_height="wrap_content"
android:text="Sup2"
android:layout_weight="1"
android:id="#+id/jumlahbar"/>
<TextView
android:textColor="#ffffff"
android:layout_marginTop="10dp"
android:layout_width="0dp"
android:textSize="11sp"
android:layout_height="wrap_content"
android:text="Sup3"
android:layout_weight="1"
android:id="#+id/stokbar"/>
<TextView
android:textColor="#ffffff"
android:layout_marginTop="10dp"
android:layout_width="0dp"
android:textSize="11sp"
android:layout_height="wrap_content"
android:text="Total"
android:layout_weight="1"
android:id="#+id/jtotal"/>
</TableRow>
</TableLayout>
<ScrollView
android:id="#+id/aaaa"
android:layout_width="match_parent"
android:layout_below="#id/tablejdl"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recyclerpenawaran">
</android.support.v7.widget.RecyclerView>
</ScrollView>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/aaaa"
android:id="#+id/tablejdl2"
android:weightSum="7"
android:layout_marginTop="15dp"
>
<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:weightSum="7"
android:gravity="center_horizontal">
<TextView
android:textColor="#ffffff"
android:layout_marginTop="10dp"
android:layout_width="0dp"
android:textSize="11sp"
android:layout_height="wrap_content"
android:layout_weight="2"
/>
<TextView
android:textColor="#ffffff"
android:layout_marginTop="10dp"
android:layout_width="0dp"
android:textSize="11sp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<CheckBox
android:textColor="#ffffff"
android:layout_marginTop="10dp"
android:layout_width="0dp"
android:textSize="11sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/checksup1"/>
<CheckBox
android:textColor="#ffffff"
android:layout_marginTop="10dp"
android:layout_width="0dp"
android:textSize="11sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/checksup2"/>
<CheckBox
android:textColor="#ffffff"
android:layout_marginTop="10dp"
android:layout_width="0dp"
android:textSize="11sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/checksup3"/>
<TextView
android:textColor="#ffffff"
android:layout_marginTop="10dp"
android:layout_width="0dp"
android:textSize="11sp"
android:layout_height="wrap_content"
android:text="Total"
android:layout_weight="1"
android:id="#+id/total"/>
</TableRow>
</TableLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tablejdl2"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="20dp"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tolak"
android:id="#+id/tolak"
android:background="#drawable/buttonback2"
android:layout_alignParentRight="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Approve"
android:layout_alignParentLeft="true"
android:id="#+id/btnapprove"
android:background="#drawable/buttonback"
/>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
</LinearLayout>
Thanks for your help
I think the problem is order ...
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(PenawaranDetail.this,LinearLayoutManager.HORIZONTAL, true));
recyclerView.setAdapter(adapter);
First, set layout manager before set adapter
recyclerView.setLayoutManager(new LinearLayoutManager(PenawaranDetail.this, LinearLayoutManager.HORIZONTAL, true));
recyclerView.setAdapter(adapter);
Second, change your item layout root parameters, the height should be wrap_content
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
....
</LinearLayout>
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);
}
}
I am new to android listviews and I am trying to populate a listview in android by a list of items coming from a webservice. I know that the list contains more than one record coming from the webservice but my custom listview displays only the first record from the list. I checked the size of the list and there is always more than one records but the listview is showing only one of them. My custom adapter is like following:
public class ListAdapter extends BaseAdapter {
Context ctx;
LayoutInflater lInflater;
ArrayList<LItem> lstItems;
ListAdapter(Context context, ArrayList<LItem> objects) {
ctx = context;
lstItems = objects;
lInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return lstItems.size();
}
#Override
public Object getItem(int position) {
return lstItems.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.list_style_task
, parent, false);
}
LItem p = getProduct(position);
((TextView) view.findViewById(R.id.tvMaterial )).setText(p.getMName() );
((TextView) view.findViewById(R.id.tvTask )).setText(p.getTName() );
((TextView) view.findViewById(R.id.tvBQuantity )).setText(p.getBQ() );
// CheckBox cbBuy = (CheckBox) view.findViewById(R.id.checkbox);
//cbBuy.setOnCheckedChangeListener(myCheckChangList);
// cbBuy.setTag(position);
// cbBuy.setChecked(p.selected);
return view;
}
LItem getProduct(int position)
{
return ((LItem) getItem(position));
}
ArrayList<LItem> getBox() {
ArrayList<LItem> box = new ArrayList<LItem>();
for (LItem p : lstItems) {
// if (p.selected)
// box.add(p);
}
return box;
}
OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
//getProduct((Integer) buttonView.getTag())= isChecked;
}
};
}
I am binding the listview as:
protected void onPostExecute(List<LItem> li ) {
super.onPostExecute(lstItm);
if(lstItm.size()>0) {
Adp=new ListAdapter(myactivity,lstItm);
lvTasks.setAdapter(Adp);
Log.d("Size---------",Integer.toString(lstItm.size()) );//here it writes more than one as size of the list.
}
}
My xml file for displaying lists is like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
>
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:focusable="false"
android:textColor="#FFF"
android:button="#drawable/custom_checkbox_design"
android:focusableInTouchMode="false"
android:text="" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Task:"
android:id="#+id/textView2"
android:layout_weight="1"
android:textColor="#FFF"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="task"
android:id="#+id/tvTask"
android:layout_weight="1"
android:textColor="#FFF"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="45dp"
android:layout_marginRight="20dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Material:"
android:id="#+id/textView1"
android:layout_weight="1"
android:textColor="#FFF"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="material"
android:id="#+id/tvMaterial"
android:layout_weight="1"
android:textColor="#FFF"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="45dp"
android:layout_marginRight="20dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Balanced Quantity:"
android:id="#+id/textView14"
android:layout_weight="1"
android:textColor="#FFF"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bquantity"
android:id="#+id/tvBQuantity"
android:layout_weight="1"
android:textColor="#FFF"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="45dp"
android:layout_marginRight="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Adjust Balanced Quantity:"
android:id="#+id/textView25"
android:layout_weight="1"
android:textColor="#FFF"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/tvAQuantity"
android:layout_weight="1"
android:textColor="#FFF"
/>
</LinearLayout>
</LinearLayout>
My xml for listview is like this:
<LinearLayout 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"
tools:context="kanix.highrise.com.highrise.generate_material_requisition">
<!-- TODO: Update blank fragment layout -->
<ScrollView android:id="#+id/ScrlView" android:layout_width="fill_parent" android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lstTaskQuantity"
android:layout_weight="1" >
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:id="#+id/txtLddsdfabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="Req. From :"
android:layout_weight="1"
android:textColor="#fff" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="date"
android:id="#+id/etDate"
android:layout_weight=".5"
android:hint="DD/MM/YYYY"/>
<ImageButton
android:layout_width="35dp"
android:layout_height="35dp"
android:id="#+id/btnimgcal"
android:src="#drawable/calender"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:id="#+id/txtLddsdfadsbel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="For Days :"
android:layout_weight="1"
android:textColor="#fff" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:text="0"
android:id="#+id/editText"
android:layout_weight="1.69" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:id="#+id/txtLddsddfgfadsbel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="Quantity :"
android:layout_weight="1"
android:textColor="#fff" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:text="0"
android:id="#+id/etQuantity"
android:layout_weight="1.60" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="center"
android:orientation="vertical"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#0080FF"
android:background="#fff"
android:text="Generate Requisition"
android:id="#+id/btnSave" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Issue was with the scrollview which was createing the issue. I just removed
<ScrollView android:id="#+id/ScrlView" android:layout_width="fill_parent" android:layout_height="fill_parent" >
</ScrollView>
and it worked for me :)
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