0

Hi all and thank you in advance and sorry for my english. I have two big doubts

1 - I haven't been too much time programming in android, and i pretty sure there is a lot of things i have made in wrong ways. For example, I have made several apps where in xml definition i include another xml.

For example, imagine 2 activities with a header_section.xml being include in both activities xml definition. That header_section has 5 buttons and more views etc. Ok, in the xml is just make an include and it works......but to implement the buttons.....do i have to REPEAT the code in both activities?? it sounds really bad practice to duplicate code in both activities.....but how can i do, for example this in activities A and B? Do i have to put this code exactly the same in both activities classes????

private View header_section;
private Button bExample;

header_section=findViewById(R.id.header_section);
bExample=(Button)header_section.findViewById(R.id.bExample);

bExample.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {               
        //Whatwever...call a number, for example
    }
});

In main xml something like:

<LinearLayout 
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="0dp" >  
    <include android:id="@+id/header_section" android:layout_gravity="center" android:gravity="center" layout="@layout/header_section" />
</LinearLayout>

and in header_section.xml something like:

<LinearLayout 
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="0dp" >  
        <Button android:id="@+id/bExample"  />
</LinearLayout>    

2 - Imagine you have like 10 activities in your app. If in all of them there are a header section and bottom section with the same functionality, changing just the central area (showing different lists, views etc)......is better have just one activitie in ALL the app, with a viewflipper in the central area? or have 10 activities having, i do not know if can be avoided, asked in point 1, code repeated in all 10 activities for the implementation of the headers and bottoms views, handlers etc?

Thanks and best regards

dmon
  • 30,048
  • 8
  • 87
  • 96
Rako
  • 249
  • 1
  • 4
  • 15

1 Answers1

0

1) Yes, usually, you should have to use them BUT you can make it simplier...

1.a) bExample=(Button)findViewById(R.id.bExample); //don't need to load the View

1.b) You can shorten a bit how you call the onclick, in your button/clicable element inside the LAYOUT, here's an example:

 <!--inside layout --> 
    <Button android:id="@+id/bExample"  android:onClick="aceptar" />

and

   //inside the Activity
   public void aceptar(View v){
       //here the code of the button
   }

For the question about implementing the same methods inside all Activities, check this post: Adding the same context menu to multiple activities

2) Depending the application If you don't do much, you can load all in the same activity and HIDE/SHOW the layout elements you don't want. But it's better to use different activities, anyway, if the layout is not "heavy" (too many elements/includes inside) you can load the SAME layout for all your activites, and you only need to change the different contents (strings) and/or HIDE/SHOW the different elements.

Community
  • 1
  • 1
Jordi
  • 616
  • 2
  • 9
  • 16
  • Hi Jordi, thx to answer. About question 1) i still see weird you have to repeat code in 10 places, but the link you posted is a really big aproach i think :) About 2) i am thinking i should have made that, and having all in the same xDD – Rako Dec 04 '12 at 16:26
  • Hi Rako, Wishing the link helps you :). About 2, try it in low category phones because if you use too many components in the layout (and/or put a lot of pictures in listviews,...) it may crash on load (also in mid-high category devices). About all in one activity, if they're not many things.. with a var or two to track your actual 'activity' it's easy to do and the menus won't move, so with a ViewFlipper to manage the transitions (if you want) would be a nice result :) – Jordi Dec 05 '12 at 07:19