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!!
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.
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.
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.
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);
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.
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.
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
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.
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.
Ok, I am able to load a specific image into ImageView from my SD card...no problem. But I need to take a step further and add a button load the next image (move/copy/delete previous image). I can add the button, etc but how do I retreive the 'next' image without knowing filenames? Here is the code so far. I would have liked to use Gallery but it has been deprecated and I can't seem to make anything else work. Thank You
package com.demo.ShowSDImages;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
public class AndroidBitmap extends Activity
{private final String imageInSD = "/sdcard/er.PNG";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
ImageView myImageView = (ImageView)findViewById(R.id.imageview);
myImageView.setImageBitmap(bitmap);
}
}
i think you can give a try to this sample here..Check this link
here i think the below code can help you in getting the images .
int imageCount = sdcardPath.listFiles().length;
for (int count = 0; count < imageCount - 1; count++) {
Bitmap bmp = BitmapFactory.decodeFile(sdcardPath.listFiles()[count]
.getAbsolutePath());
In the sample instead of using the thread or timer use the the same logic that is using of the showNext() in the button click event
Give a try. Happy Coding
i recently started programming in Android and i came across a little problem.
What i'm trying to do is:
I have NewsActivity and a NewsRows.class (in the same package). So the news activites just creates a new NewsRows object and tells it to fill the TableLayout with new rows.
It works fine as long as i try to add an image from a resource... The app just keeps crashing.
The debugger tells me it can't find the resource but i can't find out why!
My code is here:
News Acitivty
package de.myapp.app.activites.news;
import de.myapp.app.R;
import android.app.Activity;
import android.os.Bundle;
public class News extends Activity {
NewsRows rowClass = new NewsRows();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news);
NewsRows.createNewsEntries(this);
}
}
NewsRows.class
package de.myapp.app.activites.news;
import de.myapp.app.R;
import android.app.Activity;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class NewsRows {
static TextView title;
static TableRow tRow;
static TableLayout tLayout;
public NewsRows() {
}
public static void createNewsEntries(Activity contextActivity) {
ImageView image = new ImageView(contextActivity);
image.setBackgroundColor(R.drawable.myimage);
tLayout = (TableLayout) contextActivity.findViewById(R.id.NewsTable);
for(int a = 0; a < 100; a++) {
tRow = new TableRow(contextActivity);
title = new TextView(contextActivity);
//tRow.addView(image);
title.setText("This is a test.");
tRow.addView(title);
tLayout.addView(tRow);
}
}
}
EDIT:
The line
image.setBackgroundColor(R.drawable.myimage);<br />
Is actually supposed to be:
image.setImageResource(R.drawable.myimage);
You're trying to set an image into a background color:
Change this:
image.setBackgroundColor(R.drawable.myimage);
to this:
image.setBackgroundResource(R.drawable.myimage);
Fun fact:
TableLayout tLayout = (TableLayout) findViewById(R.id.NewsTable);
TableRow tRow = new TableRow(this);
ImageView image = new ImageView(this);
image.setImageResource(R.drawable.myimage);
tRow.addView(image);
tLayout.addView(tRow);
If i put this code right in the News.Activity it works...
You can't set a drawable for as a background color of your ImageView, that's why you have the resource not found exception on your logcat!!
image.setBackgroundColor(R.drawable.myimage);
change it to this:
image.setBackgroundResource(R.drawable.myimage);
Also try to change this
public static void createNewsEntries(Activity contextActivity)
with
public static void createNewsEntries(Context contextActivity)
Describing the functionality of my application: I have put in a Relative Layout a TextView, an EditText and a button. All I am trying to do is: when the user writes something in the EditText and push the button, then the content of the EditText is appeared in the TextView(just like a chat-virtual chat). Everything works perfectly, but when the EditText is empty,and the button get pushed, an empty line is appeared in the TextView(and i don't want to..). Although I've tried to solve it using an if the empty line is still appearing in the TextView. I would be really greatfull, if you could help!!! Than you in advance!
Here is my code:
package teiath.android.appliacation;
import android.app.Activity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class M_chat extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/**Code for the scroll bars in the TextView. */
final TextView tv = (TextView)findViewById(R.id.TEXT_VIEW);
tv.setMovementMethod(new ScrollingMovementMethod());//for the scroll bars
/** Code for the scroll bars in the EditText. */
final EditText wr = (EditText)findViewById(R.id.EDIT_TEXT);
wr.setMovementMethod(new ScrollingMovementMethod());//for the scroll bars
final Button button = (Button) findViewById(R.id.btn1);//find the button by id in main.xml
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
String wrcontent = wr.getText().toString();//gets the text of the EditText and put it in "tvcontent" variable.
String tvcontent = tv.getText().toString();//gets the text of the textView and put it in "tvcontent" variable.
if (wrcontent!="")//if the EditText is not empty
{
//check if the TextView is empty or not
if (tvcontent!="")//If it is not empty...
{
tv.setText(tvcontent + "\n" + wrcontent);//add its current(TextView's text) text, new line and the text of the EditText as the new text of TextView.
//tv.setVisibility(0);//makes visible the textView with the cloud1.png background
wr.setText("");//set the text of the Edit Text as empty
//wrcontent = "";
}
else//if the TextView is empty...
{
tv.setText(wrcontent);//add the text of the editText as the new text of the TextView
wr.setText("");
}
}
/**if (wrcontent=="")
{
}*/
//finish();
}
});
}
}
Don't use !="" for String comparison. To check for empty text, use something like
if ( wrcontent != null && wrcontent.trim().length() == 0 ) {
Better yet, include Guava libraries in your code.
Below is an example of the type of problem that I have. I have data in a pojo that I need to display in a textview... the data has pseudo code that denotes each paragraph with [p]
I would like to somehow parse the [p]'s into paragraphs when they are displayed in the textview. Can this be done? Is there something I can substitute for the [p] that will make a new paragraph in the textview?
Question question = new Question();
question.setText("Here is the first paragraph.[p] And this should be the second.");
TextView view = (TextView) findViewById(R.id.qtext);
view.setText(question.getParsedText());
Hi i would parse the whole String and then replace every [p] with \n or even \n\n as you like.
\n in the String makes a linebreak. For example use it like that:
question.setText("Here is the first paragraph.\n\n And this should be the second.");`
The method which can do that easily is String.replace(...)
You can also try this
String summary = "<html><font color=\"#FFFFFF\"" +
"<p align="+ "\"" +"left" + "\""+ ">" + "MY data" +"</p>"+
"</font></html>";
mTextView.setText(Html.fromHtml(summary));
TextView tw = findViewById(R.id.t);
tw.setText("first line\nsecond line\nthird line");
<br> and <p> work if you need other formatting like bold and are in the middle of an Html.fromHtml:
import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.widget.TextView;
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TextView tv = new TextView(this);
tv.setText(Html.fromHtml("first<br><b>second</b>"));
setContentView(tv);
}
}
Tested on Android 22.