dowjones123
dowjones123

Reputation: 3837

Removing duplication in Android Layout and Activity codes

I have a simple Activity/ LinearLayout combination that has two buttons and changes the background colour when each button is clicked. The code is as below

It appears to me that there is a lot of high-level duplication of code. For example, I should not need to define and make variables out of the buttons and the LinearLayout background in my Java file. When I express my intention to couple them, I should be getting them automatically

So, lines like btnBlue = (Button) findViewById(R.id.btnBlue); should not be necessary. A good framework should allow me to use some conventions (like give me a Java variable btnBlue if my resource file @+id/ resource is named as btnBlue, etc

Where can I find a framework that achieves these for me in Android programming?

Thanks

Layout xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff000000"

    android:weightSum="1"
    android:id="@+id/background">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Linear Layout Tutorial"
        android:textColor="#ff33ff"
        android:textSize="32sp"
        />

    <Button
        android:id="@+id/btnGreen"
        android:layout_width="177dp"
        android:layout_height="wrap_content"
        android:text="Change to Green"


        android:layout_weight="0.07" />

    <Button
        android:id="@+id/btnBlue"
        android:layout_width="400sp"
        android:layout_height="wrap_content"
        android:text="Change12 to Blue"

        android:layout_weight="0.19" />


</LinearLayout>

Activity Java code

package com.example.xxx.testandroid;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.renderscript.Sampler;
import android.view.Menu;
import android.view.MenuItem;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;


public class MainActivity extends Activity {

    private final String TAG = "TKT";
    LinearLayout background;
    Button btnGreen, btnBlue;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.som_linear);
        Log.d(TAG, "onCreate");

        background = (LinearLayout) findViewById(R.id.background);
        btnBlue = (Button) findViewById(R.id.btnBlue);
        btnGreen = (Button) findViewById(R.id.btnGreen);

        btnGreen.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                // click button code here
                background.setBackgroundColor(Color.parseColor("#00ff00"));
            }
        });

        btnBlue.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                // click button code here
                background.setBackgroundColor(Color.parseColor("#006699"));
            }
        });

    }



}

Upvotes: 1

Views: 109

Answers (1)

Joao Sousa
Joao Sousa

Reputation: 4123

ButterKnife by Jake Wharton seems a good solution for your problem.

Upvotes: 1

Related Questions