I am beginner in Android. My small app is working fine but its UI is looking ugly. Could you please help me make it better? For example: its image looks so big, could we make it a bit smaller.
Here is the code snippet.
MainActivity.java
package com.example.hacback17.listviewwithimagesadvanced;
import android.content.Context;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
ListView list;
String[] memeTitle;
String[] memeDescription;
// getting ids of images from drawable
int[] images = {R.drawable.number_one,R.drawable.number_two, R.drawable.number_three, R.drawable.number_four, R.drawable.number_five,
R.drawable.number_six, R.drawable.number_seven, R.drawable.number_eight, R.drawable.number_nine, R.drawable.number_ten};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources resources = getResources();
memeTitle = resources.getStringArray(R.array.titles);
memeDescription = resources.getStringArray(R.array.titles);
// Getting the reference of the ListView
list = (ListView) findViewById(R.id.listView);
MyAdapter adapter = new MyAdapter(this, memeTitle, images, memeDescription );
list.setAdapter(adapter);
}
}
// Custom ArrayAdapter
class MyAdapter extends ArrayAdapter<String>
{
Context context;
int[] images; // getting the reference of images.
String[] titleArray; // getting the reference of titles array
String[] descriptionArray; // getting the reference of description array
public MyAdapter(Context context, String[] titles, int[] img, String[] description) {
super(context, R.layout.single_row, R.id.textView, titles);
this.context = context;
this.images = img;
this.titleArray = titles;
this.descriptionArray = description;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Let's get the reference of LayoutInflater to get the XML into Java code.
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.single_row, parent, false);
ImageView myImage = (ImageView) row.findViewById(R.id.imageView);
TextView myTitle = (TextView) row.findViewById(R.id.textView);
TextView myDescription = (TextView) row.findViewById(R.id.textView2);
myImage.setImageResource(images[position]);
myTitle.setText(titleArray[position]);
myDescription.setText(descriptionArray[position]);
return row;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.hacback17.listviewwithimagesadvanced.MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
single_row.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">
<ImageView
android:src="#drawable/number_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_margin="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView"
android:layout_alignTop="#+id/imageView"
android:layout_alignParentEnd="true"
android:layout_toEndOf="#+id/imageView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/textView2"
android:layout_alignEnd="#+id/textView"
android:layout_alignStart="#+id/textView"
android:layout_below="#+id/textView" />
</RelativeLayout>
The UI looks like this:
In your ArrayAdapter use these funcitons:
myImage.setMaxWidth(MAX_WIDTH);
myImage.setMaxHeight(MAX_HEIGHT);
Use your own values in MAX_WIDTH & MAX_HEIGHT.
make your image layout widht and height wrap_content to 30dp.
<ImageView
android:src="#drawable/number_one"
android:layout_width="30dp"
android:layout_height="30dp"
android:id="#+id/imageView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_margin="10dp" />
Related
I am creating a student details application in android.I need to display student's photo at the top and display name,roll number,address etc as a table.I added a imageview at the top and listview just below the image view.Can i add the image inside the list view ( Now image is not scrollable ).
activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/def"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"/>
<ListView
android:id="#id/list"
android:layout_above="#+id/ad_view"
android:layout_width="fill_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
Activity.java
list = (ListView) findViewById(R.id.list);
ArrayList<HashMap<String, String>> productsList;
productsList = new ArrayList<HashMap<String, String>>();
/*
adding data from database to productsList
*/
ListAdapter adapter =
new SimpleAdapter(this, productsList, R.layout.full, new String[]{"left", "right"},
new int[]{R.id.left, R.id.right});
list.setAdapter(adapter);
full.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:layout_weight="1"
android:gravity="center"
android:background="#drawable/background_border"
android:padding="15dp"
>
<TextView
android:id="#+id/left"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="cnsdsdsf:"
android:layout_weight="1"
android:gravity="center"
/>
<TextView
android:id="#+id/right"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_weight="1"
android:gravity="center"
/>
</LinearLayout>
you can use
ImageView imageView = new ImageView(this);
listView.addHeaderView(imageView);
to add a imageView on the head of listview
You can treat the top row or any other row as special for the image. in getView() inflate with separate layout for that image according to position.
You can also modify your item list layout file (full.xml) and create a custom listview adapter. This method will give you more freedom in terms of design. You can play around with this full.xml
full.xml
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:paddingLeft="64dp"
android:paddingRight="32dp"
android:paddingTop="4dp"
android:paddingBottom="4dp"
tools:background="#ffaa00">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/studentPicture"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="#drawable/student_picture"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/left"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/studentPicture"
android:layout_toEndOf="#+id/studentPicture"
android:textColor="#ffffffff"
android:textSize="20sp"
android:paddingLeft="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/right"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/left"
android:layout_toEndOf="#+id/left"
android:textColor="#ffffffff"
android:textSize="20sp"
android:paddingLeft="10dp" />
</RelativeLayout>
Then you create a adapter that will use this list item layout file:
StudentsAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class StudentsAdapter extends BaseAdapter{
private Context mContext;
private String[] mNames;
private String[] mOtherInfo;
public StudentsAdapter (Context context, String[] mNames, String[] mOtherInfo) {
mContext = context;
mNames = students;
mOtherInfo = mOtherInfo;
}
#Override
public int getCount() {
return mNames.length;
}
#Override
public Object getItem(int position) {
return mNames[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
//brand new
convertView = LayoutInflater.from(mContext).inflate(R.layout.full, null);
holder = new ViewHolder();
holder.studentImageView = (ImageView) convertView.findViewById(R.id.studentPicture);
holder.left= (TextView) convertView.findViewById(R.id.left);
holder.right= (TextView) convertView.findViewById(R.id.right);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
//here you set the picture, name, etc.
holder.studentImageView.setImageResource(/* your method to find specific image view */);
holder.left.setText(mNames[position]);
holder.right.setText(mOtherInfo[position]);
return convertView;
}
private static class ViewHolder {
ImageView studentImageView;
TextView left;
TextView right;
}
}
Your activity.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"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/list"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#android:id/empty"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:text="No data to display"
android:textColor="#ffffffff"/>
</RelativeLayout>
And now implement this adapter in your Activity.java
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.Arrays;
public class Activity extends Activity {
//you will need to set this data
private String[] mNames;
private String[] mOtherInfo;
ListView mListView;
TextView mEmptyTextView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
mListView = (ListView) findViewById(android.R.id.list);
mEmptyTextView = (TextView) findViewById(android.R.id.empty);
DayAdapter adapter = new DayAdapter(this, mNames, mOtherInfo);
mListView.setAdapter(adapter);
mListView.setEmptyView(mEmptyTextView);
}
}
I have a DrawerLayout, my DrawerLayout currently display a listview that have an image and a text, and everything just works fine
Now, I decides to add an header in my DrawerLayout and add a special field for a few Rows in my listview
Thank you
this image tells everything about what i want to do:
http://8pic.ir/images/als4ye1elyo35tpju7hq.png
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/MainDrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/MainDrawerFrame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView android:id="#+id/MainDrawerListView"
android:layout_width="270dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:divider="#null"
android:background="#color/DrawerBackGround" />
</android.support.v4.widget.DrawerLayout>
drawer_listview.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="45dp">
<ImageView
android:id="#+id/DrawerIcon"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:contentDescription="#null"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="#+id/DrawerText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_centerVertical="true"
android:textColor="#color/DrawerFontColor"
android:layout_toLeftOf="#+id/DrawerIcon"
android:layout_toStartOf="#+id/DrawerIcon" />
</RelativeLayout>
CustomListAdapter.java
import android.app.Activity;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomListAdapter extends ArrayAdapter<String>
{
private final Activity ActivityContext;
private final String[] ItemName;
private final Integer[] ImageID;
public CustomListAdapter(Activity context, String[] itemname, Integer[] imgid)
{
super(context, R.layout.drawer_listview, itemname);
this.ActivityContext = context;
this.ItemName = itemname;
this.ImageID = imgid;
}
#Override
public View getView(int position, View Unused1, ViewGroup Unused2)
{
LayoutInflater inflater = ActivityContext.getLayoutInflater();
View view = inflater.inflate(R.layout.drawer_listview, null);
ImageView DrawerIcon = (ImageView) view.findViewById(R.id.DrawerIcon);
TextView DrawerText = (TextView) view.findViewById(R.id.DrawerText);
Typeface FontFace = Typeface.createFromAsset(ActivityContext.getAssets(), "Yekan.ttf");
DrawerText.setTypeface(FontFace);
DrawerIcon.setImageResource(ImageID[position]);
DrawerText.setText(ItemName[position]);
return view;
};
}
MainActivity.java
http://paste.ubuntu.com/15237990/
Do you want your header to scroll off the top of the screen when the user scrolls the listview? If so, simply add a header to the listview using the addHeaderView API.
If you want your header to stay in place, add it in the XML
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Header text"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
I'm trying to implement a ListView with an embedded Button. I've made my list item view separately and followed this tutorial.
When I try running it, I get an NPE on the button's setOnClickListener because it says it's a null reference. Kindly point out where I've gone wrong or if I need to do anything else.
Here's my code:
Main activity:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".Data">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/scrollView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="319dp"
android:layout_height="232dp"
android:id="#+id/imageView"
android:src="#drawable/image" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="3"
android:id="#+id/editText"
android:autoText="false"
android:inputType="number"
android:layout_marginTop="30dp"
android:layout_below="#+id/imageView"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter value"
android:id="#+id/textView"
android:textSize="24dp"
android:layout_alignBottom="#+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:id="#+id/button"
android:layout_alignBottom="#+id/editText"
android:layout_toRightOf="#+id/editText"
android:layout_toEndOf="#+id/editText"
android:layout_marginLeft="37dp"
android:layout_marginStart="37dp"
android:onClick="bringList"
android:clickable="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select reason:"
android:id="#+id/textView2"
android:textSize="24dp"
android:layout_below="#+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="38dp" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_below="#+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
List Item:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/listitem"
android:textSize="24dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="38dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Info"
android:id="#+id/listbutton"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignBottom="#+id/listitem"
android:textSize="15dp" />
Java code:
package com.example.abhinav.data;
import android.content.Context;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class Data extends ActionBarActivity {
private ArrayList<String> data = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data);
ListView lv = (ListView)findViewById(R.id.listView);
makeList();
lv.setAdapter(new MyListAdapter(this, R.layout.list, data));
}
private void makeList() {
for(int i = 1; i<6; i++) {
data.add("Theorem"+i);
}
}
public void bringList() {
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_data, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class MyListAdapter extends ArrayAdapter<String> {
private int listlayout;
public MyListAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
listlayout = resource;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vw = null;
if(convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(listlayout, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.title = (TextView) findViewById(R.id.listitem);
viewHolder.button = (Button) findViewById(R.id.listbutton);
viewHolder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//pop up dialog with theorem info
Toast.makeText(getContext(), "Button presss", Toast.LENGTH_SHORT).show();
}
});
convertView.setTag(viewHolder);
}
else {
vw = (ViewHolder) convertView.getTag();
vw.title.setText(getItem(position));
}
return convertView;
}
}
public class ViewHolder {
TextView title;
Button button;
}
}
Change this Lines
viewHolder.title = (TextView) findViewById(R.id.listitem);
viewHolder.button = (Button) findViewById(R.id.listbutton);
to
viewHolder.title = (TextView) convertView.findViewById(R.id.listitem);
viewHolder.button = (Button) convertView.findViewById(R.id.listbutton);
NPE is because you reading view incorrectly.
It seems to me that your List Item might only inflate the TextView element, because it's the actuall root element for the XML.
I recommend wrapping both the TextView and the Button in your list.xml file with a LinearLayout so the actual root view will wrap all your views.
Here's an example of what stacking both views vertically would look like with a LinearLayout for a ListView item.
<?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">
<TextView
android:id="#+id/listitem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="38dp"
android:text="New Text"
android:textSize="24dp" />
<Button
android:id="#+id/listbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/listitem"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:text="Info"
android:textSize="15dp" />
</LinearLayout>
I don't know if this is the case, but you are creating two different ViewHolders. Use vw = new ViewHolder();
And maybe you need to use convertView.findViewById(R.id.listbutton);
I am new to Android, and the first project I am working on is putting players onto a team. I have a listview that grabs all of the players from a database. Each name is supposed to have a checkbox next to them and then there is a button at the bottom that is supposed to update the database. So far this is what I am getting.
http://dyp.im/W79JdmfIk
Here is my activity
package com.example.sqllitetest;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.ResourceCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
public class Tester extends ListActivity {
MyAdapter mListAdapter;
PlayersDBAdapter mDbHelper;
private Button button;
private Context context = this;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDbHelper = new PlayersDBAdapter(this);
mDbHelper.open();
Cursor myCur = mDbHelper.fetchAllPlayers();
startManagingCursor(myCur);
setContentView(R.layout.playertoteam);
// registerForContextMenu(getListView());
// ((Tester) context).setContentView(R.layout.playertoteam);
//getListView().addFooterView(button);
mListAdapter = new MyAdapter(Tester.this, myCur);
setListAdapter((ListAdapter) mListAdapter);
Button button = (Button) findViewById(R.id.alertBox);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
setResult(RESULT_OK);
finish();
}
});
}
private class MyAdapter extends ResourceCursorAdapter {
public MyAdapter(Context context, Cursor cur) {
super(context, R.layout.playertoteam, cur);
}
#Override
public View newView(Context context, Cursor cur, ViewGroup parent) {
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return li.inflate(R.layout.playertoteam, parent, false);
}
#Override
public void bindView(View view, Context context, Cursor cur) {
TextView tvListText = (TextView)view.findViewById(R.id.code);
CheckBox cbListCheck = (CheckBox)view.findViewById(R.id.checkBox1);
tvListText.setText(cur.getString(cur.getColumnIndex(mDbHelper.KEY_NAME)));
cbListCheck.setChecked((cur.getInt(cur.getColumnIndex(mDbHelper.KEY_ID))==1? false:true));
}
}
}
And here is my xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dip" >
<Button
android:id="#+id/alertBox"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/confirm" />
<RelativeLayout
android:id="#+id/relativeLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="top" >
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/alertBox"
/>
<TextView
android:id="#+id/code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<CheckBox android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
/>
</RelativeLayout>
</LinearLayout>
I would like to know how to remove the buttons from the rows and the check box that is underneath the main confirm button. Also I would like to know if this is the best way to do something like this, because, once again, I am new and I don't know how to search for the correct methods.
ResourceCursorAdapter creates views ,for every row of the ListView, that are inflated in newView() method. You are getting button in every row because of this reason.
Soln: Create a separate xml defining the row layout for the ListView and inflate this new layout in the newView() method. And have only the button and ListView in main layout i.e. playertoteam.xml
playertoteam.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="wrap_content"
android:padding="6dip" >
<Button
android:id="#+id/alertBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="#string/confirm" />
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="#id/alertBox" />
</RelativeLayout>
row_layout.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="wrap_content">
<TextView
android:id="#+id/code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"/>
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_alignParentRight="true"/>
</RelativeLayout>
You can find example for ListView here
you can use addFooterView method of listview.
View footerView =
((LayoutInflater)
ActivityContext.
getSystemService
(Context.LAYOUT_
INFLATER_SERVICE)).inflate
(R.layout.footer_
layout, null, false);
ListView.addFooterView
(footerView);
I'm trying to set a Listview under some other Widgets (Buttons, editText, etc). I don't want to use another activity for the listview. After reading some I found How can I implement a ListView without ListActivity? (use only Activity) and I tried to do it, ending up with:
Here is my main.xml:
<LinearLayout android:id="#+id/relativeLayout1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical">
<TextView ...[some code].../>
<EditText ...[some code].../>
<ImageButton ...[some code].../>
<Chronometer ..[some code]..../>
<ListView android:id="#+id/listView1" android:layout_height="wrap_content"
android:layout_width="fill_parent"></ListView>
here is my onCreate:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (ImageButton) findViewById(R.id.button1);
mChronometer = (Chronometer) findViewById(R.id.chronometer1);
editText1 = (EditText) findViewById(R.id.editText1);
ListView lv = (ListView) findViewById(R.id.listView1);
String[] listword = new String[] {"Hello","World","Foo","Bar"};
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, listword));
}
and here is list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
When I debug in my physical device, the application simply stays all black. If I comment out the lines of onCreate() that involve the list, the application works (obviously without the listview).
Any ideas what might be wrong?
I actually have an app with a listview below some TextViews and above two buttons, let me grab my code!
Here's my activity with listview below the textviews and above the buttons (with quite a bit removed for brevity):
public class BNYDirectoryResults extends Activity{
public static String[] stuff;
ListView list;
BNYAdapter adapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.resultscreen);
TextView headDisplay = (TextView)findViewById(R.id.results);
TextView headCount = (TextView)findViewById(R.id.resultsTotal);
TextView headPages = (TextView)findViewById(R.id.pages);
//Set up the results list, see BNYAdapter
list = (ListView)findViewById(R.id.list);
adapter = new BNYAdapter (this, BNYDirectory.ReturnResults);
list.setAdapter(adapter);
//Sets up header information (pages, total results)
//Just some stuff to change the TextViews
//Passes EmployeeID and Phone Number to AND opens the details page
list.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
String EID = BNYDirectory.ReturnResults[position][0];
String phoneNumber = BNYDirectory.ReturnResults[position][2];
BNYDirectoryTransaction.doDetails(EID, phoneNumber);
Intent i = new Intent(view.getContext(), BNYDirectoryDetails.class);
startActivity(i);
}
});
}
}
Here's the XML file for it:
<TextView
android:id="#+id/results"
android:text="Name"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" android:textStyle="bold"/>
<TextView
android:id="#+id/resultsTotal"
android:text="Phone"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" android:textStyle="bold"/>
<TextView
android:id="#+id/pages"
android:text="AIM"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/name"
android:text="Name"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="#+id/phone"
android:text="Phone"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="#+id/aim"
android:text="AIM"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="#+id/dept"
android:text="Dept"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/prevButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Previous"
android:layout_weight="1"/>
<Button
android:id="#+id/nextButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Next"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
and here's the custom adapter for the list:
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class BNYAdapter extends BaseAdapter {
private Activity activity;
private String[][] results;
private static LayoutInflater inflater=null;
public BNYAdapter(Activity a, String[][]info) {
activity = a;
results = info;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return results.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
public TextView name;
public TextView phone;
public TextView aim;
public TextView dept;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.item, null);
holder=new ViewHolder();
holder.name=(TextView)vi.findViewById(R.id.nameItem);
holder.phone=(TextView)vi.findViewById(R.id.phoneItem);
holder.aim=(TextView)vi.findViewById(R.id.aimItem);
holder.dept=(TextView)vi.findViewById(R.id.deptItem);
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();
holder.name.setText(BNYDirectory.ReturnResults[position][1]);
holder.phone.setText(BNYDirectory.ReturnResults[position][2]);
holder.aim.setText(BNYDirectory.ReturnResults[position][3]);
holder.dept.setText(BNYDirectory.ReturnResults[position][4]);
return vi;
}
}
and all together that makes a page like
You can use your own layout with a ListActivity. Make your activity extend ListActivity, create your own layout and make sure that the ListView has android:id="#android:id/list" (this is how the ListActivity links to the ListView in your own layout), then in onCreate set your layout with setContentView.