I placed an edit text within a scroll view with height = match_parent and expected its height to be equal to the scroll view but it's not. Its height behaves like wrap_content which means if there are no text in the EditText, I will have to point the cursor at the first "line" for the soft keyboard to popup, what I want is I can touch anywhere in the screen (the scroll view) and the soft keyboard pops up.
Below is the layout, thanks for your helps.
<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:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:gravity="top"
android:padding="10dp"
android:textSize="16sp"
android:textColor="#android:color/black">
</EditText>
</ScrollView>
<Button
android:id="#+id/btnPaste"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/paste"
android:gravity="center"
android:onClick="pasteData"
android:layout_marginLeft="5dp"
android:layout_marginRight="6dp"
android:layout_marginBottom="5dp"
android:background="#drawable/rounded_corner_button"
android:textColor="#android:color/white"/>
</LinearLayout>
Layout capture :
Add this line in your ScrollView.
android:fillViewport="true"
You can control the edit text by changing minLines attribute
for example :
<EditText
android:id="#+id/ed_comment"
style="#style/EditText.NoBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:hint="#string/write_comment_hint"
android:minLines="40"
android:textSize="16sp"/>
and instead of wrapping the EditText by scrollView add this attribute to editText
android:scrollbars="vertical"
result:
Implement this to your EditText in xml file:
android:scrollbars="vertical"
Implement this in onCreate():
editText.setMovementMethod(new ScrollingMovementMethod());
Then, you won't need scrollview if you only use it for textview, it simply makes scrollable only your textview.
Related
I don't exactly know what the issue is but just to clarify on a few things:
I'm using LinearLayout
My goal is to make a text string "example" go below an EditText view and it seems that whenever I try using margins to move the views and try to center them they always end up going the opposite way from each other, do you guys have any suggestions to fix this?
you can use Relativelayout as parent Layout and position your widgets relative to each other using their id
you can put this code in your textview xml definition
layout_below:#id/the one above it
LinearLayout
Defined android:orientation:vertical, allows you to position in list form each view that you add
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="#+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textAutoComplete"/>
<TextView
android:id="#+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Message"/>
</LinearLayout>
RelativeLayout
Defined android:layout_below="id_view" where id_view is the id of the view that would go first
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textAutoComplete"/>
<TextView
android:layout_below="#id/edit_text"
android:id="#+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Message"/>
</RelativeLayout>
My app design makes me having this kind of layout for every form view: a floating button at the bottom of the view. I'm using ConstraintLayout to set dynamically the height of the button with always the same left/right margin no matter the screen width, and so I end up having this layout:
<ConstraintLayout>
<ScrollView>
<EditText/>
<EditText/>
<!-- ... -->
<ConstraintLayout /> (1)
</ScrollView>
<Button/>
</ConstraintLayout>
(1) A clear view with the height of the bottom button in order not to hide the views at the bottom in the scroll view that would be hidden by the button
Basically, that's what it looks like:
Now the problem I encounter is when I tap an edit text at the bottom, for example here the 4th one:
The edit text moves up the keyboard, but not the floating button, and it often comes to be hidden by it. I know that I have to do something in the edit text's onFocusChanged() method, but I don't know what...
I know that I have to do something in the edit text's onFocusChanged()
method.
No. You don't have to do things manually. Simply add this attribute to the button. This will make the button positioned on the bottom of the parent layout rather that on the bottom of the EditText (You can skip this if are already doing it).
app:layout_constraintBottom_toBottomOf="parent"
And add this attribute to the activity tag which represent that specific activity. It will reduce the height of the viewable area by an amount which is equal to the height of the soft keyboard in such a way that it won't go behind the content.
android:windowSoftInputMode="adjustResize"
See the doc for more detailed explanation.
Also note that a ScrollView can host only one direct child as in the below example.
<ScrollView
...>
<LinearLayout
...>
<!-- You can place multiple views here -->
</LinearLayout>
</ScrollView>
1- you can move your edit text to top of the scrollView when they focus changed to true :
here is code :
java Code :
LinearLayout containerOfScrollViewViews= findViewById(R.id.containerOfScrollViewViews);
for (int i = 0; i < containerOfScrollViewViews.getChildCount(); i++) {
containerOfScrollViewViews.getChildAt(i).setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
int[] xy = new int[2];
v.getLocationInWindow(xy);
if (hasFocus) ((ScrollView)findViewById(R.id.scrollView))
.smoothScrollBy(xy[0], xy[1]);
}
});
}
My XML :
<?xml version="1.0" encoding="UTF-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
app:layout_constraintTop_toBottomOf="parent"
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toTopOf="#id/btn">
<LinearLayout
android:id="#+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:hint="A"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<EditText
android:hint="B"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<EditText
android:hint="C"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<EditText
android:hint="D"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<EditText
android:hint="E"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<EditText
android:hint="F"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<EditText
android:hint="H"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<EditText
android:hint="J"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<EditText
android:hint="K"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<EditText
android:hint="Z"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="add"
app:layout_constraintBottom_toBottomOf="parent" />
</android.support.constraint.ConstraintLayout>
2 - or your can attach your button to bottom of the scroll view so when keyboard appears than scrollView and button both goes above the scrollView
3- you can listen to keyboard visibility Chang and hide/show your button
here is link for this solution :
SoftKeyboard open and close listener in an activity in Android?
AndroidManifest.xml add android:windowSoftInputMode="adjustPan|stateHidden"in your activity ;
Or, setKeyboardListener, changed your scrollview position
Use releative Layout and put in scrollview android:layout_above="floating button"
this should solve overlaping or hiding by that button.
also softinput adjust pan
Scoll view should contain only one view
so put all edit text inside a linear or any layout inside scroll view
<android.support.constraint.ConstraintLayout>
<ScrollView .........>
<LinearLayout..........>
<EditText ....... **//Add EditText around 6 to 7**
..............
.............. />
</LinearLayout>
</ScrollView>
<RelativeLayout **// Fix the button in bottom gravity.**
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Bottom Gravity" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
And please set androidmanifest.xml file
<activity
android:windowSoftInputMode="adjustPan"/>
I've solve this problem hope so it will help you.
I'm trying to create a layout in Andorid. I need a TextView (2 in diagram below) fixed to the bottom of the screen. With content (1 in diagram) comprising of scrollview and editTexts.
The problem I have is when the soft keyboard comes up (3 in diagram), the red box marked 2 also comes up, above the keyboard. I would like the red box to remain offscreen and the entire content to scroll in the space remaining above the soft keyboard.
I've tried putting the red box (2) in the LinearLayout but it never fixes itself to the bottom, there's always a small gap at the bottom of the screen. I've also tried changing android:windowSoftInputMode in the manifest but this isn't the desired affect.
This is what I have:
<?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">
<ScrollView
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:isScrollContainer="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText />
<EditText />
</LinearLayout>
</ScrollView>
<TextView
android:id="#+id/3"
android:layout_alignParentBottom="true"
text="3" />
</RelativeLayout>
Any suggestions would be very much appreciated.
All you need to do is switch your activity's windowSoftInputMode flag to "adjustPan". Check the official documentation for more info.
<activity
...
android:windowSoftInputMode="adjustPan">
</activity>
Try using the below layout see if this is what you are looking for..
<?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">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:isScrollContainer="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<EditText
android:layout_width="80dp"
android:layout_height="40dp"
android:id="#+id/ed1"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:layout_marginBottom="50dp"/>
<EditText
android:layout_width="80dp"
android:layout_height="40dp"
android:id="#+id/ed2"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:layout_marginBottom="50dp"/>
</LinearLayout>
</ScrollView>
<TextView
android:id="#+id/btext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="this is 3rd view"
android:layout_marginBottom="20dp"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
TL;DR version, i just removed two lines in your scrollview code:
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
with
android:layout_centerInParent="true"
See if that helps!
And any reason why you are using both android:layout_alignParentTop="true"
android:layout_alignParentBottom="true" on scrollview?
Here's one idea that works.
Adjust the layout so that the red box (2) is inside the LinearLayout
Create a small view between the red box and the last EditText
Give the red box and the LinearLayout an id, so we can manage them from the java class.
Your xml should look like this:
<?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">
<ScrollView
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:isScrollContainer="false">
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText />
<EditText />
<View
android:id="#+id/stretcher"
android:layout_width="match_parent"
android:layout_height="0dp" />
<TextView
android:id="#+id/2"
android:layout_gravity="bottom"
text="2" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
Now in your java onActivityCreated add this code:
final View mainLayout = getView();
final View mainContent = getView().findViewById(R.id.content);
final View stretcherView = getView().findViewById(R.id.stretcher);
//Main layout uses weight some, so we can't hard code the size of the circles.
//We must dynamically re-size
mainLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (android.os.Build.VERSION.SDK_INT >= 16) {
mainLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
mainLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
//Calculate the desired height of the stretcher view to be the remainder between full screen and content.
int stretchHeight = mainLayout.getHeight() - mainContent.getHeight();
//Apply calculated height remainder to stretched view.
//This enables our bottom box to be pushed to the bottom without obstructing the content when the keyboard appears.
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) stretcherView.getLayoutParams();
params.height = stretchHeight;
stretcherView.setLayoutParams(params);
}
});
The view listener measures the height of the full view then subtracts the height of the content. This leaves us with the gap between the content and the bottom of the screen. Using the stretcher view we can now push the red box down to the bottom of the page, just as if it was alignParentBottom.
When the keyboard appears, the red box is inside the scroll.
I need an activity that look like a simple text editor: full-screen EditText with submit button below it.
Important:
1. EditText has to scroll both vertically and horizontally
2. Input text goes to the next line ONLY when the user clicks nextline button
3. EditText fills the whole space between the top of the screen and the button
My code:
<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:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:fillViewport="true">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<EditText
android:id="#+id/some_edittext"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top|left"
/>
</HorizontalScrollView>
</ScrollView>
<Button
android:id="#+id/submit_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit"/>
</LinearLayout>
Why bad:
EditText AUTOMATICALLY breaks text to the next line as it reaches the end of the row
It worked fine before I set fillViewport to true. But otherwise EditText doesn't fill entire screen
Thanks in advance
EditText actually has scrolling already built in, so you don't have to use a ScrollView. The only thing is that it doesn't support flings.
<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:orientation="vertical">
<EditText
android:id="#+id/some_edittext"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top|left"
/>
<Button
android:id="#+id/submit_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit"/>
</LinearLayout>
To stop the automatic wrapping, you can activate horizontal scrolling:
EditText editText = (EditText) findViewById(R.id.some_edittext);
editText.setHorizontallyScrolling(true);
editText.setMovementMethod(new ScrollingMovementMethod());
I think you could set your EditText attribute android:maxLines="1" initially.
And when "new line" button is clicked, you can increase that value programmatically using setMaxLines(int) method.
EditText.setMaxLines(int)
here's the doc -> link
Hope this helped.
I have a horizontal scroll view that takes up the right half of the screen. On it, I have an EditText. When I select the EditText to edit the text, the HorizontalScrollView scrolls to the right. I'm guessing it's trying to place the EditText to the leftmost position. Regardless, since the HorizontalScrollView takes up the rightside of the screen, by doing this, I can no longer see the EditText since its now covered up by the contents I put on the left side of the screen. How can I prevent HorizontalScrollView from automatically scrolling when I focus on something inside its layout?
Thanks. Much appreciated.
halp
Edit: Posting code. I'm actually creating my EditText's based off of generated info, so this is not what it actually looks like, but it should capture the ideai.
<RelativeLayout 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" >
<View
android:layout_leftOf="#+id/Anchor"/>
<View
android:id="#+id/Anchor"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerHorizontal="true"
android:background="#000" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rightOf="#+id/Anchor"
android:fillViewport="true" >
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp" >
<TableRow>
<EditText>
ABOVE is the view that is going to the left
MOre views which would cause Scrolling to the right
</TableRow>
</TableLayout>
</HorizontalScrollView>
</ScrollView>
</RelativeLayout>
try this in XML the EditText if use
android:gravity="center"
remove it