Custom Keyboard is hiding the Edit box in Android - android

I have designed a custom key board with only numeric keys. I have followed the below link:
example
Now when i am touching the edit box, key board is appearing. But if i have 10 editboxes, and i am touching the 10th edit box, key board is appeared and hiding the edit box.How can i make the edit box will scroll up automatically so that it will be not hidden.
I have written the below layout code for xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/edittext0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:inputType="text" />
<EditText
android:id="#+id/edittext1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/edittext0"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:inputType="text" />
<EditText
android:id="#+id/edittext2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/edittext1"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:inputType="text" />
<EditText
android:id="#+id/edittext3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/edittext2"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:inputType="text" />
<EditText
android:id="#+id/edittext4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/edittext3"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:inputType="text" />
</RelativeLayout>
</ScrollView>
<android.inputmethodservice.KeyboardView
android:id="#+id/keyboardview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:visibility="gone" />
</RelativeLayout>
Also here is my custom keyboard java class:
class CustomKeyboard {
/** A link to the KeyboardView that is used to render this CustomKeyboard. */
private KeyboardView mKeyboardView;
/** A link to the activity that hosts the {#link #mKeyboardView}. */
private Activity mHostActivity;
/** The key (code) handler. */
private OnKeyboardActionListener mOnKeyboardActionListener = new OnKeyboardActionListener() {
public final static int CodeDelete = -5; // Keyboard.KEYCODE_DELETE
public final static int CodePrev = 55000;
public final static int CodeNext = 55001;
public final static int CodeDone = 55002;
#Override
public void onKey(int primaryCode, int[] keyCodes) {
// NOTE We can say '<Key android:codes="49,50" ... >' in the xml
// file; all codes come in keyCodes, the first in this list in
// primaryCode
// Get the EditText and its Editable
View focusCurrent = mHostActivity.getWindow().getCurrentFocus();
if (focusCurrent == null
|| focusCurrent.getClass() != EditText.class)
return;
EditText edittext = (EditText) focusCurrent;
Editable editable = edittext.getText();
int start = edittext.getSelectionStart();
// Apply the key to the edittext
if (primaryCode == CodeDone) {
hideCustomKeyboard();
}
else if (primaryCode == CodeDelete)
{
if (editable != null && start > 0)
editable.delete(start - 1, start);
}
else if (primaryCode == CodePrev) {
View focusNew = edittext.focusSearch(View.FOCUS_BACKWARD);
if (focusNew != null)
focusNew.requestFocus();
}
else if (primaryCode == CodeNext) {
View focusNew = edittext.focusSearch(View.FOCUS_FORWARD);
if (focusNew != null)
focusNew.requestFocus();
}
else { // insert character
editable.insert(start, Character.toString((char) primaryCode));
}
}
#Override
public void onPress(int arg0) {
}
#Override
public void onRelease(int primaryCode) {
}
#Override
public void onText(CharSequence text) {
}
#Override
public void swipeDown() {
}
#Override
public void swipeLeft() {
}
#Override
public void swipeRight() {
}
#Override
public void swipeUp() {
}
};
/**
* Create a custom keyboard, that uses the KeyboardView (with resource id
* <var>viewid</var>) of the <var>host</var> activity, and load the keyboard
* layout from xml file <var>layoutid</var> (see {#link Keyboard} for
* description). Note that the <var>host</var> activity must have a
* <var>KeyboardView</var> in its layout (typically aligned with the bottom
* of the activity). Note that the keyboard layout xml file may include key
* codes for navigation; see the constants in this class for their values.
* Note that to enable EditText's to use this custom keyboard, call the
* {#link #registerEditText(int)}.
*
* #param host
* The hosting activity.
* #param viewid
* The id of the KeyboardView.
* #param layoutid
* The id of the xml file containing the keyboard layout.
*/
public CustomKeyboard(Activity host, int viewid, int layoutid) {
mHostActivity = host;
mKeyboardView = (KeyboardView) mHostActivity.findViewById(viewid);
mKeyboardView.setKeyboard(new Keyboard(mHostActivity, layoutid));
mKeyboardView.setPreviewEnabled(false); // NOTE Do not show the preview
// balloons
mKeyboardView.setOnKeyboardActionListener(mOnKeyboardActionListener);
// Hide the standard keyboard initially
mHostActivity.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
/** Returns whether the CustomKeyboard is visible. */
public boolean isCustomKeyboardVisible() {
return mKeyboardView.getVisibility() == View.VISIBLE;
}
/**
* Make the CustomKeyboard visible, and hide the system keyboard for view v.
*/
public void showCustomKeyboard(View v) {
mKeyboardView.setVisibility(View.VISIBLE);
mKeyboardView.setEnabled(true);
if (v != null)
((InputMethodManager) mHostActivity
.getSystemService(Activity.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
/** Make the CustomKeyboard invisible. */
public void hideCustomKeyboard() {
mKeyboardView.setVisibility(View.GONE);
mKeyboardView.setEnabled(false);
}
/**
* Register <var>EditText<var> with resource id <var>resid</var> (on the
* hosting activity) for using this custom keyboard.
*
* #param resid
* The resource id of the EditText that registers to the custom
* keyboard.
*/
public void registerEditText(int resid) {
// Find the EditText 'resid'
EditText edittext = (EditText) mHostActivity.findViewById(resid);
// Make the custom keyboard appear
edittext.setOnFocusChangeListener(new OnFocusChangeListener() {
// NOTE By setting the on focus listener, we can show the custom
// keyboard when the edit box gets focus, but also hide it when the
// edit box loses focus
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
showCustomKeyboard(v);
else
hideCustomKeyboard();
}
});
edittext.setOnClickListener(new OnClickListener() {
// NOTE By setting the on click listener, we can show the custom
// keyboard again, by tapping on an edit box that already had focus
// (but that had the keyboard hidden).
#Override
public void onClick(View v) {
showCustomKeyboard(v);
}
});
// Disable standard keyboard hard way
// NOTE There is also an easy way:
// 'edittext.setInputType(InputType.TYPE_NULL)' (but you will not have a
// cursor, and no 'edittext.setCursorVisible(true)' doesn't work )
edittext.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
EditText edittext = (EditText) v;
int inType = edittext.getInputType(); // Backup the input type
edittext.setInputType(InputType.TYPE_NULL); // Disable standard
// keyboard
edittext.onTouchEvent(event); // Call native handler
edittext.setInputType(inType); // Restore input type
return true; // Consume touch event
}
});
// Disable spell check (hex strings look like words to Android)
edittext.setInputType(edittext.getInputType()
| InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
}
}
Help me please!!
Thanks,
Arindam

I had the exact same problem and after a full days research i have finally solved it.
Here is how:
I have nearly identical xml layout as yours but i have multiple KeyboardViews because i need different keyboards for different EditTexts.
<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"
tools:context=".MainActivity"
tools:ignore="HardcodedText,TextFields" >
<ScrollView
android:id="#+id/scroll_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:paddingBottom="16dp">
<TextView
android:id="#+id/DecLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Decimal" />
<EditText
android:id="#+id/DecText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:focusable="true"
android:focusableInTouchMode="true" />
<TextView
android:id="#+id/HexLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Hexadecimal" />
<EditText
android:id="#+id/HexText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:focusable="true"
android:focusableInTouchMode="true" />
<TextView
android:id="#+id/BinaryLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Binary" />
<EditText
android:id="#+id/BinaryText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:focusable="true"
android:focusableInTouchMode="true" />
<TextView
android:id="#+id/OctalLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Octal" />
<EditText
android:id="#+id/OctalText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_gravity="fill" />
</LinearLayout>
</ScrollView>
<android.inputmethodservice.KeyboardView
android:id="#+id/keyboardview_hex"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="0dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:keyBackground="#drawable/btn_keyboard_key_ics"
android:visibility="gone" />
<android.inputmethodservice.KeyboardView
android:id="#+id/keyboardview_dec"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="0dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:keyBackground="#drawable/btn_keyboard_key_ics"
android:visibility="gone" />
<android.inputmethodservice.KeyboardView
android:id="#+id/keyboardview_oct"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="0dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:keyBackground="#drawable/btn_keyboard_key_ics"
android:visibility="gone" />
<android.inputmethodservice.KeyboardView
android:id="#+id/keyboardview_bin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="0dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:keyBackground="#drawable/btn_keyboard_key_ics"
android:visibility="gone" />
It is not enough. I also needed to set ScrollView's android:layout_above property when the KeyboardView is shown and i did this with an interface:
public interface OnKeyboardStateChangedListener
{
public void OnDisplay(View currentview, KeyboardView currentKeyboard);
public void OnHide(KeyboardView currentKeyboard);
}
And implemented this interface to my MainActivity to edit ScrollView's properties:
public class MainActivity extends Activity BaseKeyboard.OnKeyboardStateChangedListener
Also i passed MainActivity as a parameter and saved as OnKeyboardStateChangedListener:
private OnKeyboardStateChangedListener mStateListener;
public BaseKeyboard(Activity host, int viewid, int layoutid, OnKeyboardStateChangedListener listener) {
..
..
mStateListener = listener;
}
And invoked it when displaying/hiding the keyboard:
public void showCustomKeyboard( View v ) {
mKeyboardView.setVisibility(View.VISIBLE);
mKeyboardView.setEnabled(true);
if( v!=null ) {
mStateListener.OnDisplay(v, mKeyboardView);
((InputMethodManager)mHostActivity.getSystemService(Activity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
public void hideCustomKeyboard() {
mKeyboardView.setVisibility(View.GONE);
mKeyboardView.setEnabled(false);
mStateListener.OnHide(mKeyboardView);
}
And finally i edited the parameters in the MainActivity:
#Override
public void OnDisplay(View currentview, KeyboardView currentKeyboard) {
ScrollView mScroll = (ScrollView) findViewById(R.id.scroll_content);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.ABOVE, currentKeyboard.getId());
mScroll.setLayoutParams(params);
mScroll.scrollTo(0, currentview.getBaseline()); //Scrolls to focused EditText
}
#Override
public void OnHide(KeyboardView currentKeyboard) {
ScrollView mScroll = (ScrollView) findViewById(R.id.scroll_content);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
mScroll.setLayoutParams(params);
}
Here are the screenshots:
How it should look without keyboard
With a keyboard displayed
Notes:
I have LinearLayout instead of RelativeLayout inside ScrollView because when my keyboard is shown, ScrollView works as needed but it cuts the most bottom View in half and padding or margin doesn't work with RelativeLayout in this situation but adding a paddingBottom to LinearLayout pushes it back and all of the View is visible. I couldn't find another way to fix this.
You should try different size of paddings to satisfy your needs.
I can provide full source code and additional info if you need.

Did check it and working fine.
Your main layout is a RelativeLayout which gives whole a relative layout and so everything gets change relatively in view
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/edittext0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:inputType="text" />
<EditText
android:id="#+id/edittext1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/edittext0"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:inputType="text" />
<EditText
android:id="#+id/edittext2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/edittext1"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:inputType="text" />
<EditText
android:id="#+id/edittext3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/edittext2"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:inputType="text" />
<EditText
android:id="#+id/edittext4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/edittext3"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:inputType="text" />
</RelativeLayout>
</ScrollView>
<android.inputmethodservice.KeyboardView
android:id="#+id/keyboardview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:visibility="gone" />
</RelativeLayout>
Check this output I just tried
Img1 = http://i44.tinypic.com/2412r0n.png
Img2 = enter link description here

Related

DialogPreference in Full Screen Width

I created a custom dialog preference in my Android application, but I can not figure out how to get the dialog which is displayed to span the complete width of the display.
image of dialog with too much space on left and right side
I found many proposed solutions to get a normal Dialog in full screen mode
Android get full width for custom Dialog
https://gist.github.com/koocbor/88db64192638bff09aa4
http://blog.jimbaca.com/force-dialog-to-take-up-full-screen-width/
But setting the attributes via getWindow does not work:
#Override
public Dialog getDialog() {
Dialog dialog = super.getDialog();
dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
// or
// dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
return dialog;
}
And applying a full screen theme to my dialogs root element didn't do the job neither:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
[...]
android:theme="#style/FullscreenTheme">
Moreover I'm not able to access the onCreate Method (at least I don't know how) of the Dialog, to set the style there.
Did anyone had the same problem and figured out a solution for this very specific issue?
My layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical"
android:padding="0dp"
android:paddingTop="#dimen/preferences_dialog_def_padding"
android:paddingBottom="#dimen/preferences_dialog_def_padding">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="-2dp"
android:background="#color/expandable_preference_divider"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/preferences_expandable_margin_top_bottom"
android:layout_marginTop="#dimen/preferences_expandable_margin_top_bottom">
<RelativeLayout
android:id="#+id/icon_wrapper_choose"
android:layout_width="#dimen/preferences_expandable_icon_wrapper_size"
android:layout_height="#dimen/preferences_expandable_icon_wrapper_size"
android:layout_marginBottom="0dp"
android:layout_marginEnd="#dimen/preference_expandable_icon_margin"
android:layout_marginStart="#dimen/preference_expandable_icon_margin"
android:layout_marginTop="0dp"
android:gravity="center">
<ImageView
android:layout_width="#dimen/preferences_expandable_icon_size"
android:layout_height="#dimen/preferences_expandable_icon_size"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:cropToPadding="true"
android:scaleType="centerCrop"
android:src="#drawable/ic_settings_white_36dp"/>
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="#+id/icon_wrapper_choose"
android:paddingBottom="#dimen/preferences_expandable_text_padding_top_bottom"
android:paddingTop="#dimen/preferences_expandable_text_padding_top_bottom"
android:text="#string/pref_wheel_circumference_choose"
android:textColor="#color/colorAccent"
android:textSize="#dimen/text_size_medium"
android:textStyle="bold"/>
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_marginEnd="#dimen/preference_expandable_icon_margin"
android:layout_marginStart="#dimen/preference_expandable_icon_margin"
android:layout_height="wrap_content"
android:text="#string/etrto_hint"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:orientation="horizontal"
android:paddingBottom="20dp"
android:paddingEnd="?android:attr/scrollbarSize"
android:layout_marginStart="#dimen/preference_expandable_icon_margin"
android:weightSum="3"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/etrto"/>
<Spinner
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingBottom="20dp"
android:paddingEnd="?android:attr/scrollbarSize"
android:layout_marginStart="#dimen/preference_expandable_icon_margin"
android:weightSum="3"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/manufacturer"/>
<Spinner
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"/>
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="-2dp"
android:background="#color/expandable_preference_divider"/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/preference_category_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical"
android:padding="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/preferences_expandable_margin_top_bottom"
android:layout_marginTop="#dimen/preferences_expandable_margin_top_bottom">
<RelativeLayout
android:id="#+id/icon_wrapper_manual"
android:layout_width="#dimen/preferences_expandable_icon_wrapper_size"
android:layout_height="#dimen/preferences_expandable_icon_wrapper_size"
android:layout_marginBottom="0dp"
android:layout_marginEnd="#dimen/preference_expandable_icon_margin"
android:layout_marginStart="#dimen/preference_expandable_icon_margin"
android:layout_marginTop="0dp"
android:gravity="center">
<ImageView
android:id="#+android:id/icon"
android:layout_width="#dimen/preferences_expandable_icon_size"
android:layout_height="#dimen/preferences_expandable_icon_size"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:cropToPadding="true"
android:scaleType="centerCrop"
android:src="#drawable/ic_edit_white_36dp"/>
</RelativeLayout>
<TextView
android:id="#+android:id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/icon_wrapper_manual"
android:paddingBottom="#dimen/preferences_expandable_text_padding_top_bottom"
android:paddingTop="#dimen/preferences_expandable_text_padding_top_bottom"
android:text="#string/pref_wheel_circumference_manually"
android:textColor="#color/colorAccent"
android:textSize="#dimen/text_size_medium"
android:textStyle="bold"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:orientation="horizontal"
android:paddingBottom="20dp"
android:paddingEnd="?android:attr/scrollbarSize"
android:layout_marginStart="#dimen/preference_expandable_icon_margin"
android:weightSum="2.5"
>
<EditText
android:id="#+id/pref_dialog_wheelcircumference_et"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:textAlignment="textEnd"
android:textColor="#color/colorFont"
android:textSize="#dimen/text_size_small"
android:inputType="number"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:textAlignment="center"
android:text="#string/wheel_circumference_unit"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
My custom preference class
public class WheelCircumferencePreference extends android.preference.DialogPreference {
private static String TAG = "CustomSwitchPreference";
private int mWheelCircumference;
public static int WHEEL_CIRCUMFERENCE_DEFAULT = 2125;
private int mDialogLayoutResId = R.layout.pref_dialog_wheelcircumference;
public WheelCircumferencePreference(Context context) {
this(context, null);
}
public WheelCircumferencePreference(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.dialogPreferenceStyle);
}
public WheelCircumferencePreference(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
setLayoutResource(R.layout.custom_preference);
setDialogLayoutResource(mDialogLayoutResId);
setPositiveButtonText(getContext().getString(R.string.dialog_save));
setNegativeButtonText(getContext().getString(R.string.dialog_cancel));
}
#Override
protected Object onGetDefaultValue(TypedArray a, int index) {
// Default value from attribute. Fallback value is set to WHEEL_CIRCUMFERENCE_DEFAULT.
return a.getInteger(index, WHEEL_CIRCUMFERENCE_DEFAULT);
}
#Override
protected void onSetInitialValue(boolean restorePersistedValue,
Object defaultValue) {
would load value from shared preferences
if (restorePersistedValue) {
mWheelCircumference = getPersistedInt(WHEEL_CIRCUMFERENCE_DEFAULT);
} else {
mWheelCircumference = (Integer) defaultValue;
persistInt(mWheelCircumference);
}
}
private EditText mWheelCircumferenceEt;
#Override
protected void onBindDialogView(View view) {
mWheelCircumferenceEt = view.findViewById(R.id.pref_dialog_wheelcircumference_et);
if (mWheelCircumferenceEt == null) {
throw new IllegalStateException("preference dialog view must contain" +
" a EditText with id 'pref_dialog_wheelcircumference_et'");
}
mWheelCircumferenceEt.setText(Integer.toString(mWheelCircumference));
super.onBindDialogView(view);
}
#Override
public Dialog getDialog() {
//Dialog dialog = super.getDialog();
// WindowManager.LayoutParams p = getDialog().getWindow().getAttributes();
//p.height = LinearLayout.LayoutParams.WRAP_CONTENT;
//dialog.getWindow().setAttributes(p);
return dialog;
}
#Override
protected void onDialogClosed(boolean positiveResult) {
if (positiveResult) {
String circumferenceText = mWheelCircumferenceEt.getText().toString();
try {
mWheelCircumference = Integer.parseInt(circumferenceText);
} catch (Exception e) {
NLog.e(TAG, "onDialogClosed - ", e);
mWheelCircumference = WheelCircumferencePreference.WHEEL_CIRCUMFERENCE_DEFAULT;
}
persistInt(mWheelCircumference);
}
}
Edit:
Actually I only want the dialog to span over the full width of the screen, not the height. If I would use a additional PreferenceFragment (as the DialogPreference is already embedded in a PreferenceFragment ) the "Dialog" (aka Fragment) would take the complete width and height (i guess).
I already implemented a solution without a DialogPrefrence, that works but is not exactly elegant
using just a normal EditTextPreference
adding an onPreferenceClickListener to this preference in my SettingsFragment Code
the ClickListener displays a simple Dialog
Example:
Preference preference = findPreference(EXAMPLE_PREFRENCE);
if (preference != null) {
preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
// showDialog();
}
});
But as I have a lot of preferences which will display dialogs the code for the dialog creation and display bloads the SettingsFragment and makes it nearly unreadable. Therefore I thought it would be a nice solution to put the responsibility of displaying the dialog and handling the preference values to the Preference and the XML layout.
Unfortunately I got stuck with the "full width issue" mentioned above.
Note: fixed the code of getDialog as I tested different versions (also in combination with the xml theme set)
Finally I did find a solution for this problem:
Fetch the AlertDialog of the Preference in showDialog method
#Override
protected void showDialog(Bundle state) {
super.showDialog(state);
CustomDialogPreference.makeDialogFullScreen((AlertDialog) getDialog());
}
make it span the complete width:
public static void makeDialogFullScreen(AlertDialog d) {
NLog.d(TAG, "makeDialogFullScreen enter ");
if (d != null) {
ViewGroup.LayoutParams params = d.getWindow().getAttributes();
if (params != null) {
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
d.getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
}
}
}
Try this in the onResume of your dialog.
// Store access variables for window and blank point
Window window = getDialog().getWindow();
Point size = new Point();
// Store dimensions of the screen in `size`
Display display = window.getWindowManager().getDefaultDisplay();
display.getSize(size);
// Set the width of the dialog proportional to 75% of the screen width and height
window.setLayout((int) (size.x * 0.75), (int) (size.y * 0.75));
window.setGravity(Gravity.CENTER);
// Call super onResume after sizing
Adjust accordingly for 100%. It works great for a dialogFragment. Haven't tried it for your case though.
Wait, you're not looking for the bog-standard 'Pref settings user options appear in a dialog' thing are you? That's almost definitely already done in AndroidStudio's add activity...> Settings Activity in boiler plate, check it out, or look for sample settings apps
Anyway, I do actually have a fullscreen dialog in my app, although it purposely doesn't fill the full screen, and I actually use an activity with some fragments now instead.
Personally I think this is what your problem is, I remember having this exact issue when I first needed a dialog like this. You should just use activities and have up navigation (if you want a full screen "popup" type thing you could use the Navigation pattern that makes the home/up button an 'X' instead of a '<');
Or anything else, you don't need to have a dialog explicitly, and if you do then extend activity or dialog and get what you want.
Here's my activity stuff in case it's any use
my theme:
<style name="AppTheme.FullScreenDialog"
parent="#style/Theme.AppCompat.Light.Dialog">
<item name="windowActionBar">true</item>
<item name="windowNoTitle">true</item>
</style>
my onCreate gist:
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
...
requestWindowFeature(Window.FEATURE_NO_TITLE);
...
super.onCreate(savedInstanceState);
setContentView(getConcreteContentView());
ButterKnife.bind(this);
setUpUIComponents();
...
}
my general layout gist:
<CoordinatorLayout>
<AppBarLayout>
<android.support.v7.widget.Toolbar/>
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:id="#+id/container_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:paddingTop="6dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<RelativeLayout
android:id="#+id/container_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_security_word"
android:paddingEnd="18dp"
android:paddingStart="18dp" />
<RelativeLayout
android:id="#+id/container_security"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/container_recycler"
android:minHeight="150dp"
android:paddingEnd="18dp"
android:paddingStart="18dp"
android:visibility="visible" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/security_container"
android:layout_centerHorizontal="true"
android:contentDescription="#string/app_name"
android:minHeight="50dp"
android:scaleType="centerInside" />
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
Bon Chance!

