I'm completely new to Android Studio.
I have just installed it on Windows 10 and since I wanted to learn how to use it, I have started to follow the online guide (https://developer.android.com/training/basics/firstapp/starting-activity.html#Up).
I am literally following it step by step, but when it comes to this part of the code:
public class DisplayMessageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Capture the layout's TextView and set the string as its text
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(message);
}
the "textView" part after "findViewById" appears red and I get this error message: "cannot resolve symbol 'textView'".
This happens even if I have imported android.widget.TextView.
Maybe this is stupid question, but I am completely new to Android Studio.
Thanks for the answers :)
In your file R.layout.activity_display_message, the <TextView> tag must have android:id="#+id/textView"
Can you include your XML File?
As mentioned by buutqn
<TextView> tag must have android:id="#+id/textView"
This is in the file: app.res.layout.activity_display_message.xml
under the Text tab(as opposed to Design tab).
This issue can be duplicated by deleting the TextView box from the design tab, and inserting a new (second) text view.
In my case, the problem was for other reason although the error was the same. I copied the following code out of the OnCreate method.
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Capture the layout's TextView and set the string as its text
TextView textView = findViewById(R.id.textView);
textView.setText(message);
The text (TextView) before findViewById was not neccesary.
The TextView object of the Activity_display_message is called TextView2, instead of Textview. So I tried and changed:
TextView textView = (TextView) findViewById(R.id.textView);
for
TextView textView = (TextView) findViewById(R.id.textView2);
And it worked!
Erase the statement, then retype it. There is going to be a small pop up indicating you to press a command. It is basically an import library issue when you press the command (for macOS it is ⌥⏎ ) the respective library will be added. If this does not work, close Android Studio and reopen it . I am not an expert on this matter, but this worked for me hence I am sharing.
After tiresome searches and updating settings,
I Just clicked Invalidate Catches -> Invalidate And Restart
And Done! TextView Error Disappearedenter image description here
Related
I want to integrate text widget in my application in android studio. I tried running the samples and they are working fine. Now i want to toast the digital text that appears after user input. I want to save that text somehere. I used this code in OnRecognitionEnd() function
#Override
public void onRecognitionEnd() {
String Result = mEditText.getText().toString();
Log.d(TAG, "result is: " +Result);
}
Now can you kindly tell me I want to display the text that is being stored in “Result” I used multiple ways but it says UNFORTUNETLY THE APPLICATION HAS STOPPED. For Example i simply did this
TextView textElement = null;
textElement.setText(Result);
Please tell me what I am doing wrong what should i do to display it ?
If you do this :
TextView textElement = null;
textElement.setText(Result);
You will try to setText on null. I suppose the error in your logcat is NullPointerException.
You have to instantiate your textElement.
TextView textElement=(TextView)findViewById(R.id.myid);
Where "myid" is the id in your layout.xml.
I mistakenly disable this feature but I don't know how to enable again. The feature is that warns me when I cast an object to another class. For example:
ImageView scanner;
TextView errorMessage;
#Override
protected void onCreate(Bundle savedInstanceState) {
errorMessage = (TextView) findViewById(R.id.errorMessage);
scanner = (TextView) findViewById(R.id.scanner); // studio warns me when I do this cast, but I mistakenly close this feature
}
Thanks
EDIT
this is the exact solution:
You decalred scanner as imageview on top
ImageView scanner;
and you are tying to cast it to textview in oncreate, that is not possible.
Change
scanner = (TextView) findViewById(R.id.scanner);
to
scanner = (ImageView) findViewById(R.id.scanner);
You can try the following trick:
Step 1:
Find the inspector icon (a human with a cap) at the bottom right corner of your Android Studio.
Step 2:
Click on that and you can find a seek bar. It will help you to enable/disable or even you can configure it according to your need.
I have a ScrollView containing a TextView. I linkify parts of that TextView with
Pattern pattern = Pattern.compile("MyLink");
Linkify.addLinks(textView1, pattern, "mylink://");
I habe an Intent filter in my Manifest for mylink:// so a new Activity is opened when MyLink is clicked (as described in this question).
This works most of the time, sometimes though a click on the MyLink portion of the TextView doesn't open the Activity but only scrolls the ScrollView in which my TextView resides to the top.
Where does this behaviour come from and how can I fix it?
If you are attempting to open a link that leads to your current activity, it might be recreating the same activity and gives the sensation that it's scrolling up. Probably you want to modify your manifest and set the activity's launchMode attribute to singleTask
Why dont you use TextViewWithLinks?
With that you can have two types of click on the textview,
1. On TextView
2. On Link TextView
String text = "bananas on www.bananas.ba and Google";
TextViewWithLinks textview = new TextViewWithLinks(this);
textview.setText(Html.fromHtml(text));
textview.linkify(new TextViewWithLinks.OnClickLinksListener() {
#Override
public void onTextViewClick() {
// Do whatever you want
}
#Override
public void onLinkClick(String url) {
// Do whatever you want
}
});
//SET Colors
textview.setLinkColors(Color.RED, Color.BLACK);
setContentView(textview);
Let me know if this is not resolving your issue.
Enjoy Coding... :)
Ok guys I've got a question that for the life of me I cannot get figured out. I'm using Eclipse, and I've got the Android ADT pluging all installed, the program configured with the Android SDK path, and all the extra SDK parts downloaded (Google API's, SDK Platforms, System Images). Here is my problem: whenever I go to access a method from a certain object, Eclipse gives no method "suggestions" like it usually does. I'm probably not explaining this the best, so I've attached a screenshot that kind of illustrates the issue.
public class StartingPoint extends Activity {
int counter;
Button add;
Button sub;
TextView display;
//putting add.whatever here doesn't work
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_point);
counter = 0;
add = (Button) findViewById(R.id.buttonAdd);
sub = (Button) findViewById(R.id.buttonSub);
display = (TextView) findViewById(R.id.textView1);
add.setOnClickListener(new View.OnClickListener() {
//putting add.whatever here doesn't show suggestions either.
I've tried everything I can think of to get it working, deleting & reinstalling eclipse, deleting everything Android from with Eclipse and going through all the SDK setup again, changing my workspace, creating an entirely new project, deleting the Eclipse preference file, endless Googling, resetting & altering settings, and I can't for the life of me figure out what's causing this. Any ideas? Thanks!
I decided to learn a little bit android progrramming and by using Eclipse + android plug-in I made an android "hello world" application. After that I tryed something simple and used 3 TextViews:
ID:TextView1 String-Value:A=5
ID:TextView2 String-Value:B=10
ID:TextView3 String-Value:A+B=C=
I created them in main.xml menu by drag and drop so, to show them I'm using the flowing line:
setContentView(R.layout.main);
The Question is: I want to get string values of A and B and append the result of 5+10 to the string value of C actualy I did it but it take too much codes just for get str value of a TextView and editing it. Is there any short way like in .Net?
string Str = This.Label1.Text();
And is eclipse is the best IDE or am I should use something else?
When I have several textviews etc, Why am I should write unnecessary lines if there is a short way to do it.
Another Question is I get str-value of B which is 10 but when I tryed the same thing for A it says there is nothing inside VarAStr
*My Codes are:*
setContentView(R.layout.main);
TextView VarAStr = (TextView)findViewById(R.id.VarA);
TextView VarBStr = (TextView)findViewById(R.id.VarB);
TextView VarC = (TextView) findViewById(R.id.VarC);
try{
int VarA=Integer.parseInt(VarAStr.getText().toString().substring(2));
int VarB=Integer.parseInt(VarBStr.getText().toString().substring(2));
VarC.append(String.format("%d", (VarA+VarB)));
}
catch(Exception e){
Toast.makeText(this, e.toString() + " -" + VarAStr.getText().toString() + "-", Toast.LENGTH_LONG).show();//Show();
}
For the last question I found a bad code line in main.xml :) wich has no android:text="#string/VarA" I added this and problem solved.
Thank you for any respond.
You can always put the repeated code on a function that returns a string.
Something like:
public String getTextOfView(int id) {
TextView tmp = (TextView)findViewById(id);
return tmp.getText.toString();
}