Two views in each list item - android

I'm trying to show two different views in each element of the list. Both vews are text views, but I want one of them to be enclosed in a square with a different color. I know is possible because I've read about it but I'm not able to understand it properly!
Any ideas?
Thanks!

You can create your own adapter for the list. The adapter is what decides how to display the items in the list.
Here is an example:
class MyAdapter extends ArrayAdapter<TheObjectsToPopulateYourList>{
public MyAdapter(Context context, int textViewResourceId, ArrayList<TheObjectsToPopulateYourList]> objects) {
super(context, textViewResourceId, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if( convertView== null ) convertView = getLayoutInflater().inflate(R.layout.your_layout, null);
TextView myTextView1 = (TextView)convertView.findViewById(R.id.yourFirstTextView);
myTextView1.setText(getItem(position*2)); //getItem gets the item (String in this case) from the list we specified when creating the adapter.
//position is the current position of the list, and since each position has two items, we have to multiply the position by 2 to get to the right item-list-position.
TextView myTextView2 = (TextView)convertView.findViewById(R.id.yourSecondTextView);
myTextView2.setText(getItem(position*2 +1 ));
myTextView2.setBackgroundColor(0xFFFF00FF); //Any color. Use setBackgroundResource to use a resource object (drawable etc.)
return convertView;
}
}
And you also need the line-layout to contain all the elements you need (this is what will be displayed on each line of the list), let's call it 'thelinelayoutfile.xml' and put it in the layout folder:
<?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="wrap_content"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:id="#+id/yourFirstTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="line1"/>
<TextView
android:id="#+id/yourSecondTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="line2"/>
</LinearLayout>
Then when you initialize your list (in your onCreate() method, perhaps?)
You call
//You can create the list anywhere, or use an array.
//I will create it here, just for the sake of demonstration.
ArrayList<String> myLines = new ArrayList<String>();
myLines.add("item1, line1");
myLines.add("item1, line2");
myLines.add("item2, line1");
myLines.add("item2, line2");
//set the list adapter:
ListView myList = (ListView)findViewById(R.id.whateveryourlistidis);
myList.setAdapter(new MyAdapter(this, R.layout.thelinelayoutfile, myLines));

More details would help; my solution may not be accurate, as I don't know what you want very well.
Check out the View element's background color: http://developer.android.com/reference/android/view/View.html#setBackgroundColor(int).
If you have two views, and set the background colors to different values, you can get each view to have square around it. This may be able to create the desired effect.

Related

What view(s) can I use in order to create something like the about screen of the phone?

I want to write an activity that is similar to the about screen of android phones. I want it to display some information in the style of the about screen of android phones.
Like this
title1
info
-----------------
title2
info
-----------------
etc.
Is there a special view that I can use or is it just a result of multiple views placed in a specific way? Or is there an activity template in android studio that I can use?
Use ListView. You can create a custom layout for cells and then use an array or a cursor to fill the data.
ListView: A view that shows items in a vertically scrolling list. The
items come from the ListAdapter associated with this view.
ListAdapter can receive data as input. The adapter would inflate the layout for each cell in its getView() method and assign the data to the individual views in the cell.
Read more about ListView here: http://developer.android.com/reference/android/widget/ListView.html
See PreferenceActivity or PreferenceFragment. They are special list views populated either from code or from a xml file. There are many different preference types to choose from (checkbox, switch, list etc)
An example preference fragment:
You can use ListView and a custom ArrayAdapter to create a screen like that. If you need any help about how to create a custom ArrayAdapter check this useful tutorial here.
If you want to create a simple list, then ListView is probably the simplest option. You may also want to look into ListActivity and/or ListFragment as well to further simplify the process.
If you intend to use complex animations, or have the list update dynamically with animations, you may be better served with RecyclerView, although using it is more complex.
An straightforward implementation of ListActivity could look something like this:
public class MainActivity extends ListActivity {
String[] titles = { "title one", "title two" };
String[] descriptions = { "desc 1", "desc 2" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ListAdapter() {
leave everything the same, except for getCount() and getView()
#Override
public int getCount() {
return titles.length;
}
This will ensure you list is always the correct length as your array.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) parent.getContext().
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.row, parent, false);
} else {
view = convertView;
}
TextView title = (TextView) view.findViewById(R.id.title);
TextView description = (TextView) view.findViewById(R.id.description);
title.setText(titles[position]);
description.setText(descriptions[position]);
return view;
}
And row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/title"
android:textSize="24sp"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/description"
android:textSize="20sp"/>
</LinearLayout>

Is there a way to allow user to select text from many `EditText`s in a row (which are in ListView)?

