Android : PopupWindow fill space above keyboard - android

I have a button inside a scrollView.When the button is clicked, I need a PopupWindow to show up with the softkeyboard up as soon as the button is clicked. This is how my code looks today :
final Button b = (Button) findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(popupView,WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.MATCH_PARENT,true);
popupWindow.showAtLocation(v.getRootView(), Gravity.FILL, 0, 0);
//Bring soft keyboard up : NOT WORKING
final InputMethodManager mInputMethodManager = (InputMethodManager) getBaseContext().getSystemService(Context.INPUT_METHOD_SERVICE);
EditText editText = (EditText) popupView.findViewById(R.id.editText);
mInputMethodManager.showSoftInput(editText, 0);
}
});
and my Layout XML looks like this :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/yourViewsEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Your Views"
>
</EditText>
</RelativeLayout>
When I try a fill_parent instead of the wrap_content, it starts off by filling the whole screen. I am trying to achieve something like this :

Perhaps try using the following xml layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:isScrollContainer="true" >
<EditText
android:id="#+id/yourViewsEditText"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:hint="Your Views" />
</RelativeLayout>
It should automatically set the height of your layout till the start of your soft-keyboard.

you can get the hight of the keyboard like this:
myLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
// TODO Auto-generated method stub
Rect r = new Rect();
parent.getWindowVisibleDisplayFrame(r);
int screenHeight = parent.getRootView().getHeight();
int heightDifference = screenHeight - (r.bottom - r.top);
loadDialog(heightDifference);
}
});
and then you can load the popupwindow with the height of the keyboard and the width of the device:
public void loadDialog(int height)
{
Display mDisplay = activity.getWindowManager().getDefaultDisplay();
int width = mDisplay.getWidth();
//load the popup window with the height and width...
}

Related

Android scaling popup window

i am writing a simple 2D Android Game (sort of puzzles). A user is drawing lines during the game and that is why I decided to an approach in which i resize in runtime to scale correctly on differrent devices. I prepared graphics for 1280x720 and I simply resize them in runtime inside:
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
FrameLayout container = (FrameLayout) findViewById(R.id.main);
LinearLayout mainView = (LinearLayout) findViewById(R.id.main_main_view);
TransformationOld transform = new TransformationOld();
transform.scaleViewToFitInContainerIsotropically(container, mainView);
}
}
The inspiration comes from: http://www.vanteon.com/downloads/Scaling_Android_Apps_White_Paper.pdf
It works fine for Activities, but I cannot have it working for popups.
The way I create popups:
public void onClickButtonMainAbout(View view) {
LayoutInflater inflater = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.popup_about_backup_with_pixels, null, false);
final PopupWindow pw = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
pw.showAtLocation(popupView, Gravity.CENTER, 0, 0);
Button button_ok = (Button) popupView.findViewById(R.id.about_button_ok);
button_ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pw.dismiss();
}
});
}
The popup in xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup_about2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/popup_about_main_view2">
<TableRow
android:id="#+id/tableRow1"
...
...
Any hints how to have such scaling working for popups to? Now it is simply displayed in 1280x720. I would be thankful for any advice.

Horizontal Pop up Menu?

I am trying to make share button when clicked a Popupmenu appears with three horizontal choices facebook, twitter and google+.
I keep searching for a while but I got nothing until now.
Is it possible to create horizontal or even grid PopupMenu?
Is it possible to use RecyclerView in PopupMenu?
Instead of a PopupMenu, would you be able to use a DialogFragment with a custom layout?
In this case, you can use PopupWindow. You can use ListView to display items in each line. Example code to show a PopupWindow:
public PopupWindow popupWindowsort() {
// initialize a pop up window type
popupWindow = new PopupWindow(this);
ArrayList<String> sortList = new ArrayList<String>();
sortList.add("Google+");
sortList.add("Facebook");
sortList.add("Twitter");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, sortList);
// the drop down list is a list view
ListView listViewSort = new ListView(this);
// set our adapter and pass our pop up window contents
listViewSort.setAdapter(adapter);
// set on item selected
listViewSort.setOnItemClickListener(onItemClickListener());
// some other visual settings for popup window
popupWindow.setFocusable(true);
popupWindow.setWidth(250);
//popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.white));
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
// set the list view as pop up window content
popupWindow.setContentView(listViewSort);
return popupWindow;
}
Visit my post for more details: http://www.devexchanges.info/2015/02/android-popupwindow-show-as-dropdown.html.
Hope this help!
Create a basic layout XML with an inner horizontal layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/popup_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
Assign the layout to the PopupMenu, then use the inner layout.
// PopupMenu accepts ViewGroup, so cast the inflated layout view
// Replace ROOT_VIEW_HERE with the parent view of the PopupMenu
LinearLayout popupWindow = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.popup_window, ROOT_VIEW_HERE);
PopupMenu popupMenu = new PopupMenu(this, popupWindow);
LinearLayout innerView = popupWindow.findViewById(R.id.popup_horizontal);
It really is that simple.
A complete solution is:
1 - The Custom PopupMenu class:
public class PopupMenuCustomLayout {
private PopupMenuCustomOnClickListener onClickListener;
private Context context;
private PopupWindow popupWindow;
private int rLayoutId;
private View popupView;
public PopupMenuCustomLayout(Context context, int rLayoutId, PopupMenuCustomOnClickListener onClickListener) {
this.context = context;
this.onClickListener = onClickListener;
this.rLayoutId = rLayoutId;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
popupView = inflater.inflate(rLayoutId, null);
int width = LinearLayout.LayoutParams.WRAP_CONTENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
boolean focusable = true;
popupWindow = new PopupWindow(popupView, width, height, focusable);
popupWindow.setElevation(10);
LinearLayout linearLayout = (LinearLayout) popupView;
for (int i = 0; i < linearLayout.getChildCount(); i++) {
View v = linearLayout.getChildAt(i);
v.setOnClickListener( v1 -> { onClickListener.onClick( v1.getId()); popupWindow.dismiss(); });
}
}
public void setAnimationStyle( int animationStyle) {
popupWindow.setAnimationStyle(animationStyle);
}
public void show() {
popupWindow.showAtLocation( popupView, Gravity.CENTER, 0, 0);
}
public void show( View anchorView, int gravity, int offsetX, int offsetY) {
popupWindow.showAsDropDown( anchorView, 0, -2 * (anchorView.getHeight()));
}
public interface PopupMenuCustomOnClickListener {
public void onClick(int menuItemId);
}
}
2 - Your custom layout, e.g. linearlayout with horizontal layout. In this case I use a simple LinearLayout with TextView items. You can use Buttons, etc.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="horizontal">
<TextView
android:id="#+id/popup_menu_custom_item_a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="A"
android:textAppearance="?android:textAppearanceMedium" />
<TextView
android:id="#+id/popup_menu_custom_item_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="B"
android:textAppearance="?android:textAppearanceMedium" />
// ...
</LinearLayout>
3 - Using the Custom PopupMenu like the normal PopupMenu.
PopupMenuCustomLayout popupMenu = new PopupMenuCustomLayout(
MainActivity.mainActivity, R.layout.popup_menu_custom_layout,
new PopupMenuCustomLayout.PopupMenuCustomOnClickListener() {
#Override
public void onClick(int itemId) {
// log statement: "Clicked on: " + itemId
switch (itemId) {
case R.id.popup_menu_custom_item_a:
// log statement: "Item A was clicked!"
break;
}
}
});
// Method 1: popupMenu.show();
// Method 2: via an anchor view:
popupMenu.show( anchorView, Gravity.CENTER, 0, 0);

How Adjust Activity on Keypad is shown

