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.
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
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;
}
For the record, a long time programmer, but very new to android development. Basically trying self learn. I have hit a road block, have research and tried variations on things I've found, but am really stuck.
I want to create a row of controls this is dynamically created when I tap on a button (Set). This row would be added to the bottom of each subsequent row
Picture of what a row of controls would look like
I've was able to get the program to add a layout to a scrollview and achieve the goal of adding dynamically a row, but I cannot get the textview to size out correctly. I've tried layoutparam, setwidth with no luck. The text gets wrapped no matter what I do.
Picture of running app, rows added, TextView not sizing correctly
I tried a linearlayout, but that just overlays and even with just text, the textview remains the same size.
I am trying to get the dynamic creation to match the "hardcoded" layout I did to see how it would look. Specifically, what am I missing to size the TextView and or any of the other controls (view objects?) in a row.
Here is my view 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">
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="92dp"
android:layout_height="match_parent"
android:text="New"
android:id="#+id/btnNew"
android:layout_row="0"
android:layout_column="0"
android:layout_gravity="center_horizontal" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="92dp"
android:layout_height="match_parent"
android:text="Open"
android:id="#+id/btnOpen"
android:layout_row="0"
android:layout_column="1"
android:layout_gravity="center_horizontal" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="92dp"
android:layout_height="match_parent"
android:text="Save"
android:id="#+id/btnSave"
android:layout_row="0"
android:layout_column="2"
android:layout_gravity="center_horizontal" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="92dp"
android:layout_height="match_parent"
android:text="Delete"
android:id="#+id/btnDelete"
android:layout_row="0"
android:layout_column="3"
android:layout_gravity="center_horizontal" />
</GridLayout>
<GridLayout
android:layout_width="367dp"
android:layout_height="wrap_content"
android:id="#+id/grdEntry">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/label_time"
android:id="#+id/lblTime"
android:layout_row="0"
android:layout_column="1" />
<Spinner
android:layout_width="147dp"
android:layout_height="wrap_content"
android:id="#+id/spinner_gaits"
android:layout_row="1"
android:layout_column="0"
android:focusable="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/label_Gait"
android:id="#+id/lblGait"
android:layout_row="0"
android:layout_column="0" />
<EditText
android:layout_width="124dp"
android:layout_height="wrap_content"
android:id="#+id/txtSelected"
android:layout_row="1"
android:layout_column="1"
android:layout_gravity="fill_horizontal|center_vertical"
android:singleLine="true"
android:numeric="integer" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="92dp"
android:layout_height="fill_parent"
android:text="Set"
android:id="#+id/btnSet"
android:layout_row="1"
android:layout_column="2"
android:layout_gravity="center_vertical"
android:clickable="true" />
</GridLayout>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/my_root">
</LinearLayout>
</ScrollView>
</LinearLayout>
This is the original xml I created to help me visualize what I wanted when I created the same thing dynamically.
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
<ImageView
android:src="#drawable/ic_action_pointer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_row="0"
android:layout_column="0"
android:layout_gravity="bottom" />
<TextView
android:layout_width="232dp"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Canter - 99 min"
android:id="#+id/textView5"
android:layout_row="0"
android:layout_column="1"
android:layout_gravity="center_vertical"
android:paddingLeft="15dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="79dp"
android:layout_height="wrap_content"
android:text="Del"
android:id="#+id/button2"
android:layout_row="0"
android:layout_column="2" />
Here is the function that attempts to create a row consisting of an image, a textview and a button (I've kept in the comments to show what I've tried)
#TargetApi(Build.VERSION_CODES.M)
private void buildDisplayLine(int count) {
// create a layout params to define how we add
// GridLayout.LayoutParams gridParams = new GridLayout.LayoutParams();
// gridParams.width = GridLayout.LayoutParams.MATCH_PARENT;
// gridParams.height = GridLayout.LayoutParams.WRAP_CONTENT;
// gridParams.setGravity(Gravity.CENTER_HORIZONTAL);
LinearLayout.LayoutParams imgLayout =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
// create a grid layout to add controls
GridLayout newGrid = new GridLayout(this);
newGrid.setId(100 + count);
newGrid.setColumnCount(3);
// LinearLayout newGrid = new LinearLayout(this);
// newGrid.setLayoutParams(imgLayout);
// newGrid.setVerticalGravity(LinearLayout.VERTICAL);
//create an image control
ImageView newImage = new ImageView(this);
newImage.setLayoutParams(imgLayout);
newImage.setImageResource(R.drawable.ic_action_pointer);
newImage.setId(count + 200);
Spinner spinner = (Spinner)findViewById(R.id.spinner_gaits);
TextView textGait = (TextView)spinner.getSelectedView();
String gtResult = textGait.getText().toString();
TextView txtTime = (TextView)findViewById(R.id.txtSelected);
String tmeResult = txtTime.getText().toString();
String display = gtResult + " - " + tmeResult + " min";
txtTime.setText("");
// create a text view control
TextView newText = new TextView(this);
newText.setLayoutParams(imgLayout);
newText.setGravity(Gravity.CENTER_VERTICAL);
Resources r = getResources();
int pc = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 332, r.getDisplayMetrics());
newText.setWidth(pc);
//newText.getLayoutParams().width = toPixels(432);
newText.setPadding(toPixels(15), 0, 0, 0);
// newText.setTextAppearance(android.R.style.TextAppearance_Large);
newText.setText(display);
newText.setId(300 + count);
// create a new button
Button newButton = new Button(this);
newButton.setLayoutParams(imgLayout);
newButton.getLayoutParams().height = LinearLayout.LayoutParams.WRAP_CONTENT;
newButton.getLayoutParams().width = toPixels(79);
newButton.setText("Del");
newButton.setId(400 + count);
// now add to the original grid layout
newGrid.addView(newImage);
newGrid.addView(newText);
newGrid.addView(newButton);
LinearLayout my_root = (LinearLayout) findViewById(R.id.my_root);
LinearLayout A = new LinearLayout(this);
A.setOrientation(LinearLayout.HORIZONTAL);
my_root.addView(newGrid);
spinner.requestFocus();
}
Why don't you use LayoutInflater to inflate your XML, set the values, and add the view? Something like this:
GridView newGrid = getLayoutInflater().inflate(R.layout.grid_row, null);
newGrid.setId(100 + count);
...
my_root.addView(newGrid);
I am working on chat layout in android.
My problem is that bubble is not resizing according to message text.
I followed this example Bubble Chat layout
Please suggest the solution.
http://i62.tinypic.com/4i0mcn.png (not able to upload image due to low rep)
Overall layout code :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/main">
<LinearLayout
android:id="#+id/bottom_write_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/message_bar" >
<EditText
android:id="#+id/text"
android:layout_width="10dip"
android:layout_height="wrap_content"
android:layout_margin="5sp"
android:layout_weight="1.88"
android:background="#drawable/message_field"
android:ems="15"
android:padding="5sp"
android:textColor="#color/textFieldColor" >
<requestFocus />
</EditText>
<Button
android:layout_width="40dp"
android:layout_height="27dp"
android:layout_margin="5sp"
android:layout_weight="0.07"
android:background="#drawable/ic_action_send"
android:onClick="sendMessage"
android:textColor="#FFF" />
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_above="#id/bottom_write_bar"
android:layout_alignParentTop="true"
android:cacheColorHint="#FFFFFF"
android:divider="#FFFFFF"
android:dividerHeight="2dp"
android:listSelector="#FFFFFF" >
</ListView>
<TextView
android:id="#android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/bottom_write_bar"
android:layout_alignParentTop="true"
android:gravity="center_vertical|center_horizontal"
android:text="#string/main_empty_messages" />
</RelativeLayout>
Message layout codes are:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/background"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingRight="4dip"
android:paddingBottom="4dip"
android:background="#drawable/speech_bubble_green">
<!--message -->
<TextView
android:id="#+id/tvmessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:layout_marginLeft="5dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="#string/strstatus"
android:textColor="#B5ACAF"
android:textSize="15sp"
/>
<TextView
android:id="#+id/tvdate"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:text="#string/strtime"
android:textColor="#FFFFFF"
android:textSize="12sp"
/>
<TextView
android:id="#+id/tvname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignLeft="#+id/tvmessage"
android:fontFamily="georgia"
android:paddingBottom="29dip"
android:text="#string/strname"
android:textColor="#000000"
android:textSize="14sp"
android:textStyle="bold"
android:typeface="serif" />
</RelativeLayout>
Java code :
Message message = (Message) this.getItem(position);
ViewHolder holder;
if(convertView == null)
{
holder = new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.sms, parent, false);
holder.message = (TextView) convertView.findViewById(R.id.tvmessage);
holder.tdate = (TextView) convertView.findViewById(R.id.tvdate);
holder.tsender = (TextView) convertView.findViewById(R.id.tvname);
convertView.setTag(holder);
}
else
holder = (ViewHolder) convertView.getTag();
RelativeLayout bg = (RelativeLayout)convertView.findViewById(R.id.background);
/* Current Time formatting */
Time = "";
suff = "AM";
timeHR = new SimpleDateFormat("HH").format(Calendar.getInstance().getTime());
time = Integer.parseInt(timeHR);
if(time > 12){
time = time - 12;
suff = "PM";
}
String timeMIN = new SimpleDateFormat("mm").format(Calendar.getInstance().getTime());
// SimpleDateFormat("yyyyMMdd_HHmmss") for full Date!
if(!message.isStatusMessage()){
Time = String.valueOf(time)+ ":"+timeMIN+" "+suff;
}
if(message.isMine()) {
pre = "Me";
bg.setBackgroundResource(R.drawable.bubble_green);
bg.setGravity(Gravity.RIGHT);
} else {
bg.setBackgroundResource(R.drawable.bubble_yellow);
bg.setGravity(Gravity.LEFT);
pre = this.sender;
}
holder.message.setTextColor(R.color.textColor);
holder.message.setText(message.getMessage());
holder.tdate.setText(Time);
holder.tsender.setText(pre);
if(message.isStatusMessage()){
holder.tdate.setText("");
holder.tsender.setText("");
}
//check if it is a status message then remove background, and change text color.
if(message.isStatusMessage())
{
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
holder.message.setBackgroundDrawable(null);
} else {
holder.message.setBackground(null);
}
holder.message.setTextColor(R.color.textFieldColor);
}
return convertView;
The parent RelativeLayout has:
android:layout_width="fill_parent"
And, indeed, in your image, it does will the parent's width. At the least, you will need to change that to:
android:layout_width="wrap_content"
It would also be good practice to define the relative layout fully:
<TextView
android:id="#+id/tvname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:fontFamily="georgia"
android:paddingBottom="29dip"
android:text="#string/strname"
android:textColor="#000000"
android:textSize="14sp"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:id="#+id/tvdate"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:text="#string/strtime"
android:textColor="#FFFFFF"
android:textSize="9sp"
/>
<TextView
android:id="#+id/tvmessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:layout_marginLeft="5dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_below="#id/tvname"
android:text="#string/strstatus"
android:textColor="#B5ACAF"
android:textSize="15dip"
/>
I know is bit late but may help others with the problem.
bg.setBackgroundResource(R.drawable.bubble_green);
Isn't set in the viewHolder so isn't resizing.
Declare the bg layout in the viewHolder and call it:
holder.bg.setBackgroundResource(R.drawable.bubble_green);
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);
}