How to use an EditText as an indicator - java

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.

Related

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);

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.

Set Text in Android

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.

paste option for edittext

I have an edittext and I would like to paste some text in it. I can copy the text from some web page but I am not able to paste the text in my edittext control.How can I enable my edittext to paste some text.Here is my main.xml for edittext ;
enter code here
<EditText
android:id="#+id/enter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight= "2"
android:scrollbars="vertical"
android:textColor="#color/black"
/>
Thanks
This is on Android 4.4.2 Samsung S4;
Documentation for TextView says that:
To allow users to copy some or all of the TextView's value and paste
it somewhere else, set the XML attribute android:textIsSelectable to
"true" or call setTextIsSelectable(true). The textIsSelectable flag
allows users to make selection gestures in the TextView, which in turn
triggers the system's built-in copy/paste controls.
There is also another Textview attribure called android:cursorVisible which determines if the system should be invoked about the copy/paste callbacks.
By default I believe both of these are true and selection/copy/paste mechanics are already enabled. I could not change that behaviour by using android:textIsSelectable="false" but if I set android:cursorVisible="false" initially you can't paste anything inside the EditText. Only after you type something in, cursor and selection behaviour becomes enabled again. Maybe this should be handled inside the code rather than in the layout xmls, or it might be related to android:inputType which also did not make a difference for me.
So try setting android:cursorVisible="true" in your EditText's layout xml if paste is not enabled by default.
According to your problem if you copied some data any where in your system and you want to paste it in some specific variable, like Edit TextBox, Textview etc, then this code will surely help you.
ClipboardManager clipMan = (ClipboardManager)getSystemService(v.getContext().CLIPBOARD_SERVICE);
myEdtTxt.setText(clipMan.getText());
Note:- here the clipMan object will store the data whenever copied process take place and we will return that data from that object and will set it,
To enable the standard copy/paste for TextView, U can choose one of the following:
Change in layout file:
Either add below property to your TextView
android:textIsSelectable="true"
and In your Java class write this line to set it programmatically.
myTextView.setTextIsSelectable(true);
if fragment try with
mContext.myTextView.setTextIsSelectable(true);
And long press on the TextView you can see copy/paste action bar.
Try setting the inputType="text" for the EditText field

How to programmatically set edit text hint in android?

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"

Categories

Resources