<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/customedittext"
android:layout_marginStart="25dp"
android:layout_marginEnd="25dp"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">
<EditText
android:id="#+id/answer1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:fontFamily="#font/timeandspace"
android:scrollHorizontally="false"
android:scrollbars="vertical"
android:hint="1."
android:background="#color/transparent"
android:inputType="text"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:textColor="#color/colormain"
android:textSize="25sp" /></LinearLayout>
How can I wrap the text vertically or set a maximum width of my text without using android:inputType="textmultiline"?
reason if why i didnt want that input type because i am using the Enter in keyboard to submit the text and not to add line. Hope someone can help me :)
And, how can I prevent automatically showing keyboard and focus to edittext at the start of activity?
Thanks a lot <3
This will not add a line:
reason if why i didnt want that input type because i am using the
Enter in keyboard to submit the text and not to add line
answer1.setOnEditorActionListener(new OnEditorActionListener()
{
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if (event.getAction() == KeyEvent.KEYCODE_ENTER)
{
// Do whatever you wanna do on "Enter"
return true;
}
return false;
}
});
And, how can I prevent automatically showing keyboard and focus to
edittext at the start of activity?
Remove any EditText.requestFocus() call in your activity.
Related
In my MainActivity class, I have this button:
<Button
android:id="#+id/target"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/speak"
android:layout_marginTop="20dp"
android:background="#drawable/button_red"
android:text="#string/target" />
I now want to add a badge with the number 1, using Material Design (see the documentation).
This is how I tried:
public void setBadge() {
BadgeDrawable badgeDrawable = BadgeDrawable.create(MainActivity.this);
badgeDrawable.setNumber(1);
BadgeUtils.attachBadgeDrawable(badgeDrawable, findViewById(R.id.target));
}
but what I get is the error
'attachBadgeDrawable(com.google.android.material.badge.BadgeDrawable, android.view.View, android.widget.FrameLayout)' in 'com.google.android.material.badge.BadgeUtils' cannot be applied to '(com.google.android.material.badge.BadgeDrawable, android.view.View)'
If you tried this on a device with <18 API this might work;
In your XML wrap the button with a FrameLayout;
<FrameLayout
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="HELLO"/>
</FrameLayout>
In your Activity;
//I've used post to eliminate race condition between the button and its badge.
button.post(() -> {
BadgeDrawable badgeDrawable = BadgeDrawable.create(MainActivity.this);
badgeDrawable.setNumber(1);
//Note that there is a third argument which is our FrameLayout
BadgeUtils.attachBadgeDrawable(badgeDrawable, findViewById(R.id.button), findViewById(R.id.frame));
});
EditText as ChangeContact
EditText focus is not working in android studio, i want to hide a textview when focus change in edittext, how to do this?
ChangeContact.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
Toast.makeText(mContext, "focus loosed", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(mContext, "focused", Toast.LENGTH_LONG).show();
bookLinear.setVisibility(View.GONE);
}
}
});
My edit text in XMl file
<EditText
android:id="#+id/etChangeAddress"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:layout_marginBottom="8dp"
android:background="#drawable/shape_transparent"
android:ems="10"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:hint="Address"
android:inputType="textPersonName"
android:padding="5dp"
app:layout_constraintBottom_toTopOf="#+id/etChangeNumber"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
But i cant see the Toast
Please help ....
Have you tried with this for ChangeContact?
android:focusable="true"
android:focusableInTouchMode="true"
also
ChangeContact.requestFocus() ?
In my case I can't get on focus change event because I forgot to delete setContentView() function and used it with binding initialization like this:
setContentView(R.layout.activity_main)
binding = ActivityMainBinding.inflate(layoutInflater, null, false)
also eventually I understood that binding initialization also was incorrect
I Am Making A Note Saver Application, For The Main Body of Note I Am Using A Multiline EditText Looks Like This:-
Screenshot
Code For EditText:-
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.mnotes.view.TextField
android:id="#+id/mNoteEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#null"
android:gravity="top|start"
android:inputType="textMultiLine"
android:overScrollMode="always"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:scrollHorizontally="false"
android:hint="Start Writing For Here...."
android:padding="8dp"
android:clickable="true"
android:focusable="true"
android:fontFamily="sans-serif-smallcaps"
android:longClickable="true"
android:textIsSelectable="true"/>
</LinearLayout>
When I Scroll This EditText It Gains Focus Which Makes Softkey Popup, This Behaviour Makes The Scrolling Experience Very Bad.So I Want To Disable Focus On EditText While Scrolling, Please Help.
I found a solution (only works on short texts). You can use the ScrollView onScrollChange event.
This is how I did it:
ScrollView scroll = findViewById(R.id.scrollView1);
scroll.setOnScrollChangeListener(new View.OnScrollChangeListener() {
#Override
public void onScrollChange(View view, int i, int i1, int i2, int i3) {
et.clearFocus();
}
});
The variable et is an EditText object.
I have an EditText inside of a ScrollView which is pre-filled with a dynamic amount of text. It could be one line or 50 or more. At this point, the EditText is uneditable and the text is shown just fine. However, when I press a button to make the edit text editable, the entire contents of the EditText are thrown into one very long, horizontally scrolling line.
You can see exactly what I'm talking about here:
https://zippy.gfycat.com/GenerousBigHerring.webm
How can I prevent this from happening? I've been screwing around with this for a while now. I've tried setting lines and maxLines, etc, but I can't figure it out.
Edit:
This is my current ScrollView/EditText:
<ScrollView
android:id="#+id/result_text_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/result_title_text"
android:layout_above="#+id/result_bottom_sheet"
android:layout_marginTop="20dp"
android:padding="5dp">
<EditText
android:id="#+id/extracted_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:padding="15dp"
tools:text="Hello, World"
android:textColor="#color/black"
android:textStyle="bold"
android:textSize="#dimen/extracted_text_size"
android:textIsSelectable="true"
android:inputType="none"
/>
</ScrollView>
From code, here's what I'm doing to to enable/disable editing:
#Override
public void enableTextEditing() {
mExtractedText.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
mExtractedText.setOnKeyListener(this);
}
#Override
public void disableTextEditing() {
mExtractedText.setInputType(InputType.TYPE_NULL);
mExtractedText.setOnKeyListener(null);
}
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
//All this does is call the View methods to disable text editing
//and close the keyboard.
mPresenter.doneEditingButtonPressed();
return true;
}
return false;
}
Add android:lines , android:minLines and android:maxLines to your EditText view
Also change android: inputType = "textMultiline"
You can add android:scrollbars="vertical" to your EditText as well if you wish
Making a simple calorie converter.
Here's the problem in pictures.
I'm on a Genymotion emulator.
Here is the part of the screen I'm having issues with:
When I click on an EditText number field, the numberpad comes up
But there's a next arrow I must click which takes me to the next input field
Before I can finally just get out of the numberpad.
What's going on? After I type in a number into one edit text I want it to end right there, not jump to the next Edit Text. Also, it's weird because if I enter a number in the two bottom EditTexts they end right away whereas if I enter a number in the top EditTexts they take me to one of the bottom EditTexts.
Here's the XML file.
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText2"
android:layout_below="#+id/imageView2"
android:layout_alignEnd="#+id/imageView2"
android:layout_alignStart="#+id/imageView2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText3"
android:layout_alignBottom="#+id/editText2"
android:layout_alignEnd="#+id/imageView4"
android:layout_alignStart="#+id/imageView4" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText4"
android:layout_marginBottom="32dp"
android:layout_alignParentBottom="true"
android:layout_alignStart="#+id/imageView5"
android:layout_alignEnd="#+id/imageView5" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText5"
android:layout_alignTop="#+id/editText4"
android:layout_alignStart="#+id/imageView3"
android:layout_toStartOf="#+id/editText2" />
Thank you!!!
If you want to jump next field you mentioned the id in editText itself using nextFocusDown attribute.
try this....
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText2"
android:layout_below="#+id/imageView2"
android:layout_alignEnd="#+id/imageView2"
android:layout_alignStart="#+id/imageView2"
android:nextFocusDown="#+id/editText3"/>
when you want to show done on SoftKeyboard at particular view use android:imeOptions="actionDone" attribute.
Set ime options to your EditText:
<EditText
android:imeOptions="actionDone"
... />
You can choose from various options. See the documentation:
http://developer.android.com/reference/android/widget/TextView.html#attr_android:imeOptions
The only way to truly handle what happens when the next (enter) key is pressed is to make a listener for it at tell it what to do according to the current state of the application.
final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
return true;
}
return false;
}
});
To select a edittext you need to focus it like this:
editText.requestFocus();
Simple solution would be to add:
android:imeOptions="actionNext"
android:nextFocusDown="#+id/..." <!--Obviously with the next edittext ID--