How to create a custom view that is initialize only once and common for all activity in Android? -


i trying create custom view in android. following condition :

  1. the view should initialize once
  2. the same view should used throughout application

i have tried initialize view application class , adding current activity.

following code :

viewinappapplcation

public class viewinappapplcation extends application { @override public void oncreate() {     super.oncreate();     registeractivitylifecyclecallbacks(new appactivitycallback()); }  private class appactivitycallback implements activitylifecyclecallbacks {     statusbarview statusbarview = null;      @override     public void onactivitycreated(activity activity, bundle bundle) {         final activity activity1 = activity;         activity.runonuithread(new runnable() {             @override             public void run() {                 if (statusbarview == null) {                     statusbarview = new statusbarview(activity1);                  }             }         });       }      @override     public void onactivitystarted(activity activity) {      }      @override     public void onactivityresumed(activity activity) {         linearlayout view = (linearlayout) activity.findviewbyid(r.id.status_bar_view_holder);         if (view != null) {             view.addview(statusbarview, 0);         }     }      @override     public void onactivitypaused(activity activity) {         linearlayout view = (linearlayout) activity.findviewbyid(r.id.status_bar_view_holder);         if (view != null) {             view.removeview(statusbarview);         }     }      @override     public void onactivitystopped(activity activity) {      }      @override     public void onactivitysaveinstancestate(activity activity, bundle bundle) {      }      @override     public void onactivitydestroyed(activity activity) {      } } } 

statusbarview

public class statusbarview extends relativelayout{  /**activity context object.*/ private context context = null; /** view object */ private statusbarview statusbarview = null; /**  * constructor  *  * @param context - context reference  *  */ public statusbarview(context context) {     super(context);     this.context = context;     this.statusbarview = this;     initview(); } /**  * constructor  *  * @param context - context reference  * @param attrs - set of attributes  */ public statusbarview(context context, attributeset attrs) {     super(context, attrs);     this.context = context;     this.statusbarview = this;     initview(); }  /**  * constructor  *  * @param context - context reference  * @param attrs - attribute list  * @param defstyleattr - default style attributes  */ public statusbarview(context context, attributeset attrs, int defstyleattr) {     super(context, attrs, defstyleattr);     this.context = context;     this.statusbarview = this;     initview(); }  /**  * inflate app status bar xml , add view  */ private void initview() {     layoutinflater inflater = layoutinflater.from(context);     view view = inflater.inflate(r.layout.view_app_status_bar, this, false);     new asy().execute();     statusbarview.addview(view); }   public void setvalue(string value) {     textview textview = (textview) findviewbyid(r.id.txt_status);     textview.settext(value);  }    private class asy extends asynctask<void,integer,string> {     @override     protected string doinbackground(void... params) {         (int i=0;i<20;i++)         {             publishprogress(i);             try {                 thread.sleep(3000);             } catch (interruptedexception e) {                 e.printstacktrace();             }         }         return "done";     }      @override     protected void onpostexecute(string result) {         super.onpostexecute(result);      }      @override     protected void onprogressupdate(integer... values) {         super.onprogressupdate(values);         setvalue(values[0]+"");      } } } 

mainactivity

public class mainactivity extends activity {  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     findviewbyid(r.id.next).setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             intent intent = new intent(mainactivity.this, main2activity.class);             startactivity(intent);          }     });  } } 

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>  <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:orientation="vertical">  <linearlayout     android:background="@color/coloraccent"     android:id="@+id/status_bar_view_holder"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:orientation="vertical" />  <button     android:id="@+id/next"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="hello world!" />   </linearlayout> 

this code add statusbar view current activity. there other way it?


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -