Baddog22
Baddog22

Reputation: 35

Declaring UI elements in Java (android) efficiently

Do I have to (in each function) declare a UI element again (Eg (button) button) or is there a simpler way to do it. (This is not the final code, its just to give an example of what I mean (there is a decrypt function and a simple clear button ). This is my first ever program written in Java so any hints/Improvements would be most welcome!

public class MainActivity extends Activity {

View.OnLongClickListener Longpress = new View.OnLongClickListener()
{

//A mess around with Toast - long pressing buttons will (eventually) give a description of their purpose          

 @Override
public boolean onLongClick(View v) {
    Button heldButton = (Button)v;
   String heldButtonText = heldButton.getText().toString();
    Toast.makeText(getApplicationContext(), heldButtonText, Toast.LENGTH_LONG).show();

    return true;
}
};



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

     Button buttonCompute = (Button)findViewById(R.id.btn_compute);
   Button buttonClear = (Button)findViewById(R.id.btn_clear);
    Button buttonSwitch = (Button)findViewById(R.id.btn_switch);
    buttonClear.setOnLongClickListener(Longpress);
    buttonCompute.setOnLongClickListener(Longpress);
    buttonSwitch.setOnLongClickListener(Longpress);
}

public void onButtonPress(View view)
{
    TextView output_textView = (TextView)findViewById(R.id.Output);
    TextView input_textView = (TextView)findViewById(R.id.Input);

    int pos;
    RadioButton encrypt = (RadioButton)findViewById(R.id.rdo_encrypt);
    RadioButton decrypt = (RadioButton)findViewById(R.id.rdo_decrypt);


    if(encrypt.isChecked()!= false || decrypt.isChecked()!= false  )
    {
        if (encrypt.isChecked()==true)
        {
            output_textView.setText(encryptme(input_textView.getText().toString()));
        }
        if (decrypt.isChecked()==true)
        {
            output_textView.setText(decryptme(input_textView.getText().toString()));
        }

    }
    else
    {
        new AlertDialog.Builder(this)
                .setTitle("Error!")
                .setMessage("Please check one radio button")
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                })
                .setIcon(android.R.drawable.ic_dialog_alert)
                .show();
    }
}

  private String encryptme(String inp)
{
    TextView password_textView= (TextView)findViewById(R.id.Password);
    int pos=0;

    String password = password_textView.getText().toString();
    char[] chararray = password.toCharArray();
    String  result = "";
    for(char c : inp.toCharArray())
    {

        if(pos>=password.length())
        {
            pos=0;
            char keychar = chararray[pos];
            result += (char)(c + keychar);
            pos+=1;
        }
        else
        {

            char keychar = chararray[pos];
            result += (char)(c + keychar);
            pos+=1;

        }
    }

    return result;
}

Upvotes: 0

Views: 132

Answers (1)

Apurva
Apurva

Reputation: 7911

Define all your data types and objects at class level then you can use it anywhere in any method of the class without initializing it again.

Such as,

public class MainActivity extends Activity{

    Button btn;

    onCreate(Bundle icreate){

        super.onCreate(icreate);
        setContentView(R.layout.main_layout);

        bnt = (Button) findViewById(R.id.buttonId);
        }

    public void anyMethod(){

        //here you can use btn without defining it again
        //suppose you are setting onClickListener then

        btn.setOnClickListener(new View...){
         ....
        }
    }

}

Upvotes: 1

Related Questions