android - Display layout every activity visit -
hi goal display layout containing video in every activity visited chat application possible?
if yes please guide me approach thank you.
i know possible if use fragment our app approach using lot of activities
step-1: add android.permission.system_alert_window permission androidmanifest.xml file. permission allows app create windows , shown on top of other apps.
<uses-permission android:name="android.permission.system_alert_window"/>
step-2: create layout of chat head want display.
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="65dp" android:id="@+id/chat_head_root" android:layout_height="wrap_content" android:orientation="vertical"> <!--profile image chat head.--> <imageview android:id="@+id/chat_head_profile_iv" android:layout_width="60dp" android:layout_height="60dp" android:layout_margintop="8dp" android:src="@drawable/ic_android_circle" tools:ignore="contentdescription"/> <!--close button--> <imageview android:id="@+id/close_btn" android:layout_width="26dp" android:layout_height="26dp" android:layout_marginleft="40dp" android:src="@drawable/ic_close" tools:ignore="contentdescription"/> </relativelayout>
now create service called chatheadservice. whenever want display chat head, start service using startservice() command. in oncreate() of service add layout of chat head @ top-left corner of window.
to drag chat head along user’s touch, have override ontouchlistener().and implement onclick stop service.
service class
import android.app.service; import android.content.intent; import android.graphics.pixelformat; import android.os.ibinder; import android.view.gravity; import android.view.layoutinflater; import android.view.motionevent; import android.view.view; import android.view.windowmanager; import android.widget.imageview; public class chatheadservice extends service { private windowmanager mwindowmanager; private view mchatheadview; public chatheadservice() { } @override public ibinder onbind(intent intent) { return null; } @override public void oncreate() { super.oncreate(); //inflate chat head layout created mchatheadview = layoutinflater.from(this).inflate(r.layout.layout_chat_head, null); //add view window. final windowmanager.layoutparams params = new windowmanager.layoutparams( windowmanager.layoutparams.wrap_content, windowmanager.layoutparams.wrap_content, windowmanager.layoutparams.type_phone, windowmanager.layoutparams.flag_not_focusable, pixelformat.translucent); //specify chat head position params.gravity = gravity.top | gravity.left; //initially view added top-left corner params.x = 0; params.y = 100; //add view window mwindowmanager = (windowmanager) getsystemservice(window_service); mwindowmanager.addview(mchatheadview, params); //set close button. imageview closebutton = (imageview) mchatheadview.findviewbyid(r.id.close_btn); closebutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //close service , remove chat head window stopself(); } }); //drag , move chat head using user's touch action. final imageview chatheadimage = (imageview) mchatheadview.findviewbyid(r.id.chat_head_profile_iv); chatheadimage.setontouchlistener(new view.ontouchlistener() { private int lastaction; private int initialx; private int initialy; private float initialtouchx; private float initialtouchy; @override public boolean ontouch(view v, motionevent event) { switch (event.getaction()) { case motionevent.action_down: //remember initial position. initialx = params.x; initialy = params.y; //get touch location initialtouchx = event.getrawx(); initialtouchy = event.getrawy(); lastaction = event.getaction(); return true; case motionevent.action_up: //as implemented on touch listener action_move, //we have check if previous action action_down //to identify if user clicked view or not. if (lastaction == motionevent.action_down) { //open chat conversation click. intent intent = new intent(chatheadservice.this, chatactivity.class); intent.addflags(intent.flag_activity_new_task); startactivity(intent); //close service , remove chat heads stopself(); } lastaction = event.getaction(); return true; case motionevent.action_move: //calculate x , y coordinates of view. params.x = initialx + (int) (event.getrawx() - initialtouchx); params.y = initialy + (int) (event.getrawy() - initialtouchy); //update layout new x & y coordinate mwindowmanager.updateviewlayout(mchatheadview, params); lastaction = event.getaction(); return true; } return false; } }); } @override public void ondestroy() { super.ondestroy(); if (mchatheadview != null) mwindowmanager.removeview(mchatheadview); } }
sample activity
import android.content.intent; import android.net.uri; import android.os.build; import android.os.bundle; import android.provider.settings; import android.support.v7.app.appcompatactivity; import android.view.view; import android.widget.toast; public class mainactivity extends appcompatactivity { private static final int code_draw_over_other_app_permission = 2084; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //check if application has draw on other apps permission or not? //this permission default available api<23. api > 23 //you have ask permission in runtime. if (build.version.sdk_int >= build.version_codes.m && !settings.candrawoverlays(this)) { //if draw on permission not available open settings screen //to grant permission. intent intent = new intent(settings.action_manage_overlay_permission, uri.parse("package:" + getpackagename())); startactivityforresult(intent, code_draw_over_other_app_permission); } else { initializeview(); } } /** * set , initialize view elements. */ private void initializeview() { findviewbyid(r.id.notify_me).setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { startservice(new intent(mainactivity.this, chatheadservice.class)); finish();// comment if want activity not close. } }); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == code_draw_over_other_app_permission) { //check if permission granted or not. if (resultcode == result_ok) { initializeview(); } else { //permission not available toast.maketext(this, "draw on other app permission not available. closing application", toast.length_short).show(); finish(); } } else { super.onactivityresult(requestcode, resultcode, data); } } }
Comments
Post a Comment