(Android) How to set hyperlink to a textview inside ListView? - android

I have a listview containing some news. Each news is a row which comtains a title, content, publisher and date. Now I want to set hyperlinks to every title TextView. How to implement that? The following is my code:
private void show_news() {
ArrayList<Map<String, Object>> list = new ArrayList<>();
get_data(list);
ListView lv = (ListView) findViewById(R.id.News_List);
SimpleAdapter adp = new SimpleAdapter(getApplicationContext(), list, R.layout.news_item, new String[]{"news_title", "news_content", "news_publisher", "news_date"}, new int[]{R.id.news_title, R.id.news_content, R.id.news_publisher, R.id.news_date});
lv.setAdapter(adp);
/* the code above runs properly*/
/*Now I want to set hyperlink to my title TextView..*/
}
This is my news_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/news_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:textColor="#000000"
android:textSize="25sp" />
<TextView
android:id="#+id/news_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:textSize="18sp" />
<TextView
android:id="#+id/news_publisher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:textSize="12sp" />
<TextView
android:id="#+id/news_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp" />
</LinearLayout>
I am new to Android programming. Can anyone help me with this? Thanks a lot!

LinearLayout first = (LinearLayout) adp.getView(0, null, lv);
TextView title = (TextView)first.getChildAt(0);
That doesn't necessarily get the first element of the list view. It doesn't even necessarily get the first element on screen. That's very implementation specific (you're assuming the list view doesn't have any children other than its rows, which is actually wrong in many cases).
If you want to change the first item of a list view, do
Item item = adb.getItem(0)
item.title= "New Title"
adp.notifyDataSetChanged();

You can extend the SimpleAdapter and use the derived adapter. When you extend the adapter, set the hyperlink property for the title by calling:
<the title textview>.setAutoLinkMask(Linkify.WEB_URLS);
in the method getView method of the adapter. A commonly used approach in Android ListView adapter is to use a so called view holder, which is a key technique to get better listview performance, and you can check the details from the developer document: Hold View Objects in a View Holder
Suppose you employ this approach, the code would looks more or less like:
holder.titleView.setAutoLinkMask(Linkify.WEB_URLS);
If your textview does not have a URL, then you can set the text in HTML form as:
holder.titleView.setText(
Html.fromHtml(
"Breaking News "));
holder.titleView.setMovementMethod(LinkMovementMethod.getInstance());

Related

How to add floating textview on one element of a recycler view?

I have a RecyclerView as follows
<android.support.v7.widget.RecyclerView
android:id="#+id/drawerList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/nav_header_container"
android:layout_marginTop="15dp" />
I have a string array like this
<string-array name="my_array">
<item>#string/item_1</item>
<!--<item>#string/item_2</item>-->
<item>#string/item_3</item>
<item>#string/item_4</item>
<item>#string/item_5</item>
<item>#string/item_6</item>
This string array is used to display data in RecyclerView. I want to display a textview alongside with item_4, Is it possible? How to make it?
From Activity Pass this list to RecyclerView adapter
try {
String[] array = getApplicationContext().getResources().getStringArray(R.array.my_array);
List<String> list = new ArrayList<String>();
list = Arrays.asList(array);
//pass this list to RecyclerView adapter
}catch (Exception e){e.printStackTrace();}
In your onBindViewHolder method just put a check for the element you want to display differently and do what you want to do with it.
Some solutions:
1.- You can concat a string to the 4th item when you check the position in the onBindViewHolder.
2.- You can create a layout and inflate it on onCreateViewHolder which has two textViews one for the items and one for the text that will go next to it. When you detect that you need to show the second textview change the content and the visibility of the textview.
<LinearLayout android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:id="#+id/items"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textViewNextToItemOfChoice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</LinearLayout>
Then on code you just change the visibility of textview "textViewNextToItemOfChoice" and the content of it.
I think you should make an object (You own model) instead of using just an array of string to populate the list. In that way you can create an object with 2 Strings and in the adapter when you check if it has both string you show them.

How to dynamically add and delete view in android

I want to design below screen
When i press 'Add number' button, it will insert one entry in below scrollable layout. In that when i press 'X' button it should delete that particular row.
How to achieve this??
Any idea??
use
ViewGroup.addView(View view);
to add a view to some layout.
To create a layout dynamically, use:
TextView txtView=new TextView(this);
//Its an example, you can create layouts, buttons, image view, and other components.
To Delete a layout or view dynamically, getParent of layout, and delete, by:
ViewGroup.removeView(View view);
You should use a ListView which is backed by an ArrayList of Objects or Strings. When you want to remove an item from your ListView, remove the object from the ArrayList :
mData.remove(object);
and then notify the ListView that the date has changed :
mAdapter.notifyDataSetChanged();
Use a ListView for displaying the list of patterns
Create a custom layout for each list item. e.g.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="555*" />
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="matched 5 " />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="X" />
</LinearLayout>
Create a Custom Adapter class extending BaseAdapter
It can maintain a list for the patterns to be shown
In the getView method of the custom adapter -
inflate the xml
set the information (like pattern and number of matches) based on the index parameter, using the list
set onclick listener for the button (delete that item from list and call notifyDatasetInvalidated())
return the view.
On "Add Number" add item to the list in the adapter

Text got cut of within the ListView for android-based app

