make links clickable in custom listview android - java

I created a listview wit custom addapter to display tweets from a user. Now sometimes in the tweet there is a link provided. I want them to be clickable, and the item itself from the listview doesn't have to be clickable. this is the layout of my listview with 2 rows to show an example
I have tried to make the links in the textview(text with sometimes links) clickable and also autolink: WEB in XML and programatically. But every time i click on the link it selects the listitem en not the link in the textview.
In java code it was like this :
message.setAutoLinkMask(1);
message.setLinksClickable(true);
public class UserItemAdapter extends ArrayAdapter<Tweet> {
private ArrayList<Tweet> tweets;
public UserItemAdapter(Context context, int textViewResourceId,
ArrayList<Tweet> tweets) {
super(context, textViewResourceId, tweets);
this.tweets = tweets;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.listitem, null);
}
Tweet tweet = tweets.get(position);
if (tweet != null) {
TextView username = (TextView) v.findViewById(R.id.username);
TextView message = (TextView) v.findViewById(R.id.message);
**message.setAutoLinkMask(1);
message.setLinksClickable(true);**
ImageView image = (ImageView) v.findViewById(R.id.avatar);
TextView date = (TextView) v.findViewById(R.id.date);
if (username != null) {
username.setText(tweet.username);
}
if (message != null) {
message.setText(tweet.message);
}
if (date != null) {
date.setText(tweet.hfdate);
}
}
return v;
}
}
and the xml for the listitem is like this in the textview message like this
android:autoLink="web" android:linksClickable="true"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left|center"
android:paddingBottom="5px"
android:paddingLeft="5px"
android:paddingTop="5px"
tools:context=".TwitterActivity$AsyncOp" >
<ImageView
android:id="#+id/avatar"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:src="#drawable/twitterimg" />
<LinearLayout
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="#+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#color/Orange" />
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10px"
android:autoLink="web"
android:linksClickable="true"
android:textColor="#0099CC" />
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10px"
android:textColor="#color/Orange" />
</LinearLayout>
</LinearLayout>
Anybody an idea wath I am doing wrong?

Related

Android Custom Adapter - Setting an Image relative to information in Listview

