How to divide tables evenly in a gridview? - android

I'm trying to recreate the following layout in Android:
The grid does not have to grow dynamically, it will always be 5 columns & 2 rows. The orientation is fixed to landscape. Each cell has the exact same layout, just the data in the TextViews can change.
My current approach is to fill a GridLayout with TableLayouts and setting appropriate layout_row & layout_column for each TableLayout. Like so :
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="2"
android:columnCount="4"
android:orientation="vertical">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="0"
android:background="#color/colorAccent">
Which results in this:
Code from picture above : https://pastebin.com/khr9aGWf
I'm running into a few problems.
I cannot get the cells to automatically scale to fit the screen.
The size of this layout file is starting to become very large and hard to manage
Perhaps my approach is completely wrong and I need to go towards something where I define the layout of a cell once and dynamically fill a grid with it?
Any help would be greatly appreciated.

You can create this kind of layout using GridView
<GridView
android:id="#+id/list"
android:numColumns="5"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"/>
your gridview item layout
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_column="0"
android:background="#color/colorPrimary">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
</TableLayout>
GridviewAdapter.java
public class GridviewAdapter extends BaseAdapter {
Activity context;
private LayoutInflater mInflater;
TableLayout.LayoutParams params;
public GridviewAdapter(Activity context)
{
this.context=context;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
params=new TableLayout.LayoutParams(new TableLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
(int) ((getDeviceHeight(context)-getStatusBarHeight()) / 2)));
}
#NonNull
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(position%2==0)
{
convertView.setBackgroundColor(context.getResources().getColor(R.color.colorAccent));
}else{
convertView.setBackgroundColor(context.getResources().getColor(R.color.colorPrimary));
}
convertView.setLayoutParams(params);
return convertView;
}
#Override
public int getCount() {
return 10;
}
#Override
public String getItem(int position) {
return "";
}
#Override
public long getItemId(int position) {
return position;
}
private class ViewHolder {
}
public static int getDeviceHeight(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
Point point = new Point();
wm.getDefaultDisplay().getSize(point);
return point.y;
} else {
return wm.getDefaultDisplay().getHeight();
}
}
public int getStatusBarHeight() {
int result = 0;
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = context.getResources().getDimensionPixelSize(resourceId);
}
return result;
}
}
and in your activity class add code like this
GridviewAdapter itemsAdapter = new GridviewAdapter(this);
GridView listView = (GridView) findViewById(R.id.list);
listView.setAdapter(itemsAdapter);
your output look like this

Related

why CardView causing slow down the scrolling process?

