Puguh
Puguh

Reputation: 31

Android : Making simple code. DRY

I'm new to Java programming. Can someone here help me to make my code keep DRY.

Button level01 = (Button) findViewById(R.id.level01);
level01.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Prefs.setStagePref(getApplicationContext(), 1);
        Intent play = new Intent(LevelActivity.this, PlayActivity.class);
        startActivity(play);
    }
});

Button level02 = (Button) findViewById(R.id.level02);
level02.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Prefs.setStagePref(getApplicationContext(), 2);
        Intent play = new Intent(LevelActivity.this, PlayActivity.class);
        startActivity(play);
    }
});

Button level03 = (Button) findViewById(R.id.level03);
level03.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Prefs.setStagePref(getApplicationContext(), 3);
        Intent play = new Intent(LevelActivity.this, PlayActivity.class);
        startActivity(play);
    }
});

I want make more than 20 button, so how to make this code DRY.

Thanks

Upvotes: 3

Views: 246

Answers (3)

jonk
jonk

Reputation: 1494

You could consider using butterknife.

Assuming you're using gradle, simply add

compile 'com.jakewharton:butterknife:7.0.1'

to the dependencies section of your build.gradle file under the app/ directory of your project. Then add

ButterKnife.bind(this);

to your Activity.onCreate method, or

ButterKnife.bind(this, view);

to the onCreateView method if this is outside of an Activity. Then if you slightly modify your xml to give each button a tag:

<Button
    android:id="@+id/level01"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:tag="1"/>
<Button
    android:id="@+id/level02"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:tag="2"/>
<Button
    android:id="@+id/level03"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:tag="3"/>

you'll then be able to do:

@OnClick({ R.id.level01, R.id.level02, R.id.level03 })
public void levelButtonClick(Button levelButton) {
    int value = Integer.parseInt(levelButton.getTag());
    Prefs.setStagePref(getApplicationContext(), value);
    Intent play = new Intent(LevelActivity.this, PlayActivity.class);
    startActivity(play);
}

Upvotes: 1

phnmnn
phnmnn

Reputation: 13250

    LinearLayout layout = (LinearLayout) findViewById(R.id.layout_buttons);
    for (int j = 0; j < 20; j++ {
        Button btn = new Button(this);
        btn.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               Prefs.setStagePref(getApplicationContext(), j);
               Intent play = new Intent(LevelActivity.this, PlayActivity.class);
               startActivity(play);
            }
        });
        btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        layout.addView(btn);
    }

Upvotes: 2

user2413972
user2413972

Reputation: 1355

Take your repetitive functional in the method

private void f(int i){
        Prefs.setStagePref(getApplicationContext(), i);
        Intent play = new Intent(LevelActivity.this, PlayActivity.class);
        startActivity(play);
}

Then change the way you work with clicks

In layout "onClick"

   <ImageButton
                        android:layout_width="wrap_content"
                        android:layout_height="fill_parent"
                        android:id="@+id/saveButton" android:src="@android:drawable/ic_menu_save"
                        android:layout_weight="1" android:background="@drawable/menu_button" android:onClick="onClick"/>
                <ImageButton
                        android:layout_width="wrap_content"
                        android:layout_height="fill_parent"
                        android:id="@+id/wallpaperButton" android:src="@android:drawable/ic_menu_gallery"
                        android:layout_weight="1" android:background="@drawable/menu_button" android:onClick="onClick"/>
                <ImageButton
                        android:layout_width="wrap_content"
                        android:layout_height="fill_parent"
                        android:id="@+id/shareButton" android:src="@android:drawable/ic_menu_share"
                        android:layout_weight="1" android:background="@drawable/menu_button" android:onClick="onClick"
                        android:focusableInTouchMode="false"/>

In Activity

public void onClick(View cview) throws IOException {
    switch (cview.getId()) {
        case R.id.saveButton:
            f(1);
            break;

        case R.id.shareButton:
            f(2);
            break;

        case R.id.wallpaperButton:
            f(3);
            break;
}
}

or

MainActivity implements OnClickListener

...

Button level1 = (Button) findViewById(R.id.level1);
level1.setOnClickListener(this);
Button level2 = (Button) findViewById(R.id.level2);
level2.setOnClickListener(this);
Button level3 = (Button) findViewById(R.id.level3);
level3.setOnClickListener(this);
...

public void onClick(View cview) throws IOException {
    switch (cview.getId()) {
        case R.id.level1:
            f(1);
            break;

        case R.id.level2:
            f(2);
            break;

        case R.id.level3:
            f(3);
            break;
}
}

Upvotes: 2

Related Questions