Ali Zein
Ali Zein

Reputation: 28

Making my custom view to be extended by many classes

i'm a new in android and that's the first time for me to ask, so i'm sorry if the question is easy...

i'll make an app which will contain many classes, but all of them are similar in idea.. (for example, i have a class which will show four pics of children and i should choose one of them, the another class will show four pics BUT for men, and so..)

what i should do please? can i make a class which contain the custom view and extend it in all other classes or what ??

thanks all, it's a great useful site :)

Upvotes: 0

Views: 99

Answers (3)

Syeda Zunaira
Syeda Zunaira

Reputation: 5207

I think you should make only one class and one layout.

Each time a button is clicked you should replace the old pics from the new ones and all the code will remain same.

Just assign a unique value to the correct picture (answer).

Upvotes: 1

user4230024
user4230024

Reputation:

Its too simple just by extending your activities you can achieve this

here is the code

public abstract class BaseActivity extends Activity {
@Override
public void onCreate(bundle) {
    super.onCreate(bundle);
    setContentView(getLayoutResourceId());
}

      protected abstract int getLayoutResourceId();
   }

    public class Activity1 extends BaseActivity {
@Override
public void onCreate(bundle) {
    super.onCreate(bundle);
    // do extra stuff on your resources, using findViewById on your layout_for_activity1
}

@Override
protected int getLayoutResourceId() {
    return R.layout.layout_for_activity1;
}
 }

Upvotes: 0

Supriya
Supriya

Reputation: 1098

Write a BaseActivity which extends Activity class, and then extend BaseActicity to your class.

Upvotes: 0

Related Questions