Julien Popa-Liesz
Julien Popa-Liesz

Reputation: 71

Notification drop down view

I'm building an App on a tablet and there is a notification feature I would like to implement to function like a fixed sized drop down scroll-able view that appears below the action bar to display a list of notifications to the user. I have already added a notification button to my activity bar and have the notification system built.

Upvotes: 0

Views: 1740

Answers (1)

M0CH1R0N
M0CH1R0N

Reputation: 756

I just thought of a better way to do it. You should use a ListView and then populate it with TextViews:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    tools:context=".MainActivity">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:visibility="gone"
        android:background="#EEEEEE"
        android:id="@+id/notification_list_view"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/notification_list_view">
        <!-- Content here -->
    </LinearLayout>

</RelativeLayout>

The notification bar can be shown by putting this code in your onClick function of a button in the ActionBar. And then you can add items dynamically with an ArrayAdapter:

private void onClick() {
    ListView notificationListView = (ListView) findViewById(R.id.notification_list_view);

    // if you don't need the notifications anymore use:
    // notificationListView.setVisibility(View.GONE);
    notificationListView.setVisibility(View.VISIBLE);

    final List<String> notificationList = new ArrayList<>();
    notificationList.add("Notification 1");
    notificationList.add("Notification 2");

    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, notificationList);
    notificationListView.setAdapter(adapter);
    notificationListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String chosenNotification = notificationList.get(position);
        }
    });
}

As you can see you can then get the clicked notification if it's necessary.

Upvotes: 1

Related Questions