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.
Related
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);
In an XML file, a textview was being made, now I want to edit it's text property from the .java source file. I tried referring to its id, but eclipse wont recognize it; "cannot be resolved"
relevant part from XML:
<TextView
android:id="tb_01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
from the source:
Intent intent = getIntent();
String text = intent.getStringExtra(text);
tb_01.setText(text);
also, could someone explain, what this does in an xml, within a editText tag:
android:id="#+id/edit_message"
You need to provide id like...
<TextView
android:id="#+id/tb_01"
^^^^^^^^^^
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
You can find difference between #id and #+id/ at here.
From java code reference it using this way..
Intent intent = getIntent();
String text = intent.getStringExtra(text);
TextView tb_01= (TextView) findViewById(R.id.tb_01);
tb_01.setText(text);
The id should be like this
<TextView
android:id="#+id/tb_01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
Then you will be able to get your TextView
yOU can get more information about these id's from the following link
Difference between “#id/” and “#+id/” in Android
R.id
Get the Textview and set the text:
TextView tv = (TextView) findViewById(R.id.tb_01);
tv.setText("your text");
The
"#+id/edit_Message"
just creates a new resource id, which can be used to access that element programatically. Like "findViewById()" from above sample.
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.
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.
Is there a way to set the text hint displayed inside of an android edittext with java? I would like for my app to be able to display different things in the edittext at different times.
Did you try the setHint?
myTextView.setHint("My conditional hint");
Hope it helps..
If you are using simple Editext without TextInputLayout :
EditText etUsername;
etUsername = (EditText) findViewById(R.id.etUsername);
etUsername.setHint("Your Hint");
If you are using Editext within TextInputLayout then you need to change property of TextInputLayout not Edittext:
TextInputLayout layoutUser;
layoutUser = (TextInputLayout) findViewById(R.id.inputLayoutUname);
layoutUser.setHint(getResources().getString(R.string.hintFaculty));
Call setHint() on the EditText.
((TextView) findViewById(R.id.yourEditTextId)).setHint("hint text");
Kotlin
editText.hint = "Your hint"
if you are using actionbarsherlock searchview, this is the best way to set string dynamically
AutoCompleteTextView searchTextView =
(AutoCompleteTextView) mSearchView.findViewById(R.id.abs__search_src_text);
searchTextView.setHint(getResources().getString(R.string.search));
In Kotlin
val layoutMobile: TextInputLayout = findViewById<View>(R.id.inputLayout_MobileNumber) as TextInputLayout
layoutMobile.hint = "Your Hint"
for editText.hint you must delete name of your edit.Text from activity_main.xml then set in the MainActivity in java or activity_main.xml because text is preferred to hint.
<EditText
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine|textLongMessage"
android:hint="\n"/>
To be honest I don´t understand why.. but including "\n" as hint in the xml did the trick!
Now you can call setHint from Java code:
searchEditText.setHint(getString(R.string.search_hint));
and remove the \n from strings.xml if you want to allow the text split automatically according to the room
<string name="search_hint">keyword, location, max price (e.g. 1K, 1M)</string>
in Java
yourEditTex.setText("");//First
yourEditTex.hit("Your text");
this for alls cases.
You can try following,
in Java
yourEditText.setHint("hint");
in Kotlin
yourEditText.hint = "Your hint"