Method to capture the whole Screen from a Floating Window Application - java

I'm trying to do implement a floating widget button(like the Facebook Messenger) which is displayed over the other app. The button should be able to interfere with the displayed view of the other app and capture the whole smartphone screen underneath.
The floating button is already implemented and works fine on every app. Getting the root view and printing out the bitmap of this floating view will just return the floating button image. I think the floating window cant detect the view of the apps underneath.
One possible solution could be to start the other application as a separate activity and get its view, but I am not sure how to do that. Maybe there is some easier way to achieve this?
//Creating the floating window and set its Layout parameters
mFloatViewLayoutParams = new WindowManager.LayoutParams();
mFloatViewLayoutParams.format = PixelFormat.TRANSLUCENT;
mFloatViewLayoutParams.flags = WindowManager.LayoutParams.FORMAT_CHANGED;
mFloatViewLayoutParams.type = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
? WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
: WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
mFloatViewLayoutParams.gravity = Gravity.START;
mFloatViewLayoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
mFloatViewLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
// adding the floatview to the WindowManager
LayoutInflater inflater = LayoutInflater.from(activity);
mFloatView = inflater.inflate(R.layout.float_view_layout, null);
mWindowManager.addView(mFloatView, mFloatViewLayoutParams);
...
// some clickListener to start the screenshot
ImageButton imageButton = mFloatView.findViewById(R.id.imageButton);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
mActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
// here I want to get the view of the whole screen to
screenshot it
}
});
}
});
What I don't want: I don't want to screenshot the view of my main activity which starts the floating button.
I am really out of ideas about how to handle this challenge. Hopefully, someone has a guess!
Cheers!

Related

Popover hint for imageview

Please take a look at image.
I am littile bit confused how to implement this pop over view on an Image view. Actually the design is an stepper module. so once the user choose an option, the layout showing next step. So each step should have an Image View. I implemented the stepper layout, but have some confusion on how to set the popover on each steps. ie just below to the imageview.
generated dynamic image view for showing steps.
for (int i = 0; i < CreationData.size(); i++) {
imageContainer[i] = new ImageView(getContext());
imageContainer[i].setImageResource(getResId(i, 0));// 0 determines the state of the image icon.
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(100, 100);
imageContainer[i].setLayoutParams(param);
dividerLine[i] = new View(getContext());
dividerLine[i].setBackgroundColor(getResources().getColor(R.color.stepper_line_color));
}
.
Next I want to append the pop over to each image view
Maybe using setOnLongClickListener on the ImageView help you:
myImageView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
Toast.makeText(context,"Text to show", Toast.LENGTH_LONG).show();
return true;
}
});
In the xml set the ImageView long-clickable:
<ImageButton
...
android:longClickable="true"
/>
Or you can do the same via code:
myImageView.setLongClickable(true);

How to show long Strings in pages in android?

I have a large String from the database. I showed it in the TextView with scrolling.
This is a totally boring method to show large text though; I want to show data in pages.
There are multiple questions similar to this one, but I am not able to find any solutions from them; they are incomplete and off track.
Challenges
How to count number of lines that fit to screen?
How to split string in sub parts so that each substring fits to a page?
I think this should solve the problem,
1. it will split the large string
2. display on view as you want
So if u dont want to use Scroll View then well i tried something and i hope it helps
Following i done
1. inflate required views into parent view
2. set large text into Textview
3. Check if other text views are within the (visible) screen or not
4. update appropriately
Main Activity class
public class MainActivity extends Activity {
private TextView text1, text2;
private Context context;
private LinearLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getLayoutInflater().getContext();
layout = (LinearLayout) findViewById(R.id.parentLayout);
setTextViews();
// checkViewAndUpdate();
layout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
checkViewAndUpdate();
}
});
}
private void checkViewAndUpdate() {
Rect rect = new Rect();
layout.getHitRect(rect);
if (text1.getLocalVisibleRect(rect)) {
Toast.makeText(context, "visible", Toast.LENGTH_LONG).show();
} else {
// update if not visible decreasing the view text size
Toast.makeText(context, "Not visible", Toast.LENGTH_LONG).show();
}
if (text2.getLocalVisibleRect(rect)) {
Toast.makeText(context, "visible", Toast.LENGTH_LONG).show();
} else {
// update if not visible decreasing the view text size
Toast.makeText(context, "Not visible", Toast.LENGTH_LONG).show();
}
}
private void setTextViews() {
text1 = new TextView(context);
text2 = new TextView(context);
text1.setText("your link seems to work. where/when do u trying to call getXXXVisibleRect()? if u do it at onCreate your link seems to work. where/when do u trying to call getXXXVisibleRect()? if u do it at onCreate your link seems to work. where/when do u trying to call getXXXVisibleRect()? if u do it at onCreateyour link seems to work. where/when do u trying to call getXXXVisibleRect()? if u do it at onCreate ");
text2.setText("text2");
text1.setTextSize(30);
text2.setTextSize(30);
// layout.removeAllViews();
layout.addView(text1);
layout.addView(text2);
}
}