I have an Activity masked as Dialog.
I would like to adjust the activity when the keypad is shown.
My layout resource:
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
...
</ScrollView>
</LinearLayout>
In my manifest:
android:theme="#android:style/Theme.Translucent.NoTitleBar.Fullscreen"
android:windowSoftInputMode="adjustResize"
Somethimes android:theme="#android:style/Theme.Translucent.NoTitleBar.Fullscreen" is causing some problems and the window is not resized. Remove it to see if it works and than check for an alternative.
There is not any setting in manifest you can do to achieve this you have to add an extra view of height (something you want ) and width 0 to your layout at the end of the layout. Just make it visible when keypad appears. To find state of keypad use this
int lastDiff = 0;
public static boolean keypadopen;
and to your onCreate() add this
addKeyBoardHandler();
it will set boolean keypadopen with the current status of keypad.If it is true then add an extra view of required height which will scroll up the view.
private void addKeyBoardHandler()
{
final View activityRootView = findViewById(R.id.container);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout()
{
Rect r = new Rect();
activityRootView.getWindowVisibleDisplayFrame(r);
int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
if (lastDiff == heightDiff) return;
lastDiff = heightDiff;
System.out.println("Keypad height :"+heightDiff);
if (heightDiff > 100)
{
keypadopen = true;
keypadListner.onKeypadOpen(heightDiff+10);
}
else
{
keypadopen = false;
keypadListner.onKeypadOpen(heightDiff+10);
}
}
});
where container is the id of the root view.

How to dynamically add a button to a popup window?

I'm trying to write a function that opens a popup window, and populates it with button links to all of the previous pictures or recorded audio files the app has recorded. I tried to write the function so that it will work for audio files or images. The goal is to have a pop-up window appear on the screen which has a button for each of the previous files and will open the designated file on click. The app will successfully open the popup window, but it will only be populated by the auto-defined button (back) which will close the popup window. However, white space appears underneath the back button, which increases the height of the scroll view and allows for scrolling within the pop-up window but no buttons actually appear.
Initially, my problems came from not including pw.getContentView() (pw is my popupwindow) for each of my layout elements, but I'm not sure what it does exactly so that may be a part of the problem.
This is the function that is called to open the popup window.
private void display_media(int check, String header_text){
//The check variable is a 1 or 0 depending on if it's an image or an audio.
// header_text is a simply text to replace the default header.
try{
LayoutInflater inflater = (LayoutInflater) NewPlan_Two.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.files_layout, (ViewGroup) findViewById(R.id.show_media));
pw = new PopupWindow(layout,width/4,height/3,true);//width and height are predefined values
Button close_button = (Button) layout.findViewById(R.id.back_scroll);
close_button.setOnClickListener(close_view);
TextView header =(TextView) pw.getContentView().findViewById(R.id.previous_files);
header.setText(header_text);
LinearLayout l_l = (LinearLayout) pw.getContentView().findViewById(R.id.scroll_linear);
for(String media_file: files){ // this is defined and explained below
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button new_button = new Button(this);
if(check==0){
new_button.setId(image_id_count);
image_id_count++; // this is initialized to 3000
button_id = new_button.getId();
}
else{
new_button.setId(audio_id_count);
audio_id_count++; // this is initialized to 2000
button_id = new_button.getId();
}
new_button.setText(media_file);
l_l.addView(new_button,params);
if(check==0)
button_two= (Button) pw.getContentView().findViewById(button_id);
else
button_two = (Button) pw.getContentView().findViewById(button_id);
button_two.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
pw.dismiss();
// this will be replaced with code to open the image or audio, but I haven't written it yet.
}
});
}
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
}catch(Exception e){
e.printStackTrace();
}
}
To parse the audio or image files, I use this function. It is successfully getting the names of the files. It is called with either "mp4" or "png" as the argument.
private void parse_Files(String fileType){
files = new ArrayList<String>();
File folder = new File(abs_path); // abs_path is the path to the folder with the files
File[] list_of_files = folder.listFiles();
for(int count = 0; count < list_of_files.length;count++){
String file_name = list_of_files[count].getName();
String[] parsed_list = file_name.split("\\.");
if(parsed_list[1].equals(fileType))
files.add(parsed_list[0]);
}
}
Finally here is the xml file that opens as the popup window.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/show_media"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding = "15dp"
android:background="#DCDDF7">
<TextView
android:id="#+id/previous_files"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/previous_files"
android:textSize="20sp"/>
<ScrollView android:layout_height="wrap_content" android:layout_width="match_parent" android:background="#FFFFFF" >
<LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:id="#+id/scroll_linear">
<Button
android:id="#+id/back_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/back"
android:textSize="18sp"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
Thanks!
Edit:
I tried to change it to a list view, but I am getting the same issue. It opens a pop-up and there are x buttons corresponding to x associated files, but none of the buttons have text or do anything on click. What am I missing?
Here is the revised code:
private void display_media(int check, String header_text){
try{
display_media_array=new String[files.size()];
display_media_array=files.toArray(display_media_array);
LayoutInflater inflater = (LayoutInflater) NewPlan_Two.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.files_layout, null);
pw = new PopupWindow(layout,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,true);
ListView listView = (ListView) layout.findViewById(R.id.list_view);
listView.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, display_media_array));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
pw.dismiss();
}
});
Button close_button = (Button) layout.findViewById(R.id.back_scroll);
close_button.setOnClickListener(close_view);
TextView header =(TextView) pw.getContentView().findViewById(R.id.previous_files);
header.setText(header_text);
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
}catch(Exception e){
e.printStackTrace();
}
}
And here is the new xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/show_media"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#DCDDF7"
android:orientation="vertical"
android:padding="15dp" >
<TextView
android:id="#+id/previous_files"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/previous_files"
android:textSize="20sp" />
<ListView
android:id="#+id/list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF" >
</ListView>
<Button
android:id="#+id/back_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/back"
android:textSize="18sp" />
</LinearLayout>
Any help is appreciated, thanks!

