Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Common Clickable Header for All Activities in Android

I have an application with a common Header in all layouts. I want that whenever the user clicks at the the ImageView with id btn_home, the app will go back to a specific activity, my "Main" for instance.

What is the best way to do that?

I know that I can define the onClick(View v) for every activity, but maybe there is a better way to do that. Even making every activity be some (via heritage) other that has the onClick(View v) defined sounds bad.

header.xml

<RelativeLayout ...>
    <RelativeLayout android:id="@+id/relativeLayout1" ...>
        <ImageView android:id="@+id/logo_cats"></ImageView>
        <ImageView android:id="@+id/btn_home" ...></ImageView>
    </RelativeLayout>
</RelativeLayout>

every layout

...
<include layout="@layout/header" android:id="@+id/header"
        android:layout_height="wrap_content" android:layout_width="fill_parent" />
...
like image 600
rlc Avatar asked Sep 14 '11 16:09

rlc


People also ask

How do you make a view clickable on Android?

To make a View clickable so that users can tap (or click) it, add the android:onClick attribute in the XML layout and specify the click handler. For example, you can make an ImageView act like a simple Button by adding android:onClick to the ImageView . In this task you make the images in your layout clickable.

What is Activity_main XML in Android?

The activity_main.xml is a layout file available in res/layout directory, that is referenced by your application when building its interface. You will modify this file very frequently to change the layout of your application.

What is name for parts of activity in Android programming?

An activity represents a single screen with a user interface just like window or frame of Java. Android activity is the subclass of ContextThemeWrapper class. The Activity class defines the following call backs i.e. events.


1 Answers

You can make a custom component out of your header and define 'onClick()' in it. For example, make a new class Header that would extend a RelativeLayout and inflate your header.xml there. Then, instead of <include> tag you would use <com.example.app.Header android:id="@+id/header" .... No code duplication and the header becomes totally reusable.

UPD: Here's some code examples

header.xml:

<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <ImageView android:id="@+id/logo" .../>
    <TextView android:id="@+id/label" .../>
    <Button android:id="@+id/login" .../>
</merge>

activity_with_header.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" ...>
    <com.example.app.Header android:id="@+id/header" .../>
    <!-- Other views -->
</RelativeLayout>

Header.java:

public class Header extends RelativeLayout {
public static final String TAG = Header.class.getSimpleName();

protected ImageView logo;
private TextView label;
private Button loginButton;

public Header(Context context) {
    super(context);
}

public Header(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public Header(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public void initHeader() {
        inflateHeader();
}

private void inflateHeader() {
    LayoutInflater inflater = (LayoutInflater) getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.header, this);
    logo = (ImageView) findViewById(R.id.logo);
    label = (TextView) findViewById(R.id.label);
    loginButton = (Button) findViewById(R.id.login);
}

ActivityWithHeader.java:

public class ActivityWithHeader extends Activity {
private View mCreate;

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

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_with_header);

    Header header = (Header) findViewById(R.id.header);
    header.initHeader();
    // and so on
}
}

In this example, Header.initHeader() can be moved inside Header's constructor, but generally this method provides a nice way to pass on some useful listeners. Hope this will help.

like image 111
Ash Avatar answered Sep 30 '22 17:09

Ash