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.
Related
I'm a noob Android studio programmer (this is hour 2 of learning!) and I expect this is a real rookie error I'm making!
I've got a Plain Text field in my application and I would like to set the text of this dynamically. I've given the plain text field the ID of: "resultText". Here's what I try;
public void calcnums(View v)
{
int x=firstNum + seondNum;
resultText.setText("Result: " + x);
}
For some reason I get 'resultText' highlighted in red and the hover over message is; Cannot resolve symbol 'resultText'.
I have the feeling that I'm doing something wrong by using the ID, but I'm lost!
Full code as suggested in comments;
import android.app.Application;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.Random;
public class AddNumbers extends AppCompatActivity {
private int firstNum;
private int seondNum;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_numbers);
}
public void calcnums(View v)
{
int x=firstNum + seondNum;
resultText.setText(String.format("Result: %d", x);
}
public void setNums(View v)
{
TextView tx= (TextView) findViewById(R.id.text);
Random r = new Random();
int x=r.nextInt(2) + 1; // r.nextInt(2) returns either 0 or 1
firstNum = x;
r = new Random();
x=r.nextInt(2) + 1;
seondNum = x;
num1.setText(""+firstNum);
num2.setText(""+seondNum);
}
}
It seems you need to declare the View resultText
Like,
EditText resultText = (EditText) findViewById(R.id.resultText);
Also make sure that the name you are using for the resultText View in xml is written correctly as provided in the method findViewById()
usually cannot resolve symbol means some problems in variable declarations.
I am too learning android and I stumbled upon this problem as well.
I don't have the whole snippet of your code.But the thing you might be missing is to point your EditText object to view in xml.
EditText resultText = (EditText) v.findViewById(R.id.result_edit_text);
<EditText
android:id="#+id/result_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/result_edit_text"
/>
Let's assume that we have an layout with an edittext
<EditText
android:id="#id/txt_user_email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text" />
It has an ID. In your actitivy you must find and cast the EditText as follow:
EditText txtUserEmail = (EditText) findViewById(R.id.txt_user_email);
txtUserEmail.setText("klaus.dieter#lusty-swingers.de");
You need to find your text view in onCreate(), like that:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_numbers);
TextView tx= (TextView) findViewById(R.id.text);
}
I have html content and setting this into (android) textbox as follows
textbox.setText(Html.fromHtml(myhtml));
HTML content contains some img tags as well, I do not want display images, currently it displays empty square boxes where ever there is a img tag. How do I hide those square boxes?
You could do a regex replace <img.+?> on htmlString.
textView.setText(Html.fromHtml(htmlString.replaceAll("<img.+?>", "")));
Untested
Update:
since images can look like:
<img ...></img>
and
<img... />
This solution will match both cases:
String htmlBody = htmlString.replaceAll("<img.+/(img)*>", "");
textView.setText(Html.fromHtml(htmlBody));
This works
Create Html.ImageGetter to return empty image as follows
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
public class EmptyImageGetter implements Html.ImageGetter {
private static final Drawable TRANSPARENT_DRAWABLE = new ColorDrawable(Color.TRANSPARENT);
#Override
public Drawable getDrawable(String source) {
return TRANSPARENT_DRAWABLE;
}
}
and then create a static instance
private static final EmptyImageGetter EMPTY_IMAGE_GETTER = new EmptyImageGetter();
set this into text view
textView.setText(Html.fromHtml(myhtml, EMPTY_IMAGE_GETTER, null));
Hey everyone this is a update from my previous question
https://stackoverflow.com/questions/6676440/android-showing-a-edittext-text-in-webview
I got the webview to see the text inside my Edit text field but its displaying it as text
this is the coding im using
case R.id.Preview:
Intent j = new Intent(this, Preview.class);
j.putExtra(com.afajje.htmlzero.Preview.URL,
myEditText.getText().toString());
startActivity(j);
}
return false;}
Want im trying to have it do is view the Edit text, text as HTML
For example if I put in the Edittext field
<html>
<head>
</head>
<body>
<p>This is a Preview</p>
</body>
</html>
In the web view itll just just the "This is a Preview"
Here ya go, a working demo:
package com.stackexchange.test;
import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.widget.EditText;
public class home extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText box = (EditText) findViewById(R.id.edittext);
String htmltext = "<b>This is bold. </ b> and <i> this is italic </ i>." ;
Spanned text = Html.fromHtml( htmltext );
box.setText( text );
}
}
You should see (except that I've edited the text in the app):
Try the below code to show the html text into the EditText.
String str = "<html><head></head><body><p>This is a Preview</p></body></html>";
editText = (EditText)findViewById(R.id.editText);
editText.setText(Html.fromHtml(str));
Well, in the editText, if you dont use "Html.fromHtml", the string will be display with the html tags. In a WebView, I really dont know what zou want to do...sorry
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.
I have successfully installed the Eclipse, and Android SDK to my Mac. However when I run the program using the code below. It always gives me the error. "Sorry! The application Hello, Harris(process com.example.helloandroid) has stopped unexpectedly. Please try again.
//package com.example.helloandroid;
import com.example.helloandroid.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Harris Family.");
setContentView(tv);
}
}
I think the problem is that you directly set the TextView as the ContentView. You should better use a layout instead.
Is your activity inserted in the application manifest? http://developer.android.com/guide/topics/manifest/activity-element.html
This is because you have not set a layout view, you only have a textview with no parent.
First set up the parent layout for your text view with something like this:
ScrollView sv = new ScrollView(ViewPlay.this);
LinearLayout ll = new LinearLayout(ViewPlay.this);
ll.setOrientation(LinearLayout.VERTICAL);
Then add your text view with :
TextView tv = new TextView(this);
tv.setText("Greetings");
tv.setGravity(Gravity.CENTER_HORIZONTAL);
tv.setTextSize(18);
ll.addView(tv);
Now add the view to the layout with:
this.setContentView(sv);
Adding content to the screen dynamically as above can be very thorny, so where possible use xml.