Make EditText behave as a TextView in code - android

How to make an EditText look like a TextView that is done in Java but not in XML..
I have seen how to do it using xml file like
style="#android:style/Widget.TextView"
android:editable="false"
android:cursorVisible="false"
android:longClickable="false"
but i want this to be done in java because i am not using any xml file for the layout..
everything is done in code itself ..
I am trying to use GestureListener in my code .. it worked fine for TextView but not EditText
So, anything to do for the EditText so that the GestureEvents can be implemented ?
Thanks,

EditText Et; // initialize your edittext//
Et.setCursorVisible(false);
Et.setLongClickable(false);
Et.setClickable(false);
Et.setFocusable(false);
Et.setSelected(false);
Et.setKeyListener(null);
Et.setBackgroundResource(android.R.color.transparent);
And you'll never see it like an EditText again.

add below two line into your edittext xml:
android:focusable="false"
android:inputType="none"

EditText is a subclass of TextView - so except for editing, what applies to TextView applies to EditText as well.

Make EditText behave as a TextView in code
try this
android:longClickable="false"
android:clickable="false"
android:cursorVisible="false"
android:editable="false"

Well I know this was a very old question, but I have to do this one of my apps and came up with a simple approach deduced from all the answers from Google/stack overflow. Suppose u want to make a edit text look like a text view and on button click make it editable, the procedure is as follows:
In your layout, set:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txt_postContent"
android:textSize="17sp"
android:gravity="top|left"
android:text="This is a dummy text "
android:layout_below="#+id/img_empimage"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:enabled="false"
android:background="#android:color/transparent"
style="?android:attr/textViewStyle" />
And in your .java set the color of the text by:
yourEditText.setTextColor(Color.parseColor("#000000"));
before entering the onClick of button. And in the onClick of button:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(postContent, InputMethodManager.SHOW_IMPLICIT);//shows keyboard
int position = postContent.length();
Editable text = postContent.getText();
yourEditText.setBackground(getResources().getDrawable(R.drawable.border));//shows a border around your text view making it look like a edit text
yourEditTex.setEnabled(true);//enables the edit text which we disabled in layout file
yourEditTex.setCursorVisible(true);
Selection.setSelection(text,position-1);`//places the cursor at the end of the text
Now to make the edit text show again as a textView, on the onClick of another button:
String s = postContent.getText().toString();
yourEditText.setText(s);
yourEditText.setEnabled(false);
yourEditText.setTextColor(Color.parseColor("#000000"));
yourEditText.setBackground(null);`
My answer covers two or three questions combined, but I hope it'll be helpful.

This is perfect way to make EditText as TextView.
Use in your EditText.
<EditText
android:id="#+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"
android:enabled="false"
android:focusable="false"
android:clickable="false"
android:longClickable="false"
android:inputType="none"/>

Just add an border to EditText :
First Of all add an border.xml in Resources/Drawable
<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="#ffffff" />
<stroke android:width="1dip" android:color="#000000"/>
</shape>
Second step :
add android:background="#drawable/border" to TextView
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:inputType="date"
android:layout_column="1"
android:id="#+id/dateFromTextView"
android:layout_marginRight="3dp"
android:background="#drawable/border"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_gravity="center"
android:gravity="center" />

Make EditText behave as a TextView in code
try this
In your layout, set:
<EditText
android:id="#+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:inputType="none"
android:imeOptions="actionDone"
android:enabled="false"
style="?android:attr/textViewStyle" />

This was all I needed to create the effect.
In code:
ET.setFocusable(false);
And in layout:
android:background="#android:color/transparent"
Setting the background in the layout completely removes the under bar, as apposed to just hiding it in code.
Make it editable again with:
ET.setFocusableInTouchMode(true);

Related

How to create custom edittext with button in android?

How can I create this kind of edittext with button on android.
I've done relative layout but it just won't look very presentable.
Thanks ahead.
Use android:drawableRight property of EditText.Code as
<EditText
android:id="#+id/edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/enter_the_value"
android:drawableRight="#drawable/ic_launcher" />

completely transparent EditText

