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.
Related
I am trying to create a popup when clicking on ListView image with the same image just enlarged, this code works fine to me but I tried to position the View "vv" at the middle of the screen but the popup is still at (0,0)..
rowView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (!popu) {
View popupView = inflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
TextView txt = (TextView) popupView.findViewById(R.id.texttt);
ImageButton img = (ImageButton) popupView.findViewById(R.id.imgg);
txt.setText(imageId[position]);
img.setImageResource(imageId[position]);
View vv = new View(context);
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
vv.setX(width / 4);
vv.setY(height / 4);
img.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
popupWindow.dismiss();
popu = false;
}
});
popupWindow.showAsDropDown(vv, 50, -30);
popu = true;
}
}
});
I am not on an activity, it is my customadapter class..
EDIT:
Fixed, see Yasin KaƧmaz answer.
How get height of Android Keyboard?
I try:
KeyboardView keyboardView = new KeyboardView(_activity.getApplicationContext(), null);
Log.i("","xxx height " + keyboardCustom.mKeyboardView.getHeight());
Log.i("","xxx height " + keyboardCustom.mKeyboardView.getBottom());
But always get 0.
Use OnGlobalLayoutListener for getting Keyboard height or implement above code snippet
chatRootLayout is your xml root layout
pass this rootLayout as parentLayout parameter in checkKeyboardHeight
private void checkKeyboardHeight(final View parentLayout)
{
chatRootLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
{
#Override
public void onGlobalLayout()
{
Rect r = new Rect();
chatRootLayout.getWindowVisibleDisplayFrame(r);
int screenHeight = chatRootLayout.getRootView().getHeight();
int keyboardHeight = screenHeight - (r.bottom);
if (previousHeightDiffrence - keyboardHeight > 50)
{
// Do some stuff here
}
previousHeightDiffrence = keyboardHeight;
if (keyboardHeight> 100)
{
isKeyBoardVisible = true;
changeKeyboardHeight(keyboardHeight);
}
else
{
isKeyBoardVisible = false;
}
}
});
}
Here is changeKeyboardHeight() method
private void changeKeyboardHeight(int height)
{
if (height > 100)
{
keyboardHeight = height;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, keyboardHeight);
yourLayout.setLayoutParams(params);
}
}
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 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.
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.