I was using listView without with the custom layout which is having
Linear layout as parent layout. Scrolling was fine, Then I need to
implement CardView for elevation and round corner, After implementation
list Scrolling become slow.Moreover it is also slowing down the
RecyclerView Process.
here is my xml.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/WhiteSmoke"
android:layout_margin="5dp"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="12dp"
card_view:cardElevation="6dp">
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/listviewback"
android:orientation="vertical"
android:padding="5dip">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/custom_listviewIV"
android:layout_width="40dip"
android:layout_height="40dip"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:src="#drawable/avatar2"
/>
<TextView
android:id="#+id/custom_listviewMainTv"
android:layout_width="180dip"
android:layout_height="45dip"
android:layout_marginLeft="2dip"
android:layout_toLeftOf="#+id/custom_listviewLinearLay"
android:layout_toRightOf="#+id/custom_listviewIV"
android:maxLines="2"
android:paddingTop="2dip"
android:text="Goverment "
android:textColor="#color/GreyDark"
android:textSize="15sp"
android:textStyle="bold"/>
<LinearLayout
android:id="#+id/custom_listviewLinearLay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="2dip"
android:orientation="vertical">
<TextView
android:id="#+id/custom_listviewRating"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="right"
android:layout_marginBottom="2dp"
android:background="#drawable/tv_round"
android:backgroundTint="#color/primary_dark"
android:gravity="center"
android:text="4.2/5"
android:textColor="#color/white"
android:textSize="13sp" />
</LinearLayout>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:layout_marginLeft="5dip"
android:background="#color/GreyLight" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dip"
android:orientation="vertical"
android:paddingLeft="10dip"
android:paddingTop="1dip"
android:paddingRight="10dip"
>
<TextView
android:id="#+id/custom_listviewFirstTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dip"
android:text="Fsc Pre Engineering1"
android:textColor="#color/greyligher"
android:textSize="13sp"
android:visibility="gone" />
<TextView
android:id="#+id/custom_listviewSecondTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fsc Pre Engineering2"
android:textColor="#color/greyligher"
android:textSize="13sp"
android:visibility="gone" />
<TextView
android:id="#+id/custom_listviewThirdTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fsc Pre Engineering3"
android:textColor="#color/greyligher"
android:textSize="13sp"
android:visibility="gone" />
<TextView
android:id="#+id/custom_listviewFourthTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="more+"
android:textColor="#color/black"
android:textSize="13sp"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
here is my adapter code
public class CustomAdaptor extends BaseAdapter {
private ArrayList<Data> myDataList;
private Context context;
LinearLayout linearLayout;
public CustomAdaptor(ArrayList<Data> mylist, Context context) {
this.myDataList = mylist;
this.context = context;
}
public int getCount() {
return myDataList.size();
}
public Object getItem(int position) {
return myDataList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View view = View.inflate(context, R.layout.custom_listview, null);
linearLayout= view.findViewById(R.id.layout);
TextView collegeName = view.findViewById(R.id.custom_listviewMainTv);
TextView rating = view.findViewById(R.id.custom_listviewRating);
ImageView country = view.findViewById(R.id.custom_listviewIV);
LinearLayout ll= view.findViewById(R.id.custom_listviewLinearLay);
TextView firstTv= view.findViewById(R.id.custom_listviewFirstTv);
TextView secondTv= view.findViewById(R.id.custom_listviewSecondTv);
TextView thirdTv= view.findViewById(R.id.custom_listviewThirdTv);
TextView fourthTv= view.findViewById(R.id.custom_listviewFourthTv);
collegeName.setText(myDataList.get(position).getName());
rating.setText("" + myDataList.get(position).getRating());
Double n=0.0;
if (n==myDataList.get(position).getRating()) {
ll.setVisibility(View.GONE);
}
if(!myDataList.get(position).getCourseList().isEmpty()) {
if (!myDataList.get(position).getCourseList().get(0).lst.get(0).getCourseName().equals("")) {
firstTv.setVisibility(View.VISIBLE);
firstTv.setText(myDataList.get(position).getCourseList().get(0).lst.get(0).getCourseName());
}
if (myDataList.get(position).getCourseList().get(0).lst.size()>1) {
if (!myDataList.get(position).getCourseList().get(0).lst.get(1).getCourseName().equals("")) {
secondTv.setVisibility(View.VISIBLE);
secondTv.setText(myDataList.get(position).getCourseList().get(0).lst.get(1).getCourseName());
}
}
if (myDataList.get(position).getCourseList().get(0).lst.size()>2) {
if (!myDataList.get(position).getCourseList().get(0).lst.get(2).getCourseName().equals("")) {
thirdTv.setVisibility(View.VISIBLE);
thirdTv.setText(myDataList.get(position).getCourseList().get(0).lst.get(2).getCourseName());
}
}
if (myDataList.get(position).getCourseList().size()>1) {
fourthTv.setVisibility(View.VISIBLE);
fourthTv.setText("+More");
}else if(myDataList.get(position).getCourseList().get(0).lst.size()>3) {
fourthTv.setVisibility(View.VISIBLE);
fourthTv.setText("+More");
}
String img = "" + myDataList.get(position).getImage();
img = img.replaceAll(" ", "%20");
Glide.with(context).load(img).into(country);
view.setTag(myDataList.get(position).getId());
}
else {
linearLayout.setVisibility(View.GONE);
}
return view;
}
}
Cardview is probably just the "breaking point" for how much graphic content that cna be used in an unoptimized way without lagging. As mentioned in some comments you should be using a ViewHolder, here is a simple example of using a viewholder:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_entry, null);
holder = new ViewHolder();
holder.nameTextView = (TextView) convertView.findViewById(R.id.person_name);
holder.surnameTextView = (TextView) convertView.findViewById(R.id.person_surname);
holder.personImageView = (ImageView) convertView.findViewById(R.id.person_image);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
Person person = getItem(position);
holder.nameTextView.setText(person.getName());
holder.surnameTextView.setText(person.getSurname());
//holder.personImageView.setImageBitmap(person.getImage());
return convertView;
}
you can read more about it here
Additionally you should be using RecyclerView and not ListView, to optimize performance.
Try to minimize your view hierarchy and reduce complex operations from binding.

Two views with different heights in a viewflipper on a gridview tile

