1

I have a few activities in an app I'm writing that I want to put a form on the page. It's the same form for all the activities that does the same except for different title and text.

Instead of rewriting the controls and the logic for each activities I'm looking for a way to create my own control that I can put in my layout xml files with my own properties so all I'll need to do is write it once and use that control where I need to.

how can I do such a thing?

Thanks

developer82
  • 13,237
  • 21
  • 88
  • 153

3 Answers3

0

You can use an Activity which extends android.app.Activity and define it the common logic.Then all your other activities will extend it in place of extending android.app.Activity. Then they will inherit it , and you can override what you want to Override as well. This solution is not bad if you have exactly the same layout and just some differences, you can load the layout.xml only once and then use it in the child class as you like.

SuperClass :

    public SuperClass extends Activity{

    protected TextView myTextView;

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

    myTextView = (TextView) findViewById(R.id.my_text_view);

    }
}

ChildClassA :

public ChildClassA extends SuperClass {

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

    myTextView.setText("I am in A");

    }
}

ChildClassB :

    public ChildClassB extends SuperClass {
         @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

        setContentView(R.layout.my_layout);

        myTextView.setText("I am in B");

        }
}

Other suggestion: Another idea is to create a custom view. see this tutorial also.

ahmed_khan_89
  • 2,755
  • 26
  • 49
  • but what about the controls on the screen? like the textboxes and buttons? I need it as a control so I can also place it where I want on each activity that uses it. – developer82 May 14 '14 at 13:00
  • I updated my answer to try to help a bit more, and to answer to your last question. – ahmed_khan_89 May 14 '14 at 13:16
0

What about writing your control as an android Fragment? In the xml you can declare a FrameLayout and then insert the fragment inside it using the replace() method of FragmentTransaction.

splinter123
  • 1,183
  • 1
  • 14
  • 34
  • does it support old android devices (my understanding is that fragments are new). can you point me to some guides/documentation on the subject? thanks. – developer82 May 14 '14 at 13:29
  • 1
    Fragments are supported down to Android 1.6 with Compatibility Library: http://developer.android.com/guide/components/fragments.html – einschnaehkeee May 14 '14 at 13:33
0

Depends on what differences you have in each Activity and how you access them.

If it's only some strings, that are different, put those strings inside the Intent, which starts the Activities and grab them in onCreate().

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.lyt_template_form);

    ((TextView) findViewById(R.id.txt_form_headline)).setText(getIntent().getStringExtra("headline");
    ((TextView) findViewById(R.id.txt_form_subtitle1)).setText(getIntent().getStringExtra("subtitle1");
    //etc...

If there are more differences in your control code and you need to distinguish some kind of "Form Type A", "Form Type B", etc., you can put an indicator inside your Intent and check for that to make different decisions in your code.

So e.g.

if (getArguments().getInt("Type") == 0) {
    // do stuff in control like this
} else if (getArguments().getInt("Type") == 1) {
    // do stuff in control like this
// etc.

getArgument() is the equivalent of getIntent().getXyzExtra()

einschnaehkeee
  • 1,858
  • 2
  • 17
  • 20