I am trying my custom sliding navigation. I know there are already lot's of tutorial or library is available but this is for my own purpose.
So basically i am trying to open sliding menu and it is opening left to right now i want to change it's direction from right to left. How can i do this.
Below is my code.
public class Profile extends Activity implements OnClickListener {
private int windowWidth;
boolean alreadyShowing = false;
private Animation mAnimation;
LayoutInflater mLayoutInflater;
private RelativeLayout mRelativeLayout;
ImageView sliding_menu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);
Display display = getWindowManager().getDefaultDisplay();
windowWidth = display.getWidth();
display.getHeight();
mLayoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
sliding_menu = (ImageView) findViewById(R.id.slidingMenu);
sliding_menu.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (!alreadyShowing) {
alreadyShowing = true;
openSlidingMenu();
}
}
});
}
private void openSlidingMenu() {
int width = (int) (windowWidth * 0.8f);
translateView((float) (width));
int height = LayoutParams.FILL_PARENT;
final View layout = mLayoutInflater.inflate(R.layout.settings,
(ViewGroup) findViewById(R.id.setting));
final PopupWindow optionsPopup = new PopupWindow(layout, width, height,
true);
optionsPopup.setBackgroundDrawable(new PaintDrawable());
optionsPopup.showAtLocation(layout, Gravity.NO_GRAVITY, 0, 0);
optionsPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
public void onDismiss() {
cleanUp();
translateView(0);
cleanUp();
alreadyShowing = false;
}
});
}
private void translateView(float right) {
mAnimation = new TranslateAnimation(0f, right, 0f, 0f);
mAnimation.setDuration(100);
mAnimation.setFillEnabled(true);
mAnimation.setFillAfter(true);
mRelativeLayout = (RelativeLayout) findViewById(R.id.profile);
mRelativeLayout.startAnimation(mAnimation);
mRelativeLayout.setVisibility(View.VISIBLE);
}
private void cleanUp() {
if (null != mRelativeLayout) {
mRelativeLayout.clearAnimation();
mRelativeLayout = null;
}
if (null != mAnimation) {
mAnimation.cancel();
mAnimation = null;
}
}
So basically you can see in above image that my menu is opening from left to right, how can i change it from right to left.
Thank you in advance.
I have solved this question, i have just changed gravity to right side of this below line and it is working now.
optionsPopup.showAtLocation(layout, Gravity.RIGHT, 0, 0);
So before change my code was something like this.
final PopupWindow optionsPopup = new PopupWindow(layout, width, height,true);
optionsPopup.setBackgroundDrawable(new PaintDrawable());
optionsPopup.showAtLocation(layout, Gravity.NO_GRAVITY, 0, 0);
and now it is like below
final PopupWindow optionsPopup = new PopupWindow(layout, width, height,true);
optionsPopup.setBackgroundDrawable(new PaintDrawable());
optionsPopup.showAtLocation(layout, Gravity.RIGHT, 0, 0);
Thank you all for helping me.
Related
I have a class that extends Imageview. I need to add a progress bar to that ImageView programmatically.
This is my code:
public void viewPopup() {
DisplayMetrics metrics = new DisplayMetrics();
((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
if(isFullScreen()) {
popupWindow = new PopupWindow(layout, (width), (height), true);
}else {
if (windowHeight != 0 || windowWidth != 0) {
width = windowWidth;
height = windowHeight;
popupWindow = new PopupWindow(layout, (width), (height), true);
} else {
popupWindow = new PopupWindow(layout, (int) (width * .8), (int) (height * .6), true);
}
}
popupWindow.showAtLocation(layout, Gravity.CENTER, 0, 0);
ImageView closeIcon = (ImageView) layout.findViewById(R.id.closeBtn);
if (isHideCloseIcon()) {
closeIcon.setVisibility(View.GONE);
}
closeIcon.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
popupWindow.dismiss();
}
});
if (isImageOnClickClose()) {
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
popupWindow.dismiss();
}
});
}
dimBehind(popupWindow);
}
I need to initialize the progress bar after the metrics and it should disappear after the image loads (i.e inside the popup window).
I tried this for initialization :
progressBar = new ProgressBar(activity, null, android.R.attr.progressBarStyleHorizontal);
But I don't know how to proceed after this.
I created an Activity in which i add a button that throws a popup when is clicked. Here is the code of showPopup() method:
private void showPopup() {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup_layout, (ViewGroup) findViewById(R.id.popup_element), false);
final PopupWindow pwindo = new PopupWindow(layout, LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, true);
Button btnAgree = (Button) layout.findViewById(R.id.ok);
btnAgree.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pwindo.dismiss();
}
});
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
}
}
I would center it both vertically and orizzontally. I tried several ways that i see here on SO but none worked. Why i always get the popup window at the top of the screen?
you can use setHorizontalOffset:
ListPopupWindow popupWindow = new ListPopupWindow(this);
// Position
popupWindow.setAnchorView(btn);
popupWindow.setPromptPosition (ListPopupWindow.POSITION_PROMPT_ABOVE);
popupWindow.setHorizontalOffset((btn.layoutParams.width - popupWindow.getWidth())/2);
this code will show perfect popup in center of you specific view
// click event of specific view
#Override
public void onClick(View v) {
super.onClick(v);
if (v == lin_filterActionbar) {
PopupWindow popupwindow_obj; // create object
popupwindow_obj = dialog_AllergiesSelect(RR_HomeActivity.this,lin_filterActionbar);
popupwindow_obj.showAsDropDown(lin_filterActionbar);
}
}
private PopupWindow dialog_AllergiesSelect(Context context,LinearLayout lin) {
final PopupWindow dialog_AllergiesSelect = new PopupWindow(this);
View v = View.inflate(context, R.layout.dialog_filter_actionbar, null);
dialog_AllergiesSelect.setContentView(v);
dialog_AllergiesSelect.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
dialog_AllergiesSelect.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
dialog_AllergiesSelect.setFocusable(true);
// TextView lis_testSelection = (TextView) v.findViewById(R.id.dialogfilter_txt_filter);
// LinearLayout.LayoutParams lin_laLayoutParams = new LinearLayout.LayoutParams((int) (sunApplication.getDeviceWidth() - (sunApplication.getDeviceScale() * dialog_width_dvide_allerges_spinner_width)), (int) (sunApplication.getDeviceHeight() - (sunApplication.getDeviceScale() * dialog_width_dvide_allerges_height)));
// lis_testSelection.setLayoutParams(lin_laLayoutParams);
// Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
int OFFSET_X = 0;
int OFFSET_Y = 0;
// Clear the default translucent background
dialog_AllergiesSelect.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
//dialog_AllergiesSelect.showAtLocation(v, Gravity.C, p.x + OFFSET_X, p.y + OFFSET_Y);
Rect location = locateView(lin);
dialog_AllergiesSelect.showAtLocation(v, Gravity.TOP|Gravity.CENTER, 0, location.bottom);
// Getting a reference to Close button, and close the popup when clicked.
return dialog_AllergiesSelect;
}
public static Rect locateView(View v)
{
int[] loc_int = new int[2];
if (v == null) return null;
try
{
v.getLocationOnScreen(loc_int);
} catch (NullPointerException npe)
{
//Happens when the view doesn't exist on screen anymore.
return null;
}
Rect location = new Rect();
location.left = loc_int[0];
location.top = loc_int[1];
location.right = location.left + v.getWidth();
location.bottom = location.top + v.getHeight();
return location;
}
I'm using pop-window to show some text-view while clicking the edit-text . but pop-window is not showing at particular location ,it always showing at left top corner , here is my code ,what is wrong in that
private void showPopup(Context context,final LinearLayout Parent) {
LayoutInflater layoutInflater = (LayoutInflater)context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = layoutInflater.inflate(R.layout.popup,MainActivity.parent,true);
// Creating the PopupWindow
final PopupWindow popupWindow = new PopupWindow(
layout,700,700);
popupWindow.setContentView(layout);
popupWindow.setHeight(500);
new Runnable(){
#Override
public void run() {
popupWindow.showAtLocation(layout, Gravity.CENTER,300,150);
}
};
}
Try Following Code:
public static Rect locateView(View v)
{
int[] loc_int = new int[2];
if (v == null) return null;
try
{
v.getLocationOnScreen(loc_int);
} catch (NullPointerException e)
{
//Happens when the view doesn't exist on screen anymore.
return null;
}
Rect location = new Rect();
location.left = loc_int[0];
location.top = loc_int[1];
location.right = location.left + v.getWidth();
location.bottom = location.top + v.getHeight();
return location;
}
And then use
popup.showAtLocation(parent, Gravity.TOP|Gravity.LEFT, location.left, location.bottom);
Hope it helps...
I am using Slider like gmail or Facbook in android this is fine. What i have done is i just created a new activity and given animation to it so it looks like slider. Now what i want is when user click any button in slider a new activity should open and perform task what i specify in it. But i am not getting how to pass click event in slider.
My code
public class PlayAudio extends Activity implements OnCompletionListener,
SeekBar.OnSeekBarChangeListener {
boolean alreadyShowing = false;
private int windowWidth;
private int windowHeight;
private PopupWindow fadePopup;
private Animation ta;
private RelativeLayout baseView;
LayoutInflater inflater;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
Display display = getWindowManager().getDefaultDisplay();
windowWidth = display.getWidth();
windowHeight = display.getHeight();
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
findViewById(R.id.imageView1).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (!alreadyShowing) {
alreadyShowing = true;
openSlidingMenu();
}
}
});
findViewById(R.id.imageView3).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (!alreadyShowing) {
alreadyShowing = true;
openSlidingMenu();
}
}
});
}
private void openSlidingMenu() {
showFadePopup();
int width = (int) (windowWidth * 0.8f);
translateView((float) (width));
#SuppressWarnings("deprecation")
int height = LayoutParams.FILL_PARENT;
// creating a popup
final View layout = inflater.inflate(R.layout.option_popup_layout,
(ViewGroup) findViewById(R.id.popup_element));
final PopupWindow optionsPopup = new PopupWindow(layout, width, height,
true);
optionsPopup.setBackgroundDrawable(new PaintDrawable());
optionsPopup.showAtLocation(layout, Gravity.NO_GRAVITY, 0, 0);
optionsPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
public void onDismiss() {
// Removing the fade effect
fadePopup.dismiss();
// to clear the previous animation transition in
cleanUp();
// move the view out
translateView(0);
// to clear the latest animation transition out
cleanUp();
// resetting the variable
alreadyShowing = false;
}
});
}
private void showFadePopup() {
final View layout = inflater.inflate(R.layout.fadepopup,
(ViewGroup) findViewById(R.id.fadePopup));
fadePopup = new PopupWindow(layout, windowWidth, windowHeight, false);
fadePopup.showAtLocation(layout, Gravity.NO_GRAVITY, 0, 0);
}
private void translateView(float right) {
ta = new TranslateAnimation(0f, right, 0f, 0f);
ta.setDuration(100);
ta.setFillEnabled(true);
ta.setFillAfter(true);
baseView = (RelativeLayout) findViewById(R.id.Player_Slider);
baseView.startAnimation(ta);
baseView.setVisibility(View.VISIBLE);
}
private void cleanUp() {
if (null != baseView) {
baseView.clearAnimation();
baseView = null;
}
if (null != ta) {
ta.cancel();
ta = null;
}
fadePopup = null;
}
}
Here in click event i am calling openSlidingMenu function which open's new activity option_popup_layout (http://i.share.pho.to/fb125f6f_o.png)
Now what i want is when i click any of the image in option_popup_layout a new activity should open and i want perform some operation over there but i am not getting where i have to use click event to call new activity.
I have tried some piece of code to put click event inside creating function as well as other method too but not getting work.
Please help me to solve this. Thanks in advance.
I have a little problem with my Window dimming.
Basically, I want to set the dimAmount of my Activity to zero as soon as I swipe it into one direction.
Please be aware that my Activity is displayed as a dialog, and can be swiped to the left or right. This works perfectly fine. But the setWindowDim() method has no effect when called after swipe.
If i call the setWindowDim inside my onCreate method, it works perfectly fine.
I have no idea why it doesnt work anywhere else.
public class FinishDialog extends Activity {
private FrameLayout frame = null;
private GestureDetector gd = null;
private int displaywidth = 0;
private int original_x = 0;
private int dialog_width = 0;
private final int POS_LEFT = -1;
private final int POS_MID = 0;
private final int POS_RIGHT = 1;
/** the dialogs current position, POS_MID on startup */
private int current_pos = POS_MID;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
frame = new FrameLayout(this);
gd = new GestureDetector(this, new TouchManager(this));
// fill the challenge layout into the framelayout
frame.addView(LayoutInflater.from(getBaseContext()).inflate(R.layout.finishdialog, null));
setContentView(frame);
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) frame.getLayoutParams();
GameLogic g = new GameLogic();
displaywidth = g.getDisplayWidth(this);
lp.width = displaywidth - displaywidth / 6;
lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
LinearLayout llFinish = (LinearLayout) findViewById(R.id.llFinishDialog);
LinearLayout.LayoutParams lpLL = (LinearLayout.LayoutParams) llFinish.getLayoutParams();
lp.height = lpLL.height;
frame.setLayoutParams(lp);
// setWindowDim(0.0f); ---> if i set the dim here, it works perfectly fine
original_x = (int) frame.getX();
dialog_width = lp.width;
}
private void setWindowDim(float dim) {
Log.i("FinishDialog", "Setting window dim to " + dim + ".");
WindowManager.LayoutParams lpWin = getWindow().getAttributes();
lpWin.dimAmount = dim;
getWindow().setAttributes(lpWin);
}
/**
* move the dialog, depending on the swipe direction and the
* current position of the dialog
* #param action
*/
public void interpretTouch(int action) {
switch(action) {
case TouchManager.MOVE_DIALOG_RIGHT:
if(frame != null) {
if(current_pos == POS_MID) {
swipeDialog(600, frame, 0, dialog_width, 0, 0);
current_pos = POS_RIGHT;
setWindowDim(0.0f); // here setting the dim has no effect
} else if (current_pos == POS_LEFT) {
swipeDialog(600, frame, - dialog_width, original_x, 0, 0);
current_pos = POS_MID;
setWindowDim(0.9f); // here setting the dim has no effect
} else if (current_pos == POS_RIGHT) {
// if we are on the right and swipe right, nothing happens
}
}
Log.i("Challenge", "Moving dialog to the right.");
break;
case TouchManager.MOVE_DIALOG_LEFT:
if(frame != null) {
if(current_pos == POS_MID) {
swipeDialog(600, frame, 0, -dialog_width, 0, 0);
current_pos = POS_LEFT;
setWindowDim(0.0f); // here setting the dim has no effect
} else if (current_pos == POS_LEFT) {
// if we are on the left and swipe left, nothing happens
} else if (current_pos == POS_RIGHT) {
swipeDialog(600, frame, dialog_width, original_x, 0, 0);
current_pos = POS_MID;
setWindowDim(0.9f); // here setting the dim has no effect
}
}
Log.i("Challenge", "Moving dialog to the left.");
break;
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
return gd.onTouchEvent(event);
}
private void swipeDialog(int time, View framelayout, int fromDeltaX, int toDeltaX, int fromDeltaY, int toDeltaY) {
Log.i("FinishDialog", "Dialog swiped.");
TranslateAnimation trans = new TranslateAnimation(fromDeltaX, toDeltaX,
fromDeltaY, toDeltaY);
trans.setFillAfter(true);
trans.setFillEnabled(true);
trans.setDuration(time);
framelayout.startAnimation(trans);
}
}
Starting this activity right after I change the Window dim finally did the job.
Unfortunately, now the Screen flashes pretty "ugly" when the RefreshActivity gets started, does anyone have a solution for that?
import android.app.Activity;
import android.os.Bundle;
public class RefreshActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
finish();
}
}