What i want is, when i click on Dashboard Button it will open like a SlidingDrawer and after it opened when clicked on it again it will close.
i use this custom drawer because SlidingDrawer is Deprecated.
now the problem is, its working properly except first time when i click on Button it'll open really fast without any animation, but closes properly and after that works fine.
i found the problem is when i make RelativeLayout Visible and try to calculate its getHeight() it will give zero initially, and after that gives proper height.
here's LogCat:
Here's my XML File.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relLayOne"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#0000" >
<Button
android:id="#+id/btnNEWCLICK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#drawable/dashboard" />
<RelativeLayout
android:id="#+id/relLayTwo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnNEWCLICK"
android:background="#000"
android:visibility="gone" >
<Button
android:id="#+id/loc"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:background="#000"
android:gravity="left|center_vertical"
android:paddingLeft="5dp"
android:text="My Location"
android:textColor="#fff"
android:textSize="20sp" />
<View
android:layout_width="0.1dp"
android:layout_height="108dp"
android:layout_centerInParent="true"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
android:background="#fff" />
<Button
android:id="#+id/phot"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="#000"
android:gravity="left|center_vertical"
android:paddingLeft="5dp"
android:text="Photos"
android:textColor="#fff"
android:textSize="20sp" />
<View
android:layout_width="fill_parent"
android:layout_height="0.1dp"
android:layout_below="#+id/phot"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#fff" />
<Button
android:id="#+id/free"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/loc"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:background="#000"
android:gravity="left|center_vertical"
android:paddingLeft="5dp"
android:text="Free stuff"
android:textColor="#fff"
android:textSize="20sp" />
<Button
android:id="#+id/leade"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/phot"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="#000"
android:gravity="left|center_vertical"
android:paddingLeft="5dp"
android:text="Leaderboard"
android:textColor="#fff"
android:textSize="20sp" />
<View
android:layout_width="fill_parent"
android:layout_height="0.1dp"
android:layout_below="#+id/leade"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#fff" />
<Button
android:id="#+id/live"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/free"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:background="#000"
android:gravity="left|center_vertical"
android:paddingLeft="5dp"
android:text="Live Action"
android:textColor="#fff"
android:textSize="20sp" />
<Button
android:id="#+id/home"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/leade"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="#000"
android:gravity="left|center_vertical"
android:paddingLeft="5dp"
android:text="Home"
android:textColor="#fff"
android:textSize="20sp" />
<View
android:layout_width="fill_parent"
android:layout_height="0.1dp"
android:layout_below="#+id/home"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#fff" />
<Button
android:id="#+id/app"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/live"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:background="#000"
android:gravity="left|center_vertical"
android:paddingLeft="5dp"
android:text="App stats"
android:textColor="#fff"
android:textSize="20sp" />
</RelativeLayout>
</RelativeLayout>
Here's The Code::
public class MoveView extends LinearLayout{
private Button openCloseButton;
private boolean isVisible = false;
private RelativeLayout relLayTwo, relLayOne;
private float animationHeight = 300.0f;
public MoveView(Context context , AttributeSet attr) {
super(context, attr);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.in, this);
relLayTwo = (RelativeLayout) findViewById(R.id.relLayTwo);
//relLayOne = (RelativeLayout) findViewById(R.id.relLayOne);
openCloseButton = (Button) findViewById(R.id.btnNEWCLICK);
openCloseButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
toggle();
}
});
Log.d("test", "before "+relLayTwo.getHeight());
if (relLayTwo.getHeight() == 0){
relLayTwo.setVisibility(View.VISIBLE);
relLayTwo.invalidate();
relLayTwo.refreshDrawableState();
//rel.setVisibility(View.INVISIBLE);
relLayTwo.setVisibility(View.GONE);
Log.d("test", "after "+relLayTwo.getHeight());
}
}
public void toggle() {
TranslateAnimation anim = null;
isVisible = !isVisible;
if (isVisible) {
relLayTwo.setVisibility(View.VISIBLE);
anim = new TranslateAnimation(0.0f, 0.0f, relLayTwo.getHeight(), 0.0f);
Log.d("test", " rel Height "+relLayTwo.getHeight());
} else {
Log.d("test", " rel Height else "+relLayTwo.getHeight() );
anim = new TranslateAnimation(0.0f, 0.0f, 0.0f, relLayTwo.getHeight());
anim.setAnimationListener(collapseListener);
}
anim.setDuration(600);
anim.setInterpolator(new AccelerateInterpolator(1.0f));
startAnimation(anim);
}
Animation.AnimationListener collapseListener = new Animation.AnimationListener() {
public void onAnimationEnd(Animation animation) {
relLayTwo.setVisibility(View.GONE);
}
public void onAnimationRepeat(Animation animation) {
// not needed
}
public void onAnimationStart(Animation animation) {
// not needed
}
};
}
by using onMeasure() method.
#
Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Log.d("test", " RelativeLAyout In onMeaseure "+relLayTwo.getHeight() );
}
When a View is first created, it's width and height are not available to you until onMeasure() has been called. Due to this, the first time you try to get the values, they have not been assigned yet.
Since you're using a custom View, the solution is pretty simple. Simply move the code that gets the height and width into onMeasure() and use it after that.
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = getWidth(); // Get View Width
height = getHeight();// Get View Height
}
Here's how i got solution for this problem.
the actual problem with getting layout's height when we set it to wrap content is, it will not give height until it will actually draw on a screen. and in my case the custom layout got called first and then it will call remaining layout, so there is not a chance for layout to get draw.
so what i did is i got actual height and width of the screen run time by using following code.
DisplayMetrics metrics = new DisplayMetrics();
((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metrics);
height = metrics.heightPixels;
width = metrics.widthPixels;
and after i got screens height and width, inflate view.
after that got linearlayouts height and add new height to it using following code.
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) relLayTwo.getLayoutParams();
Log.d("test", "height "+params.height);
params.height = height*40/100;
relLayTwo.setLayoutParams(params);
relLayTwo.setVisibility(View.GONE);
and its worked!!
simply use this to get the height and width of which visibility is GONE..
myView.getMeasuredHeight();
myView.getMeasuredWidth();
<RelativeLayout
android:id="#+id/relLayTwo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnNEWCLICK"
android:background="#000"
android:visibility="gone" > //don't put gone in xml
In OnViewCreated look for the height and after that make this view GONE
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
relLayTwo = (RelativeLayout) findViewById(R.id.relLayTwo);
mHeight = relLayTwo.getHeight();
relLayTwo.setVisibility(View.GONE);}
In this way you will get the height because on onViewCreated the view is already drawn, and after you have what you need, make the view GONE.
Related
I have populate images dynamically in a linear layout, which is placed inside Horizontal Scrollview. I have enabled auto scroll the horizontal scrollview, with 1 min delay. I have set left and right padding to the horizontal scrollview, as it appears from center of the screen and scrolls to left and end at center of the screen. Auto scroll works fine. My issue is when I click on the images inside the scrollview, I need to bring that image to center of the screen, and continue auto scrolling. I have searched and found answer to place the clicked item center, from the answer for this question Android : Locating the item at center position of the screen after selecting it ,In the horizontalscrollview. But in my case, clicked item, not coming to center.
final int duration = 1000;
final int pixelsToMove = 50;
private final Handler mHandler = new Handler(Looper.getMainLooper());
private final Runnable SCROLLING_RUNNABLE = new Runnable() {
#Override
public void run() {
horizontalScrollView1.smoothScrollBy(pixelsToMove, 0);
mHandler.postDelayed(this, duration);
}
};
HorizontalScrollView horizontalScrollView1 = (HorizontalScrollView) findViewById(R.id.horizontalScrollView1);
LinearLayout layLandmarks = (LinearLayout) findViewById(R.id.layLandmarks);
final ImageView[] dynamic_imageView = new ImageView[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
dynamic_imageView[i] = new ImageView(Activity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(70, 70);
dynamic_imageView[i].setLayoutParams(lp);
Glide.with(Activity.this)
.load(imageUrl)
.override(30, 30)
.error(R.drawable.button_white_color1)
.into(dynamic_imageView[i]);
dynamic_imageView[i].setId(i);
dynamic_imageView[i].setTag("landmarkName");
layLandmarks.addView(dynamic_imageView[i], i);
horizontalScrollView1.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int padding = getResources().getDisplayMetrics().widthPixels / 2;
horizontalScrollView1.setPadding(padding, 0, padding, 0);
horizontalScrollView1.setClipToPadding(false);
}
});
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
final int width = size.x;
dynamic_imageView[i].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int center = (width - dynamic_imageView[i].getWidth())/2;
horizontalScrollView1.scrollTo(dynamic_imageView[i].getLeft() - center, dynamic_imageView[i].getTop());
}
});
}
mHandler.postDelayed(SCROLLING_RUNNABLE, 100);
Adding my layout file:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/black_transparent_background">
<VideoView
android:id="#+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" />
<Button
android:id="#+id/btnBack"
android:layout_width="15dp"
android:layout_height="20dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="25dp"
android:background="#drawable/back_arrow" />
<TextView
android:id="#+id/txtEventTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="25dp"
android:layout_toRightOf="#id/btnBack"
android:text="Santa Rosa Marathon"
android:textColor="#color/white"
android:textSize="#dimen/text_24" />
<Button
android:id="#+id/btnCamera"
android:layout_width="50dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_margin="20dp"
android:background="#drawable/camera_icon_white" />
<RelativeLayout
android:id="#+id/laySeekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<RelativeLayout
android:id="#+id/seekBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="17dp"
android:layout_marginRight="17dp"
android:background="#drawable/seekbar_layout_background">
<SeekBar
android:id="#+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progressBackgroundTint="#color/white"
android:thumb="#drawable/seekbar_thumb"
android:visibility="gone"
/>
</RelativeLayout>
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:fillViewport="true"
android:scrollbars="none">
<LinearLayout
android:id="#+id/layLandmarks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
<RelativeLayout
android:id="#+id/laySeekbarText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/btnCircle"
android:layout_centerInParent="true"
android:layout_marginBottom="5dp"
android:background="#drawable/seekbar_text_back">
<TextView
android:id="#+id/seekBarProgressValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center"
android:padding="2dp"
android:text="0 mi"
android:textColor="#color/black" />
<TextView
android:id="#+id/txtVideoElevationValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/seekBarProgressValue"
android:layout_centerHorizontal="true"
android:padding="2dp"
android:text="0 sqft"
android:textColor="#color/black" />
</RelativeLayout>
<Button
android:id="#+id/btnCircle"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:background="#drawable/seekbar_thumb" />
<Button
android:id="#+id/btnStart"
android:layout_width="20dp"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:background="#drawable/start_icon" />
<ProgressBar
android:id="#+id/videoProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone" />
</RelativeLayout>
trying to make showing LinarLayout from GONE state to VISIBLE with "roll down" animation. On this Layout exist TextView. Seems to all work fine but, a TextView not shown after animation.
What I do wrong?
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:id="#+id/ll_info">
<TextView
android:id="#+id/bayer_note"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:text="#string/buyer_note"/>
<RelativeLayout
android:id="#+id/button_continue"
android:layout_width="match_parent"
android:layout_height="54dp"
android:layout_centerHorizontal="true"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:background="#drawable/button_register"
android:drawableEnd="#drawable/ic_arrow_forward_white_24dp">
<TextView
android:id="#+id/btn_cont_caption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Войти"
android:textColor="#android:color/white"
android:textSize="20sp"
android:fontFamily="fonts/SF-UI-Display-Regular.ttf"
android:layout_centerInParent="true" />
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/imageView"
android:layout_centerVertical="true"
android:layout_alignParentEnd="false"
android:src="#drawable/ic_door"
android:layout_toRightOf="#+id/btn_cont_caption"
android:layout_marginLeft="10dp"
android:scaleType="fitCenter" />
</RelativeLayout>
</LinearLayout>
And animation code:
final LinearLayout ll_info = (LinearLayout)findViewById(R.id.ll_info);
class scaleAnimation extends Animation {
public LinearLayout ll;
public int newHeight;
public void scaleTopHeight(int height)
{
newHeight = height;
}
public void setLayout(LinearLayout layout) {
ll = layout;
}
}
final LinearLayout ll_info = (LinearLayout)findViewById(R.id.ll_info);
scaleAnimation h = new scaleAnimation() {
#Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
ll.getLayoutParams().height = interpolatedTime == 1
? LinearLayout.LayoutParams.WRAP_CONTENT
: (int)(newHeight * interpolatedTime);
ll.requestLayout();
}
};
h.setDuration(300);
h.setLayout(ll_info);
ll_info.setVisibility(View.VISIBLE);
ll_info.measure(View.MeasureSpec.makeMeasureSpec(((LinearLayout)ll_info.getParent()).getWidth(), View.MeasureSpec.AT_MOST), View.MeasureSpec.makeMeasureSpec(((LinearLayout)ll_info.getParent()).getHeight(), View.MeasureSpec.AT_MOST));
h.scaleTopHeight(ll_info.getMeasuredHeight());
ll_info.getLayoutParams().height=1;
ll_info.startAnimation(h);
<RelativeLayout
android:id="#+id/button_continue"
android:layout_width="match_parent"
android:layout_height="54dp"
**android:layout_centerHorizontal="true"** /////
remove this
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:background="#drawable/button_register"
android:drawableEnd="#drawable/ic_arrow_forward_white_24dp">
<TextView
android:id="#+id/btn_cont_caption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Войти"
android:textColor="#android:color/white"
android:textSize="20sp"
android:fontFamily="fonts/SF-UI-Display-Regular.ttf"
android:layout_centerInParent="true" />
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/imageView"
android:layout_centerVertical="true"
android:layout_alignParentEnd="false"
android:src="#drawable/ic_door"
android:layout_toRightOf="#+id/btn_cont_caption"
android:layout_marginLeft="10dp"
android:scaleType="fitCenter" />
</RelativeLayout>
i think this is your xml file issue ,your relativelayout in center_horizontal and all the childs are in center in parent.so textview hides over imageview. Try it and let me know.
I have the main activity in which I have few menu buttons. When the user presses specific button I want to open a new activity which also have a couple of buttons which I need to handle their click. In other words, I need to pop up window functionality as normal activity.
I looked online and found several ways to implement this such as: just customising the size of the activity, use diaglog theme in the manifest, use it as a fragment or use Popup Window Class. But as I am new to android I want the best way to implement it for my project.
Can someone help me achieve this?
EDIT:
this is the xml file i want to use in the pop up window (for better explanation of what i want to achieve):
<?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="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:background="#0091cb"
android:padding="16dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="#dimen/activity_horizontal_margin"
android:weightSum="3"
android:id="#+id/check">
<Button
android:layout_width="85dp"
android:layout_height="85dp"
android:background="#drawable/circle"
android:drawableTop="#drawable/computer"
android:paddingTop="12dp"
android:layout_marginLeft="10dp"
android:text="button1"
android:textSize="10dp"
android:textColor="#fff"
android:layout_weight="1"
android:onClick="button1_OnClick"/>
<Button
android:layout_width="85dp"
android:layout_height="85dp"
android:background="#drawable/circle"
android:drawableTop="#drawable/electrical"
android:paddingTop="12dp"
android:layout_marginLeft="10dp"
android:text="button2"
android:textSize="10dp"
android:textColor="#fff"
android:layout_weight="1"
android:onClick="button2_OnClick"/>
<Button
android:layout_width="85dp"
android:layout_height="85dp"
android:background="#drawable/circle"
android:drawableTop="#drawable/hdtv"
android:paddingTop="12dp"
android:layout_marginLeft="10dp"
android:text="button3"
android:textSize="10dp"
android:textColor="#fff"
android:layout_weight="1"
android:onClick="button3_OnClick"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:orientation="horizontal"
android:layout_below="#id/check"
android:paddingTop="10dp">
<Button
android:layout_width="85dp"
android:layout_height="85dp"
android:background="#drawable/circle"
android:drawableTop="#drawable/bill"
android:paddingTop="12dp"
android:layout_marginLeft="10dp"
android:text="button4"
android:textSize="10dp"
android:textColor="#fff"
android:onClick="button4_OnClick"/>
<Button
android:layout_width="85dp"
android:layout_height="85dp"
android:layout_marginLeft="10dp"
android:background="#drawable/circle"
android:drawableTop="#drawable/water"
android:paddingTop="12dp"
android:text="button5"
android:textSize="10dp"
android:textColor="#fff"
android:onClick="button5_OnClick" />
<Button
android:layout_width="85dp"
android:layout_height="85dp"
android:layout_marginLeft="10dp"
android:background="#drawable/circle"
android:drawableTop="#drawable/electrical"
android:paddingTop="12dp"
android:text="button6"
android:textSize="10dp"
android:textColor="#fff"
android:onClick="button6_OnClick" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:orientation="horizontal">
<Button
android:layout_width="85dp"
android:layout_height="85dp"
android:layout_marginLeft="10dp"
android:background="#drawable/circle"
android:drawableTop="#drawable/notepad"
android:paddingTop="7dp"
android:text="button7"
android:textSize="10dp"
android:textColor="#fff"
android:onClick="button7_OnClick" />
</LinearLayout>
</LinearLayout>
Depends on your needs.
If you need just to show 'classic' popup window (error while typing in input field or small color picker) - use PopupWindow class. You can use my gist.
If your message is more general (e.g. 'There is no internet connection') or user should choose yes/no - use dialog. There is cool library.
Activity with custom size is used rarely. Maybe in some dial applications.
So choose implementation depends on your needs.
create method where u want to open popup windiw in your activity
Like this,here p is a specific point(location) where you want exactly open your window.
final Point p = new Point();
show_popup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showpopupwindows(Activity, p);
}
});
then,
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
int location[] = new int[2];
show_popup.getLocationOnScreen(location);
p.x = location[0];
p.y = location[1];
}
private void showpopupwindows(final Activity context, Point p) {
LinearLayout viewGroup = (LinearLayout) context
.findViewById(R.id.popup_menu);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Display display = context.getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int popupwidth = (size.x / 2);
int popupheight =(size.y / 2);
View layout = layoutInflater.inflate(R.layout.detail_pop, viewGroup);
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(popupwidth);
popup.setHeight(popupheight);
popup.setFocusable(true);
popup.setAnimationStyle(R.style.WindowAnimation);
popup.setBackgroundDrawable(new ColorDrawable(
android.graphics.Color.TRANSPARENT));
popup.showAtLocation(layout, Gravity.NO_GRAVITY, popupwidth,popupheight);
detail_pop1 = (TextView) layout.findViewById(R.id.detail_pop1);
detail_pop1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getContext(), "pop window is opened, Toast.LENGTH_SHORT).show();
}
});
}
detail_pop.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup_menu"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#color/skyblue"
android:gravity="center"
android:orientation="vertical"
>
<TextView
android:id="#+id/detail_pop1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:drawableTop="#drawable/ic_launcher"
android:gravity="center"
android:text="P"
android:textSize="#dimen/font_24" />
</LinearLayout>
I have a listview which look like this:
but it is after giving hardcoded hight and width in xml.
This is my XML for listview adaptor:
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="3"
android:orientation="horizontal">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="14dp"
android:layout_marginTop="4dp"
android:layout_marginRight="3dp"
android:layout_marginBottom="4dp"
android:adjustViewBounds="true"
android:contentDescription="#string/descr_image"
android:scaleType="centerCrop" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="7"
android:weightSum="7"
android:orientation="horizontal"
>
<TextView
android:id="#+id/text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:layout_gravity="left|center"
android:gravity="center|left"
android:layout_marginLeft="20dip"
android:textSize="18sp"
/>
<ImageView
android:id="#+id/arrow"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_gravity="right|center"
android:src="#drawable/bluearrowmap"
android:layout_marginRight="10dp"
/>
</LinearLayout>
I am giving First image 2% width but the problem is that now how can i give height to that imageview same like it will take width from difference devices.Because i want this image to be a square just like it is in picture.
i have tried this piece of code in my adaptor class
ViewTreeObserver vto = holder.image.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
holder.image.getViewTreeObserver().removeOnPreDrawListener(this);
int finalHeight = holder.image.getMeasuredHeight();
int finalWidth = holder.image.getMeasuredWidth();
holder.image.getLayoutParams().height=finalWidth;
return true;
}
});
i am writting this code in my getview of adaptor class.it works fine sometime but sometime it needs a scroll down to set the height of imagview according to width.
Can someone plz help? i have tried every solution on internet but all in vain.
thanks.
Try to properly use weight in LinearLayout then no need to change any height or width value at runtime :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/ic_launcher"
android:contentDescription="#string/descr_image"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"/>
</LinearLayout>
<ImageView
android:id="#+id/arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/ic_launcher"/>
</LinearLayout>
Extends image view and set it like below:
public class SquareImageView extends ImageView {
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = getMeasuredWidth();
setMeasuredDimension(width, width);
}
}
i've been scrounging the internet for a while now and have been unable to find s viable solution for my animation problem.
I have a list view where when you click on one of the items, more information animates from the bottom to give you a few lines of additional information. That is simply a linearlayout that i have inside the XML file im using for these list items, here:
<?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="wrap_content" >
<LinearLayout
android:id="#+id/friendActivityList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/friendInfo"
android:background="#color/grey"
android:orientation="vertical"
android:visibility="gone" >
<RelativeLayout
android:id="#+id/RelativeLayout04"
android:layout_width="match_parent"
android:layout_height="#dimen/freind_activity_list_height" >
<ImageView
android:id="#+id/ImageView04"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_margin="5dp"
android:src="#drawable/logo_d" />
<TextView
android:id="#+id/TextView04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/ImageView04"
android:text="TextView"
android:textColor="#color/black"
android:textSize="17dp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout03"
android:layout_width="match_parent"
android:layout_height="#dimen/freind_activity_list_height" >
<ImageView
android:id="#+id/ImageView03"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_margin="5dp"
android:src="#drawable/logo_d" />
<TextView
android:id="#+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/ImageView03"
android:text="TextView"
android:textColor="#color/black"
android:textSize="17dp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout02"
android:layout_width="match_parent"
android:layout_height="#dimen/freind_activity_list_height" >
<ImageView
android:id="#+id/ImageView02"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_margin="5dp"
android:src="#drawable/logo_d" />
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/ImageView02"
android:text="TextView"
android:textColor="#color/black"
android:textSize="17dp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="#dimen/freind_activity_list_height" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_margin="5dp"
android:src="#drawable/logo_d" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/imageView1"
android:text="TextView"
android:textColor="#color/black"
android:textSize="17dp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout01"
android:layout_width="match_parent"
android:layout_height="#dimen/freind_activity_list_height">
<ImageView
android:id="#+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_margin="5dp"
android:src="#drawable/logo_d" />
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/ImageView01"
android:text="TextView"
android:textColor="#color/black"
android:textSize="17dp" />
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:id="#+id/friendInfo"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:background="#drawable/bg_list_item_n" >
<ImageView android:id="#+id/imgCompany"
android:layout_marginLeft="5dp"
android:layout_centerVertical="true"
android:layout_width="60dp"
android:src="#drawable/ic_launcher"
android:scaleType="centerInside"
android:layout_alignParentLeft="true"
android:layout_height="50dp">
</ImageView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_toRightOf="#id/imgCompany"
android:background="#android:color/transparent"
android:gravity="left|center"
android:orientation="vertical"
android:paddingLeft="5dp" >
<TextView android:id="#+id/lblCompanyName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textStyle="bold"
android:textSize="13dp"
android:text="Company Name">
</TextView>
<TextView android:id="#+id/lblReawrdDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="13dp"
android:text="Reawrd Description">
</TextView>
<TextView android:id="#+id/lblScores"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="13dp"
android:singleLine="true"
android:text="My Score: 13434 | Top Score: 344425">
</TextView>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
Most of this is just place holder information so that i can get the animation to work correctly. Here is the code that im using for the animation:
listviewFriends.setOnItemClickListener(new OnItemClickListener() {//this is the listView that im animating inside of
public void onItemClick(AdapterView<?> parent, View view,final int pos, long id) {
Log.v("ListItemClicked", "this position was clicked: "+pos);
if(friendInfoList != null){
friendInfoList.clearAnimation();//this is the new view that gets animated and is supposed to push everything below it out of the way
friendInfoList.setVisibility(View.GONE);
}
friendInfoList = (LinearLayout) view.findViewById(R.id.friendActivityList);
friendInfoList.setVisibility(View.VISIBLE);
friendInfoList.startAnimation(infoAnim);
}
});
infoAnim = new TranslateAnimation(0,0, -150, 0);//this is the animation im using
infoAnim.setDuration(1000);
infoAnim.setFillAfter(true);
No the result of this is that when i click on the list view item the entire space that the supposed-to-be-animated view takes up at the end is white while the view animates from the top down. The location is correct, but i want it to animate and push everything below it out of the way, instead it instantly pushes everything out of the way then animates to fill that space.
Any idea how i can get it to push everything out of the way during the animation instead of immediately? Is it even possible to achieve this effect? any help is greatly appreciated.
Also, I know how i can make it simply animate ontop of the other views, but i need it to actually push everything out of the way.
The trick with this is to create your own Animation subclass.
public class ExpandAnimation extends Animation
{
private int _targetHeight;
private View _view;
private boolean _down;
public ExpandAnimation(View view, int targetHeight)
{
_view = view;
_targetHeight = targetHeight;
}
#Override
protected void applyTransformation(float interpolatedTime, Transformation t)
{
int newHeight;
if(_down)
{
newHeight = (int) (_targetHeight * interpolatedTime);
}
else
{
newHeight = (int) (_targetHeight * (1 - interpolatedTime));
}
_view.getLayoutParams().height = newHeight;
_view.requestLayout();
}
public ExpandAnimation expand()
{
_down = true;
return this;
}
public ExpandAnimation collapse()
{
_down = false;
return this;
}
#Override
public void initialize(int width, int height, int parentWidth, int parentHeight)
{
super.initialize(width, height, parentWidth, parentHeight);
}
#Override
public boolean willChangeBounds()
{
return true;
}
}
Then you can apply that to your view that should expand:
public void togglePreview()
{
if(_expanded) _preview.startAnimation(_animation.collapse());
else _preview.startAnimation(_animation.expand());
_expanded = !_expanded;
getParent().requestLayout();
}