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.
Related
Currently for inflating images (zoom in animation) we use this library.
while on top of regular activities it looks fine (Simple layout), on top of a popupview the image is "behind" the popup.
Tried to change the elevation parameters, but no luck there either.
<?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"
android:elevation="2dp"
tools:context="com.MapActivity">
<com.vatsal.imagezoomer.ImageZoomButton
android:id="#+id/bt_picture"
android:layout_width="20dp"
android:layout_height="17dp"
android:layout_alignTop="#id/tx_description"
android:layout_marginTop="1dp"
android:layout_alignLeft="#+id/bt_date_time"
android:layout_marginLeft="-25dp"
android:elevation="10dp"
android:background="#drawable/ic_picture" />
</RelativeLayout>
And this is how the popupview and windows are inflated:
popupView = getLayoutInflater().inflate(R.layout.job, null);
popupWindow = new PopupWindow(popupView,
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
popupWindow.setFocusable(true);
Should we try and use a different approach? Mabye ditch the library and inflate it differently?
Thanks
We've followed mmdreza's advice, here's a snippet of the implementation.
As the documantation states, the alertdialog will be on top of the popupview object.
myHolder.jobImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder mBuilder = new AlertDialog.Builder(v.getContext());
LayoutInflater inflater =
(LayoutInflater)v.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mView = inflater.inflate(R.layout.inflated_picture_dialog, null);
Picasso.get().load(jobView.getJobImageURL()).fit().into(((ImageButton)mView.findViewById(R.id.inflated_picture)));
mBuilder.setView(mView);
final AlertDialog dialog = mBuilder.create();
dialog.show();
}
});
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...
}
I am attempting to implement slide-in navigation (a la Facebook, Path, or Google+ Android apps). The complete source to reproduce this bug can be found here.
When animating a WebView off the screen however, on a device, flickering occurs, as if the device is taking a bite out of my WebView just before pushing it off the screen.
The artifact does not occur on the emulator! The animation proceeds smoothly.
Interestingly, this flickering seems to only occur when the view being animated is a WebView! Here is the same project using a TextView as the main content view.
Layout for the web view activity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/blue" >
<WebView
android:id="#+id/web_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
And for the text view activity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/blue" >
<TextView
android:id="#+id/web_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello there. no flicker!"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
</RelativeLayout>
Code called to slide the navigation menu in:
private void slideIn() {
LinearLayout actionBarFrame = (LinearLayout) findViewById(android.R.id.content).getParent();
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
menuView = inflater.inflate(R.layout.menu, null);
menuView.setLayoutParams(new FrameLayout.LayoutParams(SIZE, LayoutParams.MATCH_PARENT, Gravity.LEFT));
FrameLayout decorView = (FrameLayout) getWindow().getDecorView();
decorView.addView(menuView);
decorView.bringChildToFront(actionBarFrame);
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) actionBarFrame.getLayoutParams();
params.setMargins(SIZE, 0, -SIZE, 0);
actionBarFrame.setLayoutParams(params);
TranslateAnimation ta = new TranslateAnimation(-SIZE, 0, 0, 0);
ta.setDuration(DURATION);
actionBarFrame.startAnimation(ta);
}
And to slide it back out:
private void slideOut() {
LinearLayout actionBarFrame = (LinearLayout) findViewById(android.R.id.content).getParent();
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) actionBarFrame.getLayoutParams();
params.setMargins(0, 0, 0, 0);
actionBarFrame.setLayoutParams(params);
TranslateAnimation ta = new TranslateAnimation(SIZE, 0, 0, 0);
ta.setDuration(DURATION);
ta.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationEnd(Animation arg0) {
((FrameLayout) getWindow().getDecorView()).removeView(menuView);
menuView = null;
}
#Override public void onAnimationRepeat(Animation arg0) { }
#Override public void onAnimationStart(Animation arg0) { }
});
actionBarFrame.startAnimation(ta);
}
I uploaded the code to BitBucket as a public repo so you can try this project exactly as I have.
Any help or ideas about why this is happening or how to fix it would be greatly appreciated! What is desired is to smoothly animate the WebView out of the picture and back in. Thanks!
Generally speaking, it seems the problem has to do with a bug when using WebViews with hardware acceleration enabled on 3.0+ devices. I also tried using the sliding menu library with a WebView but experienced the same flicker problem. After looking around I found a workaround here:
WebView “flashing” with white background if hardware acceleration is enabled (Android 3.0+)
The workaround uses the following code to set the layer type of the WebView to software rendering:
webview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
This code solved the flicker for me with the drawback of lower performance.
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);
I have a strange problem I don't understand...
I want to do a PopupWindow from an XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#404040"
android:padding="15px">
<TextView android:id="#+id/popup_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="16dip"
android:gravity="center_horizontal" />
</LinearLayout>
and I just want to do a so simple thing: set the text in my TextView... Here is my code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//UI
ui = new RelativeLayout(this);
ui.setBackgroundColor(Color.LTGRAY);
setContentView(ui);
//PopUp
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
popUp = new PopupWindow(this);
popUp.setContentView(inflater.inflate(R.layout.popup_piece, ui, false));
//Boutton
bouton = new Button(this);
bouton.setText("POWPEUP");
ui.addView(bouton);
bouton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
popUp.showAtLocation(ui, Gravity.CENTER, 0, 0);
popUp.update(0, 0, 350, 400);
titrePopUp = (TextView)findViewById(R.id.popup_title); // THIS RETURNS NULL
Log.d("TextView", ""+titrePopUp);
//titrePopUp.setText("blop", TextView.BufferType.NORMAL); SO THIS DONT WORK
}
});
}
Seriously, I don't understand why it returns a NULL value.. Can anyone help me, please?
You are searching for the TextView in your main layout, not in the PopupWindow. You need to do this instead:
titrePopUp = (TextView) popUp.getContentView().findViewById(R.id.popup_titre);
In layout id is spelled as popup_title, and in code it's spelled as popup_titre. Maybe, this is the problem.