i want to transparent an edittext in both Background and textColor sides. so i used this lines in my java code:
tempEditText.setTextColor(Color.TRANSPARENT);
tempEditText.setBackgroundColor(Color.TRANSPARENT);
but unfortunately this tempEditText object shows when i start typing any character on it. i want it to be completely invisible when i typing somthing to it. so i want it to be focused, get some texts, and i want the ability of retrieving strings that typed on it, but all in invisible state of edittext. i test visibility=invisible but in this situation the edittext cant give texts...
<EditText
android:id="#+id/tempEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00000000"
android:textColor="#00000000"
android:singleLine="true" />
any tips?
thank you...
Try:
android:background="#android:color/transparent"
or
android:background="#null"
Edit: show the virtual keyboard:
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(exampleView.getWindowToken(), 0);
Change EditText background android:background="#00000000" or android:background="#null" in your xml file.
For Text Color android:textColor="#00000000" in xml file.
Try this,
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alpha="0">
or programmatically you can set alpha to your EditText
editText.setAlpha(0.0f);
Note: The issue is user will not able to see cursor!
I did this in my XML File :
android:textCursorDrawable="#null"
android:textColor="#android:color/transparent"
android:background="#null"
Make the EditText background attribute as #bb000000 for making it transparent.
If you are on Eclipse use the GUI to do it, or do this in your XML file android:background="#bb000000".
<EditText
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#null"
android:id="#+id/search_input"
app:layout_constraintLeft_toRightOf="#+id/filter"
app:layout_constraintRight_toLeftOf="#+id/search_button"
/>
use this line,then it will obviously work
android:background="#null"

How to make a part of text clickable like button in Android?

I would like to add button myButton to TextView. How to get it in a single line so that it looks like one set of lines. Here is sample of the text to be added
Please press this here to refresh the set of values.
I want to have "here" as clickable button. How can I do it. Any snippet on it would be helpful.
Set background of Button as null by android:background="#null" give the same text color as given to other TextView's.
or You can take 3 TextView's and setOnClickListener to middle one.
Set style of Button with borderless android attribut to remove background and border:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/borderlessButtonStyle"
android:text="here" />
make three textView ex: textView1, textView2 and textView3.
<RelativeLayout android:id="#+id/textLay"
android:layout_width="fill_parent" android:layout_height="wrap_content"
>
<TextView android:id="#+id/textView1" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please press this"/>
<TextView android:id="#+id/textView2" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="here"
android:layout_toRightOf="#+id/textView1" />
<TextView android:id="#+id/textView3" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="to refresh the set of values."
android:layout_toRightOf="#+id/textView2" />
</RelativeLayout>
Just add below attribute to TextView in XML layout file to make it clickable.
android:clickable="true"
In your Java file add OnClickListener to complete click action on text view.
Add android:clickable="true" and android:onClick="yourclickname" for the TextView in the XML. Then define yourclickname in your activity. This should make your TextView clickable and triggers the specified method when clicked.

Android EditText look

I wanna make the editText to look like this:
what else should I do after I set the editText's background how can I make the text start inside the edittext via code.
Are you trying to get the EditText to say "Username" by default, but not have that be the actual text?
For that you can use EditText.setHint() (inherited from TextView) or in the xml definition use android:hint, such as
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Username" />
To make text look like that, set the Gravity attribute to left. The gravity of EditText element.
As well as setting the background of the EditText view, set left and right padding, e.g.
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/edittext_bg"
android:paddingLeft="7dp"
android:paddingRight="7dp" />

Allow multi-line in EditText view in Android?