I'm currently struggling with a function I want to have in my application. The users Incomes are shown in a ListView. There are two types of incomes, normal "income" and "other income", this is selected in the application when the user submits the income to a SQLite Database that he or she receives. Now - I want to present the different incomes in a ListView with an image in the ListView relative to the type of category (two types of income category - "income" and "other income".
I cant figure out how I should do this ... can you do a switch-statement of some sort!?
My XML for the Custom ListView looks 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="match_parent"
android:layout_height="match_parent"
android:weightSum="100">
<ImageView
android:orientation="vertical"
android:layout_width="48dp"
android:layout_height="48dp"
android:gravity="center_vertical"
android:id="#+id/icon1"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="220dp"
android:layout_height="48dp"
android:layout_weight="33.3">
<TextView
android:text="TextView1"
android:textSize="18dp"
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:id="#+id/text1"/>
<TextView
android:text="Textview2"
android:textSize="14dp"
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:="#id/text2"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="48dp"
android:orientation="vertical">
<TextView
android:text="Textview3"
android:textSize="16dp"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_gravity=""
android:gravity="center_vertical"
android:id="#+id/text3"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="48dp"
android:layout_height="48dp">
<ImageView
android:orientation="vertical"
android:layout_width="48dp"
android:layout_height="48dp"
android:gravity="center_vertical"
android:id="#+id/icon2"/>
</LinearLayout>
</LinearLayout>
And my AdapterClass looks like this:
public class CustomIncomeListAdapter extends ArrayAdapter<IncomeModel> {
private ArrayList<IncomeModel> incomeList;
private Activity context;
private ViewHolder viewHolder;
int[] incomeIcon = {R.drawable.income, R.drawable.otherincome};
int[] forwardIcon = {R.drawable.forward};
public CustomIncomeListAdapter(Activity context, int[] incomeIcon, ArrayList<IncomeModel> incomeList) {
super(context, R.layout.adapter_view_layout, incomeList);
this.incomeList = incomeList;
this.context = context;
this.incomeIcon = incomeIcon;
}
private static class ViewHolder {
TextView incomeReference;
TextView incomeDate;
TextView incomeAmount;
ImageView incomeIcon;
ImageView forwardIcon;
}
public View getView(int position, View convertView, ViewGroup parent) {
IncomeModel incomeModel = getItem(position);
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(R.layout.adapter_view_layout, null, true);
viewHolder.incomeReference = (TextView) convertView.findViewById(R.id.text1);
viewHolder.incomeDate = (TextView) convertView.findViewById(R.id.text2);
viewHolder.incomeAmount = (TextView) convertView.findViewById(R.id.text3);
viewHolder.incomeIcon = (ImageView) convertView.findViewById(R.id.icon1);
viewHolder.forwardIcon = (ImageView) convertView.findViewById(R.id.icon2);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.incomeReference.setText(incomeModel.getIncomeCategory());
viewHolder.incomeDate.setText(incomeModel.getIncomeDate());
viewHolder.incomeAmount.setText(incomeModel.getIncomeAmount());
switch (...?) {
}
}
viewHolder.incomeAmount.setText(incomeModel.getIncomeAmount());
switch (income) {
//define your income to 1 for other income
case 1:
incomeIcon.setBackgroundResource(R.drawable.otherincome);
break;
default:
incomeIcon.setBackgroundResource(R.drawable.income);
break;
}
you can check type , if type is income means set corresponding image else set other image

Android : Creating a vertical space for ImageView and tab

I am working on an Android project in which I am creating UI for editing a Note object in the app. For that, I will be replicating the UI which is designed with JS,HTML,CSS. Unfortunately, android has discrete elements like ListView, TextView and all. As you can see in the screenshot, the top horizontal bar is just a representation and inside it contains a photo.
Also, there is a tab like option in the activity itself. I don't know how to create both these things. The choice of colour I am planning to create with a List and get the element clicked in the List. I have basic functionality working, and working on UI part now.
I don't know what all is required for creating such fancy UI's. Any help would be nice.
edit_note.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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:weightSum="1">
<EditText
android:id="#+id/noteTagEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical|center_horizontal" />
<EditText
android:id="#+id/noteTextEdit"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:layout_weight="0.97"
android:ems="10"
android:gravity="top"
android:inputType="textMultiLine"
android:scrollbarAlwaysDrawVerticalTrack="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/noteDate"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/noteNumber"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/attachCount"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginStart="50dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<GridView
android:id="#+id/attachmentDisplay"
android:layout_width="match_parent"
android:layout_height="132dp"
android:layout_gravity="center_horizontal"
android:numColumns="2"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
>
<Button
android:id="#+id/saveNoteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:text="#string/saveNote" />
<Button
android:id="#+id/cancelEditButton"
android:layout_width="98dp"
android:layout_height="wrap_content"
android:text="#string/cancel"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
/>
</RelativeLayout>
</LinearLayout>
Java code :
public class EditNoteActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_note);
}
public class getNoteByItsId extends AsyncTask<Integer, Void, ResponseEntity<RestNote>> {
#Override
protected void onPostExecute(ResponseEntity<RestNote> params) {
restNote = params.getBody();
restAttachmentList = restNote.getNotesAttachments();
final ArrayList<HashMap<String, String>> restSectionArrayList = new ArrayList<>();
for (RestAttachment restAttachment : restAttachmentList) {
HashMap<String, String> restAttachmentDisplay = new HashMap<>();
restAttachmentDisplay.put(fileName, restAttachment.getFileName());
restAttachmentDisplay.put(fileSize, readableFileSize(restAttachment.getFileSize()));
restSectionArrayList.add(restAttachmentDisplay);
}
attachmentsGridView = (GridView) findViewById(R.id.attachmentDisplay);
noteDate = (TextView) findViewById(R.id.noteDate);
noteDate.setText(restNote.getNoteDate());
noteNumber = (TextView) findViewById(R.id.noteNumber);
String noteNos = "#" + String.valueOf(restNote.getNoteNumber());
noteNumber.setText(noteNos);
attachCount = (TextView) findViewById(R.id.attachCount);
attachCount.setText(String.valueOf(restNote.getAttachCount()));
editNoteAttachments = new EditNoteAttachments(editNoteActivity, restSectionArrayList);
attachmentsGridView.setAdapter(editNoteAttachments);
}
public class EditNoteAttachments extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private LayoutInflater inflater = null;
public EditNoteAttachments(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.attachment_rows, parent, false);
TextView fileName = (TextView) vi.findViewById(R.id.fileName);
TextView fileSize = (TextView) vi.findViewById(R.id.fileSize);
HashMap<String, String> conversationList;
conversationList = data.get(position);
fileName.setText(conversationList.get(EditNoteActivity.fileName));
fileSize.setText(conversationList.get(EditNoteActivity.fileSize));
return vi;
}
}
Kindly let me know. Thank you.
For the top horizontal bar you definitely need a compound custom view like this:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<ImageView
android:id="#+id/background
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_centerVertical="true"
/>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/profileImageView"
android:layout_marginLeft="16dp"
/>
<ImageView
android:id="#+id/profileImageView"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:layout_centerVertical="true"
/>
</merge>
With code like this:
public class TopBarView extends RelativeLayout {
private TextView title;
private ImageView profileImage;
public TopBarView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.TopBarView, 0, 0);
String titleText = a.getString(R.styleable.TopBarView_titleText);
a.recycle();
setGravity(Gravity.CENTER_VERTICAL);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.view_top_bar, this, true);
title = (TextView) findViewById(R.id.title);
title.setText(titleText);
profileImage = (ImageView) findViewById(R.id.profileImageView);
profileImage.setBackgroundColor(R.color.yellow);
profileImage.setImageResource(R.drawable.myProfileDrawable);
setBackgroundColor(Color.TRANSPARENT);
}
For tab options you could use android.support.design.widget.TabLayout from design support library.

