TextView with a cursor - android

I'm trying to create a custom TextView which should have a cursor.I don't want to use EditText since the input should not be provided via the android keyboard, moreover it should look like a TextView.
I've found that the TextView Interface has a support for a cursor, but I was unable to make it show. here is what I've tried:
added
android:textCursorDrawable="#null"
called
textView.setCursorVisible(true);
textView.setSelected(true);
textView.forceLayout();
textView.moveCursorToVisibleOffset();
when I added android:editable="true" and called textView.moveCursorToVisibleOffset(); NullPointerException was thrown within the textView.
any suggestions?

you can use Edit Text like this it shows like Text view:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"/>

Related

Android AppCompatEditText,AppCompatTextView, setText without ID

I have simple Login page with some textview and edittex and i set static text to textview and hint to edittext from string.xml working fine.
But Now
i am receiving that strings(which i set form stirng.xml as i explained above) form webservices and i have to set webservice string.
Issue is i didn't set any id to that login screen elements(edittext and textview).
Update
I have AppCompatTextView like below in my XML
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/_20sdp"
android:gravity="center"
android:text="#string/login_text_welcome"
android:textColor="#color/colorTextLightGrey"
android:textSize="#dimen/textview_size_medium"
android:textStyle="bold" />
I want to settext to above AppCompatTextView without ID.
Is it possible to setText to textview and edittext without setting any id?
You can use findViewByName rather than findViewById and then continue with your code.
Just include android:id="#+id/textView1 in your XML to set ID of an element.

Android EditText.setError not working in not focusable

How can I display a text error with setError in an EditText not focusable? It's important that users don't modify this EditText, it'll be modified for the application. Maybe I have another option different than focusable=false?
Now, I have it:
<EditText
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:ems="10"
android:focusable="false"
android:inputType="text"
android:onClick="onClick" />
Thanks a lot!
Finally I think it's not possible to do... Because the first necessity is to block the text, doing it with focusable=false or with a TextView, and it also blocks the functionality setError.
An editText with focusable = false I can get a right drawable (the default red exclamation mark) but without text. For this reason I finally added the text with a Toast.
It's not completelly that I wanted, but it's the most similar.
Thanks for your help!
Just use TextView and if you have an error somewhere then show image with drawableRight.
Here's the example of doingit programmatically: https://stackoverflow.com/a/7380789/3864698

Styling an Android edit text so as to look like a drop down / spinner picker

I have two EditText views on a layout containing a date and a time. On being clicked these open up my custom DatePickerFragment and a TimePickerFragments. I would like to style the EditTexts with a picker drop corner as the following image shows.
however I have not been able to do this, my text fields look like this:
The pickers all work and are hooked up and return values to the EditTexts, I just want to do the styling correctly. I am happy to change the class of the containers from EditText if they should be something else to get the desired behaviour. I think this should be possible as the first image comes from Google's own design documentation. Looking through the EditText and TextView class documentation and xml attibute documentation. Is there a way to do this?
I am using Xamarin to compile this down but that should make no difference to the answer.
set the Style attribute to the following:
<EditText
style="?android:attr/spinnerStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
use following image as background of your edittext
make it 9 patch with whatever colour you want.
find an original image and Create a nine patch image like below..
Note : Black Lines are patches
And then add image to background of EditText like below..
<EditText
android:id="#+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/box_edittext"
android:padding="10dp" />
OR
You can use this simple way...
Define style="?android:attr/spinnerStyle" to EditText..
<EditText
style="?android:attr/spinnerStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

How to keep cursor from going to editText field on activity start android

I am having a problem with text edits. I suppressed the keyboard from poping up on activity start, but how do I keep the cursor from being on the first edit text? I want the cursor to appear only when the editText field is clicked and when the activity starts.
<EditText
android:id="#+id/stuff"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/stufflabel"
android:layout_toRightOf="#+id/stuffpic"
android:hint="#string/name_str"
android:maxLength="20"
/>
Kindly search the forum for similar posts.
one post i found is -
Android: Force EditText to remove focus?
I hope you are looking for something similar !
enjoy :)
You can add a dummy EditText on inside layout of the XML file, so that the focus will get on that EditText and not on other EditText.
<EditText
android:layout_width="0dp"
android:layout_height="0dp" />

Android: How to make TextView editable?

How can I allow user to edit a TextView? Of course, I can use EditText instead, but I don't know how to customize it and also I've read in Android documentation that TextView can be editable. So I tried this:
<TextView android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="80sp"
android:text="MyText"
android:editable="true"
android:singleLine="true"
android:inputType="text"
android:focusable="true"
android:clickable="true"
android:cursorVisible="true"/>
But it still looks like common TextView. Does anyone know what I have missed? Or, may be, how to customize EditText for it look like TextView: without borders and background?
I know you don't want to use an EditText but it's really easy to make it look like a TextView.
<EditText
android:id="#+id/Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent" >
</EditText>
You can also use android:background="#null".
Edit:
The TextView's editable param does make it editable (with some restrictions).
If you set android:editable="true" you can access the TextView via the D-pad, or you could add android:focusableInTouchMode="true" to be able to gain focus on touch.
The problem is you cannot modify the existing text, and you cannot move the cursor.
The text you write just gets added before the existing text.
You can make your TextView editable by adding these lines
tv.setFocusable(true);
tv.setEnabled(true);
tv.setClickable(true);
tv.setFocusableInTouchMode(true);
You can fake a editable Textview. You just have to hide the textview when you touch it (make it "clickable"), replace it with an EditText, and display it again when the edit is over.
TextView defines all capabilities found on EditText, but doesn't have built-in support to them. Some main differences on EditText:
a) Method getDefaultEditable() returns true. This is only a mark that defines this subclass as editable.
b) A movement method. Is an object that control the cursor behavior (position, backward/forward moves - that may change in some languages, etc). In opposition, TextView just returns null, because is not cursor anyway.
c) Method CharSequence getText(). TextView returns a single String for that. EditText uses a specific char sequence implementation (Editable) that represents a mutable text buffer.
Because that, we can't think about TextView like a restrained EditText. TextView sketch the editoring interface, but not implement itself.
If you need a text component that you can switch off editing sometimes, you are looking for the EditText component.
tv.setCursorVisible(true);
tv.setFocusableInTouchMode(true);
tv.requestFocus();
tv.setEnabled(true);
I finally found the solution to your problem by creating the TextView programmatically
TextView textView = new TextView(context, null, android.R.attr.editTextStyle) {
#Override
public boolean getDefaultEditable() {
return true;
}
};
OR IN XML
<TextView
android:id="#+id/textView_ID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:style="#android:attr/editTextStyle"
android:editable="true"/>
Enjoy!

Categories

Resources