Handle same navigation drawer but change header text on every activity - android

Is there a way we can have same navigation drawer on each activity but with change in the header text? For instance I got two activities, my home activity will display a header text as home and my "about us" activity will display "about us" header text but all will have the same navigation settings.
BaseActivity
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public class BaseActivity extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
public DrawerLayout drawer;
ImageView navDrawerBtn;
HashMap<String, List<String>> listDataChild;
List<String> listDataHeader;
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
protected LinearLayout fullLayout;
protected FrameLayout actContent;
#Override
public void setContentView(final int layoutResID) {
fullLayout= (LinearLayout) getLayoutInflater().inflate(R.layout.home, null); // Your base layout here
actContent= (FrameLayout) fullLayout.findViewById(R.id.content_frame);
getLayoutInflater().inflate(layoutResID, actContent, true); // Setting the content of layout your provided to the act_content frame
super.setContentView(fullLayout);
// here you can get your drawer buttons and define how they should behave and what must they do, so you won't be needing to repeat it in every activity class
prepareListData();
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
navDrawerBtn = (ImageView)findViewById(R.id.headerDrawer);
expListView = (ExpandableListView) findViewById(R.id.lvExp);
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
expListView.setIndicatorBounds(402,465);
} else {
expListView.setIndicatorBoundsRelative(402,465);
}
drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
navDrawerBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(!drawer.isDrawerOpen(expListView)) {
drawer.openDrawer(expListView);
} else {
drawer.closeDrawer(expListView);
}
}
});
//listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
switch (childPosition) {
case 0:
Intent a = new Intent(getApplicationContext(), MainActivity.class);
startActivity(a);
break;
case 1:
Intent b = new Intent(getApplicationContext(), ScheduleActivity.class);
startActivity(b);
break;
}
return false;
// TODO Auto-generated method stub
}
});
}
/*
* Preparing the list data
*/
protected void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("VRP Medical Bay");
//listDataHeader.add("");
//listDataHeader.add("");
// Adding child data
List<String> listUnderVRP = new ArrayList<String>();
listUnderVRP.add("eDataClinical");
listUnderVRP.add("Schedule");
listUnderVRP.add("Dictate");
listUnderVRP.add("View Messages");
listUnderVRP.add("Reports for Signature");
listUnderVRP.add("View Billing");
listUnderVRP.add("View State");
listDataChild.put(listDataHeader.get(0), listUnderVRP); // Header, Child data
//listDataChild.put(listDataHeader.get(1), nowShowing);
//listDataChild.put(listDataHeader.get(2), comingSoon);
}
}
my home layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="73dp"
android:background="#color/actionbar" >
<ImageView
android:id="#+id/headerDrawer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_drawer"
android:contentDescription="#string/desc" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/actionHeaderText"
android:layout_toRightOf="#+id/headerDrawer"
android:src="#drawable/e_icon"
android:contentDescription="#string/desc" />
<TextView
android:id="#+id/actionHeaderText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/imageView1"
android:text="#string/actionbar_title"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/white"
android:textSize="32sp"
android:textStyle="bold"
android:typeface="monospace" />
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ExpandableListView
android:id="#+id/lvExp"
android:layout_width="470dp"
android:layout_height="match_parent"
android:groupIndicator="#drawable/group_selector"
android:transcriptMode="alwaysScroll"
android:layout_gravity="start"
android:childDivider="#4abcd7"
android:divider="#626262"
android:dividerHeight="4dp"
android:cacheColorHint="#fff" >
</ExpandableListView>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
My Billing Activity:
public class ViewBillingActivity extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_billing_test);
}
my billing layout:
<?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:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="73dp"
android:background="#color/actionbar" >
<ImageView
android:id="#+id/headerDrawer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:contentDescription="#string/desc"
android:src="#drawable/ic_drawer" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/actionHeaderText"
android:layout_toRightOf="#+id/headerDrawer"
android:src="#drawable/e_icon" />
<TextView
android:id="#+id/actionHeaderText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/imageView1"
android:text="#string/view_billing"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/white"
android:textSize="32sp"
android:textStyle="bold"
android:typeface="monospace" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="90dp"
android:background="#color/view_billing_bg"
android:paddingTop="10dp" >
<TextView
android:id="#+id/latestBillingTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="#string/latest_billing"
android:textColor="#color/white"
android:textSize="34sp"
android:textStyle="normal" />
<TextView
android:id="#+id/latestBillingDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/latestBillingTitle"
android:layout_centerHorizontal="true"
android:text="(03/01/2014 - 03/31/2014)"
android:textColor="#color/white"
android:textSize="24sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="720dp"
android:layout_height="820dp"
android:layout_below="#+id/relativeLayout1"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:background="#color/white"
android:paddingBottom="50dp" >
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"
android:background="#color/white" >
<RelativeLayout
android:id="#+id/relativeLayout7"
android:layout_width="60dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#color/view_mssgs_id_bg" >
<TextView
android:id="#+id/vbTxtID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:gravity="center_horizontal"
android:text="024"
android:textColor="#color/white"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:layout_width="500dp"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="4dp"
android:layout_toRightOf="#+id/relativeLayout7"
android:background="#color/tile_box" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="#string/vb_outstanding_balance"
android:textColor="#color/white"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="#+id/vbTxTOutBalance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView1"
android:layout_alignBottom="#+id/textView1"
android:layout_marginLeft="23dp"
android:layout_toRightOf="#+id/textView1"
android:text="0.00"
android:textColor="#color/white"
android:textSize="24sp" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout3"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/relativeLayout2"
android:layout_marginTop="4dp"
android:background="#color/tile_box" >
<TextView
android:id="#+id/vbTxtLastMonthBill"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="23dp"
android:layout_toRightOf="#+id/textView1"
android:text="2004.50"
android:textColor="#color/white"
android:textSize="24sp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/vbTxtLastMonthBill"
android:layout_alignBottom="#+id/vbTxtLastMonthBill"
android:layout_alignParentLeft="true"
android:layout_marginLeft="11dp"
android:text="#string/vb_last_month_bill"
android:textColor="#color/white"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout4"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/relativeLayout3"
android:layout_marginTop="4dp"
android:background="#color/tile_box" >
<TextView
android:id="#+id/vbTxtPaymentReceived"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="23dp"
android:layout_toRightOf="#+id/textView1"
android:text="2004.50"
android:textColor="#color/white"
android:textSize="24sp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/vbTxtPaymentReceived"
android:layout_alignBottom="#+id/vbTxtPaymentReceived"
android:layout_alignParentLeft="true"
android:layout_marginLeft="11dp"
android:text="#string/vb_payments_received"
android:textColor="#color/white"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout5"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/relativeLayout4"
android:layout_marginTop="4dp"
android:background="#color/tile_box" >
<TextView
android:id="#+id/vbTxtPaymentReceivedDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="23dp"
android:layout_toRightOf="#+id/textView1"
android:text="04/05/2014"
android:textColor="#color/white"
android:textSize="24sp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/vbTxtPaymentReceivedDate"
android:layout_alignBottom="#+id/vbTxtPaymentReceivedDate"
android:layout_alignParentLeft="true"
android:layout_marginLeft="11dp"
android:text="#string/vb_payment_received_date"
android:textColor="#color/white"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout6"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/relativeLayout5"
android:layout_marginTop="4dp"
android:background="#color/tile_box" >
<TextView
android:id="#+id/vbTxtBalance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="23dp"
android:layout_toRightOf="#+id/textView1"
android:text="1575.75"
android:textColor="#color/white"
android:textSize="24sp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/vbTxtBalance"
android:layout_alignBottom="#+id/vbTxtBalance"
android:layout_alignParentLeft="true"
android:layout_marginLeft="11dp"
android:text="#string/vb_balance"
android:textColor="#color/white"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>

