Set Text in Android - java

I am new to Android programming and so kingly pardon me if this question sounds stupid. I am developing a Calculator for Android. So I have around 20 buttons and a text field. Whenever a button is pressed I update the text field so that the user knows what he has typed.
However when I update the textfield the application is crashing.
updateExpression is the method that is called when any button is pressed.
public void updateExpression (View v)
{
Log.d("string", "updateExpression Called");
EditText text = (EditText) findViewById(R.id.name);
text.setText("HELLO EVERYONE");
}
The .xml file of the buttons is:
<Button
android:id="#+id/Button03"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/name"
android:layout_below="#+id/name"
android:onClick="updateExpression"
android:text="C" />
// goes on for 20 buttons with different ids and text
<TextView
android:id="#+id/name"
android:layout_width="fill_parent"
android:text="#string/edit_message"
android:layout_height="33dp"
android:textColor="#color/opaque_red"/>
</RelativeLayout>
Finally the logcat has the string data "updateExpression Called". However immediately after that it crashes. The error has tag AndroidRuntime Fatal Exception main. There are many more errors after this. A relevant one (according to me is)
Caused by: java.lang.classCatException: android.widget.TextView
cannot be cast into android.widget.EditText
Kindly help.
Thank you.

You are trying to cast a TextView into an EditText, like the error says. These are two seperate classes.
EditText text = (EditText) findViewById(R.id.name);
should be
TextView text = (TextView) findViewById(R.id.name);

In your xml its a TextView In your code
EditText text = (EditText) findViewById(R.id.name);
Its a EditText. So choose one. This depends on your needs. If the user is able to type in this View, then use EditText. If its read-only and you will be handling these text, choose TextView.

Related

How to show Error Popup for EditText correctly

My question is about Android/Java.
My main.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="wrap_content"
android:ems="5"
android:layout_height="wrap_content"
android:id="#+id/mainEditText1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:id="#+id/mainButton1"/>
</LinearLayout>
My MainActivity.java:
b.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
e.setError("SO - Please enter a correct E-Mail!!!");
}
});
My picture:
As you can see, my problem is the following: The error pop-up is larger than the width of the EditText.
But I want the error pop-up to be the same width or less than the EditText (containing several lines).
How can achieve this? How can I show the error pop-up correctly and not as my image shows?
Change your LinearLayout to a ConstraintLayout. My guess is its adding the error message to the next available place in the layout. If using LinearLayout in horizontal mode it will put it to the right of the button.
OR
Another good answer is to wrap your EditText in a TextInputLayout and set the error of the TextInputLayout and not the error of the EditText.
I got that from here
How to fix EditText error possition in LinearLayout
Although the answer has no votes i do believe its the right one.

Text output on an AndroidApp

I'm trying to program an android App with AndroidStudio. It is like an calculator and i don't know how to print the answer on my screen. I tried it with System.out.println() but it doesn't work Log._() doesn't work too.
You can display a temporary pop-up message to the screen using a Toast as below:
Toast.makeText(context,"<Message here>", Toast.LENGTH_LONG).show();
If you want the text to persist, then use a TextView object. Add it to your .xml for that activity:
<TextView
android:id="#+id/myTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Then declare it in your Activity screen:
TextView myTextView = (TextView) findViewById(R.id.myTextView);
Then set the text using .setText(), probably in the onClick() method of whatever button you're using to calculate the result for the user:
myTextView.setText(calculatorResult);

Android Single line textView (for a calculator app) that takes input from buttons, is this an issue?

I am a beginner trying to make a calculator app in Android Studio that takes input from buttons. This is proving to be much more difficult than I thought it would be compared to just using EditText, but it has been a great learning experience so far.
I am working on the display portion of the app for which I am using a textView. I have it set up so that when a button is clicked, the button's value is appended to the textView. It's a calculator, so I want the textView to be displayed on a single line and shift horizontally for new input when the width of the textView is full.
I found a way to do this from another person's post that works by setting the following in the XML:
android:inputType="text"
android:maxLines="1"
When I did this it does exactly what I want, but I get an IDE warning that says:
Warning: Attribute android:inputType should not be used with <TextView>: Change element type to <EditText> ?
Without the android:inputType="text" piece of code, the textView doesn't seem to scroll properly and I don't want to use an EditText. Is this something to worry about? Is there a better way to do this?
Thanks for the help.
Do not use inputType in TextView.
From this link - https://youtu.be/HYt1Ntu89X4
What I understood is you want the text to scroll dynamically i.e. on Button press.
You can do it like this :
XML
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/hzw"
android:fillViewport="true"
android:layout_gravity="end">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv1"
android:maxLines="1"
android:textSize="20dp"
android:textStyle="bold"
android:gravity="end"/>
</HorizontalScrollView>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/b"
android:text="Add Text"/>
Java
tv1 = (TextView)findViewById(R.id.tv1);
tv1.setText("123+123");
hzw = (HorizontalScrollView)findViewById(R.id.hzw);
b = (Button)findViewById(R.id.b);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
tv1.append("d+1+cff");
hzw.post(new Runnable() {
#Override
public void run() {
hzw.fullScroll(View.FOCUS_RIGHT);
}
});
}
});

How to get EditText by xml id Java Android SDK

I am new to the android sdk. I have this weird gui thing and I made a Button and an EditText inside of it. I think I properly set up the onClick stuff for the button, but how to I get the text inside of the EditText with the id of editText1 from what it says inside the xml file? Here is my EditText xml element inside of the xml file:
<EditText
android:id="#+id/editText1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/button1"
android:ems="10"
android:hint="#string/test"
android:inputType="textMultiLine|none" >
<requestFocus />
Thanks! Sorry that I am so new to Android development. None of the things I have searched make an sense to me. I keep seeing stuff about the "R" class and I have no idea what that means.
Starting learning from https://developer.android.com/training/
In onCreate() of your source code say MainActivity.java
Try this!
final EditText et=(EditText)findViewById(R.id.editText1);
in onClick() listener of your button, try String ss=et.getText().toString();
Hope it helps!
To get the text inside the EditText do that:
EditText editText = (EditText) findViewById(R.id.editText1);
String yourText = editText.getText().toString();
To get the text entered inside of your EditText, you would do the following inside of your onClick:
EditText et = (EditText)findViewById(R.id.editText1);
String words = et.getText().toString();
Then do whatever you want with the text with the variable.

How to use an EditText as an indicator

I want to use an EditText as an indicator to show values, which i receive from a remote device. So the text in the EditText must not be editable. How to do that ?.
Use TextView isntead. That is what TextView is for. If you read its documentation it says: It displays text to the user and optionally allows them to edit it. A TextView is a complete text editor, however the basic class is configured to not allow editing.
Hope this helps.
For what it sounds like you're trying to use it for, a TextView would be more appropriate, as others have suggested.
However, if you're set on using an EditText for a particular reason, you can achieve what you're looking for by disabling the EditText. This greys it out and prevents the user from editing the text. You can do this either statically (in XML) or dynamically (in Java).
XML:
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:text="This is disabled." />
Java:
EditText editText1 = (EditText) findViewById(R.id.editText1);
editText1.setEnabled(false);
What you are looking for is TextView. Set any text you want. It is un-editable.
TextView myTextView;
#Override
public void onCreate()
{
....
myTextView = (TextView) findViewById(R.id.textView);
myTextView.setText("Hello world");
}
For further info LINK.

Categories

Resources