Android: Write Adapter for ListView in ListView

Im programming a app which shows meals with a picture,name and a short description. If pressed on one meal it should extend the size of the listitem to
get from this http://puu.sh/lmwKn/b5f6f1d54a.jpg to this http://puu.sh/lmwJo/943f7349d3.jpg.
Now to add meals to the ListView i wrote an MealAdapter, i dont know how to get the view of the list of the comments
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(
R.layout.gericht_list_layout, null);
viewHolder = new ViewHolder();
viewHolder.img = (ImageView) convertView.findViewById(R.id.gericht_image);
viewHolder.name = (TextView) convertView.findViewById(R.id.gericht_title);
viewHolder.descTxt = (TextView) convertView.findViewById(R.id.gericht_sub_title);
viewHolder.comments = (ListView) convertView.findViewById(R.id.gericht_comments);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.name.setText(data.get(position).name);
viewHolder.descTxt.setText(data.get(position).subName);
int id = convertView.getResources().getIdentifier(
data.get(position).img, "drawable",
context.getPackageName());
viewHolder.img.setImageResource(id);
return convertView;
}
Here is the layout:
<?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:id="#+id/gericht_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:src="#drawable/no_image"
/>
<TextView
android:id="#+id/gericht_title"
android:layout_width="fill_parent"
android:layout_height="75dp"
android:layout_toRightOf="#+id/gericht_image"
android:text="Kein Name"
android:layout_marginLeft="15dp"
android:layout_marginTop="20dp"
android:textColor="#000000"
android:textSize="25sp"
/>
<TextView
android:id="#+id/gericht_sub_title"
android:layout_width="fill_parent"
android:layout_height="25dp"
android:text="Keine genauere Beschreibung"
android:layout_toRightOf="#+id/gericht_image"
android:layout_marginTop="55dp"
android:layout_marginLeft="30dp"
android:gravity="center_vertical"
/>
<ListView
android:id="#+id/gericht_comments"
android:layout_width="fill_parent"
android:layout_marginTop="100dp"
android:layout_marginLeft="50dp"
android:layout_height="0dp"
/>
</RelativeLayout>
So i want to draw the gericht_comments listview but i dont know how

how can i change fontcolor and font type of my listview