First of all, according to design guidelines, it is better to set header text within the ActionBar when navigation drawer is open rather than having separate TextView header within navigation drawer layout.
But in case you really-really need separate "header" text within your navigation drawer - layout (and the way you interact with it) for your navigation drawer is exactly the same as any other regular layout you usually have in your activities. Just declare TextView, give it some reasonable id, get this text view in your activity (findViewById()) and set proper text

Write Change Text Code in onDrawerClosed and onDrawerOpened methods according to the text you want to display..

Related

Listview displays only one row from the list

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 :)

What's the difference between listview item and listview footer in rendering life cycle?

I have a listview contains items and footer views. Items layout works as it should, but footer's layout doesn't work like item views...
I could't understand where my fault is.
This is my item's layout xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/lstCardItem"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#drawable/pm_dashboard_cardshadow" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5.33dp"
android:layout_marginLeft="11.67dp"
android:layout_marginRight="11.67dp"
android:layout_marginTop="6dp" >
<ImageView
android:id="#+id/ivCard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:animateLayoutChanges="true"
android:scaleType="fitXY" />
<com.controls.DynamicResizeImageView
android:id="#+id/ivCardLock"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:background="#drawable/pm_dashboard_cardmask"
android:scaleType="center"
android:src="#drawable/selector_btn_dashboard_lock" />
<LinearLayout
android:id="#+id/llInfo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:gravity="top|right"
android:orientation="vertical"
android:paddingLeft="18dp"
android:paddingRight="18dp"
android:visibility="visible" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center|right"
android:gravity="center|right"
android:orientation="horizontal"
android:paddingBottom="4dp"
android:paddingTop="4dp" >
<TextView
android:id="#+id/tvCardBalance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/tvCurrency"
android:gravity="top"
android:text="10,344"
android:textColor="#color/white"
android:textSize="18dp"
android:typeface="serif" />
<TextView
android:id="#+id/tvCurrency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="3dp"
android:layout_marginTop="2dp"
android:gravity="top"
android:text="#string/global_currency"
android:textColor="#color/white"
android:textSize="10dp"
android:typeface="sans" />
<TextView
android:id="#+id/tvCardBalanceName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="#string/pgDashboard_lblBalanceBanner"
android:textColor="#color/white"
android:textSize="11.33dp"
android:typeface="monospace" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/pm_dashboard_seam" />
</LinearLayout>
For my businness side lifecycle:
private void refreshCards() {
if (applyButtons != null)
DashboardFragment.this.lvCards.removeFooterView(applyButtons);
applyButtons = new LinearLayout(getActivity());
applyButtons.setTag("FOOTER");
applyButtons.setOrientation(LinearLayout.VERTICAL);
AbsListView.LayoutParams LLParams = new AbsListView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
applyButtons.setLayoutParams(LLParams);
if (ininalCount < 32) {
View createNewCardButton = getApplyButtonView(R.string.pgDashboard_lblCreateIninalCard, R.drawable.pm_dashboard_card_yenikartolustur, R.drawable.pm_dashboard_arrow_icon, new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), CreateNewCardActivity.class);
startActivity(intent);
}
});
View AddCard = getApplyButtonView(R.string.pgDashboard_lblAddCard, R.drawable.pm_dashboard_card_yenikartekle, R.drawable.pm_dashboard_plus_icon, new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), AddCardActivity.class);
startActivity(intent);
}
});
applyButtons.addView(createNewCardButton);
applyButtons.addView(AddCard);
}
final View applyIngCard = getApplyButtonView(R.string.pgDashboard_lblApplyToIngCard, R.drawable.pm_dashboard_card_orange, R.drawable.pm_dashboard_arrow_icon, new View.OnClickListener() {
#Override
public void onClick(View v) {
CardApplicationActivity.Synchronizer.synchronize((BaseActivity) getActivity(), new OnSyncCompletedListener() {
#Override
public void onSyncCompleted() {
Intent intent = new Intent(getActivity(), CardApplicationActivity.class);
startActivity(intent);
}
});
}
});
applyButtons.addView(applyIngCard);
View view = new View(getActivity());
view.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, getResources().getDisplayMetrics())));
view.setBackgroundResource(getResources().getColor(R.color.transparent));
applyButtons.addView(view);
bindAdapter();
}
private View getApplyButtonView(int stringId, int backgroundId, int iconId, View.OnClickListener onClickListener) {
final View view = inflater.inflate(R.layout.dashboard_list_card_item, null);
return view;
}
public void bindAdapter() {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
DashboardFragment.this.lvCards.setLayoutTransition(new LayoutTransition());
DashboardFragment.this.lvCards.addFooterView(applyButtons, null, false);
if (adapter == null) {
adapter = new CardListAdapter(getActivity(), R.layout.dashboard_list_card_item, productModelContainerList);
controller = new CardListController(lvCards);
lvCards.setDropListener(this);
controller.setDragInitMode(CardListController.ON_LONG_PRESS);
lvCards.setFloatViewManager(controller);
lvCards.setOnTouchListener(controller);
lvCards.setAdapter(adapter);
}
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
DashboardFragment.this.lvCards.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
DashboardFragment.this.lvCards.setLayoutTransition(null);
}
}
Here i made some changes,, use these and let me know... otherwise i need your drawable resources to set this.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/lstCardItem"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#color/background_holo_dark" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5.33dp"
android:layout_marginLeft="11.67dp"
android:layout_marginRight="11.67dp"
android:layout_marginTop="6dp" >
<ImageView
android:id="#+id/ivCard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:animateLayoutChanges="true"
android:scaleType="fitXY" />
<ImageView
android:id="#+id/ivCardLock"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:background="#color/background_holo_light"
android:scaleType="center"
android:src="#color/bgColor" />
<LinearLayout
android:id="#+id/llInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/cropBG"
android:gravity="top|right"
android:orientation="vertical"
android:paddingLeft="18dp"
android:paddingRight="18dp"
android:visibility="visible" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right|center_horizontal"
android:gravity="center|right"
android:orientation="horizontal"
android:paddingBottom="4dp"
android:paddingTop="4dp" >
<TextView
android:id="#+id/tvCardBalance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/tvCurrency"
android:gravity="top"
android:text="10,344"
android:textColor="#android:color/white"
android:textSize="18dp"
android:typeface="serif" />
<TextView
android:id="#+id/tvCurrency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="3dp"
android:layout_marginTop="2dp"
android:gravity="top"
android:text="Currency"
android:textColor="#android:color/white"
android:textSize="10dp"
android:typeface="sans" />
<TextView
android:id="#+id/tvCardBalanceName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="pgDashboard_lblBalanceBanner"
android:textColor="#android:color/white"
android:textSize="11.33dp"
android:typeface="monospace" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/bottom_btn_bg_color" />
</LinearLayout>