I have a GridView which has a ViewFlipper in each tile. Each view in the ViewFlipper has a different size, but when the tile is flipped, both views are forced to the same size as the GridView column. What i've tried:
Googled
Set android:measureAllChildren to false on ViewFlipper(only works with ListView)
Left android:columnWidth unspecified
Made ViewFlipper root tag in grid tile layout file
grid_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="180dp" android:layout_height="150dp">
<ViewFlipper
android:layout_width="match_parent"
android:layout_height="match_parent"
android:measureAllChildren="false"
android:id="#+id/view_flipper_3"
>
<RelativeLayout
android:layout_width="180dp"
android:layout_height="150dp"
android:id="#+id/front2"
android:background="#088980"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/commitmentNumber"
android:textSize="25dp"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="220dp"
android:layout_height="190dp"
android:background="#000000"
android:id="#+id/back_2"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="#+id/commitmentPartOne"
android:paddingBottom="5dp"
android:textSize="25sp"
android:text="Part One"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/commitmentPartTwo"
android:textSize="25sp"
android:text="Part Two"
android:paddingBottom="5dp"
android:layout_below="#+id/commitmentPartOne"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/commitmentPartThree"
android:textSize="25sp"
android:text="Part Three"
android:layout_below="#+id/commitmentPartTwo"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
</RelativeLayout>
</ViewFlipper>
</RelativeLayout>
fragment_with_gridview.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<GridView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/sixCommitmentsGridView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:numColumns="2"
android:gravity="center"
android:columnWidth="179dp"
android:stretchMode="columnWidth"
/>
</RelativeLayout>
GridViewAdapter.java:
public class SixCommitmentsGridAdapter extends BaseAdapter {
Context context;
String[] numbers;
public SixCommitmentsGridAdapter(Context context, String[] numbers) {
this.context = context;
this.numbers = numbers;
}
#Override
public int getCount() {
return numbers.length;
}
#Override
public Object getItem(int position) {
return numbers[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view;
final ViewFlipper flipper;
TextView commitmentNumber;
if(convertView == null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.six_commitments_grid_item, parent, false);
}else{
view = convertView;
}
flipper = (ViewFlipper) view.findViewById(R.id.view_flipper_3);
commitmentNumber = (TextView) view.findViewById(R.id.commitmentNumber);
commitmentNumber.setText(numbers[position]);
flipper.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(position % 2 == 0 || position == 0){
AnimationFactory.flipTransition(flipper, AnimationFactory.FlipDirection.RIGHT_LEFT, 200);
}else {
AnimationFactory.flipTransition(flipper, AnimationFactory.FlipDirection.LEFT_RIGHT, 200);
}
}
});
return view;
}
}
Managed to achieve the same grid-style layout with my intented ViewFlipper effect using a StaggeredGridLayoutManager and a RecyclerView.
In Main RelativeLayout contain ViewFlipper, you set height and width!
set them "wrap_content"
<?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">
maybe worked.
good luck.
Update
Use this library:
https://github.com/etsy/AndroidStaggeredGrid

Android ArrayAdapter, BaseAdapter not calling getView()

