3

I am working on application which have same header in all activites,in which there are some views in image views, buttons, textviews etc.

Now the The question is i want to use these header views in all the activites,but i do not want to re-code and re-design it in every activity.so is it possible to create a fragment of header views and reuse its ui and functionalities in all other activites.

  • if no,then any idea to make it reusable header?
  • if yes,then how ?

thanks for the help.!

updates in this question according to my research! i have researched and fond that it can easily acheived by using fragments.but i am still not getting that how? how can i acheive that functionality? according to this: reusable UI in android app

and this:

reusable ui with fragments

any links and code would be helpful!

Community
  • 1
  • 1
Hamad
  • 5,096
  • 13
  • 37
  • 65
  • Have you ever read the official Android APi Guides and Training?If you did,there is no more question here! Read them first![Fragment1](https://developer.android.com/guide/components/fragments.html),[Fragment](https://developer.android.com/training/basics/fragments/creating.html) and so on! – fearless Apr 25 '13 at 07:47
  • i read.but the proglem is! they are using listview in one fragment and web view in other fragment,and i want use it as header functional on all my other activites. – Hamad Apr 25 '13 at 07:50
  • I don't understand why you are confused.Yes,they can definitely use ListView or WebView in one Fragment.You can also combine a lot of widgets sush as Button,ImageView,TextView in one Fragment and use it in different Activity.As the official doc says "You can think of a fragment as a modular section of an activity". So,do you mean you can't add a lot of widgets in one Fragment or you don't know how to reuse the Fragment? If the problem is the first one,you can review how to write activity layout.If the problem is the second one,you can think about the use of Button and other widgets. – fearless Apr 25 '13 at 08:06
  • 1
    Well,Why don't you use ActionBar?If the platform is before 2.3 you can use this library [actionbarsherlock](http://actionbarsherlock.com/) – fearless Apr 25 '13 at 08:28
  • fearles thanks for all the answers – Hamad Apr 25 '13 at 08:32
  • 1
    Check this out. [Common Header UI in activities][1] may be helpful to you. [1]: http://stackoverflow.com/questions/11579635/common-header-in-different-activities-using-baseactivity-in-android – Qadir Hussain Apr 25 '13 at 08:38

2 Answers2

2

guyz i got my answer one of my friend shared a link with me which have solved my problem

the link is: same header on all the activites working like same on all

Complete Answer after applying:

this is BaseActivity class used to give functionalities to header

abstract public class BaseActivity extends Activity{
    ImageView imVBattery,imVWIFI,imVSabaqLogo;
    Button btnSettings,btnTheme;
    WifiManager wifiManager;

    //set resources to all the contols

    public void setHeader(){    
    imVBattery=(ImageView)findViewById(R.id.imVBattery);
    imVWIFI=(ImageView)findViewById(R.id.imVWIFI);
    imVSabaqLogo=(ImageView)findViewById(R.id.imVSabaqLogo);
    btnSettings=(Button)findViewById(R.id.btnSettings);
    btnTheme=(Button)findViewById(R.id.btnTheme);
    wifiManager= (WifiManager)this.getSystemService(getBaseContext().WIFI_SERVICE);

    }


}

and this is my main class in which i extends this base activity:

public class Main extends BaseActivity implements OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.s_main);
            //with the help of this i assign all resources to controls of headers
        setHeader();
    }

}

thanks to all who replied and helped me.!

Community
  • 1
  • 1
Hamad
  • 5,096
  • 13
  • 37
  • 65
1

Have you tried implementing ViewStub. Here you get your solution.

Check here.

Chintan Soni
  • 24,761
  • 25
  • 106
  • 174
  • thanks for inhancing my knowledge.but shree202 i want to use that header with all its java coding features like we use include in our xml layouts to include that layout,then its all design and dimensions looks same on all but that is static,no functionalities only ui features includes in ur ui.but i want to include all its ui _ functionalities like click events touch events etc – Hamad Apr 25 '13 at 08:24
  • 1
    Did you refer the Android ViewStub example ? download it from here. [http://technotalkative.com/Android-Examples/ViewStubDemo.zip] and ofcourse you can get all the functionalities in ViewStub also. – Chintan Soni Apr 25 '13 at 08:28