WELCOME Abdennour : Software engineer

Apr 28, 2012

Best Practices : Tuto3 Horizontal Sliding Drawer



The SlidingDrawer configuration expand horizontally or vertically ,expand the two (the android: orientation) mode 
must be specified in XML, use the "android: handle" with the "android: content".










these two events has been closed, set the Listener for SlidingDrawer.setOnDrawerOpenListener () 
SlidingDrawer.setOnDrawerCloseListener ().

-Established in the main program SlidingDrawer object, in order to capture the Open finished with 






1.Main XML Layout : 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:textSize="16sp" />

    <SlidingDrawer
        android:id="@+id/drawer1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:content="@+id/myContent1"
        android:handle="@+id/layout1"
        android:orientation="horizontal" >

        <LinearLayout
            android:id="@id/layout1"
            android:layout_width="35px"
            android:layout_height="fill_parent"
            android:background="@drawable/black"
            android:gravity="center" >

            <ImageView
                android:id="@+id/myImage1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/open" />
        </LinearLayout>

        <GridView
            android:id="@id/myContent1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/black"
            android:gravity="center"
            android:numColumns="2" />
    </SlidingDrawer>

</RelativeLayout>



2.Main Activity : 

package slm.abdennour.coach;

import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SlidingDrawer;

public class SlidVerticalActivity extends Activity {

  private GridView gv;
  private SlidingDrawer sd;
  private ImageView im;

  private int[] icons = R.drawable.quran, R.drawable.alarm,

      R.drawable.calendar, R.drawable.camera, R.drawable.clock,

      R.drawable.tv };

  private String[] items = "quran""alarm""calendar""camera""clock",

      "tv" };



  /** Called when the activity is first created. */

  @Override

  public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    gv = (GridViewfindViewById(R.id.myContent1);

    sd = (SlidingDrawerfindViewById(R.id.drawer1);

    im = (ImageViewfindViewById(R.id.myImage1);

    MyGridViewAdapter adapter = new MyGridViewAdapter(this, items, icons);

    gv.setAdapter(adapter);
    sd.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener()
    {

      @Override

      public void onDrawerOpened() {

        im.setImageResource(R.drawable.clock);

      }

    });

    sd.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {
      @Override
      public void onDrawerClosed() {
        im.setImageResource(R.drawable.open);
      }

    });

  }

}


3.grid.xml Layout
    <?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" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dip" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="15dip" />

</LinearLayout>

4-Adapter Activity :



package slm.abdennour.coach;



import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.ImageView;

import android.widget.TextView;



public class MyGridViewAdapter extends BaseAdapter {

  private Context _con;

  private String[] _items;

  private int[] _icons;



  public MyGridViewAdapter(SlidVerticalActivity slidVerticalActivity,

      String[] items, int[] icons) {

    _con = slidVerticalActivity;

    _items = items;

    _icons = icons;

  }



  @Override

  public int getCount() {

    return _items.length;

  }



  @Override

  public Object getItem(int position) {

    return _items[position];

  }



  @Override

  public long getItemId(int position) {

    return position;

  }



  @Override

  public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater factory = LayoutInflater.from(_con);



    View v = (Viewfactory.inflate(R.layout.grid, null);



    ImageView iv = (ImageViewv.findViewById(R.id.icon);

    TextView tv = (TextViewv.findViewById(R.id.text);



    iv.setImageResource(_icons[position]);

    tv.setText(_items[position]);

    return v;

  }

}





5-Demo Video :










































3 comments:

  1. السلام عليكم
    بارك الله فيك أخي
    أصبحنا نتعلم منك
    أحمد

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. و عليكم السلام و رحمة الله و بركاته
    و فيك بارك الله
    حللتم , أهلا و سهلا

    ReplyDelete