Hidden Field/Tag in TextView? - android

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>

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>

Change text color of android.R.layout.simple_list_item_multiple_choice

I am trying to figure out how I can change the text color of android.R.layout.simple_list_item_multiple_choice
I know you can create your own listview with checkboxes but I really do not want to have to do that as the android.R.layout.simple_list_item_multiple_choice works perfectly, I would just like to know how to change the Text color.
I have come accross this question here but I do not understand how I can use the most voted up answer.
How can I override android.R.layout.simple_list_item_multiple_choice textview so I can change the color of it. Thank you.
You can change color easily.
You just have to override the getView() method.
And inflate the android.R.layout.simple_list_item_multiple_choice in particular view. now you can access the whole template.
So that you can get the TextView used in this template by finding using id android.R.id.text1. after that you can change the behavior of textview.
ArrayAdapter<String> list = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, your_data_container) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = LayoutInflater.from(InvitationFlowActivity.this).inflate(android.R.layout.simple_list_item_multiple_choice, null);
TextView tv = (TextView) v.findViewById(android.R.id.text1);
tv.setTextColor("Your_color");
}
};
And if you are using custome adapter then you can also override the same getView() method and you can customize it as per your requirement.
Just make custom layout - simple_list_item_multiple_choice.xml like this -
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:textColor="#FF0000"
/>
and use it in place of android.R.layout.simple_list_item_multiple_choice.xml
There are two ways to do this.
1. Override the getView method
2. Use custom layout instead of standard layout.
No. 2 is better as you have more control on the same and you can edit at your ease.
Alt+Click will clone the required layout to your res/layout folder.
Make changes to the new layout and use it in the adapter.
ArrayAdapter aa = new ArrayAdapter(getApplicationContext(),R.layout.text_change,listItems);
listView.setAdapter(aa);
Here listItems is the String array of list.
text_change.xml is the updated layout xml
listView is your listview in the main layout.
In order to dynamically update color of content use the following code
AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position==0)
{
//Intent intent = new Intent(CoffeeShop.this,DrinkCategoryActivity.class);
//startActivity(intent);
TextView txtV = (TextView)view;
txtV.setTextColor(Color.BLUE);
}
else
{
TextView txtV = (TextView)view;
txtV.setTextColor(Color.YELLOW);
Toast toast = Toast.makeText(getApplicationContext(),(CharSequence)"These Methods are yet to be made public", Toast.LENGTH_SHORT);
toast.show();
}
}
};
ListView listView = (ListView)findViewById(R.id.list_opt);
listView.setOnItemClickListener(itemClickListener);
Adding onclicklistner for the view will allow updating the color of the text upon clicking. You can add respective logic as to which color should the text be displayed.

Double AddView on adapter

I have made linear layout and add view on it, however, the view appear twice, I dont know why it happen.Can anyone fix it??
I have problem about the adapter and I look few time and I find no strange here. But I delete the statement of addView it will not appear any View I have added before.
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView == null)
{
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
// inflater.inflate(R.layout. parent,false);
convertView = inflater.inflate(R.layout.exerciseui_item,parent,false);
}
Exercise question = exercises.get(position);
TextView question_view = (TextView) convertView.findViewById(R.id.exercise_question);
String question_test = question.getOrder() + " " + question.getText() ;
question_view.setText(question_test);
int answer_num = question.getAnswer().size();
LinearLayout linear = (LinearLayout) convertView.findViewById(R.id.exercise_answer);
ExerciseAnswer answer = question.getAnswer().get(0);
int answer_order = answer.getOrder();
String answer_text = answer.getText();
String answer_final = answer_order + " " + answer_text;
TextView answer_view = new TextView(linear.getContext());
answer_view.setPadding(20, 5, 5, 5);
answer_view.setTextSize(30);
answer_view.setText(answer_final);
linear.addView(answer_view);
return convertView;
}
The following is the xml of the exerciseui_item
<?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"
android:id="#+id/exercise_answer" >
<TextView
android:id="#+id/exercise_question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="13dp"
></TextView>
</LinearLayout>
It sounds like view re-cycling might be behind this. Your adapter is responsible for creating the view of each item in the data set (that is the purpose of the getView method). Android, in order to conserve resources, will re-cycle views. That is why the getView method is passed a View. If the passed-in view is non-null, that means it has already been used to display data in this list.
Thus it is the responsibility of the adapter, to "clean up" the view. In your case, that means that you have to account for the fact that you may have already dynamically added the TextView element to this view (in a previous getView call). Failure to "clean up" your view means that each time a view is re-cycled, your method will be adding yet another TextView to the layout.
In your case, I would suggest searching the LinearLayout for the answer TextEdit. (Give this TextEdit an id so that you can find it by using findViewById()). If it already exists, then you do not need to add it.
An other approach would be to include the 2nd TextEdit right in your XML layout. It is not clear to me why this needs to be added dynamically.

Two views in each list item

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.

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.

Categories

Resources