Android Keyboard appear after Tabbar

I have four tabs in my home screen and one of them takes user input,
I have a two problem one is when open tab activity then android keyboard automatically open
and another problem is keyboard coming after tabbar.
I already added android:windowSoftInputMode="adjustPan" in my menifetch file
I Share My code
My createchallan.xml code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="38dp"
android:text="Search" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/search"
android:layout_alignBottom="#+id/search"
android:layout_toRightOf="#+id/search"
android:background="#android:drawable/editbox_background"
android:layout_marginLeft="2dip"
android:singleLine="true"
android:ems="5" >
<requestFocus android:layout_width="wrap_content" />
</EditText>
<CheckBox
android:id="#+id/review"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/search"
android:layout_alignParentRight="true"
android:layout_marginTop="6dip"
android:layout_alignBaseline="#+id/search"
android:text="Review Item" />
<RelativeLayout
android:id="#+id/header"
android:layout_marginTop="10dip"
android:layout_below="#+id/review"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#E5E4E2"
android:orientation="horizontal" >
<TextView
android:id="#+id/txtItemcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item Code"
android:singleLine="true"
android:layout_marginLeft="3dip"
android:textStyle="bold"
/>
<TextView
android:id="#+id/txtItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="23dp"
android:layout_toRightOf="#+id/txtItemcode"
android:textStyle="bold"
android:text="Item" />
<TextView
android:id="#+id/txtItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="14dip"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:textStyle="bold"
android:text="Quantity" />
</RelativeLayout>
<LinearLayout
android:id="#+id/listlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header"
android:layout_above="#+id/lastbutton">
<ListView
android:id="#+id/createlist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:cacheColorHint="#00000000"
android:divider="#adb8c2"
android:dividerHeight="1dp"
android:scrollingCache="false"
android:smoothScrollbar="true"
>
</ListView>
</LinearLayout>
<RelativeLayout
android:layout_alignParentBottom="true"
android:id="#+id/lastbutton"
android:layout_marginBottom="8dip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/createcancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/createsavedraft"
android:background="#drawable/roundedbutton"
android:text=" Cancel " />
<Button
android:id="#+id/createsavedraft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/creatapprove"
android:background="#drawable/roundedbutton"
android:text=" Save Draft " />
<Button
android:id="#+id/creatapprove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/roundedbutton"
android:text=" Approve " />
</RelativeLayout>
my custom xml create_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/txtItemcode"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:text="323232"
android:singleLine="true"
android:layout_marginTop="9dip"
android:layout_marginLeft="5dip"
android:textStyle="bold"
android:focusable="false"
android:focusableInTouchMode="false"
/>
<TextView
android:id="#+id/txtItem"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_marginTop="9dip"
android:layout_marginLeft="25dp"
android:layout_toRightOf="#+id/txtItemcode"
android:layout_alignBaseline="#+id/txtItemcode"
android:text="5456455565456"
android:focusable="false"
android:focusableInTouchMode="false" />
<EditText
android:id="#+id/editcreateQuantity"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#android:drawable/editbox_background"
android:ems="10"
android:inputType="number"
android:layout_marginRight="5dip"
android:layout_alignBaseline="#+id/txtItemcode"
android:focusable="true"
android:singleLine="true" >
</EditText>
</RelativeLayout>
My java code
public class CreateChallan extends Activity {
ListView lstCreate;
String[] strmainItemCode;
String[] strItem;
String[] strQuantity;
Context context=this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.createchallan);
lstCreate= (ListView) findViewById(R.id.createlist);
lstCreate.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
strmainItemCode= new String[]{"55555551","255555","355555","455555","555555"};
strItem =new String[]{"A","B","C","D","F"};
strQuantity =new String[]{"100","200","30","400","500"};
CreateAdapter adapter= new CreateAdapter(this, strmainItemCode, strItem, strQuantity);
lstCreate.setAdapter(adapter);
lstCreate.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position1, long id) {
// TODO Auto-generated method stub
Toast.makeText(context, "Position", Toast.LENGTH_LONG).show();
}
});
}
// Create List Adapter
class CreateAdapter extends ArrayAdapter<String>
{
TextView txtItecode, txtItem;
EditText editQuantity;
String[] strItecode;
String[] strItem;
String[] strQuantity;
Context context;
CreateAdapter(Context context, String[] strItemcode, String[] strItem, String[] strQauntity)
{
super(context,R.layout.create_list_item,R.id.txtItemcode,strItemcode);
this.context= context;
this.strItecode= strItemcode;
this.strItem= strItem;
this.strQuantity= strQauntity;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View row;
row=mInflater.inflate(R.layout.create_list_item, parent,false);
txtItecode= (TextView) row.findViewById(R.id.txtItemcode);
txtItem =(TextView) row.findViewById(R.id.txtItem);
editQuantity = (EditText) row.findViewById(R.id.editcreateQuantity);
editQuantity.setSelected(false);
txtItecode.setText(strItecode[position]);
txtItem.setText(strItem[position]);
editQuantity.setText(strQuantity[position]);
txtItecode.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "click", Toast.LENGTH_LONG).show();
}
});
return row;
}
}
}
below code on you manifest in TabHostActivity or tab parent activity..
android:windowSoftInputMode="adjustPan"
Hi try below code on you manifest for activity
android:windowSoftInputMode="adjustPan"
hope this will work,Thanks

