I have this master view where I have a TableLayout to which I add new views programmatically. The master view is given below (masterLayout.xml)
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tableLayoutActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:padding="5dp"
android:stretchColumns="*">
<TableRow
android:id="#+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:weightSum="2">
<ScrollView
android:id="#+id/queryScrollView"
android:layout_width="match_parent"
android:layout_span="2"
android:padding="5dp">
<TableLayout
android:id="#+id/queryTableLayoutActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:stretchColumns="*"></TableLayout>
</ScrollView>
</TableRow>
In the above view I add new views to queryTableLayoutActivity Layout. The layout to be added is given below (childLayout.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/border"
android:orientation="vertical"
tools:context="com.teamtreehouse.oslist.ClassActivity">
<TextView
android:id="#+id/activeClassActivities"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|center_horizontal"
android:text="Active Class Activities"
android:textSize="18dp"
android:textStyle="bold"
android:visibility="gone" />
<TextView
android:id="#+id/classNameActivity"
android:layout_width="#dimen/abc_action_bar_stacked_tab_max_width"
android:layout_height="wrap_content"
android:background="#ccc"
android:textColor="#000"
android:textSize="#dimen/text_size"
android:layout_below="#+id/editClassActivity"
android:layout_alignRight="#+id/deleteActivity"
android:layout_alignEnd="#+id/deleteActivity" />
<TextView
android:id="#+id/courseNumberActivity"
android:layout_width="#dimen/abc_action_bar_stacked_tab_max_width"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/classNameActivity"
android:layout_below="#id/classNameActivity"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#ccc"
android:textColor="#000"
android:textSize="#dimen/text_size" />
<TextView
android:id="#+id/typeActivity"
android:layout_width="#dimen/abc_action_bar_stacked_tab_max_width"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/classNameActivity"
android:layout_below="#id/courseNumberActivity"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#ccc"
android:textColor="#000"
android:textSize="#dimen/text_size" />
<TextView
android:id="#+id/nameActivity"
android:layout_width="#dimen/abc_action_bar_stacked_tab_max_width"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/classNameActivity"
android:layout_below="#id/typeActivity"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#ccc"
android:textColor="#000"
android:textSize="#dimen/text_size" />
<TextView
android:id="#+id/dueDateActivity"
android:layout_width="#dimen/abc_action_bar_stacked_tab_max_width"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/classNameActivity"
android:layout_below="#id/nameActivity"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#ccc"
android:textColor="#000"
android:textSize="#dimen/text_size" />
<TextView
android:id="#+id/descriptionActivity"
android:layout_width="#dimen/abc_action_bar_stacked_tab_max_width"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/classNameActivity"
android:layout_below="#id/dueDateActivity"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#ccc"
android:textColor="#000"
android:textSize="#dimen/text_size" />
<TextView
android:id="#+id/maxGradeActivity"
android:layout_width="#dimen/abc_action_bar_stacked_tab_max_width"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/classNameActivity"
android:layout_below="#id/descriptionActivity"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#ccc"
android:textColor="#000"
android:textSize="#dimen/text_size" />
<TextView
android:id="#+id/enterGradeActivity"
android:layout_width="#dimen/abc_action_bar_stacked_tab_max_width"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/classNameActivity"
android:layout_below="#id/maxGradeActivity"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#ccc"
android:textColor="#000"
android:textSize="#dimen/text_size" />
<Button
android:id="#+id/editClassActivity"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:textStyle="bold"
android:layout_height="25dp"
android:background="#drawable/icon_document_edit"
android:onClick="editListener"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="#+id/deleteActivity" />
<Button
android:id="#+id/deleteActivity"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:textStyle="bold"
android:layout_height="wrap_content"
android:background="#drawable/icon_document_delete"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="deleteActivities"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Class Name :"
android:id="#+id/classNameDisplay"
android:textStyle="bold"
android:layout_alignTop="#+id/classNameActivity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/courseNumberActivity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Course ID :"
android:id="#+id/courseIDDisplay"
android:textStyle="bold"
android:layout_alignTop="#+id/courseNumberActivity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="#+id/courseNumberActivity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Course Type :"
android:id="#+id/courseTypeDisplay"
android:textStyle="bold"
android:layout_alignTop="#+id/typeActivity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/nameActivity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Activity Name :"
android:textStyle="bold"
android:id="#+id/activityNameDisplay"
android:layout_alignTop="#+id/nameActivity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/dueDateActivity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Due Date :"
android:textStyle="bold"
android:id="#+id/dueDateDisplay"
android:layout_alignTop="#+id/dueDateActivity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/descriptionActivity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Description :"
android:textStyle="bold"
android:id="#+id/descDisplay"
android:layout_alignTop="#+id/descriptionActivity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/maxGradeActivity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Maximum Grade :"
android:id="#+id/maxGradeDisplay"
android:textStyle="bold"
android:layout_alignTop="#+id/maxGradeActivity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="#+id/maxGradeActivity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Grade :"
android:id="#+id/gradeDisplay"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="#+id/enterGradeActivity" />
</RelativeLayout>
I add the childLayout.xml to masterLayout.xml using the following code
TableLayout queryTableLayout = (TableLayout) findViewById(R.id.queryTableLayoutActivity);
LayoutInflater inflater = (LayoutInflater) getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
String courseIDValue = c.getString(courseIdIndex);
View newTagView = inflater.inflate(R.layout.activity_class_activities, null);
queryTableLayout.addView(newTagView, activeClassActivityIndex++);
I want to sort childLayout based on dueDateActivity field. How can I achieve this?
First of all you dont want to sort layouts - you want to sort views that you inflated with certain layout. So there is no "childLayout" there is newTagView.
Your question is a form of more general question: how to reorder n relative child views on a parent layout in certain order. This is not about changing their sizes and positions(that's easy, you just individually because you must know all new params for each) this is about reordering them. This problem breaks into 4:
Note: You can't just copy-paste code, you need to make changes based on what you specifically need (like what Parameter you need and how to compare it)
1. Establish the current order and the order in which they need to be :
use
getChildCount()
to know number of TableLayout children, use
getChildAt(i)
to traverse and access them.
Now make the Map that will determine the transition to the new order.
Map<View, Parameter> map = new HashMap<Integer, Parameter>();
for(int i = 0; i < queryTableLayout.getChildCount(); i++){
map.put(queryTableLayout.getChildAt(i),
queryTableLayout.getChildAt(i).findViewById(R.id.dueDateActivity)
.getParameter());
//since newTagView should inherit all RelativeLayout methods after infation
//getParameter() - the method from TextView(dueDateActivity) you need
Now to make the order right you need to sort the map by parameter,
which in Java 8 looks like
LinkedList<View> ordering = map.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue(new Comparator<Parameter>(){
/*implement your comparator*/
})/*now when stream is sorted collect it to list*/
.collect(Collectors.toList(Map.Entry::getKey);
Now you have your ordering
2. Method to Swap
easiest way to swap views is to swap their layout params
like in this answer
RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams) b1.getLayoutParams();
RelativeLayout.LayoutParams params2 = (RelativeLayout.LayoutParams) b2.getLayoutParams();
b1.setLayoutParams(params2);
b2.setLayoutParams(params1);
Make a method void swap(View A, View B){...} which will work for your RelativeLayout View with that code.
3. Do the thing.
for(int i = 0; i < queryTableLayout.getChildCount(); i++)
swap(queryTableLayout.getChildAt(i), ordering.get(i));
If you want them add dynamically in already sorted place that is much easier You just need to get the right pos, and params like in (1)
and use
public void addView (View child, int index, ViewGroup.LayoutParams params)
According to bedbad's 4 steps I followed first 2 and found solution
I used this answer to help my situation.
This is my sort method (on button click)
public void sortByWeek(View v){
queryTableLayout = (TableLayout) findViewById(R.id.queryTableLayoutActivity);
int childCount = queryTableLayout.getChildCount();
Map<View,String> map = new HashMap<View,String>();
for(int i = 0; i<childCount;i++){
map.put(queryTableLayout.getChildAt(i),((TextView)queryTableLayout.getChildAt(i).findViewById(R.id.dueDateActivity)).getText().toString());
}
Map<View,String> sortedMap = sortByComparator(map,sort);
ArrayList<View> viewSet = new ArrayList<>(sortedMap.keySet());
queryTableLayout.removeAllViews();
for(int i = 0;i<childCount;i++){
queryTableLayout.addView(viewSet.get(i));
}
if(sort){
sort = false;
}
}
public Map<View,String> sortByComparator(Map<View,String> map,final boolean order){
List<Map.Entry<View,String>> list = new LinkedList<Map.Entry<View,String>>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<View, String>>() {
#Override
public int compare(Map.Entry<View, String> lhs, Map.Entry<View, String> rhs) {
if(order){
return lhs.getValue().compareTo(rhs.getValue());
}else{
return rhs.getValue().compareTo(lhs.getValue());
}
}
});
Map<View,String> sortedMap = new LinkedHashMap<View,String>();
for(Map.Entry<View,String> entry: list){
sortedMap.put(entry.getKey(),entry.getValue());
}
return sortedMap;
}
Related
I have a horizontal linear layout that is filled with TextViews from a dynamic list, but it doesn't show all elements since it seems to be out of the parent's bounds. What I need is to fill the space below if needed.
Here's my code:
The xml:
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/imageView_serie" android:contentDescription="imatge_serie"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nom_serie"
android:textSize="20dp"
android:layout_marginLeft="8dp"
android:id="#+id/nom_serie" android:contentDescription="nom_serie" tools:text="Nom_serie"
android:layout_toRightOf="#id/imageView_serie"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize = "16dp"
android:layout_marginLeft="8dp"
android:textStyle="bold"
android:id="#+id/any_serie" android:contentDescription="any_serie" tools:text="any_serie"
android:layout_toRightOf="#id/nom_serie"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sinopsis"
android:textSize="12dp"
android:layout_marginLeft="8dp"
android:layout_marginTop = "8dp"
android:id="#+id/sinopsi_serie" android:contentDescription="sinopsis_serie" tools:text="sinopsis_serie"
android:layout_toRightOf="#id/imageView_serie"
android:layout_below = "#id/nom_serie"
/>
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="26dp"
android:id="#+id/ratingbar_serie"
android:layout_below = "#id/imageView_serie"
android:layout_centerVertical="true"
style="?android:attr/ratingBarStyleSmall"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Puntuacio"
android:textSize="12dp"
android:layout_marginLeft="8dp"
android:layout_marginTop = "8dp"
android:id="#+id/puntuacio" android:contentDescription="puntuacio" tools:text="puntuacio"
android:layout_toRightOf="#id/ratingbar_serie"
android:layout_below = "#id/imageView_serie"
/>
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
android:layout_width="65dp"
android:layout_height="wrap_content"
android:text="Casting:"
android:textSize="14dp"
android:layout_marginLeft="8dp"
android:layout_marginTop = "24dp"
android:id="#+id/casting" android:contentDescription="casting" tools:text="casting"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
android:layout_marginLeft="8dp"
android:id="#+id/cast" android:contentDescription="cast" tools:text="cast"
android:layout_toRightOf="#id/casting"
/>
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
android:layout_width="65dp"
android:layout_height="wrap_content"
android:text="Direccio:"
android:textSize="14dp"
android:layout_marginLeft="8dp"
android:layout_marginTop = "24dp"
android:id="#+id/direccio" android:contentDescription="direccio" tools:text="direccio"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
android:layout_marginLeft="8dp"
android:id="#+id/dir" android:contentDescription="dir" tools:text="dir"
android:layout_toRightOf="#id/direccio"
/>
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Temporades:"
android:textSize="24dp"
android:textStyle="bold"
android:id="#+id/nTemporades" android:contentDescription="nTemporades" tools:text="nTemporades"
/>
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Comentaris"
android:textSize="24dp"
android:textStyle="bold"
android:id="#+id/titol_comentaris_serie" android:contentDescription="titol_comentari_serie" tools:text="titol_comentari_serie"
/>
</RelativeLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="#+id/panell"
android:orientation="horizontal">
</LinearLayout>
And this how I generate the text views from the list:
LinearLayout panell = (LinearLayout) findViewById(R.id.panell);
for (int i = 1; i <= 20; i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
TextView btn = new TextView(this);
btn.setId(i);
final int id_ = btn.getId();
btn.setText("button " + id_);
//btn.setBackgroundColor(Color.rgb(70, 80, 90));
panell.addView(btn, params);
btn = ((TextView) findViewById(id_));
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(view.getContext(),
"Button clicked index = " + id_, Toast.LENGTH_SHORT)
.show();
}
});
}
Here's my :
output
I recommend Google's flexbox-layout project: https://github.com/google/flexbox-layout
A FlexboxLayout (when configured correctly) has the ability to add any number of child TextViews in a line (like LinearLayout would), but will automatically "wrap" to the next line if a child wouldn't fit.
Why not try to use a ListView , if u add the same propreties every time
In my app what i want is to dynamically add buttons to a linear layout based on the length of the array.For example if the length of array is 4 then ,4 buttons should be added to the linear layout.I tried doing that but always my app crashes
The linearlayout which is at the bottom of the xml is where i wanna add the buttons
XMl
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/hsjobslogo"
android:layout_gravity="center"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/mrd_home"
android:layout_marginRight="10dp"
android:src="#drawable/home168"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#drawable/header"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/linearLayout2">
<TextView
android:layout_width="0dp"
android:layout_height="33dp"
android:layout_weight="2"
android:layout_marginTop="04dp"
android:text="Resource"
android:gravity="center|start"
android:textColor="#fff"
android:textStyle="bold"
android:layout_marginLeft="10dp"
android:textSize="18sp" />
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:padding="10dp"
android:visibility="gone"
android:layout_marginTop="-5dp"
android:src="#drawable/edit" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.35"
android:visibility="visible"
android:id="#+id/ll_main_pi"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textStyle="bold"
style="#style/Job_on_Call"
android:textColor="#000"
android:id="#+id/tv_mrd_pi_name" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" [ "
style="#style/Job_on_Call"
android:singleLine="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gender"
style="#style/Job_on_Call"
android:textColor="#000"
android:id="#+id/tv_mrd_gender" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=","
style="#style/Job_on_Call"
android:singleLine="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age"
style="#style/Job_on_Call"
android:textColor="#000"
android:id="#+id/tv_mrd_age" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" ]"
style="#style/Job_on_Call"
android:singleLine="false" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Profession"
style="#style/Job_on_Call"
android:textColor="#000"
android:id="#+id/tv_mrd_profession" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" [ "
style="#style/Job_on_Call"
android:singleLine="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yrs"
style="#style/Job_on_Call"
android:textColor="#000"
android:id="#+id/tv_mrd_exp_yrs" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="."
style="#style/Job_on_Call"
android:singleLine="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Months"
style="#style/Job_on_Call"
android:textColor="#000"
android:id="#+id/tv_mrd_exp_months" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" ]"
style="#style/Job_on_Call"
android:singleLine="false" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
style="#style/Job_on_Call"
android:singleLine="false"
android:layout_marginBottom="2dp"
android:id="#+id/tv_mrd_area" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
style="#style/Job_on_Call"
android:singleLine="false"
android:layout_marginBottom="2dp"
android:id="#+id/tv_mrd_city" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
style="#style/Job_on_Call"
android:singleLine="false"
android:layout_marginBottom="2dp"
android:id="#+id/tv_mrd_state" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
style="#style/Job_on_Call"
android:singleLine="true"
android:id="#+id/tv_mrd_country" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:layout_width="0dp"
android:layout_height="1dp"
android:gravity="bottom"
android:layout_gravity="bottom"
android:background="#drawable/divider_light"
android:layout_weight="0.2" />
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1.6"
android:layout_height="wrap_content"
android:background="#drawable/rectangle">
<TextView
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:text="Personal Info"
android:textStyle="bold"
android:textSize="18sp"
android:id="#+id/tv_mrd_header"
style="#style/Job_on_Call"
android:textColor="#fff"
android:layout_marginTop="05dp"
android:layout_marginLeft="19dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/plus_one"
android:visibility="gone"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:id="#+id/iv_mrd_add" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/edit_one"
android:visibility="visible"
android:layout_marginRight="5dp"
android:layout_marginLeft="10dp"
android:id="#+id/iv_mrd_edit" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.5"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:padding="5dp"
android:id="#+id/iv_mrd_go_left"
android:src="#drawable/left_arrow" />
<ViewFlipper
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent"
android:visibility="visible"
android:id="#+id/mrd_view_flipper"
android:layout_gravity="center_horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="2dp"
android:id="#+id/ll_my_resource_personal_details">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nationality"
android:textStyle="bold"
android:id="#+id/tv_mrd_nationality_header"
android:layout_marginTop="02dp"
style="#style/Job_on_Call"
android:textColor="#000" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="2dp"
android:id="#+id/ll_my_resource_refference_details">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textStyle="bold"
android:id="#+id/tv_mrd_ref_name_header"
android:layout_marginTop="02dp"
style="#style/Job_on_Call"
android:textColor="#000" />
</LinearLayout>
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Document Type"
android:textStyle="bold"
android:id="#+id/dumm1"
android:layout_marginTop="05dp"
style="#style/Job_on_Call"
android:textColor="#000" />
</RelativeLayout>
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="#+id/mrd_profile_pic"
android:layout_margin="10dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</ViewFlipper>
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:padding="5dp"
android:id="#+id/iv_mrd_go_right"
android:src="#drawable/right_arrow" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/add_doc_circles"
android:orientation="horizontal">
The layout where i want to create buttons dynamically
</LinearLayout>
</LinearLayout>
Code
if (NewDataSet.get("Table2") instanceof JSONArray) {
isDocPresent = true;
JSONArray array = NewDataSet.getJSONArray("Table2");
numOfDocCircles = array.length();
LayoutInflater layoutInflater;
Button button = new Button(context);
for (int k = 0; k < array.length(); k++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(30, 30);
button.setLayoutParams(params);
button.setText("" + k);
llAddDocCircles.addView(button);
}
Error
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Please help me out whats the problem?
Move your button creation into the for loop, in your code you are basically just adding the same Button instance to the parent layout several times:
for (int k = 0; k < array.length(); k++) {
Button button = new Button(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(convertDpToPixel(30), convertDpToPixel(30));
button.setLayoutParams(params);
button.setText("" + k);
llAddDocCircles.addView(button);
}
Use below method to convert dp to pixel.
public static float convertDpToPixel(float dp){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return px;
}
In your for loop call this method as:
for (int k = 0; k < array.length(); k++) {
llAddDocCircles.addView(addMoreButton("" + k));
}
And your addMoreButton() method is:
public Button addMoreButton(String text) {
Button button = new Button(context);
button.setId(buttonID);
LinearLayout.LayoutParams buttonLayoutParams = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
button LayoutParams.setMargins(10, 10, 10, 10); //Add this if you want margin of 10dp
button.setLayoutParams(buttonLayoutParams );
buttonArrayList.add(button);
buttonID++;
return button;
}
And you can easily access these button value by:
private static int buttonID = 0;
private ArrayList<Button> buttonArrayList= new ArrayList<Button>();
// loop to handle each button
for (int i = 0; i < buttonArrayList.size(); i++) {
Button button = buttonArrayList.get(i);
// do your stuff with each button
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
I Have a ListView. In my listview I create a custom items on every rows. this custom item as some textviews and two buttons. The problem I have now is I want to detect when the user clicks on the buttons in the listview items.
But My code is not working.
Please don't mind the numerouse text views that are to be hidden from the user only one is to be seen by the user and the two buttons are visible.
Each time I click the button nothing happens, no response
My listview custom item is pasted below.
Thanks for reading my post, am also grateful for your time and effort.
<?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:background="#drawable/listitemdesign"
android:orientation="vertical"
android:padding="10dp"
android:layout_marginBottom="10dp">
<TextView
android:id="#+id/itemprescrip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="gone"
/>
<TextView
android:id="#+id/agerange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:visibility="gone"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/itemanufac"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:visibility="gone"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/itemexdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:visibility="gone"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/itemdcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:visibility="gone"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/itemnafno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:visibility="gone"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/drugID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:visibility="gone"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/plus"
android:layout_marginTop="8dp"
/>
<TextView
android:id="#+id/itemname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:layout_margin="10dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<View android:layout_width="wrap_content" android:layout_height="2dip"
android:background="#color/wine"
android:layout_marginTop="5dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="#+id/ordernow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/whitebackground"
android:src="#drawable/ordernow"
android:layout_marginTop="15dp"
/>
<ImageButton
android:id="#+id/details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/detailsicon"
android:layout_marginTop="15dp"
android:layout_marginLeft="10dp"
android:background="#drawable/whitebackground"
/>
</LinearLayout>
</LinearLayout>
My activity code is also pasted below. The OnItemClickListener of the ListView component, is what is below :
private AdapterView.OnItemClickListener itemclickListener = new AdapterView.OnItemClickListener(){
String drugID = null,drugName = null, drugPrecription = null, drugManufacturedate = null, drugExpiratorydate = null,drugCode = null, nafdacNo=null,drugageRange = null;
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
drugID = ((TextView)view.findViewById(R.id.drugID)).getText().toString();
drugName = ((TextView)view.findViewById(R.id.itemname)).getText().toString();
drugPrecription =((TextView)view.findViewById(R.id.itemprescrip)).getText().toString();
drugManufacturedate = ((TextView)view.findViewById(R.id.itemanufac)).getText().toString();
drugExpiratorydate = ((TextView)view.findViewById(R.id.itemexdate)).getText().toString();
drugCode = ((TextView)view.findViewById(R.id.itemdcode)).getText().toString();
nafdacNo = ((TextView)view.findViewById(R.id.textViewName)).getText().toString();
drugageRange = ((TextView)view.findViewById(R.id.agerange)).getText().toString();
orderButton = (ImageButton)parent.getChildAt(9);//view.findViewById(R.id.ordernow);
detailsButton = (ImageButton)parent.getChildAt(0);//view.findViewById(R.id.details);
Object o = orderButton.getClass();
Toast.makeText(context, o.toString(), Toast.LENGTH_SHORT).show();
//Now we are going check for the button that would clicked
//int elementID = view.getId();
//this.detectItemClicked(elementID);
}
I want to crate custom TableLayout with rows like this:
TV is for TextView, i.e. I want to add 11 TextViews to the row:
Each row begins with a title and then I add 5 pairs of TextViews, so that table row is as wide, as screen is.
Here's my code:
public class FlowTable extends TableLayout {
private Context context;
public FlowTable(Context context) {
super(context);
this.context = context;
}
public FlowTable(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
public void addContent(List<ResultItem> data) {
TableRow tableRow = new TableRow(context);
LayoutParams params = new LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1);
for (int i = 0; i < data.size(); i++) {
if (i % 5 == 0) {
this.addView(tableRow, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
tableRow = new TableRow(context);
TextView tvRange = new TextView(context);
tvRange.setLayoutParams(params);
tvRange.setText(genRange(i+1));
tableRow.addView(tvRange);
}
TextView tvDistance = new TextView(context);
tvDistance.setLayoutParams(params);
tvDistance.setText(String.valueOf(data.get(i).distance));
TextView tvResult = new TextView(context);
tvResult.setLayoutParams(params);
tvResult.setText(data.get(i).result);
tableRow.addView(tvDistance);
tableRow.addView(tvResult);
}
}
private String genRange(int currIndex){
/********************/
return somestring;
}
}
Using table:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<packagename.FlowTable
android:id="#+id/flowTable"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
In fragment:
View root = inflater.inflate(R.layout.fragment_session_summary, container, false);
FlowTable flowTable = (FlowTable)root.findViewById(R.id.flowTable);
flowTable.addContent(data);
The problem: the screen is just empty! Nothing at all. Before I added the layout params to textview it worked, but row didn't occupy the screen width. My initial solution was based on the LinearLayout samples, because TableRow is an extention of LinearLayout. But I can't make it work.
Thanks.
Try programmatically setting all columns to stretch (didn't seem to work in XML for me):
...
flowTable.addContent(data);
flowTable.setStretchAllColumns(true);
Some other quick facts:
No need to try and specify height and width for TableRow within TableLayout because it will always be height=WRAP_CONTENT and width=MATCH_PARENT. See TableLayout documentation where this is listed in the Class Overview section
No need to try and specify height and widget for children of TableRow because they will always be height=WRAP_CONTENT and width=MATCH_PARENT. See TableRow documentation where this is listed in the Class Overview section
Might I also humbly suggest a bit of refactoring:
public class FlowTable extends TableLayout {
private TableRow mCurrentRow;
public FlowTable(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public FlowTable(Context context) {
super(context);
init();
}
private void init() {
mCurrentRow = new TableRow(getContext());
mCurrentRow.addView(createAndFillTextView("0")); // title for first row
setStretchAllColumns(true);
}
public void addContent(List<ResultInfo> data) {
for (int i = 0; i < data.size(); i++) {
if ((i % 5 == 0) && (i != 0) /** Don't do this on 0! */) {
finishRowAndStartNew(i);
}
mCurrentRow.addView(createAndFillTextView(data.get(i).distance));
mCurrentRow.addView(createAndFillTextView(data.get(i).result));
}
}
private void finishRowAndStartNew(int newRowIndex) {
addView(mCurrentRow);
mCurrentRow = new TableRow(getContext());
mCurrentRow.addView(createAndFillTextView(genRange(newRowIndex+1)));
}
private String genRange(int currIndex){
/********************/
return String.valueOf(currIndex);
}
private TextView createAndFillTextView(String text) {
TextView tv = new TextView(getContext());
tv.setText(text);
return tv;
}
}
If you want to use the xml Layout for that then try this:
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<TableRow android:weightSum="1">
<LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Title" android:layout_weight="1"/>
</LinearLayout>
<!-- First Group -->
<LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv1"/>
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv2"/>
</LinearLayout>
<!-- Second Group -->
<LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv1"/>
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv2"/>
</LinearLayout>
<!-- Third Group -->
<LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv1"/>
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv2"/>
</LinearLayout>
<!-- Fourth Group -->
<LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv1"/>
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv2" />
</LinearLayout>
<!-- Fifth Group -->
<LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="2dp">
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv1"/>
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv2"/>
</LinearLayout>
</TableRow>
<TableRow android:weightSum="1">
<LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Title" android:layout_weight="1"/>
</LinearLayout>
<!-- First Group -->
<LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv1"/>
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv2"/>
</LinearLayout>
<!-- Second Group -->
<LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv1"/>
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv2"/>
</LinearLayout>
<!-- Third Group -->
<LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv1"/>
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv2"/>
</LinearLayout>
<!-- Fourth Group -->
<LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv1"/>
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv2" />
</LinearLayout>
<!-- Fifth Group -->
<LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="2dp">
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv1"/>
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv2"/>
</LinearLayout>
</TableRow>
</TableLayout>
Hope it will help you. . .
And if you want other then xml then let me know.
Thanks.
i have a custom listview i want to add one lat row to it which will calculate total of all the list when i scroll the last row gets added again and again
if(position==searchResult.size() && searchResult.size()!=1)
{
holder.checkImg.setVisibility(ImageView.GONE);
holder.fvtImg.setVisibility(ImageView.GONE);
holder.type.setVisibility(TextView.GONE);
holder.name.setVisibility(TextView.GONE);
holder.offer_price.setVisibility(TextView.GONE);
holder.real_price.setVisibility(TextView.GONE);
holder.total.setVisibility(TextView.VISIBLE);
holder.total_price.setVisibility(TextView.VISIBLE);
//DecimalFormat df = new DecimalFormat("0.00");
//holder.txt_distance.setText(df.format(mData.get(position).get("distance")).toString()+"...");
DecimalFormat twoDForm = new DecimalFormat("0.00");
String totalPrice = (twoDForm.format(mTotalPrice)+"").replace('.',',');
holder.total.setText("Total");
holder.total_price.setText("Kr. "+totalPrice);
}
else
{
holder.total.setVisibility(TextView.GONE);
holder.total_price.setVisibility(TextView.GONE);
if(isEdit){
/*LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.leftMargin=30;
holder.fvtImg.setLayoutParams(lp);*/
/* TranslateAnimation anim = new TranslateAnimation(0,35,0,0);
holder.fvtImg.setAnimation(anim);
anim.setFillAfter(true);*/
holder.fvtImg.setVisibility(ImageView.GONE);
holder.name.setPadding(10,0, 0, 0);
holder.type.setPadding(10,0, 0, 0);
holder.checkImg.setVisibility(ImageView.VISIBLE);
Layout file :
<?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="wrap_content"
>
<ImageView
android:id="#+id/list_background"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/price_list_background"
/>
<CheckBox android:id="#+id/checkBox"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:visibility="gone">
</CheckBox>
<ImageView
android:id="#+id/img_pol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/pol_icon_tag"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
/>
<TextView
android:id="#+id/txt_name"
android:layout_width="wrap_content"
android:layout_height="15dp"
android:textSize="16dp"
android:textColor="#000000"
android:layout_marginTop="5dp"
android:layout_marginLeft="35dp"
/>
<TextView
android:id="#+id/txt_type"
android:layout_width="200dp"
android:layout_height="13dp"
android:textSize="12dp"
android:textColor="#464647"
android:layout_marginTop="35dp"
android:layout_marginLeft="35dp"
/>
<TextView
android:id="#+id/txt_real_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12dp"
android:textColor="#464647"
android:layout_marginTop="35dp"
android:layout_marginLeft="90dp"
/>
<TextView
android:id="#+id/txt_offer_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:textColor="#464647"
android:layout_marginTop="31dp"
android:layout_marginLeft="190dp"
/>
<TextView
android:id="#+id/txt_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textColor="#464647"
android:layout_centerVertical="true"
android:layout_marginLeft="100dp"
android:text="Total"
android:visibility="gone"
/>
<TextView
android:id="#+id/txt_total_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:textColor="#464647"
android:layout_centerVertical="true"
android:layout_marginLeft="230dp"
android:text="kr. 222.22"
android:visibility="gone"
/>
</RelativeLayout>
Have you considered using a footer instead of placing the last item inside the ListView?
What you do is, before you set the adapter of the ListView, you inflate a footer layout (or perhaps just a TextView, if that's all you need). Store a reference to it, and then add it to the ListView:
TextView myFooter = new TextView( context );
myListView.addFooterView( myFooter );
Then when you add items to the list or when the calculation needs to occur, you just calculate and set the text of myFooter.
myFooter.setText( myCalculatedValue );
Setting the text of the footer must ofcourse happen on the UI-thread so if you use a background thread or a AsyncTask to add to your listview, be sure you update stuff the right place.