I have one activity that contains text view, buttons, spinner and list view
my main XML file contains :
<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:background="#ffff00"
tools:context="com.example.taxitabriz.MainActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:text="#string/app_name"
android:textSize="35dp"
android:textStyle="bold"
android:textColor="#996600"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#+id/linearlayout1" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#996600"
android:text="#string/exit"
android:background="#drawable/backbutton" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#996600"
android:text="#string/about"
android:background="#drawable/backbutton" />
</LinearLayout>
<Spinner
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/textView1"
android:layout_marginTop="19dp" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/spinner1"
android:layout_above="#+id/linearlayout1"
android:layout_alignParentRight="true"
android:layout_weight="49.94" >
</ListView>
and part of my java code is here:
ArrayAdapter<String> ard=new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line,s1);
sp.setAdapter(ard);
lv1=(ListView) findViewById(R.id.listView1);
ArrayAdapter<String> ard1=new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line,s1);
lv1.setAdapter(ard1);
but i can't change font color or font type in list view or spinner. how can i do it?
You need to create a CustomListAdapter.
private class CustomListAdapter extends ArrayAdapter {
private Context mContext;
private int id;
private List <String>items ;
public CustomListAdapter(Context context, int textViewResourceId , List<String> list )
{
super(context, textViewResourceId, list);
mContext = context;
id = textViewResourceId;
items = list ;
}
#Override
public View getView(int position, View v, ViewGroup parent)
{
View mView = v ;
if(mView == null){
LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = vi.inflate(id, null);
}
TextView text = (TextView) mView.findViewById(R.id.textView);
if(items.get(position) != null )
{
text.setTextColor(Color.WHITE);
text.setText(items.get(position));
text.setBackgroundColor(Color.RED);
int color = Color.argb( 200, 255, 64, 64 );
text.setBackgroundColor( color );
}
return mView;
}
}
The list item looks like this (custom_list.xml):
<?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="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:textSize="20px" android:paddingTop="10dip" android:paddingBottom="10dip"/>
</LinearLayout>
Use the TextView api's to decorate your text to your liking
and you will be using it like this
listAdapter = new CustomListAdapter(YourActivity.this , R.layout.custom_list , mList);
mListView.setAdapter(listAdapter);
If you really want to use androids simple list layout, we see that they declare their textview's identifier as such:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/dropDownItemStyle"
android:textAppearance="?android:attr/textAppearanceLargeInverse"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="marquee" />
So just make your custom adapter, inflate their layout & find that text view's id. Something like the following:
public final class CustomAdapter extends ArrayAdapter<String> {
private Context context;
ViewHolder holder;
private ArrayList<String> messages;
public CustomAdapter(Context context, ArrayList<String> data) {
this.context = context;
this.messages = data;
Log.d("ChatAdapter", "called constructor");
}
public int getCount() {
return messages.size();
}
public View getView(int position, View convertView, ViewGroup viewGroup) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.simple_dropdown_item_1line, null);
holder = new ViewHolder();
holder.message = (TextView) convertView
.findViewById(R.id.text1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.message.setText(data.get(position));
holder.message.setTextColor(Color.BLUE);
// don't do this, remember the face variable once just showing explicitly for u
Typeface face=Typeface.createFromAsset(getAssets(), "fonts/HandmadeTypewriter.ttf");
holder.message.setTypeface(face);
return convertView;
}
public static class ViewHolder {
public TextView message;
}
}
Edit: The following is how you would use the custom array adapter in your activity.
// I'm not sure if your sp or lv1 wants the adapter, but
// whatever you require your list view's data to be just
// set the custom adapter to it
CustomAdapter<String> myAdapter = new ArrayAdapter<String>(this, s1);
lv1=(ListView) findViewById(R.id.listView1)
lv1.setAdapter(myAdapter);
You can do like this, add a ListView item in xml,at res/layout/list_item1.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#0000ff"
android:testStyle="italic"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight"
/>
then,
ArrayAdapter<String> ard=new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line,s1);
sp.setAdapter(ard);
lv1=(ListView) findViewById(R.id.listView1);
ArrayAdapter<String> ard1=new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line,s1);// **change R.layout.list_item1**
lv1.setAdapter(ard1);
You have to put the .ttf file of font(which you want to add ) in asstes/fonts/ folder and write code in onCreate method like below. i have puted Chalkduster.ttf in my asstes/fonts/ folder
TextView txttest = (TextView) findViewById(R.id.txttest);
Typeface custom_font = Typeface.createFromAsset(getAssets(),
"fonts/Chalkduster.ttf");
txttest.setTypeface(custom_font);
You can handle clicks of selected item by write code just like below
ListView lvNews = (ListView) findViewById(R.id.lvNews);
lvNews.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// Write your code here
//int newsId = newsArray.get(position).getId();
//Intent i = new Intent(NewsListActivity.this,
// NewsDetailActivity.class);
//i.putExtra(Constants.NEWS_ID, newsId);
//i.putExtra("isFromNotification", false);
//startActivity(i);
}
});

