How to jump line on android - java

I'm develop an android app but I don't know how to jump line or more while set the text of text view so how can I do this ?
On html I use \n but I don't know How to do it from java
Like this
Android
A
n
d
r
o
i
d
(Every space meanning new line)
Note that this text will appear on text view

Before using \n, you must set the android:maxLines attribute in XML. For example, in XML for the above text, you could say:
<TextView
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:maxLines = "7"
android:text = "A\nn\nd\nr\no\ni\nd"
/>
Or if you wanted to do it from Java, you could do:
textView.setMaxLines(7);
textView.setText("A\nn\nd\nr\no\ni\nd");

Related

Android change specific words on TextView from Java or XML

Since Android XML layouts are all rectangular, how can I have just some parts of the TextView with different font sizes and params?
In the example below, the time (which is in grey and has a smaller font size) is always at the end of the text. I don't know how to reproduce this in XML. If I separate the text in two different textviews and the width of both were wrap content it would just appear at the right of the TextView Layout (at the right of every line from the main message, not at the end).
Is it possible to do this layout configuration with XML or do I need to do it programmatically using JAVA? How can I change the params of certain parts of a textview with Java?
With two TextViews and Wrap Content (not what I want):
What I have understood from your question is you want a single textView with multiple style inside this. to do so you can do this:
SpannableStringBuilder text = new SpannableStringBuilder(); // a spannable string builder
text.append("your friend "); // appending first text
SpannableString fname = new SpannableString("Joana"); //making a span that would have different style
fname.setSpan(new StyleSpan(Typeface.BOLD), 0, fname.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); // set different style
text.append(fname); // append value
text.append(" his sending you a message ");
SpannableString time_ago = new SpannableString("41 min ago"); //another span
time_ago.setSpan(new RelativeSizeSpan(.7f), 0, time_ago.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
time_ago.setSpan(new ForegroundColorSpan(Color.GRAY), 0, time_ago.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
text.append(time_ago); // append value
textView.setText(text);
XML:
<TextView
android:padding="5sp"
android:textColor="#color/black"
android:textSize="24sp"
android:gravity="start"
android:id="#+id/tvtest"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

How to display rating value(Integer) from an API (Google api) using ratings bar in Android (preferably using Kotlin)?

val rating = jsonObject.getInt("rating")
for instance, a rating of 4, to be displayed as 4 stars in my view.
I am looking for ways to display it programatically as image views or to use Ratingsbar
Use RatingBar, in your view:
<RatingBar
android:id="#+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
In your Activity:
Use synthetic Just type the id of your RatingBar and it'll get your RatingBar
import kotlinx.android.synthetic.main.your_activity.*
val rating = jsonObject.getInt("rating")
ratingBar.numStars = rating
or just old school:
val rBar: RatingBar = findViewById(R.id.ratingBar)
rBar.numStars = rating
Edit: As you wanted to know about resizing:
<RatingBar
android:id="#+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="5"
android:rating="3.5"
android:scaleX=".5"
android:scaleY=".5"
android:transformPivotX="0dp"
android:transformPivotY="0dp" />
You can use scaleX & scaleY to resize accordingly (chose a balanced value of both) the base value is 1 for both. (.5,.5 will make it half of the original size.)
Here's a question about resizing: How can I decrease the size of Ratingbar?

How to change the font language in android

In my app i want to convert my english font into hindi, for that i did localization. that is not supported for lower versions(i.e 2.3). Then i downloaded "shusha.ttf" file and added to assets/fonts folder. Then i set the typeface for a textviews. Like this i am able to convert the english font to hindi. but when i get the text from the text view it is not showing in Hindi(getting the english font). i am using that text to send the mail through gmail.
If any body have idea about this please give me a suggestion.
This is my Code
t = ((TextView)findViewById(R.id.textView1));
t2 = ((TextView)findViewById(R.id.textView2));
Typeface Hindi = Typeface.createFromAsset(getAssets(), "fonts/shusha.ttf");
t.setTypeface(Hindi);
t.setTextSize(20);
String hello=" Hyderabad, Andhra Pradesh, India ";
t.setText(hello);
String lang=t.getText().toString().trim();
t2.setText(lang);
Instead of using and playing with font, use UNICODE characters for hindi words. I had done it for a project of mine and it works great.
You have to change the font of the text of text view.
First put the font in your asset folder.
Then in your java file add code like this to change your font:
Typeface myTypeface = Typeface.createFromAsset(this.getAssets(),
"shusha.ttf");
TextView digital= (TextView) findViewById(R.id.your_textView);
digital.setTypeface(myTypeface);
Put shusha.ttf in your asset folder of your project.
This will might help you.
Create styles in values/styles like,
<style name="MyStyle" parent="#android:style/Theme">
<item name="typeface">font_in_assest.ttf</item>
</style>
Here, font_in_assest.ttf is the font you placed in your assests/fonts.
Then assign the style for you view,
<TextView
android:id="#+id/title"
style="#style/MyStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
It will be helpful for you..

Add images dynamically based on choice selected

I have the following layout which displays an image and a text string:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center" >
<ImageView
android:id="#+id/ivIcon"
android:layout_width="#dimen/number_icon"
android:layout_height="#dimen/number_icon"
android:scaleType="fitXY"
android:layout_marginTop="#dimen/letter_icon_margin_top" />
<Button
android:id="#+id/btnNumber"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="5dp"
android:layout_weight="3"
android:text=""
android:gravity="center"
android:textSize="#dimen/word_size_text"
android:textColor="#000000"
android:textStyle="bold"
android:background="#drawable/borderaction" />
</LinearLayout>
The ivIcon is displaying the images based on user input. It worked great when displaying one image per letter. But how do i fix the above layout so it displays the number of images based on user selection.
#dimen is used to change the view size based on the screen.
For example this is what is looking like when I use it for letter:
For the number, if the user chooses 5 the display should be:
If the user chooses 1 the display should be:
The code that is setting the image for the letter is:
ivLetterIcon = (ImageView) findViewById(R.id.ivIcon);
ivLetterIcon.setImageResource(R.drawable.apple);
How would I accomplish the number images?
Would it be best to have a blank layout without the images and add the ImageView at runtim depending on the number and have it centered? This way the images(s) are centered to the parent view layout?
Well I got an idea maybe you can try this out.
Try using GridView for displaying the number of apples by dynamically changing the column numbers and row numbers depending upon the screen.
For ex, if gridView is the GridView and the entered number is 11, you can do this.
gridView.setNumberOfColumns(5);
gridView.setNumberOfRows(3);
so 5x3 can accomodate 15 elements which can fit in 11 easily.
Then inflate the custom view containing the Apple into the columns.
Make the layout_height for the ImageView a fixed height
--edit--
int dp = TheFixedSize;
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
Imageview.setHeight(px);

Android text justification issue within checkbox

In one of the activity of my application, I am displaying user names with their points in front of the names.
The name may have different number of characters, but I want the strings to end at the same place.
I am able to provide correct number of spaces and the strings are equal in character lengths but the characters take uneven space and ruin the symmetry of the strings.
Is there any work around for this?
Here's the code :
private String separateNamePoints{
String text="";
//text is separated by ,
String[]splittedRawText=rawText.split(",");
String name=(splittedRawText)[0];
String points=(splittedRawText)[1];
int pointsLength=points.length();
int reqSpaceLength=40-name.length();
String space="";
for(int i=0;i<reqSpaceLength;i++){
space+=" ";
}
if(pointsLength==1)
space+=" ";
else if(pointsLength==2)
space+=" ";
else if(pointsLength==3)
space+=" ";
text=name+space+points;
return text;
}
And here's the image :
There are ways you can handle this..
Your approach
In here, you can try using String.format() to format as per requirement .. here read this for ref
You can use 2 textViews and use alignment to solve your problem..
Try to use this xml for list item and write adapter for this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"/>
<TextView
android:id="#+id/points"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
Android do not provide complete text justification.
In this case, need to create a custom control containing separate edittexts to display the data.
Your code will work fine. I think your input string having some space after the number.
please try with this
String[]splittedRawText=rawText.trim().split(",");

Categories

Resources