I am extending ListFragment to display data retrieved from a web server in an AsyncTask in another class. If I set a custom height (i.e. 400dp) on the #android:id/list widget in my xml, getView() does not get called at all. If I set it to wrap_content, getView() gets called for the list items but the list items don't show up! It seems that the listview does not expand to fit the item heights, even though the list item's xml specifies a layout height. When a fixed height is defined I can see the colored background of the list, but no data. getCount() never returns 0.
Part of the adapter class:
public class gAdapter extends ArrayAdapter<GroupObject> {
ArrayList<GroupObject> groups;
public gAdapter(Context context, int textViewResId, ArrayList<GroupObject> groups) {
super(context, R.layout.groups_listview_row, groups);
mInflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
con = context;
this.groups = groups;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final int pos = position;
View row = convertView;
if(convertView == null)
row = mInflater.inflate(R.layout.groups_listview_row, parent, false);
TextView name = (TextView) row.findViewById(R.id.groups_listview_name);
//Set name
try {
name.setText( groups.get(position).groupName);
Log.d("test", "Got name " + groups.get(position).groupName);
} catch(Exception e) {
e.printStackTrace();
name.setText("Could not load name");
}
return row;
}
#Override
public int getCount() {
Log.d("test", "Returning count " + (this.groups.size() != 0 ? this.groups.size() : 0));
return this.groups.size() != 0 ? this.groups.size() : 0;
}
#Override
public GroupObject getItem(int position) {
Log.d("test", "get item " + position);
return this.groups.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
groups_listview_row.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical" >
<ImageView
android:id="#+id/groups_listview_imageview"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/avatar" />
<RelativeLayout
android:id="#+id/RelativeLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/groups_listview_imageview"
android:layout_alignTop="#+id/groups_listview_imageview"
android:layout_toRightOf="#+id/groups_listview_imageview" >
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/groups_listview_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="0.8"
android:ellipsize="marquee"
android:scrollHorizontally="true"
android:lines="1"
android:marqueeRepeatLimit="marquee_forever"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/groups_listview_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginRight="5dp"
android:gravity="right|bottom"
android:singleLine="true"
android:text="zCode: 000000000" />
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/LinearLayout1"
android:layout_gravity="bottom"
android:gravity="bottom" >
<TextView
android:id="#+id/groups_listview_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginLeft="10dp"
android:layout_weight="0.8"
android:gravity="left|bottom"
android:text="Location" />
<TextView
android:id="#+id/groups_listview_numMembers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginRight="10dp"
android:layout_weight="0.2"
android:gravity="right|bottom"
android:text="0 Members" />
</LinearLayout>
</RelativeLayout>

Binding data to 5 different textbox in a linear layout

I have obtained the xml response after parsing the web response and here it is
<NewDataSet>
<Table>
<Restaurant_ID>1705</Restaurant_ID>
<restaurant_name>1947</restaurant_name>
<address>296, Ram Towers, 100 Feet Ring Road, Banashankari, Bangalore</address>
<costoftwo>Approx Rs. 600 for two (without alcohol)</costoftwo>
<rating>3.3</rating>
<timings>11 AM to 11 PM</timings>
<Phone>08026791213</Phone>
<Location_ID>34</Location_ID>
<Location_name>Banashankari</Location_name>
<city_ID>1</city_ID>
<city_name>Bangalore</city_name>
<Cuisine_ID>8</Cuisine_ID>
<Cuisine_name>North Indian</Cuisine_name>
</Table>
and i have a XML layout to which i need to bind the above response. my Xml layout is
<?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:gravity="right"
android:orientation="vertical"
android:weightSum="1.0" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#color/headerbgcolor"
android:weightSum="1.0"
>
<Button
android:id="#+id/restaurant_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:onClick="finishActivity"
android:text="#string/back" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0.6"
android:gravity="center"
android:text="#string/restauran_details" />
<Button
android:id="#+id/restaurant_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:text="#string/home" />
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Button
android:id="#+id/Refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="#string/refresh" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:id="#+id/Restaurant_name"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:text="#string/restaurant_name"
/>
<TextView
android:id="#+id/Restaurant_add"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:text="#string/restaurant_address"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical"
android:weightSum="1" >
<TextView
android:id="#+id/Restaurant_rating"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/restaurant_rating"
android:layout_weight="0.1" />
<TextView
android:id="#+id/Restaurant_timings"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/restaurant_timings"
android:layout_weight="0.1" />
<TextView
android:id="#+id/Restaurant_cstfortwo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/cost_for_two"
android:layout_weight="0.1" />
<TextView
android:id="#+id/Restaurant_cuisine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/restaurant_cuisine"
android:layout_weight="0.1" />
<TextView
android:id="#+id/Restaurant_location"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/restaurant_location"
android:layout_weight="0.1" />
<TextView
android:id="#+id/Restaurant_city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/restaurant_city"
android:layout_weight="0.1" />
</LinearLayout>
<TextView
android:id="#+id/Restaurant_phone"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:text="#string/restaurant_phone"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/footerimage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#808000"
android:src="#drawable/btn_bg_cuisine" />
</RelativeLayout>
</LinearLayout>
And i dont know whether it's good or not, but i am using customadapter to bind. Here is my customadapter.
public class CustomAdapterforRestaurant extends BaseAdapter {
private List<Item> mlist = null;
private Context mcontext;
private LayoutInflater l_Inflater;
public CustomAdapterforRestaurant(Context context, List<Item> lstresponse) {
mlist = lstresponse;
mcontext = context;
l_Inflater = (LayoutInflater) mcontext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolderforcities holder;
if (convertView == null) {
convertView = l_Inflater.inflate(R.layout.restaurantdetails, null);
holder = new ViewHolderforcities(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolderforcities) convertView.getTag();
}
holder.restaurant_location.setText(mlist.get(position).getLocation_name());
holder.restaurant_name.setText(mlist.get(position).getRestaurant_name());
holder.restaurant_add.setText(mlist.get(position).getAddress());
holder.restaurant_rating.setText(mlist.get(position).getRating());
holder.restaurant_timings.setText(mlist.get(position).getTimings());
holder.restaurant_cstfortwo.setText(mlist.get(position).getCostoftwo());
holder.restaurant_cuisine.setText(mlist.get(position).getCuisine_name());
holder.restaurant_city.setText(mlist.get(position).getCity_name());
holder.restaurant_phone.setText(mlist.get(position).getPhone());
return convertView;
}
class ViewHolderforcities {
TextView restaurant_name,restaurant_add,restaurant_rating,restaurant_timings,
restaurant_cstfortwo,restaurant_cuisine,restaurant_location,restaurant_city,
restaurant_phone;
public ViewHolderforcities(View base)
{
restaurant_name = (TextView) base.findViewById(R.id.Restaurant_name);
restaurant_add = (TextView) base.findViewById(R.id.Restaurant_add);
restaurant_rating = (TextView) base.findViewById(R.id.Restaurant_rating);
restaurant_timings = (TextView) base.findViewById(R.id.Restaurant_timings);
restaurant_cstfortwo = (TextView) base.findViewById(R.id.Restaurant_cstfortwo);
restaurant_cuisine = (TextView) base.findViewById(R.id.Restaurant_cuisine);
restaurant_location = (TextView) base.findViewById(R.id.Restaurant_location);
restaurant_city = (TextView) base.findViewById(R.id.Restaurant_city);
restaurant_phone = (TextView) base.findViewById(R.id.Restaurant_phone);
}
}
Can some 1 please explain me how will i bind the data to it.
Thanks
Ramu.
You need to parse this XML using one of the available XML parsers (The core android reference is http://developer.android.com/training/basics/network-ops/xml.html). Once you have got the information from the xml, You fill in the textViews programaticly using the textView.setText("text") method.

ListView Rowlayout in a own coded Chat

What I'm trying to do
I'm trying to create a chat which has to diffrent row's. For every row I made a own layout file, but the problem is that the layoutfile of one row dosn't fit the screen.
Question
What do I need to change in the row layout that it fits like it should. You'll find the code and also a printscreen of what I'm trying.
Code
ListAdapter
public class ChatListAdapter extends BaseAdapter {
private ArrayList<String> ContentList;
private ArrayList<Integer> ChatUserList;
private static LayoutInflater inflater = null;
public ChatListAdapter(Activity activity, ArrayList<String> _ContentList,
ArrayList<Integer> _ChatUserList) {
ContentList = _ContentList;
ChatUserList = _ChatUserList;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return ContentList.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
TextView tv_text;
TextView tv_date;
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
if (ChatUserList.get(position) == 1) {
// I'm RECIVER -> Message is left align
vi = inflater.inflate(R.layout.rowlayout_leftalign, null);
tv_text = (TextView) vi.findViewById(R.id.tv_chat_leftalign);
tv_date = (TextView) vi.findViewById(R.id.tv_chat_leftalign_date);
} else {
// I'm SENDER -> Message is right align
vi = inflater.inflate(R.layout.rowlayout_rightalign, null);
tv_text = (TextView) vi.findViewById(R.id.tv_chat_rightalign);
tv_date = (TextView) vi.findViewById(R.id.tv_chat_rightalign_date);
}
tv_date.setText(sdf.format(new Date()));
tv_text.setText(ContentList.get(position));
tv_text.setMaxWidth(((Chat.display.getWidth() / 4) * 3));
return vi;
}
RowLayout XML Left (Blue bubbles)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#android:color/transparent" >
<RelativeLayout
android:id="#+id/ly_rf_row_r"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginBottom="2.5dp"
android:layout_marginTop="2.5dp"
android:background="#drawable/shape_rightalign"
android:paddingBottom="2.5dp"
android:paddingTop="2.5dp" >
<TextView
android:id="#+id/tv_chat_rightalign"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="right"
android:paddingBottom="2dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="2dp"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/tv_chat_rightalign_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="-3dp"
android:layout_marginLeft="-3dp"
android:layout_toLeftOf="#id/tv_chat_rightalign"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:textColor="#android:color/black"
android:textSize="8.5dp" />
</RelativeLayout>
</RelativeLayout>
RowLayout XML right (red bubbles)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#android:color/transparent" >
<RelativeLayout
android:id="#+id/ly_rf_row_l"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginBottom="2.5dp"
android:layout_marginTop="2.5dp"
android:background="#drawable/shape_leftalign"
android:paddingBottom="2.5dp"
android:paddingTop="2.5dp" >
<TextView
android:id="#+id/tv_chat_leftalign"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:gravity="left"
android:paddingBottom="2dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="2dp"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/tv_chat_leftalign_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="-3dp"
android:layout_marginLeft="-3dp"
android:layout_toRightOf="#id/tv_chat_leftalign"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:textColor="#android:color/black"
android:textSize="8.5dp" />
</RelativeLayout>
</RelativeLayout>
for both XML add android:layout_width="fill_parent" and android:layout_height="wrap_content" like followings
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#android:color/transparent"
android:layout_height="wrap_content"
android:layout_width="fill_parent" >
and so on...

Categories

Resources