How can I expand an EditText when it is not focused? - android

I'm doing a message view like whats app and I want my EditText in the bottom to have the "send" button/icon if it's focused and if it's not, then hide/delete that button and expand the edit text. I don't know how to do the the expand and delete thing in run time. If anyone has some clue I would be really thankful.

Use OnFocusListener to trigger your desired events whenever focus of EditText changes.
Use setVisibility to hide and show your Button, and change width of EditText via getLayoutParams().width
m_editText.setOnFocusChangeListener((v, hasFocus) -> {
if (hasFocus) {
m_button.setVisibility(View.GONE);
m_editText.getLayoutParams().width=400;
} else {
m_button.setVisibility(View.VISIBLE);
m_editText.getLayoutParams().width=200;
}
});
You can also turn your size to dp with:
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, YOUR_SIZE_HERE, getApplication().getResources().getDisplayMetrics());

Related

How to avoid AlertDialog positive button to become invisible?

My app uses android API 21 (android 5) with the Theme.Material.Light.DarkActionBar theme.
In an AlertDialog I add positive, negative and neutral buttons. On small screens this can cause the positive to be hidden if the (translated!) button texts are too long to fit on the (portrait) screen. Previous versions of android did not have this problem (text was wrapped to make the buttons fit). The app will work OK if the neutral button is hidden, but I need the positive button to be visible at all times and regardless of translations. Is there any way to enforce this, other than adding "normal" buttons in the layout?
Alt: is ther any way to check if buttons will fit? So I can at runtime decide not to use the neutral button?
I found one solution that more or less works. In the onResume() method I modify the neutral button like so:
public void onResume() {
super.onResume();
final Button btn = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_NEUTRAL);
if (btn != null) {
if (Build.VERSION.SDK_INT >= 21) {
DisplayMetrics metrics = getResources().getDisplayMetrics();
int maxWidth = (metrics.widthPixels / 3);
btn.setSingleLine(false);
btn.setMaxWidth(maxWidth);
btn.setMaxLines(2);
btn.setEllipsize(TextUtils.TruncateAt.END);
// something in the above code reverts the allCaps
btn.setAllCaps(true);
}
}
Setting maxWidth works, but I have not found a way to accurately calculate the actual max width that would cause the positive button to disappear. The code above unconditionally limits to a third of the display width, which seems to work OK in my case.
The issue can be solved at all places by creating custom view ( Dialog with custom view )which will have buttons as the bottom. set onclick listeners to all and handle separately.

Prevent Softkeyboard from opening on EditText Focus

I need to prevent softkeyboard from showing up when Edit Text gains focus or if user taps on the edit text because user has to use a barcode scanner as an input method. But I need is open the keyboard when user clicks/taps a specific button.
I have tried focusableInTouchMode property and it works fine in preventing the softkeyboard from popping but it won't let the edit text gain focus.
Any hint on how to achieve this?
myEditText.setInputType(InputType.TYPE_NULL);
myEditText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// showMyDialog();
}
});
myEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// showMyDialog();
}
}
});
Try this.... Just return null or anything on onclicklistner..
Njoy.. :)
And m using it so don't tell me its not working.. LOL..
I have one condition that if user clicks on edittext then it ll pops DatePicker Dialog and its working.
So njoy dude...: ;)
you can hide the keyboard using this code
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
You should use stateAlwaysHidded. Either in runtime as in the first answer, or directly in your project Manifest:
android:windowSoftInputMode="stateAlwaysHidden"
This will prevent keyboard from appearing during onCreate/onResume.
To prevent keyboard appearing after clicking EditText, you can place some blank view with transparent backround right on top of your EditText:
<View
android:id="#+id/view"
android:layout_width="editTextWidth"
android:layout_height="editTextWidth"
android:background="#android:color/transparent"
android:clickable="true"/>
The key attribute here is android:clickable="true". It will prevent from gaining focus on click (actually users will click on this blank view, not EditText).
Then, when user clicks special button you've mentioned, you just remove this blank view and call editText.requestFocus()

using button to switch the input panel and input preview panel in the same activity in android

I aims to click the button to switch the input panel and input preview panel in the same activity so i am doing sth like that:
In one activity,there are one EditText,one button with the text value 'preview' and one textView. Initially, the visibility of editText and button is visible, the textview is set gone value. When the button is clicked,it gets the input value from the editText and set the value to textview. Also, the visibility of editText is set gone and textview is set visible.Finally, the text of button change to 'edit'.
To do this, I have written some code like that:
public void showNewsPreview(){
txtContentPreview.setText(edtContent.getText());
edtContent.setVisibility(View.GONE);
txtContentPreview.setVisibility(View.VISIBLE);
btnPreview.setText("Edit");
}
As a result,the code has been run successfully but the view dose not change.
whether i was missed something or the workflow has problem?
Slight change:
public void showNewsPreview(){
txtContentPreview.setText(edtContent.getText().toString());
edtContent.setVisibility(View.GONE);
txtContentPreview.setVisibility(View.VISIBLE);
btnPreview.setText("Edit");
}

