JohnPix
JohnPix

Reputation: 1853

Code optimisation. (Architecture)

I'm making a quiz app. User has to finish the phrase shown on display and write the name of the car in the edittext, after pushing on button, if the answer right, edittext become green, if doesn't, become red. If all answers right (green), intent move on next activity.

The question is: how to optimize my code, I don't like how it's look like? If I decide to add some more options it wouldn't be 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 my Layout I 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>

Upvotes: 2

Views: 74

Answers (2)

pptang
pptang

Reputation: 1921

You can use RecyclerView to implement this. With it, you can dynamically add as much EditText for cars as you want. The sample is shown as below:

In your Activity:

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private String[] answerArray;

Button buttonCheck;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    mRecyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);

    answerArray = new String[]{

        "maserati",
        "mercedes",
        "bmw"
        ... (you can add as much as you want)
    }

    buttonCheck = (Button) findViewById(R.id.buttonCheck);



}

For RecyclerView.Adapter

public class MyRecyclerAdapter extends   RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder> {

String[] mAnswerArray;


public static class ViewHolder extends RecyclerView.ViewHolder {

    public EditText editText;

    public ViewHolder(View v) {
        super(v);
        editText = (EditText) v.findViewById(R.id.editText);

    }
}

public MyRecyclerAdapter(String[] answerArray) {

    this.mAnswerArray = answerArray;
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position){

    super.onBindViewHolder(holder, position);

    final String answer = mAnswerArray.get(position);

    if ( holder.editText.getText().toString().toLowerCase().equals(answer) ) {

        holder.editText.setBackgroundColor(Color.GREEN);


    } else {

        holder.editText.setBackgroundColor(Color.RED);

    }

}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.my_list_item, parent, false);

    ViewHolder vh = new ViewHolder(v);

    return vh;
}

@Override
public int getItemCount() {
    return mAnswerArray.size();
}


}

For "my_list_item.xml", you just need to put inside and as for the ButtonCheck you can also pass an array or flag into adapter to record the state of each answer (correct or wrong) in order to decide whether to go to SecondActivity.

Upvotes: 1

Gomoku7
Gomoku7

Reputation: 1372

I would strongly suggest a nice library called Butterknife by JakeWharton :

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

In your case your code would look like this :

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 you can group all your edit texts in a group :

@Bind({ R.id.et_one_one, R.id.et_one_two, R.id.et_one_three })
List<EditText> nameViews;

And apply some 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 the text of the view to lowercase and disable textview
  }
};

Upvotes: 1

Related Questions