TextView with a single, editable line - android

I'd like to do textual back-and-forth interaction in an Android control. The idea is to have something like this:
This is some text output by the program.
What is your name? |
with the cursor at | (note that editing doesn't start at the beginning of the last line). The user is then free to enter text (using whatever Android input method, keyboard, etc.) but isn't allowed to change any of the output so far. Ideally, the user's input would be styled differently.
Then, as soon as newline is entered, I want the program to be notified and editing to stopped:
This is some text output by the program.
What is your name? Foo Bar
Hello, Foo Bar!
Note that this needs to be a proper control, i.e. one I can compose with other controls to make it just one part of the app's main layout.

Make a TextView and the EditText next to each other then your problem is solved and add the following line of code in EditText.
android:singleLine= 'true';
It allow only one line to be entered to the EditText. let me know whether this is what your expecting.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="What is your Name?"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:id="#+id/editText"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/textView" />
</RelativeLayout>

This is some text output by the program.
What is your name? |
with the cursor at | (note that editing doesn't start at the beginning
of the last line). The user is then free to enter text (using whatever
Android input method, keyboard, etc.) but isn't allowed to change any
of the output so far. Ideally, the user's input would be styled
differently.
I would strongly recommend to rethink about your design as the same thing can be done with the help of LinearLayout,Editext,TextView with very simple and more manageable way.

I would suggest you to create a new LinearLayout(TextView + EditText) and assign the background of layout like EditText and edittext's no background.
Upon editText done, you could show a new TextView in the bottom

You need a ListView at top, to show your conversation & then below it, needs a horizontal view with a TextView (to show question) and EditText(with background transparent - to ask user to fill an answer).

Related

Android EditText Multiline does not work as it should

I'm pretty desperate about this feature.
I tried pretty much everything there is to find to made these EditTexts multiline enabled,
but they just keep going on a single line scrolling the entire EditText with it.
How hard can it be to stop at the end of the border of the EditText and move to the next line?
I have this activity with an EditText and 2 buttons. One of these buttons adds a predetermined line of text to the EditText. The other puts the EditText's text into some form of object that I use later in the app.
However I can't get this multiline feature to work.. I've tried limiting the size. Setting the multiline flag. Disabling singleline. Giving lines, and minLines a random number (10).
Disabling horizontalscroll on the EditText. But nothing works....
Can anyone tell me what the hell I'm doing wrong? And how I can fix this horrid abomination of an EditText.
This is how my nightmare looks like now.
<EditText
android:id="#+id/callofedittext"
android:layout_width="wrap_content"
android:inputType="textMultiLine"
android:width="300dp"
android:minLines="10"
android:layout_height="wrap_content"
android:gravity="top|left"
android:textColor="#color/textWhite"
android:background="#color/textBlack"
android:paddingLeft="3dp"
android:singleLine="false"
android:scrollHorizontally="false"
>
<requestFocus />
</EditText>
It haunts my dreams...
EDIT: > Light at the end of the tunnel.
While I was focussing on the xml.. A new clean project pointed out to me that EditText textMessage = (EditText)findViewById(R.id.callofedittext); textMessage.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES); is causing all of my problems. Not specifically the properties inside the xml.
From this comment, the inputType was set in the code as well with:
textMessage.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE |
InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
This is actually not correct, because TYPE_TEXT_FLAG_MULTI_LINE and TYPE_TEXT_FLAG_CAP_SENTENCES are only flags, and do not contain the actual input type. In order for them to work, they must be layered as flags on top of InputType.TYPE_CLASS_TEXT. Without this type class flag, the edit text does not have a base input type class to apply your flags to, and defaults to no specified input type.
So, the correct way to set the input type with both of these flags is:
textMessage.setInputType(InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_FLAG_MULTI_LINE |
InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
For official details on how these flags work, see the Android Developer Docs on InputType and TextView - android:inputType
I'm not sure why the design decision is this. Personally, I think they should have hidden how they are representing their flags (as ints/bit flags), and instead had enums and/or subclasses of InputType for their public interface.
hey you have to add the following code in xml file ..
android:gravity="top"
android:maxLines="4"
android:inputType="textMultiLine"
android:scrollbars="vertical"
android:textSize="16sp"
android:padding="10dp"
and you have to put activity file ...
edtComment = (EditText) findViewById(R.id.edtComment);
edtComment.setMovementMethod(new ScrollingMovementMethod());
this is works for me and hope it will works for you .....
Have you tried using
android:layout_height="wrap content"
instead of
android:layout_height="match_parent"

Difference between setText() and append()

I'm curious about the difference setText() and append() are creating. I'm writing a very basic editor with line numbers. I have a TextView to hold line numbers on the left, paired with an EditText on the right to hold the data. Here's the XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top">
<TextView
android:id="#+id/line_numbers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="0dip"
android:gravity="top"
android:textSize="14sp"
android:textColor="#000000"
android:typeface="monospace"
android:paddingLeft="0dp"/>
<EditText
android:id="#+id/editor"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="text|textMultiLine|textNoSuggestions"
android:imeOptions="actionNone"
android:gravity="top"
android:textSize="14sp"
android:textColor="#000000"
android:typeface="monospace"/>
</LinearLayout>
Ignoring some of the other things I'm doing, the most curious thing I came across was the extra spacing that showed up when I used append() (assuming things have been initialized and all that).
This below, in combination with the XML, sets a flush border between the TextView and EditText.
theEditor = (EditText) findViewById(R.id.editor);
lineNumbers = (TextView) findViewById(R.id.line_numbers);
theLineCount = theEditor.getLineCount();
lineNumbers.setText(String.valueOf(theLineCount)+"\n");
Change the last line to this, though, and suddenly each line in the TextView has padding on the right before the EditText.
lineNumbers.append(String.valueOf(theLineCount)+"\n");
It's not the end of the world. but I was curious what was causing this behavior. Since I'm new to the language, the only thing I could think of was maybe, when append throws the Editable on there, it adds the padding. If I can get an answer, I get to replace all of these nasty lines with simpler appends:
lineNumbers.setText(lineNumbers.getText().toString()+String.valueOf(newLineCount)+"\n");
lineNumbers.setText("It is test,");
//Here lineNumbers have It is test
lineNumbers will have "It is test,". After that, if you use setText again, text will completely change
lineNumbers.setText("It is second test,");
//Here you'll lose first text and lineNumbers text will be "It is
second test,"
After that, if you use append, lets see what will happen..
lineNumbers.append("It is third test,");
// Here you will not lose lineNumbers text.. It will be like this
"It is second test,It is third test"
setText(): Destroys the buffer content by filling the text to be set.
append(): Adds a text to a buffer and then prints the result.
Example: example.setText("Hello"); would print Hello on the output screen. If you then execute example.append("World"); you would get HelloWorld as the output.
setText will replace the existing text with new text.
From Android doc:
Sets the text that this TextView is to display (see setText(CharSequence)) and also sets whether it
is stored in a styleable/spannable buffer and whether it is editable.
append will keep the old text and add the new one more like concatenating.
From Android Doc
Convenience method: Append the specified text to the TextView's display buffer, upgrading it to
BufferType.EDITABLE if it was not already editable.
I think changing BufferType to EDITABLE by append method caused the unexpected padding.
If you want to use append method instead of setText method and remove that padding,
you can try to remove it by using
textView.setincludeFontPadding(false)
or adding this line to your textview in your xml file
android:includeFontPadding="false"
Hope this helps.
The basic difference is that setText() replaces all the text from the existing one and append() adds your new value to existing one. Hope i helped.

Dynamic table layout - rows and data changes on each run

In my android application I will show user to enter some input values (in main layout) and when presses a button it fetches data from a website(uses AsyncTask). And after fetching i want to display output in a separate layout.
Layout should be like this:
----------------------------
Status: Success
----------------------------
Details are as follows:
----------------------------
Name of cust. ABCDEFG
Age 16
Total days 365
Present 300
Absent 65
Salary 10000
etc..
----------------------------
BACK
----------------------------
For first two rows, a LinearLayout with TextViews in it is in my mind. For last button "BACK", it is going to be displayed in a LinearLayout. For details i am going to use TableLayout. But this details section is dynamic. that is number of rows varies. When user presses "BACK" button he can do a new fetching and details of new fetch will again be displayed.
I am going to use ScrollView for details section to allow users to view contents if there is larger rows. So I can create the layout dynamically by using code. But I donot know what to do when user presses BACK button and again fetches. Will this already created design stays in memory ? Or any better approach or suggestions ? Also the text in first column of the details section may contain larger texts. So what to do there?
I would advice against the use of tableview, have had several problems with it in the past, and in your case a relative layout will do just as well. Example below highlights what you would need for every row of your details.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="TextView" />
</RelativeLayout>
Regarding larger test, assuming the text is long, and not just large font size, you can add
android:ellipsize="end"
to you TextView to make it cut the text if its getting to long, and end it with "...". For that to work you need to tell the first textView1 in the example above to:
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/textView2"
and if you dont want the text cut, just remove the android:ellipsize line.

Android Text Input Special UI Item

I'm creating an IM client/server application for the Android OS, and am currently putting together the user interface. Right now, my interface consists of a EditText element, and a Button element. When I tap on the EditText element, a keyboard pops up and allows you to type.
What I would like to have is something like the text entry area and send button in the default Android SMS app. Something like this:
The text input field and Send button would stay at the bottom of the screen, and when tapped on, the keyboard would push the text field and button up above it.
Is this possible using only EditText and Button elements?
Thank you for any suggestions or advice!
Try setting android:windowSoftInputMode=adjustResize for the activity in AndroidManifest.xml.
You can find details here.
Is this possible using only EditText and Button elements?
Answer-This type of functionality is possible in any type of view
I give just short tutorial on your question
Generally we use only linearlayout in xml file.But at view level android gives many more feature like Relative layout and much more.At this time we just discuss about the relative layout because it can solve your purpose.
In Relative layout it not use the android:orientation feature like linear layout it used another feature.In relative layout take some points in your mind...
we always give id to every view using android:id="#+id/GiveName"
for alignment of any view we used android:layout_toLeftOf="#id/givesname" same for
right,above and below where givesname=id of that view from which this view is align.
Ex. is gives example with screen shot
After this i give the sample xml file in which you get the above type of feature in your question
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llWriteCommentWall" android:layout_height="fill_parent"
android:layout_width="fill_parent" android:background="#ffffff">
<Button android:id="#+id/btButtonComments"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Comments"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"/>
<EditText android:id="#+id/etEdittext"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:hint="Write a comment.... "
android:layout_marginLeft="2dip" android:layout_marginRight="2dip"
android:layout_toLeftOf="#id/btButtonComments"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
ScreenShot of above example
In this Example we used android:layout_alignParentBottom="true" - this attribute is the main reason for view like this,it always align any view in bottom even softkeyboard is shown.it contain boolean value,true gives always align bottom and false nothing.
the other relative attribute is android:layout_alignParentRight="true",android:layout_alignParentLeft="true" ,android:layout_alignParentTop="true"-all attribute give feature as written.
Lastly you include this xml file at any java file through setContentView(xmlFileName)

Android - customized keyboard key and action

If you own Android phone you are no doubt have noticed how in the certain apps the keyboard layout can change from the standard issue to digits-only or to have .com or .net special buttons based on the text field input type (e.g. phone number). So I have 2 questions:
how to trigger this customization? I suspect it has to do with EditText format
Can this be taken even further if I want to add some custom buttons to inject a specific pattern? Say I would have an AND button which when pressed will add all uppercase " AND " surrounded by spaces to the text field. Can this be done?
What I'm not asking is how to capture some key combination in onKeyPress event and then populate text field with a pattern - I pretty much know how to do that already.
It is controlled by the android:inputType XML attribute (or the setInputType() method).
For info on the available options see the pages for the XML attribute or the object's method.
As an example, the following XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<EditText
android:text="example text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone" />
</LinearLayout>
will give you this layout:
whereas changing the inputType to textEmailAddress will give you this:
You can customize the "action" button as explained here, but I don't believe there's any way to do full customization of keyboards at this time, but I could be wrong.
The thing that concerns me is that "inputType" is listed as a deprecated property, meaning it may work for a while, but - eventually - Android will stop supporting it. Is there another alternative?
UPDATED: My bad - I'm confusing with inputMethod.

Categories

Resources