I'm trying to use a ListView for a message history in an IM app, which previously used single WebView for displaying history, and I'm thinking about how to implement text selection.
I see at least one major problem — the ListView recycles views, so the naive way wouldn't work, if user tries to select text from a lot of items, and scrolls the list far away from the beginning of the selection. But anyway, Android doesn't let selection to "jump" from one EditText to another. So, is there a (cool) way to implement such feature? (It's OK if it will work on Android 4.0+ only.)
My current example is made of the following code:
Adapter:
private class MessagesAdapter extends ArrayAdapter<String> {
public MessagesAdapter(Context context, List<String> objects) {
super(context, 0, objects);
}
#Override
public EditText getView(int position, View convertView, ViewGroup parent) {
final EditText result = (EditText)(convertView == null ?
LayoutInflater.from(getContext()).inflate(R.layout.message_row, parent, false) :
convertView);
result.setText(getItem(position));
return result;
}
}
Row layout:
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cursorVisible="false"
android:editable="false"
android:textIsSelectable="true"/>
I can select text in a single EditText, but not in many EditTexts at once.

What's the quickest way to add several views to a LinearLayout?

I have a LinearLayout view that already contains several elements. I want to add a lot more Views to it, programmatically. And because this is inside a ScrollView, everything will be scrolled.
So what I do is go through my list, and add new instances of my custom View to it. That custom view inflates a XML layout and adds a few methods.
This approach works well. The problem is that it's super slow, even without any crazy code... a list with 10 items takes around 500ms to instantiate. As an user experience standpoint, this is hard to swallow.
My question is, is this the correct/best approach? Android seems to take a lot of time inflating the layout, even though "R.layout.my_list_item" is super simple. I wonder if there's a way to maybe to reuse "inflated" layouts for additional views, kinda caching the more complex parsing?
I've tried doing this with a ListView (and adapter and a wrapper) and it seems to be much faster. The problem is that I can't use a simple ListView; my layout is more complex than a simple list (the LinearLayout itself contains additional custom icons, and it has another parent with even more Views before it's wrapped by the ScrollView).
But is there a way to use an adapter for a LinearLayout? Would that be faster than trying to add the views myself?
Any help is appreciated. I'd love to make this faster.
Code follows.
Main Activity:
// The LinearLayout that will contain everything
lineList = (LinearLayout) findViewById(R.id.lineList);
// Add a lot of items for testing
for (int i = 0; i < 10; i++) {
addListItem("Item number " + i);
}
protected void addListItem(String __title) {
MyListItem li;
li = new MyListItem(this);
li.setTitle(__title);
lineList.addView(li);
}
MyListItem:
public class MyListItem extends RelativeLayout {
protected TextView textTitle;
public MyListItem(Context __context) {
super(__context);
init();
}
public MyListItem(Context __context, AttributeSet __attrs) {
super(__context, __attrs);
init();
}
public MyListItem(Context __context, AttributeSet __attrs, int __attrsdefStyle) {
super(__context, __attrs, __attrsdefStyle);
init();
}
protected void init() {
// Inflate the XML layout
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.my_list_item, this);
// Create references
textTitle = (TextView)findViewById(R.id.textTitle);
}
public void setTitle(String __text) {
textTitle.setText(__text);
}
}
What I'm trying to accomplish is this. Consider this layout:
This layout is a FrameLayout (outer box) containing a ImageView (in gray), a TextView (inner rectangle, on top) and a LinearLayout (inner rectangle, on bottom). This LinearLayout rectangle is the one I'm dynamically populating with a few items.
After I populate it, I want the final result to be this (where every new rectangle is a new MyListItem instance):
That is, everything is scrollable (the background image, for example, is aligned on top). The LinearLayout isn't scrollable by itself (everything else follows) hence why a ListView, from what I know, wouldn't work very well
in my case.
3 Options:
Replace everything with a ListView, with the other parent and custom icons as a header view for the ListView. ListView is faster, because it only creates Views as it needs them.
Programatically create the contents of my_list_item instead of inflating, might be quicker
Use of ViewStubs may allow you to load views on-demand.
Maybe it isn't loading the views but the data? in which case prepare the data in a background thread.
A ListView is the way to go.
You say that your layout is too complex. But it is completely okay to inflate a complex layout as a child. For example a layout that has text and then an icon:
<?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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Could be inflated in your adapter as so:
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LinearLayout root = null;
ImageView editImageView;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
root = (LinearLayout)inflater.inflate(R.layout.item, null);
} else {
root = (LinearLayout)convertView;
}
}
You can also be a little more clever in order to support a header. Just add a check if the index is the root and inflate a different view. Since the header is the only one that is different you will still take advantage of all the other rows being reusable. You can even pre-inflate and store the header and reuse it to completely get rid of inflation.
Just use a ListView!
It's the easiest to set up and easiest to maintain. You define an XML layout for the List-Row, and an XML layout for the View which holds the entire List. The ListAdapter does the rest for you.
Just create a:
List<HashMap<String, String>> services = new ArrayList<HashMap<String, String>>();
...and loop through your data to add as many items as you like to the Map. Then set this map to your ListAdapter. Whether 10 items or 100 items the ListAdapter will create a List with that many items.
Example:
public void updateProductList(String searchTerm) {
createOrOpenDB(this);
Cursor cursor = (searchTerm!=null)? dbAdapter.fetchWhere(TBL_NAME, KEY_NAME+" LIKE '%"+searchTerm+"%'")
: dbAdapter.fetchAll(TBL_NAME);
int rows = cursor.getCount();
if (rows<=0) return;
services.clear(); //clear current list
for (int i=0; i<rows; i++) {
HashMap<String, String> map = new HashMap<String, String>();
cursor.moveToPosition(i);
map.put(KEY_NAME, "" + cursor.getString(cursor.getColumnIndex(KEY_NAME)));
map.put(KEY_DESC, "" + cursor.getString(cursor.getColumnIndex(KEY_DESC)));
map.put(KEY_COST, "" + cursor.getDouble(cursor.getColumnIndex(KEY_COST)));
services.add(map);
}
cursor.close();
closeDB();
ListAdapter adapter = new SimpleAdapter(this, services, R.layout.products_view_row,
new String[] {KEY_NAME, KEY_DESC, KEY_COST},
new int[] {R.id.listViewText1, R.id.listViewText2, R.id.listViewText3});
setListAdapter(adapter);
}