Custom Adapter ViewHolder issue

I'm trying to use a custom adapter for a list. This list has 2 types of rows but I only use one layout (keeping the items that I don't need with visibility set to View.GONE). However I keep getting a Class cast exception when trying to access the editbox-style row. I have very little experience with custom adapters. Your help is really appreciated :D
Here's the code(I removed the setonclicklisteners to keep it short):
public class SubEventListAdapter extends ArrayAdapter<MobileSubEvent>
{
private ArrayList<MobileSubEvent> _items;
private Context _context;
public SubEventListAdapter(Context context, ArrayList<MobileSubEvent> items)
{
super(context, R.layout.view_select_event_item3, items);
this._items = items;
this._context = context;
}
static class ViewHolder
{
TextView text;
ImageButton imagebutton;
ImageView check;
EditText editText;
Button button;
}
#Override
public int getCount()
{
return this._items.size();
}
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View v = convertView;
final ViewHolder viewHolder;
final MobileSubEvent event = _items.get(position);
if (v == null)
{
LayoutInflater _inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = _inflater.inflate(R.layout.view_select_event_item3, null);
viewHolder = new ViewHolder();
viewHolder.imagebutton = (ImageButton) v.findViewById(R.id.ibNext);
viewHolder.text = (TextView) v.findViewById(R.id.EVENT);
viewHolder.check = (ImageView) v.findViewById(R.id.ivCheck);
viewHolder.button = (Button) v.findViewById(R.id.bScanOrSign);
viewHolder.editText = (EditText) v.findViewById(R.id.etInput);
v.setTag(viewHolder);
} else
{
v = convertView;
viewHolder = (ViewHolder) v.getTag(); //here is where the class cast exception occurs
}
if (viewHolder.text != null)
viewHolder.text.setText(this._items.get(position).get_description());
v.setTag(this._items.get(position));
...
return v;
}
Logcat:
view_select_event_item3:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:padding="5dip" >
<ImageView
android:id="#+id/ivCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_gravity="center_vertical"
android:background="#drawable/check"
android:focusable="false"
android:visibility="invisible" />
<TextView
android:id="#+id/EVENT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/ivCheck"
android:background="#android:color/transparent"
android:padding="5dp"
android:text="Sample text"
android:textSize="20sp"
android:textStyle="bold"
android:visibility="visible"
/>
<EditText
android:id="#+id/etInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="130dp"
android:maxWidth="165dp"
android:layout_toRightOf="#id/ivCheck"
android:background="#android:drawable/editbox_background"
android:padding="5dp"
android:hint="Hint text"
android:singleLine="true"
android:text=""
android:textSize="20sp"
android:visibility="invisible"
/>
<Button
android:id="#+id/bScanOrSign"
android:layout_width="62dp"
android:layout_height="32dp"
android:layout_gravity="center_vertical"
android:layout_toLeftOf="#id/ibNext"
android:text="Scan"
android:visibility="invisible"
/>
<ImageButton
android:id="#+id/ibNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="right|center_vertical"
android:background="#drawable/right"
android:visibility="invisible" />
</RelativeLayout>
Try this in the getView, check for class name of the convertView object (The View Holder object) if it does not matches class name of your ViewHolder (in my case it is VisitsListViewHolder) - set it as null so new instance is created for the view holder
if(convertView != null)
{
String className = convertView.getTag().getClass().getName();
if(!className.equalsIgnoreCase("VisitsListViewHolder"))
{
convertView = null;
}
}

Categories