DialogFragment not visible but selectable

I have a very strange problem on Android using DialogFragments.
I have a FrameLayout with no content and the OnClickListener set to open a FragmentDialog where the user can choose what type of content he wants to add.
If I choose an image from the gallery, this image will be loaded and an image view inside the Frame-Layout is created and the image is shown. If the user clicks again on the layout the selection dialog should open again and the user can select a new image and the old one would be replaced.
This works quite well on my device with Android 4.1. But if I test it on Android 2.3 something strange happens:
The first dialog appears and the user can choose an image from the gallery. But if the user clicks again the dialog is not shown again. But the display becomes darker as if the dialog would be there but is not shown. If I click on the position where the selection dialog should be the gallery is started again. So the dialog is definitely there, but it is simply not shown.
I have tried almost everything that came to my mind(and what I found in the internet) to fix this issue, but it does not help anything. Of course I am using the support library to import the Fragment and DialogFragment.
I start this dialog from a Fragment which is embedded in a ViewPager. So it is basically a tabbed view. What's interesting: I I run into this bug and the display is just getting darker but no dialog is visible, I can cancel the invisible dialog and just drag the ViewPager a bit to left or right(to the next fragment) and if I get back and click on the content again the dialog is shown again.
But if I am dragging the ViewPager around there are no log messages so I have no idea why the dialog is suddenly visible again if I firstly move the page(only a bit is enough).
Here is some of my code:
in the onCreateView method I do the following:
rootView = inflater.inflate(args.getInt(ARG_OBJECT_TYPE), container, false);
editorActivity = ((NoteEditorActivity) EditorSectionFragment.this.getActivity());
// ...
if( fragmentId == R.layout.fragment_note_preferences_editor ){
// the other page
else if( fragmentId == R.layout.fragment_note_editor ) {
final View addLeftElement = rootView.findViewById( R.id.addLeftElement );
final View addRightElement = rootView.findViewById( R.id.addRightElement );
final View addTopElement = rootView.findViewById( R.id.addTopElement );
final View addBottomElement = rootView.findViewById( R.id.addBottomElement );
final FrameLayout contentLayout = (FrameLayout) rootView.findViewById( R.id.contentLayout );
showNavigation(editorActivity, contentLayout, editorActivity.currentPosition.x, editorActivity.currentPosition.y);
contentLayout.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
Log.i("CONTENT CREATOR", "create new element at " + editorActivity.currentPosition);
//((NoteEditorActivity) EditorSectionFragment.this.getActivity()).showContentSelectionDialog();
((NoteEditorActivity) EditorSectionFragment.this.getActivity()).showCameraChooseDialog();
}
});
}
showCameraChoose(I also did it without the FragmentTransaction, but this didn't work either)
protected void showCameraChooseDialog() {
getSupportFragmentManager().executePendingTransactions();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment prev = getSupportFragmentManager().findFragmentByTag("cameraChoose");
if( prev != null ){
Log.i("PREVIOUS", "Remove previous dialog");
ft.remove(prev);
}
(new CameraSelectionDialog()).show( ft, "cameraChoose");
}
CameraSelectionDialog:
public static class CameraSelectionDialog extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final CharSequence[] items = {"Camera", "Gallery"};
AlertDialog.Builder builder = new AlertDialog.Builder( this.getActivity() );
builder.setTitle("Choose how to get the image!");
builder.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if( which == 0){
((NoteEditorActivity)(getActivity())).startCamera();
CameraSelectionDialog.this.dismiss();
}
else{
((NoteEditorActivity)(getActivity())).startGallery();
CameraSelectionDialog.this.dismiss();
}
}
});
return builder.create();
}
}
The startGallery method simply starts an gallery intent:
protected void startGallery() {
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto , ActionCodes.GALLERY_ACTION_CODE);
}
This image is handled in the onActivityResult method. But it does not matter what I choose to do in the onActivityResult method. Even if I don't create the image, the problem occurs.
I have no idea what I can do to solve this problem and I hope that maybe you can think of a reason for this strange bug. I am grateful for any advice or hint what could be wrong.
Thank you in advance!