How to allow multi-line in Android's EditText view?
By default all the EditText widgets in Android are multi-lined.
Here is some sample code:
<EditText
android:inputType="textMultiLine" <!-- Multiline input -->
android:lines="8" <!-- Total Lines prior display -->
android:minLines="6" <!-- Minimum lines -->
android:gravity="top|start" <!-- Cursor Position -->
android:maxLines="10" <!-- Maximum Lines -->
android:layout_height="wrap_content" <!-- Height determined by content -->
android:layout_width="match_parent" <!-- Fill entire width -->
android:scrollbars="vertical" <!-- Vertical Scroll Bar -->
/>
You may find it better to use:
<EditText
...
android:inputType="textMultiLine"
/>
This is because android:singleLine is deprecated.
This works for me, actually these 2 attributes are important: inputType and lines. Besides, you may need a scrollbar, the code below shows how to make one:
<EditText
android:id="#+id/addr_edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top|left"
android:inputType="textEmailAddress|textMultiLine"
android:lines="20"
android:minLines="5"
android:scrollHorizontally="false"
android:scrollbars="vertical" />
This is how I applied the code snippet below and it's working fine. Hope, this would help somebody.
<EditText
android:id="#+id/EditText02"
android:gravity="top|left"
android:inputType="textMultiLine"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:lines="5"
android:scrollHorizontally="false"
/>
Cheers! ...Thanks.
EditText has singleLine property. You can set in the XML or by calling setSingleLine(false);
http://developer.android.com/reference/android/widget/TextView.html#setSingleLine%28%29
Try this,
add these lines to your edit text view, i'll add mine. make sure you understand it
android:overScrollMode="always"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical"
<EditText
android:inputType="textMultiLine"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText_newprom_description"
android:padding="10dp"
android:lines="5"
android:overScrollMode="always"
android:scrollbarStyle="insideInset"
android:minLines="5"
android:gravity="top|left"
android:scrollbars="vertical"
android:layout_marginBottom="20dp"/>
and on your java class make on click listner to this edit text as follows, i'll add mine, chane names according to yours.
EditText description;
description = (EditText)findViewById(R.id.editText_newprom_description);
description.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
view.getParent().requestDisallowInterceptTouchEvent(true);
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK){
case MotionEvent.ACTION_UP:
view.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
return false;
}
});
this works fine for me
android:inputType="textMultiLine"
This code line work for me. Add this code you edittext in your xml file.
Just add this
android:inputType="textMultiLine"
in XML file on relevant field.
if some one is using AppCompatEditText then you need to set inputType="textMultiLine" here is the code you can copy
<androidx.appcompat.widget.AppCompatEditText
android:padding="#dimen/_8sdp"
android:id="#+id/etNote"
android:minLines="6"
android:singleLine="false"
android:lines="8"
android:scrollbars="vertical"
android:background="#drawable/rounded_border"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/_25sdp"
android:layout_marginTop="32dp"
android:layout_marginEnd="#dimen/_25sdp"
android:autofillHints=""
android:gravity="start|top"
android:inputType="textMultiLine"
/>
for background
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="#dimen/_5sdp"/>
<stroke android:width="1dp" android:color="#C8C8C8"/>
</shape>
All of these are nice but will not work in case you have your edittext inside upper level scroll view :) Perhaps most common example is "Settings" view that has so many items that the they go beyond of visible area. In this case you put them all into scroll view to make settings scrollable. In case that you need multiline scrollable edit text in your settings, its scroll will not work.
To disable number of lines that was previously assigned in theme use xml attribute: android:lines="#null"
<EditText
android:id="#id/editText" //id of editText
android:gravity="start" // Where to start Typing
android:inputType="textMultiLine" // multiline
android:imeOptions="actionDone" // Keyboard done button
android:minLines="5" // Min Line of editText
android:hint="#string/Enter Data" // Hint in editText
android:layout_width="match_parent" //width editText
android:layout_height="wrap_content" //height editText
/>
I learned this from http://www.pcsalt.com/android/edittext-with-single-line-line-wrapping-and-done-action-in-android/, though I don't like the website myself. If you want multiline BUT want to retain the enter button as a post button, set the listview's "horizontally scrolling" to false.
android:scrollHorizontally="false"
If it doesn't work in xml, doing it programmatically weirdly works.
listView.setHorizontallyScrolling(false);
Another neat thing to do is to use android:minHeight along with android:layout_height="wrap_content". This way you can set the size of the edit when it's empty, and when the user inputs stuff, it stretches according to how much the user inputs, including multiple lines.
<EditText
android:hint="Address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textMultiLine|textNoSuggestions"
android:id="#+id/edittxtAddress"
android:layout_marginLeft="#dimen/textsize2"
android:textColor="#android:color/white"
android:scrollbars="vertical"
android:minLines="2"
android:textStyle="normal" />
and in Activity to remove "\n" is user press nextline
String editStr= edittxtAddress.getText().toString().trim();
MyString= editStr.replaceAll("\n"," ");

Categories

Resources