EditText doen't open keyboard in popup

This layout is used for popup window to change name of a textview.But the edittext doesnt show keyboard even on clicking the edittext. I'm using it in a fragment. fragment has a namefield which is meant to be changed on clicking OK in this popup.
This is the xml file for popup window.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:id="#+id/name_status_btns"
>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:elevation="2dp"
android:background="#fff"
android:text="CANCEL"
android:textSize="18sp"
android:textColor="#000"
android:id="#+id/cancel_change_name"/>
<TextView
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#b1b0b0"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="OK"
android:textColor="#000"
android:textSize="18sp"
android:elevation="2dp"
android:background="#fff"
android:id="#+id/ok_change_name"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#b1b0b0"
android:layout_above="#id/name_status_btns"
android:id="#+id/name_status_horizontal_liner"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:id="#+id/name_status_edit_field">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:hint="write your name here"
android:scrollHorizontally="true"
android:id="#+id/change_name"
android:focusable="true"
android:focusableInTouchMode="true"/>
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/mainColor"
android:layout_below="#id/name_status_edit_field"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
/>
</RelativeLayout>
This is the java file for above xml.
public class Name_Status extends AppCompatActivity implements View.OnClickListener {
EditText name_change;
RelativeLayout name_status_edit_field;
String name;
Button cancel_name_change , ok_name_change;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.name_status);
DisplayMetrics dm=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;
getWindow().setLayout((int)(width*.8),(int)(height*.3));
name_change=(EditText)findViewById(R.id.change_name);
name_status_edit_field=(RelativeLayout)findViewById(R.id.name_status_edit_field);
name=name_change.getText().toString();
cancel_name_change=(Button)findViewById(R.id.cancel_change_name);
ok_name_change=(Button)findViewById(R.id.ok_change_name);
ok_name_change.setOnClickListener(this);
name_change.setOnClickListener(this);
name_status_edit_field.clearFocus();
name_change.setFocusable(true);
//name_change.update();
}
#Override
public void onClick(View v) {
if(v.getId()==R.id.ok_change_name)
{FragActivity1 obj=new FragActivity1();
obj.changeName(name);
Intent intent=new Intent();
intent.setClass(Name_Status.this , MainActivity.class);
startActivity(intent);}
if(v.getId()==R.id.change_name)
{
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInputFromWindow(name_change.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
}
}
}
The main problem is your EditText has a 0dp width and a layout_weight attribute in a RelativeLayout, which does not make any sense.
Either make the parent of the EditText a LinearLayout or remove the layout_weight attribute from the EditText and give it a proper width:
<EditText
android:id="#+id/change_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="write your name here"
android:scrollHorizontally="true"
android:focusable="true"
android:focusableInTouchMode="true"/>
The following code is completely unnecessary, you should just remove it from onClick():
if(v.getId()==R.id.change_name)
{
InputMethodManager imm = (InputMethodManager) getSystemService(Context.
INPUT_METHOD_SERVICE);
imm.toggleSoftInputFromWindow(name_change.getApplicationWindowToken(),
InputMethodManager.SHOW_FORCED, 0);
}
You can also try the following for EditText
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(final View v, final boolean hasFocus) {
if (hasFocus && editText.isEnabled() && editText.isFocusable())
editText.post(new Runnable() {
#Override
public void run() {
final InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
});
}
});
For more details refer Keyboard not shown when i click on edittextview in android?
EditText is not suppose to have 0dp width and layout_weight inside a RelativeLayout. Weight works inside LinearLayout. So change your EditText to below:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="write your name here"
android:scrollHorizontally="true"
android:id="#+id/change_name"
android:focusable="true"
android:focusableInTouchMode="true"/>
And change your OnClick to because EditText will take care of click by itself:
#Override
public void onClick(View v) {
if(v.getId()==R.id.ok_change_name){
FragActivity1 obj=new FragActivity1();
obj.changeName(name);
Intent intent=new Intent();
intent.setClass(Name_Status.this , MainActivity.class);
startActivity(intent);
}
}
And finally remove the below line from your OnCreate() method:
name_change.setOnClickListener(this);
Because EditText will open keyboard by itself when clicked.

