How to get text from textview? - android

I'm trying to get a text from a TextView so I tried to use getText() method but didn't work. I'm using fragments only, so there's a way I can get my text from the TextView?
Ps. I tried with getEditText() but I get NULL.

you are probably calling TextView textView = (TextView) findViewById(R.id.yourInFragmentTextView); on your activity layout?
You need to get the reference to the actual view in the fragment.
First you need a reference to the inflated fragment and than call
TextView textView = (TextView) myFragment.findViewById(R.id.yourInFragmentTextView);
String text = textView.getText().toString();

String str = textview.getText().toString(); should work
try clearing cache and restart

Related

how can i decode HTML code to Android by kotlin?

I'm trying to decode HTML entities, currently my code is:
val str = name
val textView = findViewById<View>(R.id.text) as TextView
textView.text = Html.fromHtml(str, Html.FROM_HTML_MODE_COMPACT)
and here the error message is:
kotlin.TypeCastException: null cannot be cast to non-null type android.widget.TextView
Your error has nothing to do with the HTML handling, you're getting the exception on this line, because findViewById is returning null, and then the cast to TextView is failing:
val textView = findViewById<View>(R.id.text) as TextView
Why this happens exactly is hard to tell without context, but the issue is that the View with the ID text was not found.
If you're in an Activity, make sure you're doing this after you call setContentView.
If you're in a Fragment, make sure you're doing this after the onCreateView method has run.
In either case, make sure you're actually using a layout that contains a TextView with the ID #+id/text.
Additionally, you're looking up the TextView as a View first and then casting it, you could do either of these instead:
val textView = findViewById<TextView>(R.id.text)
val textView: TextView = findViewById(R.id.text)

How can i retrieve text of textview given its resource id

I have 10 textviews each with the following id(s) tv1, tv2, tv3, etc. and each have text in them. These textviews are clickable, I saved the resource ID of the clicked textview in sharedprefrence. This ID is an int.
Now using this resource ID, can I somehow get text attached to this resource(TextView here). Below is code the code I am trying to use, but it gives a null pointer exception.
if (!sharedPref.getString(String.valueOf(i), "error").equals("error")) {
int u = Integer.parseInt (sharedPref.getString(String.valueOf(i), "error"));
System.out.println("For "+i+" object Value of id retrieved is-"+u);
String dohass= ((TextView) findViewById(u)).getText().toString();
}
I have checked value of u and is correct(correct resource id), but the above code says I am using getText on a null object.
Here are the logs
java.lang.RuntimeException: Unable to start activity ComponentInfo{dhritiapps.tulsiramayan/dhritiapps.tulsiramayan.Fav}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
First you have to get reference of your textview.
TextView textview= ((TextView) findViewById(R.id.youtextviewid));
then you can get the string value by calling getText on TextView reference.
String yourtext= textview.getText().toString();
Make sure you perform this step after setContentLayout() method call in onCreate. If tried before that, it will give null pointer exception because it will not be able to find your textview.
Get reference of texview.
TextView textview= (TextView) findViewById(R.id.textviewid);
call this method
String yourtext= textview.getText().toString();

NullPointerException while setting a value to Textview passed from previous activity

Intent i = getIntent();
categorie_label = i.getStringExtra("categorie_label");
TextView txtrank = (TextView) findViewById(R.id.catid);
txtrank.setText(categorie_label);
Why the code below returning a NullPointerException ? Please help
Your activity probably did not find the TextView with the specified ID in it´s layout and thus it is null. You should check if it is contained in that layout.

Android: give textfield 'id' in java

I was wondering how to give a TextField an 'id' in java. So I am pretty much wanting to know how to give the TextField android:id="#id/NAMEHERE" but do it in java. Here is the code so far:
TextView taskname = new TextView(this);
taskname.setText("Task " + tasknumber);
tasklayout.addView(taskname);
What do I need to add to give it an id?
If your question is to how to set id into activity, then understand purpose of id.
Id is specially required when you want to fetch view reference into activity class, where view has been declared into xml file. But in Activity, if you are creating any view, by
TextView taskname = new TextView(this);
here, you already have view reference, but if you still want to set id, then you can use method
taskname.setId(10002);
Try this
static final int TEXT_ID = 80001;
TextView taskname = new TextView(this);
taskname.setId(TEXT_ID);
and later in your code you can refer to this textview by
TextView t = (TextView)findViewById(TEXT_ID);
or
TextView t = taskname;

How to recall the username previously entered in another activity

I am passing a value for username in one activity through XML. I want to recall the value in the next screen, like, 'Hello/Welcome, '. How is it possible using GetExtra and PutExtra?
Pls. find the below code, which I have tried for the same.
For storing the value of username in one activity:
Intent myActivity2 = new Intent(this,Activity1.class);
myActivity2.putExtra("nickname", R.id.mynickname3);
startActivity(myActivity2);
For recalling the value in other activity:
Bundle extras = getIntent().getExtras();
String val = extras.getString("nickname");
TextView tv=new TextView(this);
tv.setText(val);
If you want to reuse a username, use sharedPreference.
It is not a good idea to pass a userName to another activity then use that.Field like userName may need to use again and again.
In the first activity, you are passing in an integer value (R.id.mynickname3). Instead you need to give it a String. Use this instead:
myActivity2.putExtra("nickname", getString(R.id.mynickname3));
As a side note, you are trying to get a Bundle unnecessarily. Try this:
String val = getIntent().getStringExtra("nickname");
TextView tv = new TextView(this);
if (val != null)
{
tv.setText(val);
}
You're nearly right.
It is not enough to just pass the id of the EditText. You have to use below code -
((EditText)findViewById(R.id.mynickname3)).getText().toString();
int putExtra. Everything else should be fine.
UPDATE: Don't forget to set your TextView as contentView in OnCreate via:
setContentView(..)
Just a silly mistake in your code:
myActivity2.putExtra("nickname", R.id.mynickname3);
Where you have written ID of the EditText (nickname), instead it should be as a:
txtNickName = (EditText) findViewById (R.id.mynickname3);
myActivity2.putExtra("nickname", txtNickName.getText().toString());

Categories

Resources