Set height and width in an Dialog Box?

I am using a XML-layout which I am prompting as the dialog box.
Designing of XML-layout is well formatted with enough required height and width..
But when I open it as the dialog box its width is getting disturbed so how to set height and width of dialog box through coding.
I even had referred this previous STACK OVERFLOW QUESTION
Here is the code:
// Layout Inflater Code..
editDialog = new Dialog(this);
layoutEdit = LayoutInflater.from(this).inflate(R.layout.createlayout, null);
//layoutEdit.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
editDialog.setContentView(layoutEdit);
// Called the Dialogbox to inflate
updateButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
editDialog.show();
}
});
// XML File Code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/bd"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="false"
android:text="Enter Name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/whtie"
android:typeface="monospace" />
<EditText
android:id="#+id/txtname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" >
</EditText>
</LinearLayout>
</ScrollView>
Try this...
1.Dialog snippet:
private void CustomDialog(String msg) {
final Dialog dialog = new Dialog(YourActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
LinearLayout.LayoutParams dialogParams = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, 300);//set height(300) and width(match_parent) here, ie (width,height)
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dislogView = inflater
.inflate(R.layout.my_custom_popup, null);
dialog.setContentView(dislogView, dialogParams);
TextView popupMsg = (TextView) dialog.findViewById(R.id.popupMsg);
Button popupOk = (Button) dialog.findViewById(R.id.popupOk);
popupMsg.setText(msg);
popupOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
2.Then call CustomDialog(Str) where you want to prompt in your activity.
CustomDialog("This is customized popup dialog!");
You better use an activity that looks like a dialog (I feel it will be better in your case). Here is an example code:
public class DialogActivity extends Activity {
/**
* Initialization of the Activity after it is first created. Must at least
* call {#link android.app.Activity#setContentView setContentView()} to
* describe what is to be displayed in the screen.
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
// Be sure to call the super class.
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_LEFT_ICON);
// See assets/res/any/layout/dialog_activity.xml for this
// view layout definition, which is being set here as
// the content of our screen.
setContentView(R.layout.dialog_activity);
getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
android.R.drawable.ic_dialog_alert);
}
}
This code is from api demos
View layout = inflater.inflate(R.layout.view, NULL);
layout.setMinimumWidth(200);
layout.setMinimumHeight(200);
dialog.setContentView(layout);
Try
dialog.getWindow().setLayout(height, width);

Categories

Resources