HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Android - Simple multi-activity app

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
simplemultiandroidappactivity

Problem

This is a task from lab classes at my university. Problem statement was to showcase following features:

  • Multiple activities in app.



  • Passing data from one activity to another.



  • Changing behavior of back button in one of activities.



  • Getting picture from camera and displaying it in app.



  • Binding callback action in XML file instead of Java code.



It's based heavily on examples, but I wonder if I can get any general advice about code quality.

Whole project on GitHub:
https://github.com/rogalski/tmlab2

And Java and layout code:

MyActivity.java:

package lab.tm.rogalski.lab2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MyActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setListeners();
    }

    @Override
    public void onResume() {
        EditText editText = (EditText) findViewById(R.id.TextToSend);
        editText.setText("");
        super.onResume();
    }

    private void setListeners() {
        Button sendButton = (Button) findViewById(R.id.SendBtn);
        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText editText = (EditText) findViewById(R.id.TextToSend);
                String data = editText.getText().toString();
                sendIntent(data);
            }
        });
    }

    private void sendIntent(String data) {
        Intent intent = new Intent(this, SecondActivity.class);
        intent.putExtra("data", data);
        startActivity(intent);
    }

}


SecondActivity.java

```
package lab.tm.rogalski.lab2;

import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bun

Solution

There is just a couple of small things you can improve on but mostly are personal preferences.

The first thing that you should do is getting used to have all the string in the strings.xml file in the values folder. This is very useful to keep organized and for localization.

Ids of elements in the xml often go in the format camelCase, like in imageView. If you don't like it I suggest at least sticking to one version.

I suggest avoiding using very short names (like v for the Views) as variable names. In long methods this could get messy. The only case where I see it reasonable is for the index in a loop.

In the xml fill_parent has been deprecated since API 8, you should use match_parent everywhere.

Context

StackExchange Code Review Q#90506, answer score: 3

Revisions (0)

No revisions yet.