OnItemCLickLIstener doesn't work on ListView

I have an activity with a ListView. ListView with custom views. I add OnItemClickLIstener to the ListView. and when i click on item, in result i see nothing.
Activity with ListView:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#color/silver_conv">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="40dp" android:layout_alignParentTop="true" android:id="#+id/topcontainer"
android:background="#color/black">
</FrameLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/chat_list" android:layout_below="#+id/topcontainer"
android:layout_above="#+id/last_action"
android:cacheColorHint="#00000000"
android:layout_marginRight="2dp" android:clickable="true"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="last action was at time" android:id="#+id/last_action"
android:longClickable="false"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="false" android:layout_above="#+id/action_container"
android:layout_marginBottom="5dp" android:layout_alignParentLeft="false" android:layout_marginLeft="5dp"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="43dp" android:layout_alignParentBottom="true" android:id="#+id/action_container"
android:background="#drawable/conv_botom_action_gradient">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="#string/send"
android:id="#+id/send_message_btn" android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:background="#drawable/blue_button_selector" android:textColor="#color/white"
android:layout_marginLeft="3dp" android:layout_marginTop="3dp"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/add_attach_btn"
android:layout_alignParentLeft="true" android:layout_alignParentBottom="true"
android:background="#drawable/add_attach_button_selector"
android:layout_marginRight="2dp" android:layout_marginBottom="3dp" android:layout_marginTop="3dp"/>
<EditText android:layout_width="40dp" android:layout_height="wrap_content" android:id="#+id/message_et"
android:layout_toRightOf="#+id/add_attach_btn" android:layout_toLeftOf="#+id/send_message_btn"
android:singleLine="true" android:layout_alignParentBottom="true" android:hint="Type message here"
android:background="#drawable/message_input" android:layout_marginBottom="3dp"
android:gravity="center_vertical" android:layout_marginTop="3dp"/>
</RelativeLayout>
View item:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:gravity="center_vertical|left" android:focusable="false">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#drawable/incoming_message"
android:id="#+id/container" android:layout_marginTop="5dp" android:layout_marginBottom="5dp"
android:focusable="false">
<FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content"
android:focusableInTouchMode="true" android:id="#+id/attach_container" android:focusable="false"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="saa"
android:id="#+id/message_text" android:textSize="17sp" android:textColor="#color/black"
android:focusable="false"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/date" android:textColor="#color/black" android:singleLine="true" android:lines="1"
android:maxLines="1" android:ellipsize="none" android:layout_marginLeft="5dp" android:focusable="false"/>
And finally clickListener:
private AdapterView.OnItemClickListener clickLister = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
try {
LinearLayout container = (LinearLayout)view.findViewById(R.id.container);
TextView message = (TextView)container.findViewById(R.id.message_text);
message.setTextColor(mContext.getResources().getColor(R.color.white));
Log.e("My item is", "" + pos);
}catch (Exception e){
e.printStackTrace();
}
}
};
And here is Initialization of ListView:
mConvListView = (ListView)findViewById(R.id.chat_list);
mConvListView.setDivider(null);
mConvListView.setDividerHeight(0);
mConvListView.setItemsCanFocus(false);
mConvListView.setOnItemClickListener(clickLister);
mConvListView.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
mConvListView.setStackFromBottom(true);
P.s. Sorry for a lot code. But I can't find any suggestion a second day.
By setting focusable objects in your row layout, you are preventing the ListView from getting the touch event.
This FrameLayout is consuming the touch event:
<FrameLayout
android:id="#+id/attach_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:focusable="false"/>
Remove the focusable settings so it looks like this:
<FrameLayout
android:id="#+id/attach_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
(You really should organize your XML so that is is readable, in Eclipse use Ctrl+Shift+F.)
Make Focus for all components as follows :
android:focusable="false"
android:focusableInTouchMode="false"
I guess you are not getting the item the right way. Try this:
int position = (int) adapterView.getSelectedItemId();
Log.i("Position:", Integer.toString(position));
Edit
Try this piece of code.
ListView lv = (ListView)findViewById(R.id.chat_list);
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
int position = (int) parent.getSelectedItemId();
Log.i("Position:", Integer.toString(position));
}
});

