I have created a list view with 3 items in each list view item using two separate .'xml's.
1) list_view.xml
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ListView">
</ListView>
2) list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:paddingTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Text here"
android:id="#+id/textView"
android:layout_alignTop="#+id/TextView"
android:layout_alignLeft="#+id/TextView"
android:layout_centerVertical="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="-"
android:id="#+id/seperator"
android:layout_alignBottom="#+id/textView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="000"
android:id="#+id/price"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="$"
android:id="#+id/prefix"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/price"/>
</RelativeLayout>
Now, I have created a list view with all the items using a 'SimpleAdapter'. Now I want to access the items in the list view (textView, separator,...) and change the color, size etc programmatically.
Here is the code of my MainActivity.java
public class MainActivity extends Activity {
public RelativeLayout relativeLayout;
public RelativeLayout.LayoutParams lp;
public SimpleAdapter mList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);
ListView list = (ListView) findViewById(R.id.ListView);
//background color
list.setBackgroundColor(Color.YELLOW);
TextView itemText = (TextView)findViewById(R.id.textView);
itemText.setTypeface(Typeface.SANS_SERIF);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
//Dummy data
//add all elements to map
map.put("text", "click to edit0");
map.put("seperator", "-");
map.put("price", "100");
// add map elements to list
mylist.add(map);
//add all elements to map
map = new HashMap<String, String>();
map.put("text", "click to edit1");
map.put("seperator", "-");
map.put("price", "102");
//add all elements to map
mylist.add(map);
map = new HashMap<String, String>();
map.put("text", "click to edit2");
map.put("seperator", "-");
map.put("price", "104");
mylist.add(map);
//use adapter to set list items
mList = new SimpleAdapter(this, mylist, R.layout.list_item,
new String[] {"text", "seperator", "price"}, new int[] {R.id.textView, R.id.seperator, R.id.price});
list.setAdapter(mList);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
All worked fine until I tried to access the list view items and change them.
My code crashes and shows a null pointer exception at
TextView itemText = (TextView)findViewById(R.id.textView);
itemText.setTypeface(Typeface.SANS_SERIF);
I suspect this might be because I did not the setContentView to list_item.xml.
Is there a way to access the elements? If yes how?
for setting color follow the bellow code
String text = "<font color=#8469af>By tapping Checkout, you agree to our </font> <font color=#ffffff>terms.</font>";
tvTerms.setText(Html.fromHtml(text));
when you try to call findviewbyid you didnt create listitems yet so it returns null pointer
if you want to set type face with standart font use
android:typeface="serif"
but if you insist do it programmatically
first get list items from list then
(TextView) list_item.findViewById(R.id.textView);
As Talha pointed out the exception comes from the call on findViewById on this where your id textView does not exists, so it returns null
If you want to modify your list items programmatically you should implement your own adapter and apply your modifications in the getView method
Related
I will put a picture on top of a custom list view for the header image, but the image is repeated, how can I make it can't click and non-repeatable?
this is my xml code :
<?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:background="#color/abs__background_holo_light" >
<ImageView
android:id="#+id/detailImage"
android:layout_width="wrap_content"
android:layout_height="140dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:contentDescription="#string/app_name"
android:scaleType="centerCrop"
android:src="#drawable/pantaigoacina" />
<RelativeLayout
android:id="#+id/detailLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/detailImage"
android:background="#color/abs__background_holo_light" >
<LinearLayout
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="5dip"
android:layout_marginTop="20dip"
android:background="#color/abs__background_holo_light"
android:padding="5dip" >
<ImageView
android:id="#+id/list_image"
android:layout_width="15dip"
android:layout_height="15dip"
android:src="#drawable/arrow"
android:contentDescription="#string/app_name"/>
</LinearLayout>
<TextView
android:id="#+id/NamaLokasi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail"
android:text="Pantai Sipelot"
android:textColor="#040404"
android:textSize="18sp"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="#+id/detailLokasi"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/NamaLokasi"
android:layout_marginRight="20dip"
android:layout_marginTop="5dip"
android:layout_toRightOf="#+id/thumbnail"
android:text="Deskripsi Konten, Lokasi Berada di Desa Wilayah Malang"
android:textColor="#343434"
android:textSize="12sp" />
</RelativeLayout>
</RelativeLayout>
this is my adapter :
String[] from = { "pantai", "detail" };
int[] to = { R.id.NamaLokasi, R.id.detailLokasi };
SimpleAdapter adapter = new SimpleAdapter(getActivity()
.getBaseContext(), aList, R.layout.fragment_detail, from,
to);
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
Remove the image form the xml.
ImageView iv = new ImageView(getActivity());
// set image to imageview
ListView list = getListView();
list.addHeaderView(headerView);
The docs
public void addHeaderView (View v)
Added in API level 1
Add a fixed view to appear at the top of the list. If addHeaderView is called more than once, the views will appear in the order they were added. Views added using this call can take focus if they want.
Note: When first introduced, this method could only be called before setting the adapter with setAdapter(ListAdapter). Starting with KITKAT, this method may be called at any time. If the ListView's adapter does not extend HeaderViewListAdapter, it will be wrapped with a supporting instance of WrapperListAdapter.
Parameters
v The view to add.
Edit:
#Override
public void onActivityCreated(Bundle savedInstanceStat)
{
super.onActivityCreated(savedInstanceState);
list = getListView();
ImageView imageHeaderView = new ImageView(getActivity());
imageHeaderView.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.pantaigoacina));
list.addHeaderView(imageHeaderView);
List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < DaerahWisata.pantai.length; i++) {
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("pantai", DaerahWisata.pantai[i]);
hm.put("detail", "Lokasi : \r\n" + DaerahWisata.detailp[i]);
aList.add(hm);
}
String[] from = { "pantai", "detail" };
int[] to = { R.id.NamaLokasi, R.id.detailLokasi };
SimpleAdapter adapter = new SimpleAdapter(getActivity()
.getBaseContext(), aList, R.layout.fragment_detail, from,
to);
setListAdapter(adapter);
}
Edit, if want to set imageview from other xml layout :
list = getListView();
Context context = getListView().getContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.image_header, null);
list.addHeaderView(view);
Try below code:-
ImageView imageHeaderView = new ImageView(this);//use getActivity() on fragment
imageHeaderView.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.deckblatt));
myList.addHeaderView(imageHeaderView);
I've made an application with a ListView. I'm to trying to add a footer to my ListView (which scrolls along with the ListView), using addFooterView. In this footer I want to add a TextView with some text which is defined in my Activity as a String called notification.
I've tried several things but it wouldn't work.
My ListActivity, RoosterWijzigingen.java:
public class RoosterWijzigingen extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
ArrayList<HashMap<String, String>> roosterwijziginglist = new ArrayList<HashMap<String, String>>();
String hour = "4";
String info = "Af 123";
String notification = "Het is 40min-rooster!";
// Creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// Adding each child node to HashMap key => value
map.put("hour", hour);
map.put("info", info);
// Adding HashList to ArrayList
roosterwijziginglist.add(map);}
ListAdapter adapter = new SimpleAdapter(this, roosterwijziginglist , R.layout.roosterwijzigingen,
new String[] {"hour", "info"},
new int[] {R.id.hour, R.id.info});
setListAdapter(adapter);
setContentView(R.layout.listplaceholder);
}
}
I get the information for the strings hour, info and notification from a JSON file, but I omited this to shorten the code.
listplaceholder.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true" />
<TextView
android:id="#id/android:empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="No data" />
roosterwijzigingen.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/hour"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
<TextView
android:id="#+id/info"
android:layout_width="match_parent"
android:layout_height="fill_parent" />
</LinearLayout>
You have to put your String in a TextView before adding it to your list, and it will look something like that :
String footerText = "Footer text";
TextView textView = new TextView(this);
textView.setText(footerText);
getListView().addFooterView(textView);
Remember that 99% of the time when you want to display something on the screen, it has to be a View.
In a 2-column row layout, is there a way to tell which of the columns the user clicked or pressed on?
If the second column can be a button, it would be even better, but I am not sure how to make it into a button. Here is what I am doing:
private List<HashMap<String, String>> fillMaps;
private SimpleAdapter simple_adapter;
ListView list = null;
and later:
list = (ListView) findViewById(android.R.id.list);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
// My data
fillMaps = new ArrayList<HashMap<String, String>>();
simple_adapter = new SimpleAdapter(this, fillMaps, R.layout.comment_list,
new String[] {"train", "from"},
new int[] {R.id.TRAIN_CELL, R.id.FROM_CELL });
// This was the middle item R.id.FROM_CELL,
list.setAdapter(simple_adapter);
list.setTextFilterEnabled(true);
and I populate the list like this:
for ( int i = 0; i < obj.length(); i++ )
{
JSONObject o = obj.getJSONObject(i);
HashMap<String, String> map = new HashMap<String, String>();
... Some variables set here...
map.put("train", comment);
map.put("from", "Edit");
fillMaps.add(map);
discussion.add( d );
}
}
simple_adapter.notifyDataSetChanged();
Here is the comment_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="13sp"
android:weightSum="1.0"
android:orientation="horizontal">
<TextView android:id="#+id/TRAIN_CELL"
android:textSize="16sp"
android:layout_height="wrap_content"
android:layout_width="275dip"/>
<TextView android:id="#+id/FROM_CELL"
android:textSize="16sp"
android:textStyle="bold"
android:layout_height="wrap_content"
android:layout_width="50dip"/>
</LinearLayout>
Thanks!
try using some clickable items in the two columns of the row and handle those click events.
Suppose give two buttons in those two columns and add setOnClickListeners to them and finally check which button is click in the activity
You can use something like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="13sp"
android:weightSum="1.0"
android:orientation="horizontal">
<Button
android:id="#+id/TRAIN_CELL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/FROM_CELL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/label_grid_view" />
</LinearLayout>
Be back if you have any issues
I have a listview and two textveiws in it for two columns. My goal is to set the colour of e.g. the 5th row of the listview to blue. Details:
config.xml has the layout for the acitivty: buttons and the listview. Part of it:
<ListView android:id="#+id/ListView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/TextView01">
</ListView>
row.xml defines the two coloumns for the listview:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/linlay0">
<LinearLayout android:orientation="horizontal"
android:id="#+id/linlay"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/left"
android:layout_width="160px"
android:layout_height="wrap_content"
android:textColor="#FF0000"
android:paddingLeft="5px"/>
<TextView android:id="#+id/right"
android:layout_width="140px"
android:layout_height="wrap_content"
android:maxWidth="140px"
android:singleLine="false"/>
</LinearLayout>
</LinearLayout>
MainActivity.java is for the management for the activity, as well as the listview. So contentview is set to config.xml:setContentView(R.layout.config);
This is how i upload the listview with data:
lv1=(ListView)findViewById(R.id.ListView01);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
for (int i=0; i<31; i++)
{
HashMap<String, String> map = new HashMap<String, String>();
map.put("left1", date[i]);
map.put("right1", name[i]);
mylist.add(map);
}
simpleAdapter = new SimpleAdapter(this, mylist, R.layout.row,
new String[] {"left1", "right1"}, new int[] {R.id.left, R.id.right});
lv1.setAdapter(simpleAdapter);
date[i] and name[i] are arrays declared and uploaded at the beginning of the class.
I am running some queries, comparing arrays and now i want to the set colour of a specific row in the ListView to blue. Like i said contentview is set to config.xml, while the TextViews of the ListView is in row.xml.
So TextView txt1 = (TextView) findViewById(R.id.left); is uninterpretable for Eclipse.
I would have solved this by implemented my own adapter, and used the position parameter in the getView() method of the adapter to set the background of the LinearLayout by code.
In your CustomAdapter in getView method use this
if(position==5)
{
convertView.setBackgroundColor(Color.BLUE);
}
simpleAdapter is a system adpater,you can not control it.In my opioion,if you want set color at 5th row,you should implementation any adapter and override the getView or bindView method
,accord the position(row number) you can do anything you want
I'm just starting to develop on Android so this may be a very basic thing, but. . .
I'm having a really hard time setting up a ListView within a LinearLayout. Maybe it's a problem in my XML layout, or maybe it's a problem with my databinding code.
I'm confused by the many tutorials that say that my Activity should extend ListActivity. Why would this one widget require that my entire UI be changed?
The thing is, I only want the scrolling box of items to occupy a part of this one screen. Is ListView the wrong widget to use? Should I be using a ScrollView instead?
Thanks -- here's my XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFF"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/enter_text"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<EditText
android:id="#+id/txtTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_weight="1"
android:width="200px"/>
</LinearLayout>
<ListView android:id="#+id/listRecent"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
And here's my code:
public class MyApp extends Activity {
private String TAG = "MyApp";
//UI ELEMENTS
EditText txtTo;
ListView listRecent;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//GET REFERENCES TO UI ELEMENTS
listRecent = (ListView) this.findViewById(R.id.listRecent);
try {
//----------------------------------------
// create the grid item mapping
String[] from = new String[] {"zero", "one", "two", "three"};
int[] to = new int[] {0, 1, 2, 3};
// prepare the list of all records
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for(int i = 0; i < 10; i++){
HashMap<String, String> map = new HashMap<String, String>();
map.put("rowid", "" + i);
map.put("col_1", "col_1_item_" + i);
map.put("col_2", "col_2_item_" + i);
map.put("col_3", "col_3_item_" + i);
fillMaps.add(map);
}
// fill in the grid_item layout
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.recently_dingdonged, from, to);
listRecent.setAdapter(adapter);
//----------------------------------------
}
catch(Exception e) {
Log.e(TAG, "Error here (3116019)", e);
}
}
ListView should be nested inside of scrollview. By extending listActivity you gain the ability to setAdapter and populate the data into your list view through the main object and not through instantiating the list. This is by no means a requirement though.
You don't need to make your Activity extend ListActivity. You can, but it's not necessary.
In relation with your xml, try this out:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFF"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/enter_text"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/txtTo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_weight="0.5"
android:width="200px"/>
</LinearLayout>
<ListView android:id="#+id/listRecent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5" />
</LinearLayout>