Create more advanced lists in Android

I need to create a more advanced list in my android application, that will contain more then just one string per item. Actually, I'm looking for something similar to the appointments view in the calendar app: I want to categories my items by day, and show something similar to the hour of the appointment.
No idea how to go about this though, I'm guessing it's not possible with the ListActivity?
What do you guys suggest?
Edit:
If someone could give a code example I'd really appreciate it, I'm not sure how to execute what was suggested in the current replies..
It's easily doable with ListACtivity. You need to create a subclass within it, that extends ArrayAdapter (or any other list adapter) and overrides it's getView() method. You will want to create an xml file defining the "view" for each list item. Then within your overridden getView() method, you will need to inflate that view, then use findViewById() for each of the elements you want to assign a value to.
public class YourListActivity extends ListActivity {
private String[] values = new String[]{"Row 1", "Row 2", "Row 3"};
private class Adapter extends ArrayAdapter<String> {
private LayoutInflater li = LayoutInflater.from(this.getContext());
public Adapter() {
super(YourListActivity.this, 0, values);
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View v = li.inflate(R.layout.row, parent);
TextView field1 = (TextView) v.findViewById(R.id.field1);
field1.setText(values[position]);
return v;
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(new Adapter());
}
}
row.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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/field1"
/>
</LinearLayout>
main.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"
>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#android:id/list"
/>
</LinearLayout>
You can further modify the appearance of the row, and the fields it holds, by adding additional TextViews to the row.xml, and using the position passed through to getView() to set the values of your extra fields as appropriate, and you can even customise the rows on a row-by-row basis if you need.
This recorded talk from Google I/O last year explains what you want to know: http://www.google.com/events/io/2010/sessions/world-of-listview-android.html
Create a ListAdapter. That's the class responsible for providing the list with its UI elements. With its getView you can create whatever Views you like. If you have more than one type of list element, beware not to reuse the convertView that getView receives.

Hidden Field/Tag in TextView?

I want to pass a value that will be generated at runtime,through a TextView. the text property is used for some other data and the data that I want to pass will not be displayed. So, it's a like a hidden tag. Is it possible to do with TextView? If so, which property of the TextView.
For simplicity's sake imagine I pull the ID and TEXT from the data table. Now the TEXT is displayed on the TextView but when I want to pass the reference to that particular row of the table to some other function I want to pass the ID as an argument/handle. So, the ID will be hidden and associated with the TextView. How can I do it? If not possible can you suggest any alternative to accomplish this? BTW, the TextView is embedded within a ListView.
Adapter code :
cursor = db.rawQuery("SELECT * FROM EmpTable", null);
adapter = new SimpleCursorAdapter(
this,
R.layout.item_row,
cursor,
new String[] {"Emp_Name"},
new int[] {R.id.txtEmployee});
Try setTag(int, Object) and getTag(int). There are even versions that don't take a key, if you just want to store one value. From the docs:
Sets the tag associated with this
view. A tag can be used to mark a view
in its hierarchy and does not have to
be unique within the hierarchy. Tags
can also be used to store data within
a view without resorting to another
data structure.
So you can do:
textView.setTag(myValue);
and get it back later with:
myValue = textView.getTag();
Since the interface uses Object, you will need to add casts. For example, if your value is an int:
textView.setTag(Integer.valueOf(myInt));
and:
myInt = (Integer) textView.getTag();
Edit - to subclass and add the tag, use:
adapter = new SimpleCursorAdapter(this, R.layout.item_row,
cursor, new String[] {"Emp_Name"}, new int[] R.id.txtEmployee}) {
#Override
public View getView (int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
view.setTag(someValue);
return view;
}
};
You can use setTag() and getTag().
One way to do this would be to have your ListAdapter inflate a layout instead of a TextView for each item of the list. Then you can have other (invisible) fields hidden in the layout.
The xml might look like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/visible_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Visible text"/>
<TextView android:id="#+id/hidden_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hidden value"
android:visibility="gone"/>
</LinearLayout>

Categories

Resources