Reputation: 897
Hi I'm a newbie in android. I just wanted to know if there is any way to change the font size of a String in android?
String name = db.getName(Id)
String str = "Name : " + name;
I want to have "Name" with bigger font size than the value in "name".Where "name" is the value I get from the database.
Please do suggest any method to do this!! Thanks in advance!!
Upvotes: 2
Views: 13527
Reputation: 590
Spannable font: In order to set a different font size to some portion of text, a RelativeSizeSpan can be used, as shown in the following example:
Spannable spannable = new SpannableString(firstWord+lastWord);
spannable.setSpan(new ForegroundColorSpan(firstWordColor), 0, firstWord.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new ForegroundColorSpan(lastWordColor), firstWord.length(), firstWord.length()+lastWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textview.setText( spannable );
Credit: Copy and Pasted from this webpage.
Upvotes: 0
Reputation: 1520
The best Idea to show variances in a ListView is showing headers. A Simple Example is explained here It states the following code:
Simple activity Xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/add_journalentry_menuitem"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/list_journal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
List Header
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_header_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="2dip"
android:paddingBottom="2dip"
android:paddingLeft="5dip"
style="?android:attr/listSeparatorTextViewStyle" />
List Item
<?xml version="1.0" encoding="utf-8"?>
<!-- list_item.xml -->
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item_title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:paddingLeft="15dip"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
Main Activity
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class ListSample extends Activity
{
public final static String ITEM_TITLE = "title";
public final static String ITEM_CAPTION = "caption";
// SectionHeaders
private final static String[] days = new String[]{"Mon", "Tue", "Wed", "Thur", "Fri"};
// Section Contents
private final static String[] notes = new String[]{"Ate Breakfast", "Ran a Marathan ...yah really", "Slept all day"};
// MENU - ListView
private ListView addJournalEntryItem;
// Adapter for ListView Contents
private SeparatedListAdapter adapter;
// ListView Contents
private ListView journalListView;
public Map<String, ?> createItem(String title, String caption)
{
Map<String, String> item = new HashMap<String, String>();
item.put(ITEM_TITLE, title);
item.put(ITEM_CAPTION, caption);
return item;
}
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
// Sets the View Layer
setContentView(R.layout.main);
// Interactive Tools
final ArrayAdapter<String> journalEntryAdapter = new ArrayAdapter<String>(this, R.layout.add_journalentry_menuitem, new String[]{"Add Journal Entry"});
// AddJournalEntryItem
addJournalEntryItem = (ListView) this.findViewById(R.id.add_journalentry_menuitem);
addJournalEntryItem.setAdapter(journalEntryAdapter);
addJournalEntryItem.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long duration)
{
String item = journalEntryAdapter.getItem(position);
Toast.makeText(getApplicationContext(), item, Toast.LENGTH_SHORT).show();
}
});
// Create the ListView Adapter
adapter = new SeparatedListAdapter(this);
ArrayAdapter<String> listadapter = new ArrayAdapter<String>(this, R.layout.list_item, notes);
// Add Sections
for (int i = 0; i < days.length; i++)
{
adapter.addSection(days[i], listadapter);
}
// Get a reference to the ListView holder
journalListView = (ListView) this.findViewById(R.id.list_journal);
// Set the adapter on the ListView holder
journalListView.setAdapter(adapter);
// Listen for Click events
journalListView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long duration)
{
String item = (String) adapter.getItem(position);
Toast.makeText(getApplicationContext(), item, Toast.LENGTH_SHORT).show();
}
});
}
}
Two more additional XMLs are present there, but with these given structure you can Implement what you want in a better way rather than Just size difference.
P.S.- the full app is available here
Upvotes: 0
Reputation: 2106
When you are putting the text into a textView you can increase the font. For example
textView.setText(yourString);
textView.setTextSize(20);
Or you can give the font size in the Layout.xml file itself
<TextView
android:id = "@+id/textView"
........
android:textSize = "20dp"/>
Please feel free to ask any further doubts if you need further clarifications.
Upvotes: 2
Reputation: 6131
Use
TextView textView = (TextView) findViewById(R.id.textView);
Spannable span = new SpannableString(str);
span.setSpan(new RelativeSizeSpan(0.8f), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(span);
on a TextView to get different TextSizes.
Upvotes: 2
Reputation:
You can't. As you can't in any other language. What you need to do is to change the fontsize of the element that will display the text, not the String itself.
Try creating a layout in res/layout/
folder, add a TextView element and then search for the textSize
property.
Upvotes: 0
Reputation: 6622
You need to use a Spannable and give it to your TextView in order to modify just a portion of the text. To change the size use :
span.setSpan(new RelativeSizeSpan(0.8f), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Upvotes: 1
Reputation: 7626
You cannot do that. You can add the string
to the View lets say EditText
or TextView
and set the text Size for the view in xml as below:
android:textSize="18dp"
or programatically, you can do like this:
<YourTextView>.setTextSize(18);
Due to lack of idea before, i had asked similar question here. I hope you will get an idea with this link.
Upvotes: -1
Reputation: 40193
You can't change the font size of a String
, because String
is just a set of characters, but you can alter the font size of a TextView
that contains this String
using the setTextSize() method. Hope this helps.
Upvotes: 0
Reputation: 70929
You do not change the font size of a String
instead you change the font size of the text when you display the String
(for instance in the TextView
if you are using one). A String
is simply a data object holding the text you want to display and has nothing to do with the way you display it.
Upvotes: 1