EditText value is reversed

I've got an odd one. I'm trying to format some text while the user types into an EditText field. I am using the TextWatcher and responding to afterTextChanged event (code below). What is odd is after the first time this logic runs the text starts to become reversed. It seems that the Editable object contains the string backwards. Does anyone have any idea how to fix this?
_textWatcher = new TextWatcher()
{
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3)
{
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3)
{
}
#Override
public void afterTextChanged(Editable editable)
{
Editable e = textView.getText();
String text = e.toString();
logger.d("[FormListAdapter][addTextChangedListener] Format Text: " + text);
Number numVal = FormFieldBusiness.ConvertStringToNumber(text, formField.GetFormFieldType());
String fText = FormFieldBusiness.GetFormattedValue(numVal, formField.GetFormFieldType());
editText.removeTextChangedListener(_textWatcher);
textView.setText(text);
editText.addTextChangedListener(_textWatcher);
}
};
editText.addTextChangedListener(_textWatcher);
* UPDATE *
In an attempt to help anyone out there who's looking at this here is my XML layout file.
As I mentioned before the text is correct until after the first the time setText method is called. After that the Editable object is reversed.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="120dp">
<!-- Top Layout -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:weightSum="10"
android:id="#+id/TopLayout">
<TextView
android:id="#+id/TitleTextView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="7"
android:gravity="center_vertical"
android:text="Headling Rent (pa)"
android:textSize="16dp"
style="#style/KelFormListItemLabel" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:weightSum="2"
android:background="#color/formListItemValueBackgroundColor">
<ImageView
android:id="#+id/CurrencyIconView"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_weight="0"
android:src="#drawable/icon_currency_pound"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp" />
<EditText
android:id="#+id/ValueTextView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:gravity="right|center_vertical"
android:text="20,000"
android:textSize="16dp"
android:inputType="number"
android:selectAllOnFocus="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:textIsSelectable="true"
style="#style/KelFormListItemValue"/>
</LinearLayout>
</LinearLayout>
<!-- Bottom Layout -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:weightSum="10"
android:id="#+id/BottomLayout"
android:layout_below="#+id/TopLayout">
<TextView
android:id="#+id/BottomTitleTextView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="7"
android:gravity="center_vertical"
android:text="Headling Rent (pa)"
android:textSize="16dp"
style="#style/KelFormListItemLabel" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:weightSum="2"
android:background="#color/formListItemValueBackgroundColor">
<ImageView
android:id="#+id/BottomCurrencyIconView"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_weight="0"
android:src="#drawable/icon_currency_pound"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp" />
<EditText
android:id="#+id/BottomValueTextView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:gravity="right|center_vertical"
android:text="20,000"
android:textSize="16dp"
android:inputType="number"
android:selectAllOnFocus="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:textIsSelectable="true"
style="#style/KelFormListItemValue"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
The issue is you are setting the text in the EditText field (even though you have removed the listener) you are listening for. this is causing the cursor to move to the beginning of the line, therefore making it type backwards
try changing from a TextWatcher to TextView.OnEditorActionListener and listen for the action key set on the EditText so when they hit the action key, it does your desired functionality.
add
android:imeOptions="actionDone"
to your button xml and assign it with
TextView.OnEditorActionListener editingActionListener = new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_DOWN) || actionId == EditorInfo.IME_ACTION_DONE) {
//do stuff where you set the text here
}
return true;
}
};
editText.setOnEditorActionListener(editingActionListener);
alternatively, you can use OnFocusChangeListener
View.OnFocusChangeListener focusChangeListener = new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (v.getClass().equals(EditText.class)) {
if (!hasFocus) {
//do your stuff here
}
}
}
};
editText.setOnFocusChangeListener(focusChangeListener);
Simply move the cursor to the correct position in your TextWatcher after setting the EditText content:
#Override
public void afterTextChanged(Editable editable)
{
...
int position = editText.getSelectionStart();
editText.setText(text);
editText.setSelection(position);
...
}

