to answer my question you should have uber application in your mobile. Actually my doubt is, in uber application once we will open the application, at bottom we will get one dialog kind of thing for example if I am from Bangalore it will show one dialog with heading like "Uber Bangalore", if we will swipe that dialog up then it is opening other activity how to implement the same. Is that a dialog box or something else. Please explain me.
-
i'm looking for this, thank you for quetion – Rucha Bhatt Joshi Aug 09 '17 at 06:30
2 Answers
That cannot be an activity. It could be done using a Bottom Sheet. Use the design support library.
compile 'com.android.support:design:24.1.1'
1.Create a class and extend it as BottomSheetDialogFragment. Create your desired layout and inflate it in the setupDialog()
method.
public class MyBottomDialogFragment extends BottomSheetDialogFragment {
@Override
public void setupDialog(final Dialog dialog, int style) {
super.setupDialog(dialog, style);
View contentView = View.inflate(getContext(), R.layout.fragment_bottomsheet3, null);
dialog.setContentView(contentView);
}
}
2.Call your Dialog fragment in your required activity
BottomSheetDialogFragment bottomSheetDialogFragment = new MyBottomDialogFragment ();
bottomSheetDialogFragment.show(getSupportFragmentManager(), bottomSheetDialogFragment.getTag());

- 1,687
- 2
- 22
- 33
Few clarifications first. I saw the app and this answer is according to my understanding after using the functionality you wrote about. Below are my observations.
- It is not opening an Activity, instead, it is opening a fragment.
- It is not the dialog but the Fragment visible parially
- When you pull up, the fragment is loaded completely over the activity.
How can you build this?
It shouldn't be very hard.
- Use a FrameLayout in the MapsActivity layout file. This FrameLayout will be the container for the fragment.
- Load yout fragment in the activity but it should be moved down the screen so that only 10% of the fragment is visible.
- On Swiping up, load it completely on the screen.
Now for the first point, it's fairly straight forward process, you can find blogs for that. However the most basic implementation can be found in the Official Google documentation
For the second part, i don't have much idea as I have never implemented such a thing but these links should be helpful:
Moving a fragment partially off screen
https://github.com/StevenRudenko/ActionsContentView
And for the third part, you have to detect and identify the direction in which user swipes. I can provide a class for that:
All Credits to : How to detect swipe direction between left/right and up/down
import android.view.GestureDetector;
import android.view.MotionEvent;
public class OnSwipeListener extends GestureDetector.SimpleOnGestureListener
{
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// Grab two events located on the plane at e1=(x1, y1) and e2=(x2, y2)
// Let e1 be the initial event
// e2 can be located at 4 different positions, consider the following diagram
// (Assume that lines are separated by 90 degrees.)
//
//
// \ A /
// \ /
// D e1 B
// / \
// / C \
//
// So if (x2,y2) falls in region:
// A => it's an UP swipe
// B => it's a RIGHT swipe
// C => it's a DOWN swipe
// D => it's a LEFT swipe
//
float x1 = e1.getX();
float y1 = e1.getY();
float x2 = e2.getX();
float y2 = e2.getY();
Direction direction = getDirection(x1,y1,x2,y2);
return onSwipe(direction);
}
/** Override this method. The Direction enum will tell you how the user swiped. */
public boolean onSwipe(Direction direction){
return false;
}
/**
* Given two points in the plane p1=(x1, x2) and p2=(y1, y1), this method
* returns the direction that an arrow pointing from p1 to p2 would have.
* @param x1 the x position of the first point
* @param y1 the y position of the first point
* @param x2 the x position of the second point
* @param y2 the y position of the second point
* @return the direction
*/
public Direction getDirection(float x1, float y1, float x2, float y2){
double angle = getAngle(x1, y1, x2, y2);
return Direction.get(angle);
}
/**
*
* Finds the angle between two points in the plane (x1,y1) and (x2, y2)
* The angle is measured with 0/360 being the X-axis to the right, angles
* increase counter clockwise.
*
* @param x1 the x position of the first point
* @param y1 the y position of the first point
* @param x2 the x position of the second point
* @param y2 the y position of the second point
* @return the angle between two points
*/
public double getAngle(float x1, float y1, float x2, float y2) {
double rad = Math.atan2(y1-y2,x2-x1) + Math.PI;
return (rad*180/Math.PI + 180)%360;
}
public enum Direction{
up,
down,
left,
right;
/**
* Returns a direction given an angle.
* Directions are defined as follows:
*
* Up: [45, 135]
* Right: [0,45] and [315, 360]
* Down: [225, 315]
* Left: [135, 225]
*
* @param angle an angle from 0 to 360 - e
* @return the direction of an angle
*/
public static Direction get(double angle){
if(inRange(angle, 45, 135)){
return Direction.up;
}
else if(inRange(angle, 0,45) || inRange(angle, 315, 360)){
return Direction.right;
}
else if(inRange(angle, 225, 315)){
return Direction.down;
}
else{
return Direction.left;
}
}
/**
* @param angle an angle
* @param init the initial bound
* @param end the final bound
* @return returns true if the given angle is in the interval [init, end).
*/
private static boolean inRange(double angle, float init, float end){
return (angle >= init) && (angle < end);
}
}
}
Now you can use it implementing View.OntouchListener
. The example
public class YOURCLASS implements View.OnTouchListener {
gestureDetector=new GestureDetector(getActivity(),new OnSwipeListener(){
@Override
public boolean onSwipe(Direction direction) {
if (direction==Direction.up){
//LOAD FRAGMENT FULLY
}
return true;
}
});
@Override
public boolean onTouch(View v, MotionEvent event) {
gestureDetector.onTouchEvent(event);
return true;
}
}
This should do the trick for you..
It is always to break down your problem and look for solutions to those individual parts. We have the stackoverflow to guide us through every problem if we are able to find the right questions
GOOD LUCK!
EDIT : Answer by @SripadRaj can solve your problem really easily, my process is pretty long. But do try to understand what's happening behind the code before you implement.

- 1,085
- 1
- 13
- 31