I'm currently working on my first app - no experience in java or android so please have patience with me :)
In the app I'm using a custom list view with text only and 4 text views in each list item/container.
With these text views I'm using a custom font (applied using java in the listview adapter).
The font is loaded in assets/fonts Text is fetched from a string array in strings.xml
I have figured all this out so far using all the Q&A's on this website however can't seem to find a solution for the following...
I want to change the style of my text (which has the custom font) during the paragraph/sentence. I want to highlight specific words or phrases in Italics for example:
I enjoy coding.
The word enjoy is Italics where the rest of the sentence is in Regular format.
I've seen plenty of guides on how to change a whole class or text-view into italics or bold styles however not specific portions of text.
So far the only possible solution I've found is to try italic markers in strings.xml however this doesn't seem to work and I don't know why
I'm assuming what I need to do is: 1. load the italic version of the font separately in java 2, then write some code that tells the app to apply this font to specific portions of text, either in strings xml or some other way. (preferably in strings xml as there is alot of text)
Your help is much appreciated, Many thanks in advance.
Apologies if my explanation is too brief its my first post.
my code: (if you need anything else let me know.)
the main activity
custom listview adapter
main xml layout
row xml layout
public class WirdActivity extends Activity {
MediaPlayer mp;
String[] Arabic;
String[] Transliteration;
String[] Translation;
String[] Number;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wird);
Resources res=getResources();
Arabic=res.getStringArray(R.array.wirdarabic);
Transliteration=res.getStringArray(R.array.wirdtransliteration);
Translation=res.getStringArray(R.array.wirdtranslation);
Number=res.getStringArray(R.array.wirdnumber);
mp = MediaPlayer.create(this, R.raw.wird);
Button playwird = (Button) findViewById(R.id.playwird);
playwird.performClick();
playwird.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mp.start();
}
});
ImageButton pausewird = (ImageButton) findViewById(R.id.pausewird);
pausewird.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mp.pause();
}
});
ListAdapter theAdapter = new Wirdadapter(this, Arabic, Transliteration, Translation, Number);
final ListView wirdlist = (ListView) findViewById(R.id.wirdlist);
wirdlist.setAdapter(theAdapter);
}
@Override
protected void onDestroy() {
super.onDestroy();
mp.stop();
}
class Wirdadapter extends ArrayAdapter<String> {
private AssetManager assets;
Typeface font = Typeface.createFromAsset(getContext().getAssets(),
"fonts/trado.ttf");
Typeface font2 = Typeface.createFromAsset(getContext().getAssets(),
"fonts/JaghbUni-Rom.ttf");
String[] titleArray;
String[] descriptionArray;
String[] subtitleArray;
String[] numberArray;
public Wirdadapter(Context context, String[] titles, String[] desc,String[] sub,String[] num) {
super(context, R.layout.row_layout_3, R.id.textview2,titles);
this.titleArray=titles;
this.descriptionArray=desc;
this.subtitleArray=sub;
this.numberArray=num;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater theInflater = LayoutInflater.from(getContext());
View theView = theInflater.inflate(R.layout.row_layout_3, parent, false);
TextView Arabic = (TextView) theView.findViewById(R.id.textview2);
TextView Transliteration = (TextView) theView.findViewById(R.id.transliteration);
TextView Translation = (TextView) theView.findViewById(R.id.translation);
TextView Number = (TextView) theView.findViewById(R.id.number);
Arabic.setText(titleArray[position]);
Transliteration.setText(descriptionArray[position]);
Translation.setText(subtitleArray[position]);
Number.setText(numberArray[position]);
Arabic.setTypeface(font);
Transliteration.setTypeface(font2);
Translation.setTypeface(font2);
Number.setTypeface(font2);
return theView;
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="5000pt"
android:background="@drawable/gradient_background3"
tools:context=".WirdActivity"
android:orientation="vertical"
android:focusableInTouchMode="true"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#927e68"
>
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="@string/wird_title"
android:textSize="16sp"
android:padding="10dp"
android:layout_marginStart="5dp"
android:textColor="#ffffff"
android:id="@+id/wirdtitle"
/>
<ImageButton
android:layout_height="25dp"
android:layout_width="30dp"
android:id="@+id/pausewird"
android:background="@drawable/pause"
android:clickable="true"
android:layout_gravity="center"
android:layout_marginEnd="65dp"
android:layout_alignParentEnd="true"
android:layout_alignBottom="@+id/playwird"
/>
<Button
android:layout_height="25dp"
android:layout_width="25dp"
android:id="@+id/playwird"
android:background="@drawable/play"
android:clickable="true"
android:layout_alignBottom="@id/wirdtitle"
android:layout_marginEnd="15dp"
android:layout_marginBottom="17.5dp"
android:layout_alignParentEnd="true"
/>
</RelativeLayout>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="515dp" >
<LinearLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitStart"
android:src="@drawable/w1"
android:id="@+id/wirdhead"
android:adjustViewBounds="true"/>
<ListView
android:layout_width="match_parent"
android:layout_height="4100dp"
android:id="@+id/wirdlist"
android:gravity="right"
android:divider="#f1f1f1"
android:dividerHeight="0.5dp"
>
</ListView>
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/number"
android:textSize="10sp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="15dp"
android:textColor="#4c4c4c">
</TextView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textview2"
android:textSize="30sp"
android:padding="15dp"
android:layout_alignParentRight="true"
android:layoutDirection="rtl"
android:gravity="right"
android:textColor="#4c4c4c">
</TextView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/transliteration"
android:textSize="15sp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingBottom="5dp"
android:textColor="#4c4c4c">
</TextView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/translation"
android:textSize="15sp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingBottom="15dp"
android:textColor="#4c4c4c">
</TextView>
</LinearLayout>