checkbox - Android - checked perform action -


as i'm new android, problem i'm developing application want increment/decrement numbers clicking on buttons in home page,but first of should check in settings menu check box checked,if checked perform actions...settings menuhome page

mainactivity.java

package com.example.sanple;  import android.app.activity; import android.content.intent; import android.content.sharedpreferences; import android.content.sharedpreferences.editor; import android.os.bundle; import android.preference.preferencemanager; import android.util.log; import android.view.keyevent; import android.view.menu; import android.view.menuinflater; import android.view.menuitem; import android.view.view; import android.view.view.onclicklistener; import android.view.window; import android.view.windowmanager; import android.widget.button; import android.widget.checkbox; import android.widget.textview; import android.widget.toast;  public class mainactivity extends activity {      public button incre;     public button dec;     public textview nub;     public static int count = 0;     public string string;      /*      * @override protected void onstart() { super.onstart();      *       * bundle bundle = getintent().getextras(); if (bundle != null) { if      * (bundle.getboolean("checked")) { mainactivity activity = new      * mainactivity(); activity.addlisteneroncheckbox(); } else {      * toast.maketext(getapplicationcontext(), "try check checkbox",      * toast.length_short).show(); }      *       * } }      */      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         requestwindowfeature(window.feature_no_title);         setcontentview(r.layout.activity_main);         addlisteneroncheckbox();          this.getwindow().setflags(windowmanager.layoutparams.flag_fullscreen,                 windowmanager.layoutparams.flag_fullscreen);      }      void addlisteneroncheckbox() {         incre = (button) findviewbyid(r.id.increment);         dec = (button) findviewbyid(r.id.decrement);         nub = (textview) findviewbyid(r.id.brakecounter);          incre.setonclicklistener(new onclicklistener() {             public void onclick(view v) {                 log.d("src", "button clciked");                 count++;                 string = integer.tostring(count);                 nub.settext(string);             }         });          dec.setonclicklistener(new onclicklistener() {              public void onclick(view v) {                 log.d("src", "button clciked");                 count--;                 string = integer.tostring(count);                 nub.settext(string);              }         });     }      @override     public boolean dispatchkeyevent(keyevent event) {          int action = event.getaction();         int keycode = event.getkeycode();         switch (keycode) {         case keyevent.keycode_volume_up:             if (action == keyevent.action_up) {                 int = count++;                 if (i > 10) {                     toast.maketext(getapplicationcontext(),                             "applying many times brake", toast.length_short)                             .show();                  }                 string = integer.tostring(count);                 nub.settext(string);              }             return true;         case keyevent.keycode_volume_down:             if (action == keyevent.action_down) {                 count--;                 string = integer.tostring(count);                 nub.settext(string);              }          default:             break;         }         return super.dispatchkeyevent(event);     }      @override     public boolean oncreateoptionsmenu(menu menu) {         // menuinflater inflater = new menuinflater(getapplicationcontext());         getmenuinflater().inflate(r.menu.main, menu);         return true;     }      @override     public boolean onoptionsitemselected(menuitem item) {          switch (item.getitemid()) {         case r.id.action_settings:             intent startactivity = new intent(this, settings.class);             startactivity(startactivity);              break;          default:             break;         }         return super.onoptionsitemselected(item);     } } 

settings.java

package com.example.sanple;  import android.r.color; import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.menu; import android.widget.button; import android.widget.checkbox; import android.widget.compoundbutton; import android.widget.compoundbutton.oncheckedchangelistener; import android.widget.textview;  public class settings extends activity {      public textview view;     public checkbox box;     public button button;      // keyevent keyevent = null;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_settings);          view = (textview) findviewbyid(r.id.enable_counter);         box = (checkbox) findviewbyid(r.id.checkbox1);         box.setbackgroundcolor(color.black);         button = (button) findviewbyid(r.id.save);          box.setoncheckedchangelistener(new oncheckedchangelistener() {              public void oncheckedchanged(compoundbutton buttonview,                     boolean ischecked) {                 /*//here r setting checked true                 box.setchecked(true);                 intent intent = new intent();                 intent.putextra("checked", true);*/              }         });     }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.settings, menu);         return true;     }  } 

check box xml..

activity_settings.xml

<relativelayout 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"     tools:context=".settings" >      <textview         android:id="@+id/enable_counter"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignparentleft="true"         android:layout_alignparenttop="true"         android:layout_marginleft="51dp"         android:layout_margintop="34dp"         android:text="enable_counter"         android:textcolor="#f00" />      <checkbox         android:id="@+id/checkbox1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignbaseline="@+id/enable_counter"         android:layout_alignbottom="@+id/enable_counter"         android:layout_alignparentright="true"         android:layout_marginright="39dp" />      <button         android:id="@+id/save"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignparentbottom="true"         android:layout_alignright="@+id/checkbox1"         android:layout_marginbottom="64dp"         android:text="save" />  </relativelayout> 

do this, in button click event code :

 @override     public boolean onoptionsitemselected(menuitem item) {          switch (item.getitemid()) {         case r.id.action_settings:             intent startactivity = new intent(this, settings.class);             startactivityforresult(startactivity);              break;          default:             break;         }         return super.onoptionsitemselected(item);     } 

in settings :

public void oncheckedchanged(compoundbutton buttonview,                              boolean ischecked) {     intent intent = getintent();     if(ischecked)       intent.putextra("checked", true);     else       intent.putextra("checked",false);              setresult(i,100); } 

//put in main activity

 @override     protected void onactivityresult(int requestcode, int resultcode, intent data) {             if (resultcode == 100) {                boolean val = data.getbooleanextra("checked",false);                if(val==true)                //allow increment decrement                else                //not             }         }     } 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -