2

I am developing an Android Application. In this application, Logo bar is shown on all pages(Activities) or we can say it has header on all pages. This Logo Bar have few icons like Home, Login, Notification, etc. and on Clicking on these icons corresponding navigation will perform.

for example if user is any where in application and click on home icon, he will navigate to the home page of application.

I am able to inflate logobar.XML into my All Activity by coding. but problem is i have to call onClickListener on all pages for all icons in Logo Bar. This is not a good programming way. How can i implement Logo Bar Activity in all other activity without repeating of code? Is android have any Master Page concept as in .Net or tiles concept as in Struts?

Please guide me.

Manoj Agarwal
  • 703
  • 3
  • 17
  • 39

5 Answers5

1

The solution was pretty easy.

You need to extends "Activity" Class,in onCreate function SetContentView to your base xml layout and also need to override setContentView in base Activity Class

For Example:

1.Create "base_layout.xml" with the below code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical"
      android:background="#000000"
      android:padding="15dp" >
    <LinearLayout android:orientation="horizontal" android:background="#000000"
           android:layout_width="fill_parent" android:layout_height="wrap_content"
           android:minHeight="50dp" android:paddingLeft="10dp">
           <ImageView android:layout_width="wrap_content" android:id="@+id/ImageView01"
               android:adjustViewBounds="true" android:layout_height="wrap_content"
               android:scaleType="fitCenter" android:maxHeight="50dp" />
   </LinearLayout>
   <LinearLayout android:id="@+id/linBase"
     android:layout_width="fill_parent"
       android:layout_height="fill_parent" >
   </LinearLayout>
</LinearLayout>    

2.Create "BaseActivity.java"

public class BaseActivity extends Activity {
    ImageView image;
    LinearLayout linBase;     
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        super.setContentView(R.layout.base_layout);        
        image = (ImageView)findViewById(R.id.ImageView01);
        image.setImageResource(R.drawable.header);
        linBase = (LinearLayout)findViewById(R.id.linBase);
    }
    @Override
    public void setContentView(int id) {
        LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(id, linBase);
    }
}

and

public class SomeActivity extends BaseActivity {    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.some_layout);
       //rest of code
    }
}

The only thing I noticed so far was that when requesting a progress bar (requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS)) this needs to be done before calling super.onCreate. I think this is because nothing can be drawn yet before calling this function.

This worked great for me and hopefully you will find this useful in your own coding.

Ahmad Ronagh
  • 790
  • 10
  • 16
1

Edit: ok i got it. may be this answer will help you.

Try using Tab widget with tabactivity check this link for using fragment and tab http://developer.android.com/reference/android/app/TabActivity.html for android. i think for lower versions also we can use this. this si what the link says - "you can use the v4 support library which provides a version of the Fragment API that is compatible down to DONUT."

Community
  • 1
  • 1
AD14
  • 1,218
  • 19
  • 32
  • Thanks for reply but tab Layout does not solve my problem. I want to use my Logo Bar as Header or Title bar, not like different tabs for each icons on Logo Bar. This should give the appearance of Header not Tabs. I am developing my app using android 2.1 – Manoj Agarwal Jan 19 '12 at 05:50
  • Thanks AnDro. I think it is what i want and it should work in my application. – Manoj Agarwal Jan 19 '12 at 06:11
1

you have to create your masterLayout in xml and that you have to include it in your other layouts in which you have to have it.

Prashant Mishra
  • 627
  • 5
  • 18
  • yes I am doing so but problem is How to use all functionality(like Click events on every icons of Logo Bar ) of master Layout activity in other activity where i include masterLayout without repeating code. Presently I have to repeat OnClickListener for each icon on every page. – Manoj Agarwal Jan 19 '12 at 06:47
  • @MANOJAGARWAL as you have included layout then find view by id from that layout and apply your event listners. – Prashant Mishra Jan 19 '12 at 07:04
  • This is fine. I am already using this approach. But this approach repeating the same code on all pages as my logo bar is placed in all pages. I want a way so that repetition do not occur and i just call my master page activity within other activities. – Manoj Agarwal Jan 19 '12 at 08:33
  • super.onCreate(savedInstanceState); setContentView(R.layout.activity2); footer = (Footer) findViewById(R.id.layoutFooter); footer.setActivity(this); – Prashant Mishra Jan 19 '12 at 09:59
  • public class Footer extends LinearLayout { private Context mContext; private Button btn11; private Button btn12; TextView tv; // use Activity Object to call finish() method which is not possible using // context private Activity mActivity; btn11 = (Button) findViewById(R.id.Button11); btn11.setOnClickListener(new OnClickListener() { public void onClick(View v) { – Prashant Mishra Jan 19 '12 at 10:01
0

I have done this using XML file.

I am just creating runtime view from XML file , and add it to the Activity layout.

I have created method for that

public static void setLoginview(Context ctx, RelativeLayout layout) {
    LayoutInflater linflater = (LayoutInflater) ctx
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View myView = linflater.inflate(R.layout.loginheader, null);    
    layout.addView(myView);
    try {
        layout.getChildAt(0).setPadding(0, 50, 0, 0);
    } catch (Exception e) {
    }
}

ctx is the application contetx and layout is the layout in which i want to add that view.

Jigar Parekh
  • 595
  • 10
  • 23
0

There is something like that, but only available on api 11+ (3.2 and Android 4.0.x Ice Cream Sandwich). Its called actionbar ( http://developer.android.com/reference/android/app/ActionBar.html).

Leandros
  • 16,805
  • 9
  • 69
  • 108