Your question requires deep use of SpannableString , Androider has already provided a good hint to solve your problem , but you said that you are getting words dynamically from database , so here I have written a code for you which will process dynamic content. I have not tested this , Hope it should work for you .
MainActivity.java :
public class MainActivity extends AppCompatActivity {
ListView lv ;
public static String [] prgmNameList={"Android Dev","SpearHead Inc"}; // You can put dynamic strings in this array .
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView) findViewById(R.id.listView);
lv.setAdapter(new CustomAdapter(this, prgmNameList));
}
}
CustomAdapter :
public class CustomAdapter extends BaseAdapter {
String [] result;
Context context;
private static LayoutInflater inflater=null;
public CustomAdapter(MainActivity activity, String[] prgmNameList) {
result = prgmNameList;
context = activity;
inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return result.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public class Holder
{
TextView tv;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder holder=new Holder();
View rowView = inflater.inflate(R.layout.list_item, null);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
//holder.tv.setText(result[position]);
String span[] = result[position].split(" ") ;
SpannableString ss = new SpannableString(result[position]);
ClickableSpan spans[] = new ClickableSpan[span.length];
for(int spanCount = 0 ; spanCount < span.length ; spanCount++){
spans[spanCount] = new ClickableSpan() {
@Override
public void onClick(View textView) {
TextView v = (TextView)textView ;
String text = v.getText().toString() ;
Log.d("View" , text);
}
};
}
int start = 0 ;
int end ;
try {
for(int spanCount = 0 ; spanCount < span.length ; spanCount++){
end = span[spanCount].length()-1 ;
ss.setSpan(spans[spanCount], start, end , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
start = span[spanCount].length()+1;
end = span[spanCount].length() + 2 + end ;
}
} catch (Exception e) {
e.printStackTrace();
}
holder.tv.setText(ss);
Log.d("SpannableString" , ss.toString());
return rowView;
}
}
list_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="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:text="Hello World" />
</LinearLayout>
activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.shishupalshakya.spannablestringsample.MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</RelativeLayout>
Embed above code in your application and run .