How to separate header/footer and data in listview?

my app needs to display data in a list, I walked through some tutorials and figured out to use a header.xml and row.xml. It works fine but now I would like to add more stuff on my header.xml and I found out that the entire header.xml is scrolling with the row.xml as well, which I dont really want.
Any solution that doesn't require me to rewrite and change my code style completely?
Activity:
public class HistoryActivity extends ListActivity
{
private static final String TAG = "HistoryActivity";
ListView lv;
SimpleAdapter sd;
RecordDAO dao = new RecordDAO(HistoryActivity.this);
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
lv = getListView();
lv.addHeaderView(getLayoutInflater().inflate(
R.layout.header, null, false));
}
#Override
protected void onResume()
{
super.onResume();
ArrayList<Record> Records = new ArrayList<Record>();
Records = (ArrayList<Record>) dao.findAll();
ArrayList<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
for (int x = Records.size()-1; x >=0; x--)
{
map = new HashMap<String, String>();
map.put(....// all my data
aList.add(map);
}
sd = new SimpleAdapter(this, aList, R.layout.row,
new String[]
{ "date", "name", "time",
"rating" }, new int[]
{ R.id.date, R.id.name, R.id.time,
R.id.rating});
lv.setAdapter(sd);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3)
{
TextView tx = (TextView) view.findViewById(R.id.date);
String s = tx.getText().toString();
Intent intent = new Intent(HistoryActivity.this, EditRecordActivity.class);
intent.putExtra("date", s);
startActivity(intent);
}
});
}
private void insertNewRecord()
{
dao.add(newRecord);
}
}
header.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:orientation="horizontal"
android:paddingBottom="6dip"
android:paddingTop="4dip" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Date"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="name"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Time"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Rating"
android:textColor="#FFFFFF"
android:textSize="16dp" />
</LinearLayout>
</LinearLayout>
row.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingBottom="6dip"
android:paddingTop="4dip" >
<TextView
android:id="#+id/date"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_weight="1"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/time"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/Rating"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#FFFFFF" />
</LinearLayout>
Don't use ListActivity. I think it is a bad practice. Use a regular Activity. Just insert a ListView with an id "#android:id/list" into header.xml.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:orientation="horizontal"
android:paddingBottom="6dip"
android:paddingTop="4dip" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Date"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="name"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Time"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Rating"
android:textColor="#FFFFFF"
android:textSize="16dp" />
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
And you can get the list reference by doing:
ListView lv = (ListView)findViewById(R.id.list);
Setting the Header of a ListView means that it will be the first item of your list, but still will scroll with the list itself.
In your case, you can create some kind of layout (similar to header.xml) and place it above the ListView.
Instead of ListActivity, extend from Activity
Create a main_layout.xml that define some views and a ListView.
main_layout
<include header.xml/>
<listview>
If you don't want your header and footer views to scroll with your ListView, then make sure that you use the addHeaderView() and addFooterView() BEFORE you call your setAdapter() method. I would recommend moving the setAdapter() method to your onCreate() method.

Categories

Resources