Space between keyboard and EditText in Android - java

Soft input mode is "SOFT_INPUT_ADJUST_PAN", and when keyboard shown, EditText moves up to stay visible, but the keyboard and txt's bottom are always sticked together as seen on screenshot, I want some space between them, is it possible?
Also input mode must stay as "SOFT_INPUT_ADJUST_PAN".
Sorry form my english, Thanks in Advance...

I applied above methods which worked for me was
Set android:windowSoftInputMode on the Activity to "adjustPan"
than i added the padding to the edittext with gravity bottom
and it works like charm.
Thanks,

May this help you:
If this line is written in your code then remove it:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
OR:
You should not be setting your layout_width and layout_heights with explicit pixel values. Instead, use wrap_parent or fill_parent as appropriate.
OR:
If you have set your minSdkVersion to 3 then change it to minSdkVersion=4
Edit:
Set android:windowSoftInputMode on the Activity to "adjustPan"
OR:
Add this code in your main activity in setOnTouchListener() of your EditText:
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED);

Why don't you try this?
android:windowSoftInputMode="adjustResize"
I know you want to adjust pan but this would solve your problem aswell.
Since you really want to stay with adjustPan, maybe you can try re-designing the XML layout a little bit.
You must also add your required padding to the outer most container element in the layout file.

Simple padding in edit text won't work.
Use drawable with your edit text like below:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="8dp"
android:left="8dp"
android:right="8dp"
android:top="8dp">
<shape android:shape="rectangle">
<corners android:radius="10dp" />
<stroke
android:width="1dp"
android:color="#color/white" />
<solid android:color="#color/transparent" />
</shape>
</item>
</layer-list>
and then use
android:windowSoftInputMode="adjustPan"
in your manifest file.

Just wondering, is your app full screen? does it have the status bar? If it is try to disable full screen mode and try to see if that solves your problem. I remember there was a bug about this some time ago.

try to use GlobalLayoutListener:
private var focusView:View?=null
// Register global layout listener.
decorView?.viewTreeObserver?.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
private val windowVisibleDisplayFrame = Rect()
private var lastVisibleDecorViewHeight: Int = 0
override fun onGlobalLayout() {
// Retrieve visible rectangle inside window.
decorView.getWindowVisibleDisplayFrame(windowVisibleDisplayFrame)
val visibleDecorViewHeight = windowVisibleDisplayFrame.height()
// Decide whether keyboard is visible from changing decor view height.
if (lastVisibleDecorViewHeight != 0) {
if (lastVisibleDecorViewHeight > visibleDecorViewHeight + MIN_KEYBOARD_HEIGHT_PX) {
// Calculate current keyboard height (this includes also navigation bar height when in fullscreen mode).
val currentKeyboardHeight = decorView.height - windowVisibleDisplayFrame.bottom
//keyboardHeight = currentKeyboardHeight
focusView = activity?.currentFocus
focusView.setPadding(0, 0, 0, SET_BOTTOM_PADDING_SPACE) // <---set space here
} else if (lastVisibleDecorViewHeight + MIN_KEYBOARD_HEIGHT_PX < visibleDecorViewHeight) {
focusView.setPadding(0, 0, 0, 0)
}
}
// Save current decor view height for the next call.
lastVisibleDecorViewHeight = visibleDecorViewHeight
}
})

Related

Set height of RelativeLayout to match height of Soft Keyboard

I need to set the height of this RelativeLayout to match the height of the Soft Keyboard. To make sure the EditText appears just above the keyboard.
Is it possible to set some kind of Listener to detect every time the keyboard height changes?
The proper way to do this would be to set soft input to match your needs.
try following for a fragment put this line in your fragment's onCreate method:
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
You can set multiple modes with a single line like
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE|WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
And if you are working in an activity you can just adjust it in your android manifest file for each activity
<activity
android:name=".SomeActivity"
android:windowSoftInputMode="adjustResize" />
or
android:windowSoftInputMode="adjustPan|adjustResize"
Check which settings suits your needs better between adjustPan, adjustUnspesified, adjustResize or some other soft input options.
I figured out a solution that seems to work fine. With only 1 problem. For some reason the Status Bar turns white with this piece of code.. Any idea why?
ViewCompat.setOnApplyWindowInsetsListener(this.getWindow().getDecorView(), (v, insets) -> {
int keyboardHeight = insets.getInsets(WindowInsetsCompat.Type.ime()).bottom;
//Code logic
return insets;
});
How I set the Status Bar color:
<style name="AppTheme.GuessWord">
<item name="colorPrimaryDark">#color/primaryColor</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

Dim Complete Background When a Fragment is opened in Android