How to set EditText values to the RangeSeekbar when edittext values based moving the rangeseekbar?

I am facing one issue while using customRangeSeekBar in android when rangeseekbar movies based on Edittext values my RangeSeekbar values Startingvalue startingvalue 0 and endingvalue 30
My requirement is suppose i will take two edittexts
firstedittext value is 10 and second edittextvalue is 20 based on this values RangeSeekbar will move
I wrote xml like:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >
<!-- <RelativeLayout
android:id="#+id/relative1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" >
<WebView
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout> -->
<RelativeLayout
android:id="#+id/relative2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/relative1"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentLeft="true"
>
<TextView
android:id="#+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:text="Start"
android:paddingBottom="5dp"
android:textColor="#0072ff"
android:textStyle="bold" />
<EditText
android:id="#+id/edt_starttime"
android:layout_width="50dp"
android:layout_height="30dp"
android:background="#0072ff"
android:inputType="number"
android:imeOptions="actionDone"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentRight="true"
>
<TextView
android:id="#+id/end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingBottom="5dp"
android:text="End"
android:textColor="#0072ff"
android:textStyle="bold"/>
<EditText
android:id="#+id/edt_endtime"
android:layout_width="50dp"
android:layout_height="30dp"
android:background="#0072ff"
android:inputType="number"
android:imeOptions="actionDone"/>
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/relative2"
android:orientation="horizontal"
android:layout_margin="10dp"
android:id="#+id/layout">
</LinearLayout>
</RelativeLayout>
I used custom RangeSeekbar class like
RangeSeekBar.java
this class is developed based on this link
and finally my activity class is
MainActivity2.java
public class MainActivity2 extends Activity
{
//declaring widgets and variables...............
WebView webView1;
TextView start,end;
EditText edt_starttime,edt_endtime;
String url,edt_starttime1;
View handler1;
int minValue1;
#Override
//onCreate() starts............
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//getting refrences of widgets.......
webView1=(WebView)findViewById(R.id.webView1);
start=(TextView)findViewById(R.id.start);
end=(TextView)findViewById(R.id.end);
edt_starttime=(EditText)findViewById(R.id.edt_starttime);
edt_endtime=(EditText)findViewById(R.id.edt_endtime);
edt_starttime = (EditText) findViewById(R.id.edt_starttime);
// edittext listenr starts...........
// editettxt start listener ends.........
//editetxt end listener starts..............
edt_endtime.setOnEditorActionListener(new OnEditorActionListener()
{
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
// TODO Auto-generated method stub
if (actionId == EditorInfo.IME_ACTION_DONE)
{
Log.e("Ram san","jhgkfdjgh");
String edt_endtime1=edt_endtime.getText().toString();
if(edt_endtime1.length()!=0)
{
minValue1=Integer.parseInt(edt_endtime1);
Log.e("", ""+minValue1);
}
return true; // consume.
}
// pass on to other listeners.
return false;
}
});
//edittext listeners ends.................
// create RangeSeekBar as Integer range between 20 and 75
RangeSeekBar<Integer> seekBar = new RangeSeekBar<Integer>(0, 30, MainActivity2.this);
seekBar.setLeft(25);
seekBar.setOnRangeSeekBarChangeListener(new OnRangeSeekBarChangeListener<Integer>()
{
#Override
public void onRangeSeekBarValuesChanged(RangeSeekBar<?> bar, final Integer minValue, Integer maxValue)
{
edt_starttime.setText(""+minValue);
edt_endtime.setText(""+maxValue);
Log.e("values ", ""+ minValue + ", MAX=" + maxValue);
}
});
// add RangeSeekBar to pre-defined layout
ViewGroup layout = (ViewGroup) findViewById(R.id.layout);
// layout.setP
layout.addView(seekBar);
}
//onCreate() ends.................
}
How can I set EditText values to the RangeSeekbar when edittext values are based on moving the rangeseekbar?

