I want my rows of gridview be placed in center(horizontally)
I have two columns with same width.
Each item will be placed in center of the respective column.
I tried to follow other answers. None of them worked.
So frustrating when simple layout doesn't work as intended...
<?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:id="#+id/gridView_album"
android:numColumns="2"
android:stretchMode="columnmWidth"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:gravity="center"
android:layout_gravity="center"
>
</GridView>
</RelativeLayout>
row's xml is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:orientation="vertical" >
<TextView
android:id="#+id/textView_albumTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="album title" />
</LinearLayout>
<ImageView
android:id="#+id/imageView1"
android:layout_width="105dp"
android:layout_height="100dp"
android:src="#drawable/abs__ab_share_pack_holo_light" />
</LinearLayout>
This is how I've done it
Your gridView.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<GridView
android:id="#+id/home_grid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:listSelector="#drawable/holo_blue_color"
android:numColumns="2"
android:layout_marginTop="10dip"
android:verticalSpacing="10dip" >
</GridView>
</LinearLayout>
Now Your gridView Items xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="3dp"
android:textColor="#000000"
android:textSize="16dip"
android:textStyle="normal" >
</TextView>
Now I made an adapter class which set the image and text
class MyGridAdapter extends BaseAdapter {
ArrayList<String> name;
ArrayList<Integer> resId;
LayoutInflater inflater;
public MyGridAdapter(Context context, ArrayList<String> name,
ArrayList<Integer> resId) {
this.name = name;
this.resId = resId;
inflater = LayoutInflater.from(context);
}
public int getCount() {
// TODO Auto-generated method stub
return name.size();
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return resId.get(arg0);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.grid_textview, null);
TextView tv = (TextView) convertView.findViewById(R.id.textview);
tv.setText(name.get(position));
tv.setCompoundDrawablesWithIntrinsicBounds(0, resId.get(position),
0, 0);
return convertView;
}
}
You do not need to include ImageView. You can align the image around TextView using setCompoundDrawablesWithIntrinsicBounds
Now just two line in your activity
final MyGridAdapter adapter = new MyGridAdapter(this, name, resId);
gridView.setAdapter(adapter);
Related
I am a beginner to android. I want to display a customized list view which contains a number. I customized my ListView size(android:layout_height="50dp"). But when I run the app it display the size largely which am not given. I don't know how to fix this problem. Please anyone help me.
activity_topic_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.yuvi.secrectsforhappylife.TopicList">
<ListView
android:id="#+id/topiclist"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
titledesign.xml
<?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="50dp"
android:background="#drawable/storylist">
<TextView
android:id="#+id/topictitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/title"
android:textSize="12pt"
android:textStyle="bold"
android:textColor="#android:color/background_light" />
</RelativeLayout>
TopicList.java
public class TopicList extends AppCompatActivity {
String topic[]={"one","two","three","four","five","six","seven","eight","nine","ten"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_topic_list);
ListView listView=(ListView)findViewById(R.id.topiclist);
CustomAdapter customAdapter=new CustomAdapter();
listView.setAdapter(customAdapter);
}
class CustomAdapter extends BaseAdapter
{
#Override
public int getCount() {
return topic.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView=getLayoutInflater().inflate(R.layout.titledesign,null);
TextView title=(TextView)convertView.findViewById(R.id.topictitle);
title.setText(topic[position]);
return convertView;
}
}
}
I want to fix the list cell height
FYI
You want to add HEIGHT for each Cell
Then
<?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:background="#drawable/storylist">
<TextView
android:id="#+id/topictitle"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/title"
android:textSize="12sp"
android:textStyle="bold"
android:textColor="#android:color/background_light" />
</RelativeLayout>
Note
Use TextView Size unit sp instead of pt .
Try below changes on your parent layout on titledesign.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/storylist">
and if you want that your list capture full width of device screen make change on layout_width like
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/storylist">
This should help you out..
ListAdapter listadp = lv_ancillaryservice.getAdapter();
if (listadp != null) {
int totalHeight = 0;
for (int i = 0; i < listadp.getCount(); i++) {
View listItem = listadp.getView(i, null, lv_ancillaryservice);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = lv_ancillaryservice.getLayoutParams();
params.height = totalHeight + (lv_ancillaryservice.getDividerHeight() * (listadp.getCount() - 1));
lv_ancillaryservice.setLayoutParams(params);
lv_ancillaryservice.requestLayout();
Create the List view as
activity_word.xml
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:padding="5dp"
android:textSize="25dp"
android:gravity="center_horizontal|center_vertical"
android:text="Text"
android:id="#+id/text_list_view" />
</LinearLayout>
create another xml file and create a text view in it
listitem.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">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:id="#+id/listView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
in the Toplist java class add this as setContentView
setContentView(R.layout.activity_word);
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
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...
In my application I need to create two lists each with different data . I have designed the UI like adding two listviews one after another in a TableLayout within a LinearLayout. I have stored the data in two String arrays and used arrayadapter to populate into the listview.My listview contain a textview and an image.The problem is
1.There is no error shown in the code but the list is getting populated in weird manner. 1st list item is shown, 2nd,3rd,4th list item not shown and 5th is shown 6th not shown, from 7th its fine. When i scroll up, the list is showing all the list items.
2.each list should have its own scrolling control. like if I scroll List "A" ,it should only be scrolled not the List"B" and vice versa. I have attached the code here . Thanks in advance.
mainscreen.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_LinearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
android:id="#+id/request_Table"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#color/black"
android:orientation="vertical"
android:padding="20dp" >
<TableRow
android:id="#+id/request_Row1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/blue"
android:orientation="vertical" >
<TextView
android:id="#+id/request_TextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:text="REQUESTS"
android:textColor="#color/white"
android:textStyle="bold" />
<ImageView
android:id="#+id/widget163"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</TableRow>
<TableRow
android:id="#+id/request_Row2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/gray"
android:orientation="horizontal" >
<ListView
android:id="#+id/request_ListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:scrollbarAlwaysDrawVerticalTrack="true" />
</TableRow>
</TableLayout>
<TableLayout
android:id="#+id/offered_Table"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#color/black"
android:orientation="vertical"
android:padding="20dp" >
<TableRow
android:id="#+id/offered_Row1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/blue"
android:orientation="horizontal" >
<TextView
android:id="#+id/offered_TextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:text="OFFERED"
android:textColor="#color/white"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="#+id/offered_Row2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ListView
android:id="#+id/offered_ListView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#color/gray"
android:padding="5dp"
android:scrollbars="vertical"
android:smoothScrollbar="true" />
</TableRow>
</TableLayout>
</LinearLayout>
mainscreen.java
public class MainMenu extends Activity {
RequestAdapter mRequestAdapter;
ListView mListView_req ;
public class RequestData {
int icon;
String list_text;
public RequestData( String mText) {
super();
//this.icon=image;
this.list_text=mText;
}
}
private RequestData[] getListItems(){
Resources mRes = getResources();
String[] sListItems= mRes.getStringArray(R.array.request_list);
int listsize = sListItems.length;
RequestData[] obj = new RequestData[listsize];
int size = obj.length;
for(int i=0; i<size; i++){
obj[i] = new RequestData(sListItems[i]);
}
return obj;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenu_screen);
mListView_req= (ListView)findViewById(R.id.request_ListView) ;
mRequestAdapter = new RequestAdapter(this,R.layout.listitem, getListItems());
mListView_req.setAdapter(mRequestAdapter);
}
class RequestAdapter extends ArrayAdapter{
private RequestData rList[]= null;
Context context;
public RequestAdapter(Context context, int textViewResourceId,
RequestData[] data) {
super(context, textViewResourceId, data);
// TODO Auto-generated constructor stub
this.rList= data;
}
#Override
public RequestData getItem(int position){
return rList[position];
}
#Override
public View getView(int pos, View convertView, ViewGroup parent) {
View row= convertView;
if (row== null){
row = getLayoyutinflater().inflate(R.layout.listitem,null);
}
else {
RequestData items = getItem(pos);
TextView text= (TextView) row.findViewById(R.id.List_Text);
if(text!= null){
text.setText(items.list_text);
}
}
return row;
}
listitem.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="150dp">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:id="#+id/text"/>
<ImageView>
android:layout_width="40dp"
android:layout_height="wrap_content"
android:src="#drawable/arrow"
android:layout_marginRight="10dp"/>
</LinearLayout>
Here is the main layout for a screen:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/splashContent"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/splashscreenbg">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight=".008"
android:gravity="center"
android:background="#drawable/mbstabtitle_bg"
android:paddingTop="5dip"
android:paddingBottom="5dip">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" >
<TextView
android:id="#+id/mbstabtitle_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:text="Please Select Your Location"
android:textColor="#FFFFFF">
</TextView>
</LinearLayout>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top"
android:layout_weight=".99">
<ListView
android:id="#+id/locationlist"
android:background="#7B0326"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</TableRow>
</TableLayout>
And here is the Adapter used to populate the listview.
class LocationAdapter extends BaseAdapter {
private Activity activity;
private String[][] data;
private LayoutInflater inflater=null;
public LocationAdapter(Activity a, String[][] d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return data.length;
}
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;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.locationitem, null);
holder=new ViewHolder();
holder.locationName=(TextView)vi.findViewById(R.id.locationName);
vi.setTag(holder);
if(position%2 == 0)
vi.setBackgroundColor(0xFF4D0418);
else
vi.setBackgroundColor(0xFF7B0326);
}
else
holder=(ViewHolder)vi.getTag();
holder.locationName.setText(data[position][1]);
vi.setId(Integer.parseInt(data[position][0]));
return vi;
}
class ViewHolder {
TextView locationName;
}
}
And here is the xml for the items(locationitem.xml).
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical">
<ImageView
android:id="#+id/centerbullet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:src="#drawable/centerbullet"/>
<TextView android:id="#+id/locationName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:paddingLeft="10dip"
android:text="hello hello egele efsdffgxnd"
android:textSize="15dip" />
</LinearLayout>
Now my problem is the the listview and the item in it taking just minimum width it required to display, whereas I want that it takes the total width available. What is the problem in my code?
android:layout_weight="1" in ListView solved the problem.
use can use style
style="?android:attr/spinnerItemStyle"