How to draw a view on top of everything?

I want to make an app that can create notification on the screen on top of anything that is currently being displayed. Something like the Go SMS message popup or something like the ChatHead in the following picture:
It would be even better if it is possible to draw it dynamically including touch events.What is the conventional or standard way to do this?
Example:
Like an Icon that can be clicked or dragged no matter whether you are on home screen or app drawer or other apps.Pay attention to the circular icons near the edges of the screen in the picture posted. You can drag them anywhere in any app.
What you are looking for is System Alert Window.
There's a library called StandOut! which will assist you in creating such apps.
Here is how things like Toast and dialog windows work:
In the case where just adding or bringing to front does not work, say when you are having a service add its own view to another client activity or application (FaceUnlock does this), or you cannot depend on hierarchies, you need to use the window manager and a window token to do the trick. You can then create layouts and take advantage of animations and hardware acceleration as before.
WindowManager windowManager = (WindowManager) getBaseContext().getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.FIRST_SUB_WINDOW);
layoutParams.width = 300;
layoutParams.height = 300;
layoutParams.format = PixelFormat.RGBA_8888;
layoutParams.flags =
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
| WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
layoutParams.token = getWindow().getDecorView().getRootView().getWindowToken();
//Feel free to inflate here
mTestView = new View(this);
mTestView.setBackgroundColor(Color.RED);
//Must wire up back button, otherwise it's not sent to our activity
mTestView.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
onBackPressed();
}
return true;
}
});
windowManager.addView(mTestView, layoutParams);
Then be sure to remove the view onDestroy (or onPause) or you will crash
if (mTestView != null) {
WindowManager windowManager = (WindowManager) getBaseContext().getSystemService(Context.WINDOW_SERVICE);
if (mTestView.isShown()) {
windowManager.removeViewImmediate(mTestView);
}
}
You don't need a new activity to do this. All you need to do is to add another view into your existing activity and bring it to the front, and draw/write the things that you want into that view.
If you want to do special things with this extra view, you could create your own view class
class DrawOnTop extends View {
public DrawOnTop(Context activity) {
super(activity);
}
#Override
protected void onDraw(Canvas canvas) {
// put your drawing commands here
}
}
and then you do something like
DrawOnTop mDraw = new DrawOnTop(this);
addContentView(mDraw, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mDraw.bringToFront();
Then to force it to draw, you need to use mDraw.invalidate();
You could have the parent of your whole layout as RelativeLayout. The first child being the "root" of your main layout. Anything after that can be considered an overlay which is placeable to your whims.
Example:
<RelativeLayout>
<LinearLayout>
... Main Layout here ...
</LinearLayout>
<TextView left="20dip" top="20dip" text="Overlay" alpha="0.7" />
</RelativeLayout>
The best way is to start a service with your application.
Create an ImageView.
Set the LayoutParams of the Image View.
Add the view along with the params to the window manager when the service is created.
ALL SET
Your Image sticks to your window (At any screen over all apps), till you application is closed.
You can even add onclicklisteners and ontouchlisteners to the imageview.
Eg. OnClick listeners to perform some actions and Ontouchlisteners move the image along the screen.

Android PopupWindow showAtLocation() vs showAsDropDown() - first one is working for me and second is not

I used the following code to show a PopupWindow in android, I want to pin a popup to a Button,
btnShowPopup = (Button)findViewById(R.id.btnShowPopup);
btnShowPopup.setOnClickListener(this);
#Override
public void onClick(View v) {
if(v == btnShowPopup){
View popupViw = getLayoutInflater().inflate(R.layout.popup_demo, null);
PopupWindow popup = new PopupWindow(this);
popup.setContentView(popupViw);
popup.showAsDropDown(btnShowPopup,10,10);
//popup.showAtLocation(popupViw, Gravity.BOTTOM, 10, 10);
}
}
In this case popup.showAtLocation() is working well, but popup.showAsDropDown() not displaying any popups for me.
It is probably working, but showing the popup below the visible window, as it is attaching to the lower left corner of the btnShowPopup view.

Categories