How to dim complete background when a fragment is opened likewise when a DialogFragment is created.
Rightnow I am able to dim the background of only the app window using a view with match_parent as height and width. But using it does not dim the background of the status bar which happens with the DialogFragment.
I want to dim complete background including apps window and the status bar when the fragment is created or opened.
Right now this happens with the DialogFragment. But I have to use only Fragment.
That's what I did
I took a relative layout with height and width match_parent, apart from my fragment's main layout
<RelativeLayout
android:id="#+id/rl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/background_transition"/>
This is my background_transition file
<?xml version="1.0" encoding="UTF-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="00FFFFFF" />
<item android:drawable="#B3FFFFFF" />
</transition>
Then in order to dim the background, create TransitionDrawable object like this in your fragment
private TransitionDrawable transition;
and then call
transition.startTransition(300);
to dim the background, and
transition.reverseTransition(300);
to go back to normal background.
Hope that helps.
I tried Nuzha's version and my app crashed as well.
I did find two possible solutions.
1) If you use the following code it will make the back-fragment whiter, not dimmed:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
if (Build.VERSION.SDK_INT >= 23) {
view.alpha = 0.2F
}
Note:
Depending on the value you set, the closer the Float is to 0, the more transparent-white the background will be (only works with 23+)
2) If you would like the back-fragment to be dimmed (dark)
First, you can create a color in your resources folder and make it a bit transparent. I for one used the following one for a darker effect: #7E000000
Next, in the onViewCreated() method, create a variable of the color and add it as a ColorDrawable to the view's foreground.
if (Build.VERSION.SDK_INT >= 23) {
val color: Int = R.color.black_transparent
view.foreground = ColorDrawable(resources.getColor(color, resources.newTheme()))
Note
Make sure to disable any buttons that might be visible in the back-fragment, otherwise, if the user presses it, the app will crash
buttonRegisterEvent.isEnabled = false
As a final step, if you want the user to navigate back to the former fragment, make sure to remove the foreground color and enable any buttons that you might have disabled.
Try this:
fragmentParentLayout.getForeground().setAlpha(215);

Change the text color of an unidentified radio button

I have a radio group with 4 radio buttons. I use this code :
int radioButtonID = radioGroup.getCheckedRadioButtonId();
View checkedRadioButton = radioGroup.findViewById(radioButtonID);
int idx = radioGroup.indexOfChild(checkedRadioButton);
To retrieve the index of the checked radio button.
The problem is that i want to change the text color of the checked radio button, while i dont know the specific radio button each time. so: checkedRadioButton.setTextColor(color);
shows me an error that i need to 'Add a qualifier', which basicaly is showing me that i should use a specific radio button to call that method, like:
radioButtonAns1.setTextColor(color);
I would like if someone would also explain why i have that error and if there is a solution on that. The only method i can call is .setBackgroundColor() which looks ugly.
Thanks in advance!
You can use the setTextColor method if you cast the View that you found to RadioButton:
RadioButton checkedRadioButton = (RadioButton) radioGroup.findViewById(radioButtonID);
checkedRadioButton.setTextColor(color);
Well it is kinda of straight forward, the error is basically saying hey you trying to change the color of the group containing the groups of radio button.Please choose which one you want first then change the it. To remedy this declare a Boolean array,then set a on click listener to detect the position of the clicked radio button then set then set Boolean value to true in the array.
An XML alternative would be:
set the TextColor as a #Color selector.
<RadioButton
android:id="#+id/radio_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio 1"
android:textColor="#color/radiobuttonstate"/>
The color selector would be:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#000000"/>
<item android:state_checked="true" android:color="#ffffff"/>
<item android:color="#00f"/>
</selector>

Android: set margin of TableRow's contents (means: margin between columns)

I create a TableLayout dynamically via code and want to set a margin between columns. The only type of content my TableRows contain are TextViews.
My intention was to put a simple android:layout_marginRight on each TextView. But I want to definde this via xml instead of code.
What I tried:
The code:
txtView.setTextAppearance(context, R.style.TableTextView);
txtView.setText(content);
tableRow.addView(txtView);
The XML
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="TableTextView">
<item name="android:textAppearance">?android:attr/textAppearanceLarge</item>
<item name="android:textStyle">bold</item>
<item name="android:layout_marginRight">5dip</item>
</style>
</resources>
What happens:
The layout_marginRight set in the XML does not work, but the textAppearance and textStyle set in the XML do work. I assume the setTextAppearance-method is the wrong way for assigning a margin to a TextView? It would be really nice if I could do this via XML (like I tried it above) instead of Java-code.
Thank you!
This happens because you're setting the style to the text itself and not the TextView element.
You should set the element style in the XML. it's possible to achive this from the code as well, but i think it's best to do that in the XML Layout file.
Something like :
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/mTableTextView" />
About setting that from code, i'm not an expert but i understood you can inflate it somehow.
Check out This, And This questions.
You want to give margin between columns
android.widget.TableRow.LayoutParams param = new android.widget.TableRow.LayoutParams();
param.rightMargin = Converter.dpToPixel(10, getContext()); // right-margin = 10dp
button.setLayoutParams(param);
// Converter:
private static Float scale;
public static int dpToPixel(int dp, Context context) {
if (scale == null)
scale = context.getResources().getDisplayMetrics().density;
return (int) ((float) dp * scale);
}
you can set different of value table parameters.

Expandable Listview Group Indicator half off screen?

I am using a custom Group indicator as shown in the code below. Now if I leave the default I can use setBounds perfectly, but when using my custom image which is the same dimensions and setup as the same .9 patch file it is always half off screen. No matter what values I use for setBounds()
Drawable plus = (Drawable) getResources().getDrawable(R.drawable.expander_group);
getExpandableListView().setGroupIndicator(plus);
Display newDisplay = getWindowManager().getDefaultDisplay();
int width = newDisplay.getWidth();
getExpandableListView().setIndicatorBounds(0,0);
XML expander_group
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_expanded="true"
android:drawable="#drawable/minus" />
<item
android:drawable="#drawable/plus" />
</selector>
EDIT:
Interestingly. I have copied the exact xml and image files from the standard Android files. And when I use the above method to define it the exact same thing happens! So it is not the xml or image files that are causing the issue I think.

Categories

Resources