Android AppCompatEditText,AppCompatTextView, setText without ID - android

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.

Related

Android detect and clickable web address in textview

I am working on textview, I want to do that if there is any web link in textview so it will detect that and also clickable. How can I achieve that?
There are two main ways - xml which does most work for you (see #Massimo answer) and code which is very flexible, it allows you to make some text clickable and intercept link clicks (see LinkMovementMethod)
Set the autoLink and linksClickable attributes in your xml's TextView
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:linksClickable="true"
android:autoLink="web"/>

how to add android:labelfor to multiple text fields

android:labelFor="#+id/tip10et"
android:labelFor="#+id/tip15et"
android:labelFor="#+id/tip20et"
it is showing error android:labelfor is already specified for one text field
labelFor is an accessibility attribute.
If you have a textview that serves at the label for another field ( say an edittext )
you can reference the id of the edit text in the labelfor of the textview.
android:labelFor
Specifies the id of a view for which this view serves as a label for accessibility purposes. For example, a TextView before an EditText in the UI usually specifies what infomation is contained in the EditText. Hence, the TextView is a label for the EditText.
You can't do that as is. You should use multiple label views, each one should have an android:labelFor to their respective labeled View.
BTW: Could use the same string resource for all of them.
Example (minimalist)
<ImageView android:id="#+id/img1"/>
<TextView android:text="#string/sametext"
android:labelFor="#id/img1" />
<ImageView android:id="#+id/img2"/>
<TextView android:text="#string/sametext"
android:labelFor="#id/img2" />
<ImageView android:id="#+id/img3"/>
<TextView android:text="#string/sametext"
android:labelFor="#id/img3" />

TextView with a cursor

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"/>

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!

Set ImageButton text from the code?

In examples found on the net I saw that the text is set from XML file only. I need to attach the text from another View, and I tried to find any setter that I can use to set text to ImageButton. I didn't succeed. I even tried using this
<ImageButton
android:background="#ffffff"
android:text="setText()"
/>
hoping that I can use setText() in the code, but it did not work as well.
How can I set the text for ImageButton programmatically?
Thanks
PS. This is a custom ImageView which inherits ImageView.
ImageButtons can't have text (or, at least, android:text isn't listed in its attributes). It looks like you need to use Button (and look at drawableTop or setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)).
You cannot set text to ImageButton because it has no method as setText() or android:text property.
Here is workaround, using a Button and android:drawableTop / Left / Right or Bottom like this :
<Button android:layout_height="wrap_content"
android:drawableTop="#drawable/icon" android:text="Button"
android:layout_weight="1" android:layout_width="fill_parent"
android:textSize="11sp"
android:layout_margin="1sp" />

Categories

Resources