I'm using a ListView to store the data retrieve from my database but somehow the text inside the ListView has been cut off. I know that TextView could use the setSingleLine, setEllipsize but I have no idea what I could do for ListView nor why it was cut off. Not sure is it because of the submit button though.
I tried using the setHorizontalScrollBarEnabled(false) which is used in Textview, but it seems useless.
my xml for the listview :
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:id="#+id/list"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/list"
android:layout_alignParentRight="true"
android:layout_marginTop="20dp"
android:id="#+id/submit"
android:text="Submit"
/>
my code :
listView = (ListView)this.findViewById(R.id.list);
listView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice, dataList));
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setHorizontalScrollBarEnabled(false);
my ui display result :
Hey vam i do this in my APPLICATION.
In Listview use this
android:padding="10dp"
May be you get solution.
if not getting then please tell me.
Mehul Patel
this is because each row has a specific height and your text view is not big enough to show that. For try to create a custom layout for row and inflate it in getview method of Adapter

Custom Listview Adapter, adding a static footer, and understanding R.id.list

There is something I'm just not getting, and I'm looking for assistance in understanding what is happening here.
I have a custom list adapter (that just extends BaseAdapter) that I have successfully been using to generate and display a list. Now I want to add a static footer to the bottom of my list. After looking at a number of resources (specifically this one) I've come to realize that my reluctance of using XML has to come to an end, and set up the following xml layout in a file called devices_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout android:id="#+id/bottom_control_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<ToggleButton android:id="#+id/bottom_control_toggle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textOff="Filter Favourites OFF"
android:textOn="Filter Favourites ON"/>
</LinearLayout>
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_above="#id/bottom_control_bar">
</ListView>
<TextView android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/main_empty_list"
android:layout_above="#id/bottom_control_bar"/>
</RelativeLayout>
After some adjustments to the activity that holds the list, I ran the code. I see my footer, (and also the tab widget which is parent to everything), but the area where the list goes is empty.
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.setContentView(R.layout.devices_list);
db = new DbManager(this);
db.open();
AllCur = db.fetchAllDevices();
startManagingCursor(AllCur);
list = new DeviceListAdapter(this, AllCur); //make my custom list adapter
setListAdapter(list);
}
Is there some way to link up the ListView widget declared in my xml with my DeviceListAdapter? It's pretty clear to me now that I'm not entirely sure about how this is all working. Any help in clarification would be much appreciated.
You have both the ListView and the TextView set to android:layout_above="#id/bottom_control_bar", which means the TextView will overlap the ListView. And, you have said that your ListView height is 0dip, which will make for an extremely short list.
I would define the ListView as being above the TextView and anchored to the top of the screen (android:layout_alignParentTop="true").
Is there some way to link up the
ListView widget declared in my xml
with my DeviceListAdapter?
You already are, by calling setListAdapter().

Android: failed to setContentView when switching to ListActivity

[update] I got the error, which says "Your content must have a ListView whose id attribute is 'android.R.id.list'". Appearently nothing in my xml is ListView. But is that required?
This is an follow-up issue on my previous question
android: which view should I use for showing text and image?
I read the article about creating ListView for LinearLayout. However, my following code failed at the setContentView() function when I changed "extends Activity" to "extends ListActivity", any idea why?
private TextView mSelection;
//private ImageView mImages;
static final String[] keywords = new String[]{"China", "Japan", "USA", "Canada"};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contactLayout);
mSelection = (TextView)findViewById(R.id.ContactNames);
ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.contactlayout, R.id.ContactNames,keywords);
setListAdapter(adapter);
}
My Layout is from this article: http://www.curious-creature.org/2009/02/22/android-layout-tricks-1/
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:src="#drawable/icon" />
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="#+id/ContactNames"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="My Application" />
<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:text="Simple application that shows how to use RelativeLayout" />
</LinearLayout>
I think you misunderstood the other posts I showed you in the previous question. They were explaining how to use a custom layout for each row in your list, not how to define the entire layout file for the activity. You need something like this:
(main.xml)
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:id="#android:id/list">
</ListView>
Note the very important line android:id="#android:id/list". You must have that in your ListView as that's what tells Android where your list is. The cacheColorHint is useful if your background isn't black - see this post for more details about that.
With the above lines you can give your activity a list that will be recognised properly. Here's a basic example:
public class TestProject extends ListActivity {
final static String[] ITEMS = {"blah", "floop", "gnarlp", "stuff"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.listrow, R.id.textview, ITEMS);
setListAdapter(adapter);
}
}
Then the listrow layout is just this:
<?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"
android:id="#+id/textview"/>
</LinearLayout>
This is a really simple layout. If you want to get something more complicated, changes are you'll have to use a BaseAdapter, as that gives you calls getView(...) before each row. In that you can use different layouts depending on the contents of each row. However, BaseAdapter looks scary when you first try it, so be warned! :)
Yes, if you are using a ListActivity, you need to have a ListView who's id is android.R.list in your layout file.
If you aren't using a ListView in your layout, and I don't see one in there, then switch to using a regular Activity.
Actually, your (custom) layout doesn't need a ListView when using a list activity. The easy way to solve this is just remove the setContentView() line altogether. In simple terms, when you do it, Android "assumes" the layout you're using to contain a single full-screen ListView, and provides it for you.
If you want a different (richer) interface for the Activity though, you must code the XML and use the informed ID for Android to know how to show the list implied by the activity being a ListActivity after all. Note that the layout for an item isn't the same as the list, and although I haven't tried that, I assume you can have a custom item layout without having an explicit ListView in the activity layout.

Categories

Resources