requestFocus doesn't work properly for EditText

A lot of time was spent to solve the problem, and it looks easy, but I'm really tired and couldn't find the solution.
I have an Activity, activity has 4 EditText components, 2 of them has popup menu (AlertDialog) which contain the list, next one - is disabled for edit, and last one - is editable, and should show the soft keyboard, when user is tapping on it.
Also, my root LinearLayout has LinearLayout which contain inside RelativeLayout. The last one is need for AdvBanner. Last LinearLayout(RelativeLayout) is aligned to the bottom of root layout.
The part of XML that describes it:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="bottom"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="bottom">
<RelativeLayout
android:id="#+id/AdvLayoutReserveArea"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#FFFFFF"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="bottom" />
</LinearLayout>
When activity is start, editable EditText has focus with GREEN border and cursor is blinking. After few seconds left, the AdvBanner is loaded and shown. When it happens, editable EditText lost focus.. from this moment, my life be like a nightmare.
Let's look step by step.
Problem 1.
If in THIS MOMENT (when Adv loaded and appears) user is editing an EditText field via the soft keyboard, focus is lost, border take a GRAY color, and if user continue to typing a text is have no result - symbols are not printed (CURSOR in EditText is too lost).
I THINK any user will be annoyed - when you typing text, and cursor is inactive, because in background some adv is loaded and it take focus for self.
To solve this, in method when Adv is loaded (is shown) I try to back focus manually to EditText by requestFocus method.
public void onAdLoaded()
{
// TODO Auto-generated method stub
// add app specific code for this event here...
// called when an ad is successfully displayed on device
CountEdit1.requestFocus();
}
Yes, the cursor is returned to EditText field, and if soft keyboard is active, user can still typing text, but border of EditText field stay GRAY...
NOTE: actually I'm not sure about the difference between GREEN and GRAY border of focused EditText.. GREEN is usually when user is tapping on it, and GRAY, probably, when we want to request a focus manually (using requestFocus() etc)
Problem 2. (As result of solvation Problem #1).
After soft keyboard was closed, if user tap on editable EditText field, it take focus and cursor appears inside, but no reaction for showing soft keyboard again! Tapping of it do not show soft keyboard, but looks like the edit field in copy mode - when user can select a text and cut/copy it to clipboard.
My goal is easy for a first look. I just want to SAVE the cursor and focus to editable EditText field (CountEdit1) while soft keyboard is shown and user typing some text.
And normal reaction when user tapping EditText - as usually, just show me the soft keyboard!
I read all issues here, I combined different methods (clearFocus, requestFocusFromTouch etc), just not enough of time and space to describe all that I tried to do to solve this. The main problems are described above.
Hope for help and solving the problem...
Thanks in advance..
The goal is solved, the workaround is an easier than I thought. Problem #2 is fixed by using onClick () method. Sufficient condition for appearing of soft keyboard that use of both clearFocus() and requestFocus() methods.
CountEdit1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
CountEdit1.clearFocus();
CountEdit1.requestFocus();
}
});
The soft keyboard appears when user is tapping on the EditText field.
Fixed.
Works for me:
InputMethodManager mgr = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
editField.requestFocus();
mgr.showSoftInput(editField, InputMethodManager.SHOW_IMPLICIT);
userInput.post(new Runnable() {
public void run() {
userIdInput.requestFocus();
}
});
Have a go at this and tell if your problem is still unsolved.
You should request focus after view is created in fragment or activity:
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
CountEdit1.requestFocus();
}
try this:
public void onAdLoaded()
{
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
CountEdit1.requestFocus();
InputMethodManager mgr = (InputMethodManager) base.getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(CountEdit1, InputMethodManager.SHOW_IMPLICIT);
CountEdit1.setSelection(CountEdit1.getText().length());
}
},0);
}

Can I set something like "tooltip" for a view in android?

Can I set some message to appear like a "tooltip" for a TextView or Button?
There's no concept of "hovering" in a touch screen, but you could set a LongClickListener for your View, and have a Toast appear after a long press. Something like this:
Toast viewToast = Toast.makeText(this, "My View Tooltip", Toast.LENGTH_SHORT);
View myView = (View)findViewById(R.id.my_view);
myView.setOnLongClickListener(new OnLongClickListener() {
#Override
public void onLongClick(View v) {
viewToast.show();
}
});
EDIT: After reading your comment, you should just use the hint attribute in your EditText XML layout:
<EditText
android:hint="My tip here" />
-First set a textview with your hint and set it to invisible.
-Create an animation xml with alpha animation,specify how long you would like to display(at the end set the animation to zero alpha so that it remains invisible) and put it in res->anim folder
-Inside your onCreate and onClick methods of view that need tooltip
set the text view to visible
Hook the animation(like
R.anim.tooltip) to this text view
-Use boolean flags and allow the user to switch off the tool tips in menu.
I'll leave the code specifics to you. You find them easily in stackoverflow.

Categories

Resources