How to make button come down after edit text field?

I have a piece of code which shows a static text, an edit text as a search box and a submit button to submit the query .
The code kind of looks like this :
public class MyActivity extends Activity {
TextView textView;
private EditText edittext;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mine);
textView = (TextView) findViewById(R.id.textView1);
textView.setText("Enter your search String :");
addKeyListener();
addListenerOnButton();
}
//Implement the method
public void addKeyListener() {
// get edit text component
edittext = (EditText) findViewById(R.id.searchText);
// add a key listener to keep track user input
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// if key down and "enter" is pressed
if ((event.getAction() == KeyEvent.ACTION_DOWN)
&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
// display a floating message
Toast.makeText(RecipeActivity.this,
edittext.getText(), Toast.LENGTH_LONG).show();
return true;
} else if ((event.getAction() == KeyEvent.ACTION_DOWN)
&& (keyCode == KeyEvent.KEYCODE_9)) {
// display a floating message
Toast.makeText(RecipeActivity.this,
"Aboriginal text!", Toast.LENGTH_LONG).show();
return true;
}
return false;
}
});
}
//Implement the button
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
/* I will capture the search string here */
}
});
}
}
The activity screen comes up like this :
EDIT : The XML layout:
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".RecipeActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<EditText
android:id="#+id/searchText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:label="#string/search_label"
android:hint="#string/search_hint" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</RelativeLayout>
As you can see the submit button is overlapping the edit text search box. How can i make the button element come down ?
It would be better to use LinearLayout with android:orientation="vertical" in your xml Layout because it's much cheaper than RelativeLayout to render.
Solution with LinearLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="#+id/searchText"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
If you have to use RelativeLayout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="#+id/searchText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/textView1"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/searchText"
android:text="Submit" />
</RelativeLayout>
See RelativeLayout Documentation for further Informations.
Use android:layout_alignParentBottom="true" in your
add android:below reference
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:below="#+id/searchText"
android:text="Submit" />

Categories

Resources