I have an android app that lists baby names for people ..... it has a textview
that connects different strings of text and data and then displays them...... (the baby name + "means" + the meaning of the name)
I am trying to change the font size and color and bold the first string of the text (the baby name) and then add a horizontal line or divider or spacer after it.
Then I would like to style the second text string (the "means" text) font size and color separately and make it bold as well.
then i would like to style the third string font size and color separately and make it bold as well.
I have been reading about SpannableString
and tried to implement it for the last 3 hours with no luck.If anyone could help with this it would be greatly appreciated!
Here is the current working code I have
MY TextView
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/common_name_description_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="24dp"
android:layout_gravity="center"
android:gravity="center"
android:padding="16dp">
MY JAVA
package mypackage.android;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import mypackage.android.database.CommonNamesAdapter;
public class CommonNameDescription extends AppCompatActivity {
String common_name;
String common_name_meaning;
long common_name_rowid;
CharSequence mybreak = "\n";
CharSequence text = "Means";
CharSequence description;
public static TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.common_names_description);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Bundle extras = getIntent().getExtras();
common_name_rowid = extras.getLong(CommonNamesAdapter.COMMON_NAME_ROWID );
common_name = extras.getString(CommonNamesAdapter.COMMON_NAME);
common_name_meaning = extras.getString(CommonNamesAdapter.COMMON_NAME_MEANING).toString();
description = common_name+mybreak+text+mybreak+common_name_meaning;
tv = (TextView)findViewById(R.id.common_name_description_text);
tv.setText(description);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
boolean bRet=false;//set true is menu selection handled
switch (item.getItemId()) {
case R.id.action_settings_get_pro:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(getString(R.string.pro_version_url)));
startActivity(intent);
bRet=true;
break;
case R.id.action_settings_get_pro2:
Intent intent2 = new Intent(Intent.ACTION_VIEW);
intent2.setData(Uri.parse(getString(R.string.pro_version_url)));
startActivity(intent2);
bRet=true;
break;
case R.id.action_settings_app_help:
Toast.makeText(this, this.getString(R.string.action_settings_app_help_text), Toast.LENGTH_SHORT).show();
bRet=true;
break;
case R.id.action_settings_about_app:
Toast.makeText(this, this.getString(R.string.action_settings_about_text), Toast.LENGTH_SHORT).show();
bRet=true;
break;
case R.id.action_settings_rate_app:
Intent intent3 = new Intent(Intent.ACTION_VIEW);
intent3.setData(Uri.parse(getString(R.string.rate_this_app_url)));
startActivity(intent3);
bRet=true;
break;
case R.id.action_settings_privacy_policy:
Intent intentprivacy = new Intent(Intent.ACTION_VIEW);
intentprivacy.setData(Uri.parse(getString(R.string.privacy_policy_url)));
startActivity(intentprivacy);
bRet=true;
break;
case R.id.action_settings_all_our_apps:
Intent intent4 = new Intent(Intent.ACTION_VIEW);
intent4.setData(Uri.parse(getString(R.string.all_our_apps_url)));
startActivity(intent4);
bRet=true;
break;
default:
bRet=super.onOptionsItemSelected(item);
}
return bRet;
}
}
and here is an example of what i am trying to do
(Note I know html tags don't work .... that is just to show what i am trying to wrap the text in and where i am trying to put the divider)
package mypackage.android;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import mypackage.android.database.CommonNamesAdapter;
public class CommonNameDescription extends AppCompatActivity {
String common_name; < --- NEED TO CHANGE FONT SIZE AND COLOR AND MAKE BOLD
String common_name_meaning; < --- NEED TO CHANGE FONT SIZE AND COLOR AND MAKE BOLD SEPERATLY FROM THE STRING ABOVE
long common_name_rowid;
CharSequence mybreak = "\n";
CharSequence text = "Means"; < --- NEED TO CHANGE FONT SIZE AND COLOR AND MAKE BOLD SEPERATLY FROM THE OTHER 2 STRINGS ABOVE
CharSequence description;
public static TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.common_names_description);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Bundle extras = getIntent().getExtras();
common_name_rowid = extras.getLong(CommonNamesAdapter.COMMON_NAME_ROWID );
common_name = extras.getString(CommonNamesAdapter.COMMON_NAME);
common_name_meaning = extras.getString(CommonNamesAdapter.COMMON_NAME_MEANING).toString();
description = <b><font size="size here" color="color here">common_name</b></font>+horizontal_line_or_divder+<b><font size="size here" color="color here">text</b></font>+mybreak+<b><font size="size here" color="color here">common_name_meaning</b></font>;
tv.setText(description);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
boolean bRet=false;//set true is menu selection handled
switch (item.getItemId()) {
case R.id.action_settings_get_pro:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(getString(R.string.pro_version_url)));
startActivity(intent);
bRet=true;
break;
case R.id.action_settings_get_pro2:
Intent intent2 = new Intent(Intent.ACTION_VIEW);
intent2.setData(Uri.parse(getString(R.string.pro_version_url)));
startActivity(intent2);
bRet=true;
break;
case R.id.action_settings_app_help:
Toast.makeText(this, this.getString(R.string.action_settings_app_help_text), Toast.LENGTH_SHORT).show();
bRet=true;
break;
case R.id.action_settings_about_app:
Toast.makeText(this, this.getString(R.string.action_settings_about_text), Toast.LENGTH_SHORT).show();
bRet=true;
break;
case R.id.action_settings_rate_app:
Intent intent3 = new Intent(Intent.ACTION_VIEW);
intent3.setData(Uri.parse(getString(R.string.rate_this_app_url)));
startActivity(intent3);
bRet=true;
break;
case R.id.action_settings_privacy_policy:
Intent intentprivacy = new Intent(Intent.ACTION_VIEW);
intentprivacy.setData(Uri.parse(getString(R.string.privacy_policy_url)));
startActivity(intentprivacy);
bRet=true;
break;
case R.id.action_settings_all_our_apps:
Intent intent4 = new Intent(Intent.ACTION_VIEW);
intent4.setData(Uri.parse(getString(R.string.all_our_apps_url)));
startActivity(intent4);
bRet=true;
break;
default:
bRet=super.onOptionsItemSelected(item);
}
return bRet;
}
}