android - Code optimisation. (Architecture) -


i'm making quiz app. user has finish phrase shown on display , write name of car in edittext, after pushing on button, if answer right, edittext become green, if doesn't, become red. if answers right (green), intent move on next activity.

the question is: how optimize code, don't how it's like? if decide add more options wouldn't readable.

public class mainactivity extends appcompatactivity {  edittext et_one_one, et_one_two, et_one_three; button buttoncheck;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      et_one_one = (edittext) findviewbyid(r.id.et_one_one);     et_one_two = (edittext) findviewbyid(r.id.et_one_two);     et_one_three = (edittext) findviewbyid(r.id.et_one_three);      buttoncheck = (button) findviewbyid(r.id.buttoncheck);      buttoncheck.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             boolean allanswerscorrect = true;             string t1 = et_one_one.gettext().tostring().tolowercase();             string t2 = et_one_two.gettext().tostring().tolowercase();             string t3 =  et_one_three.gettext().tostring().tolowercase();             if (t1.equals("maserati")){                 et_one_one.setbackgroundcolor(color.green);             }             else {                 allanswerscorrect = false;                 et_one_one.setbackgroundcolor(color.red);             }             if (t2.equals("mercedes")){                 et_one_two.setbackgroundcolor(color.green);             }             else{                 allanswerscorrect = false;                 et_one_two.setbackgroundcolor(color.red);             }             if (t3.equals("bmw")){                 et_one_three.setbackgroundcolor(color.green);             }             else{                 allanswerscorrect = false;                 et_one_three.setbackgroundcolor(color.red);             }             if(allanswerscorrect) {                 intent intent = new intent(mainactivity.this, secondactivity.class);                 startactivity(intent);             }         }     }); } 

}

in layout use scrollview:

<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" tools:context=".mainactivity"     android:orientation="vertical">      <scrollview         android:layout_width="match_parent"         android:layout_height="match_parent"         android:id="@+id/scrollview2" >          <linearlayout             android:orientation="vertical"             android:layout_width="match_parent"             android:layout_height="match_parent"             android:padding="20dp">              <textview                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="@string/task1"                 android:id="@+id/textview1"                 android:textsize="20sp"                 android:textcolor="#010101" />              <linearlayout                 android:orientation="horizontal"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content">                  <textview                     android:layout_width="wrap_content"                     android:layout_height="wrap_content"                     android:paddingtop="10dp"                     android:paddingleft="10dp"                     android:paddingright="15dp"                     android:textsize="20sp"                     android:text="@string/one_one"                     android:id="@+id/textview2" />                  <edittext                     android:layout_width="wrap_content"                     android:layout_height="wrap_content"                     android:id="@+id/et_one_one"                     android:inputtype="textcapsentences"/>             </linearlayout>              <linearlayout                 android:orientation="horizontal"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content">                  <textview                     android:layout_width="wrap_content"                     android:layout_height="wrap_content"                     android:paddingtop="10dp"                     android:paddingleft="10dp"                     android:paddingright="15dp"                     android:textsize="20sp"                     android:text="@string/one_two"                     android:id="@+id/textview3" />                  <edittext                     android:layout_width="wrap_content"                     android:layout_height="wrap_content"                     android:id="@+id/et_one_two"                     android:inputtype="textcapsentences"/>             </linearlayout>              <linearlayout                 android:orientation="horizontal"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content">                  <textview                     android:layout_width="wrap_content"                     android:layout_height="wrap_content"                     android:paddingtop="10dp"                     android:paddingleft="10dp"                     android:paddingright="15dp"                     android:textsize="20sp"                     android:text="@string/one_three"                     android:id="@+id/textview4" />                  <edittext                     android:layout_width="wrap_content"                     android:layout_height="wrap_content"                     android:id="@+id/et_one_three"                     android:inputtype="textcapsentences"/>             </linearlayout>              <linearlayout                 android:orientation="horizontal"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content">                  <textview                     android:layout_width="wrap_content"                     android:layout_height="wrap_content"                     android:paddingtop="10dp"                     android:paddingleft="10dp"                     android:paddingright="15dp"                     android:textsize="20sp"                     android:text="@string/one_four"                     android:id="@+id/textview5" />                  <edittext                     android:layout_width="wrap_content"                     android:layout_height="wrap_content"                     android:id="@+id/et_one_four"                     android:inputtype="textcapsentences"/>              </linearlayout>              <linearlayout                 android:orientation="horizontal"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content">                  <textview                     android:layout_width="wrap_content"                     android:layout_height="wrap_content"                     android:paddingtop="10dp"                     android:paddingleft="10dp"                     android:paddingright="15dp"                     android:textsize="20sp"                     android:text="@string/one_five"                     android:id="@+id/textview6" />                  <edittext                     android:layout_width="wrap_content"                     android:layout_height="wrap_content"                     android:id="@+id/et_one_five"                     android:inputtype="textcapsentences"/>             </linearlayout>              <linearlayout                 android:orientation="horizontal"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:layout_gravity="right">                  <button                     android:layout_width="wrap_content"                     android:layout_height="wrap_content"                     android:text="check"                     android:id="@+id/buttoncheck" />             </linearlayout>         </linearlayout>     </scrollview>  </linearlayout> 

i suggest nice library called butterknife jakewharton :

http://jakewharton.github.io/butterknife/

in case code :

public class mainactivity extends appcompatactivity {  @bind(r.id.et_one_one) edittext et_one_one; @bind(r.id.et_one_two) edittext et_one_two; @bind(r.id.et_one_three) edittext et_one_three; @bind(r.id.buttoncheck) button buttoncheck;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     butterknife.bind(this);     ... }  @onclick(r.id.submit) public void submit(view view) {     // check code here } 

also can group edit texts in group :

@bind({ r.id.et_one_one, r.id.et_one_two, r.id.et_one_three }) list<edittext> nameviews; 

and apply setters or actions on them :

... butterknife.apply(nameviews, lowercase); ...  static final butterknife.action<view> lowercase= new butterknife.action<view>() {   @override public void apply(view view, int index) {      // todo set text of view lowercase , disable textview   } }; 

Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -