Soft keyboard covers an EditText in a PopupWindow - java

I've thrown together a simple test project that displays a PopupWindow containing an EditText (on Android 2.2). When I tap the EditText, the soft keyboard is shown, as I would expect. However, the soft keyboard covers the EditText, and I cannot get the screen to pan to keep the EditText in view in the way I would have thought it should. My code:
TestAdjustPanActivity.java:
package com.example;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.PopupWindow;
public class TestAdjustPanActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final PopupWindow pw = new PopupWindow(this);
pw.setContentView(getLayoutInflater().inflate(R.layout.popup, null, false));
pw.setWidth(400);
pw.setHeight(600);
pw.setFocusable(true);
pw.setOutsideTouchable(true);
pw.setTouchable(true);
pw.setBackgroundDrawable(new BitmapDrawable());
// This is the line referred to in the edit:
pw.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
findViewById(R.id.main).post(new Runnable() {
public void run() {
pw.showAtLocation(findViewById(R.id.main), Gravity.NO_GRAVITY, 0, 0);
}
});
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
</LinearLayout>
popup.xml:
<?xml version="1.0" encoding="UTF-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#666666"
android:orientation="vertical" >
<EditText
android:id="#+id/current_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="300dp" />
</LinearLayout>
</ScrollView>
...and my AndroidManifest.xml does contain the line android:windowSoftInputMode="stateUnspecified|adjustPan" in the tag for the one and only activity in the app.
Any ideas? None of the other posts on SO related to this issue have done it for me. What am I missing that's keeping the view from panning the way I would expect it to?
EDIT: I tried adding a line in TestAdjustPanActivity as indicated, which caused the screen to pan perfectly on an Android 3.2 device I have. However, it still doesn't pan on my Android 2.2 device, which makes this even more confusing.

For anyone stumbling upon this in the future, I ended up just using a Dialog instead of a PopupWindow, and panning to keep the EditText in view works fine on 2.2 with a Dialog.

There is simpler way. Just provide alternative layout for active keyboard.
Left click on project select: "Android tools/New Resource File..." .
Chose layout, give file name "main" (don't worry about conflicts).
Click "next". Then on list on the left select "keyboard" and move it to right (click "->").
On right side select keyboard state.
Click finish.
Now copy content of your main.xml located in "res/layout" to new file in res/layout-keyssoft".
Correct new layout in such way that keyboard is not in that way. Remember to maintain same "id"s for respective components in those two layouts (that is why copy paste was needed).
Enjoy how it works
Read about configuration changes to understand how it works. Note that EVERY configuration change (orientation change, language change, ...) will cause recreation of Activity (in such case argument of onCreate is not null) so you can provide different layouts for different cases.

I doubt the problem is because of using that ScrollView in XML,
There has been another problem similar to this, You can check all different answers there, and may work for you.

change this
android:windowSoftInputMode="stateUnspecified|adjustPan"
to
android:windowSoftInputMode="adjustPan"
only in your manifest file

You should change
android:windowSoftInputMode="stateUnspecified|adjustPan
to
android:windowSoftInputMode="stateHidden|adjustPan"

It took couple of hours to figure out and I ended up using scrollview.
Apparently there is no way to overcome this problem when you use popup window and soft input keypad hide your view. Just go for Scroll View.

Related

Click arrow to reveal more content in application

i am trying to achieve a behavior where user click an arrow that can reveal more content such as more description abort something. It can a recycler view as well where more things can be added dynamically and the list will expand.Right now i do not have any idea how it can be achieved. I tried searching on the internet for solutions and saw a widget called spinner but i do not think it can help me achieve my desired behavior. YouTube does apply similar behavior as well
Below are the pictures which will make my question clear. Any help would be appreciated Thank You
Before clicking the arrow pic 1
After clicking the arrow pic 2
In your layout.xml include a nested layout that includes a Textview that holds the additional information and set android:visibility="gone". Use an OnClickListener to the button that is meant to expand the view. In the onClick method check if the view is visible or not. If it's not you make it visible, otherwise you set it to gone again.
layout:
...
<ImageView
android:id="#+id/chevron"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/chevron"
/>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="your additional info here"
android:visibility="gone"/>
...
In your Activity:
ImageView yourView = findViewById(R.id.chevron);
..
yourView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (view.getVisibility() == View.Gone) {
view.setVisibility(View.Visible);
} else {
view.setVisibility(View.Gone);
}
}
});
I used for this purpose ExpandbleLayout from this github library
ExpandableLayout. In readMe of the github repo you can find example of using it, you can get similar experience with as in your example, without need to manually create View for arrow and handling the animation.
You can use it like this :
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.github.aakira.expandablelayout.ExpandableRelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:ael_expanded="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some text goes here"
android:textSize="28sp" />
</com.github.aakira.expandablelayout.ExpandableRelativeLayout>
And in your java/kotlin code : do additional logic to expand/collapse call : expandableLayout.toggle();.
All the credit goes to the author of the library.
https://github.com/AAkira/ExpandableLayout

Display PDF On My App Offline

I am a beginner and I am asking this just to gain knowledge. so in my app, I have a MainActivity And its layout activity_main.xml.
here is the code of activity main
<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="com.testapp.myapp.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 1"
android:id="#+id/btn1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btn1"
android:text="Button 2"
android:id="#+id/btn2"/> </RelativeLayout>
And here is the MainActivity.java
package com.testapp.myapp;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}}
I Have created Two Blank activities named By Button1PDF And Button2PDF And Each Has layout named button1_pdf.xml and button2_pdf.xml respectively.
So what i wanna do is, i want to set a pdf file against these New Activities and when I will click on btn1 or btn2 On MainActivity, I want it to display the new activity Button1PDF or Button2PDF activity which will be containing the pdfs.
I tried this online using webview and it works flawlessly. but now i want it to display offline(means complete offline)
i have done some research and i found some libraries like
compile 'com.github.barteksc:android-pdf-viewer:2.0.3'
but i don't know how to use it. hope someone can clear my doubts.
thanks.
You will need to download the files first and then display them to the user using that library.
Read this question on how to download file from internet

How add multiple TextView inside an ImageButton?

Is it possible to add multiple TextView inside one ImageButton with colour background ?
The core need is to have a button with the action text on it, and a subtext nearby explaining the action or giving other information related to the action. This subtext can vary from time to time.
Considering this requirement, one solution is to have a normal button and a subtext below, not clickable. But I find it messy. A better approach which I like is, on iOS for instance, to have a clickable UIView containing the action as bold text and the explanation as light text. See the image bellow containing 4 buttons :
How to achieve the same on Android with Java ? The closest I can have is to have an ImageButton bellow a TextView, and it does not sound right.
Is possible to nest TextViews inside an ImageButton ? If not, what is the best alternative ?
I hope this may be useful it explains how to position a textView within and in front of a imageView in the XML.
TextView inside of ImageButton/ImageView XML - Android Dev
Obviously make sure each view has a unique id/name which you can assign as shown here on this link
Sorry I cannot explain specifically myself but it has been a while since developing in Java for Android.
I dont know why you want this behaviour but you can make a container for your views and add a click listener to the whole view. you can also use it anywhere.
an example of this would be.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:background="#drawable/container_background"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="0.33"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="0.33"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="0.33"
/>
add a selector background
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#color/text_pressed" />
<item android:color="#color/normal" />
</selector>
and the listener
findViewById(R.id.container).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});

Creating UI in Android Studio and using it in Unity makes elements jump to 0,0

I made an Activity in Android Studio and added a button in there, added the constraints so the error would go away. When I run an App from Android Studio to my phone, it works fine. When I use an AAR file in Unity and call the activity from there, the buttons jump back to 0,0 like the error said if I did not add constraints, which I did. I'm not getting any errors either as to why it's not able to constraint the button.
Here is how I made the button in my activity.
activity_main.xml
<Button
android:id="#+id/button"
android:layout_width="86dp"
android:layout_height="wrap_content"
android:layout_marginBottom="431dp"
android:layout_marginEnd="130dp"
android:layout_marginStart="168dp"
android:layout_marginTop="32dp"
android:text="#string/StringName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
I've never really worked with Android Studio UI until now, so I might have forgotten something important here, just hoping one of you guys knows what could be the problem, thanks in advance.
EDIT
The problem is with "app:", everywhere you use it should be replaced with "android:". However android:layout_constraintBottom_toEndOf="parent" doesn't work. Instead you have to use: android:layout_alignParentEnd="true". Also replace "android.support.constraint.ConstraintLayout" with "RelativeLayout".
Unity doesn't work with "app:" for some reason so replacing with "android:" is always necessary. Thanks Soon Santos for telling me about RelativeLayout, I looked at this: https://developer.android.com/guide/topics/ui/layout/relative.html and remembered I had gotten an app: problem before and fixed by changing to android:.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.Company.MainActivity">
<Button
android:id="#+id/button"
android:layout_width="86dp"
android:layout_height="wrap_content"
android:layout_marginStart="168dp"
android:layout_marginTop="25dp"
android:text="#string/StringName"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="false"/>
</RelativeLayout>
You can actually leave out android:layout_alignParentEnd/Start. With RelativeLayout it seems to not jump back to 0,0 at all, so basically always use android: and RelativeLayout when using Unity (it seems like that at least).
Nothing wrong with the code, it works for me too. Maybe it is because your other way to run the code does not support constraintLayout, you can try RelativeLayout instead and see if it'll work.

Fragments are not running perfectly in real device

I have used fragments to define a simple splitview, in the right site i used a layout containing videoview the xml code is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<VideoView
android:id="#+id/tvT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
and in the left site in another fragment layout I have defined some buttons to play different videos, The program is running well in emulators, but when I run it on device, the videoview overlapped the whole area and creates a horrible look, what should I do?
You need to post more of your code if you can; what you have posted isn't really enough for people to help you. [Edit: Darn, I thought this was trivial enough to be automatically moved to a comment on the original question :-\ ]
May you could try with "fill_parent" because the video could be to big and uses the hole display:
<VideoView
android:id="#+id/tvT"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
If you can it would be good if you post some code how you setup your fragments.

Categories

Resources