Thursday 24 January 2013

Android: Sliding Drawer having mutiple ImageView's


 <SlidingDrawer
        android:id="@+id/slidingDrawer1"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="15dp"
        android:content="@+id/content"
        android:handle="@+id/handle" >

        <RelativeLayout
            android:id="@+id/handle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="#FFFFFF"
            android:orientation="horizontal" >

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerInParent="true" >

                <ImageView
                    android:id="@+id/arrow"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:onClick="gotoArrowTopBottom"
                    android:padding="10dp"
                    android:background="@drawable/red"/>
            </RelativeLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true" >

                <ImageView
                    android:id="@+id/listid"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:onClick="gotoListView"
                    android:padding="10dp"
                    android:src="@drawable/list" />

                <ImageView
                    android:id="@+id/supportid"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:onClick="gotoTextView"
                    android:padding="10dp"
                    android:src="@drawable/supportimage" />

                <ImageView
                    android:id="@+id/lockid"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:src="@drawable/lockicon" />

                <ImageView
                    android:id="@+id/favouriteid"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:src="@drawable/favourite" />

                <ImageView
                    android:id="@+id/mapid"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:src="@drawable/map" />

                <ImageView
                    android:id="@+id/contactinfoid"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:src="@drawable/contactinfo" />

                <ImageView
                    android:id="@+id/noteid"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:src="@drawable/note" />

                <ImageView
                    android:id="@+id/remainderid"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:src="@drawable/reminder" />

                <ImageView
                    android:id="@+id/recordid"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:src="@drawable/recording" />

                <ImageView
                    android:id="@+id/emailid"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:src="@drawable/email" />

                <ImageView
                    android:id="@+id/settingsid"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:src="@drawable/settings" />
            </LinearLayout>
        </RelativeLayout>

        <LinearLayout
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="#FFF000" >

            <EditText
                android:id="@+id/supportTextView"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:hint="enter"
                android:visibility="gone" />

            <ListView
                android:id="@+id/listView1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:visibility="gone" >
            </ListView>
        </LinearLayout>
    </SlidingDrawer>



public class SlidingDrawerBottom extends Activity {

protected ListView mList;
protected SlidingDrawer mSlider;
protected TextView mTextView;
protected ImageView mArrowImage;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mTextView = (EditText) findViewById(R.id.supportTextView);
mArrowImage=(ImageView)findViewById(R.id.arrow);
mList = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
SlidingDrawerBottom.this, R.layout.slider_row);
arrayAdapter.add("vvvvvvvvvv");
arrayAdapter.add("xxxxxxxxxx");
arrayAdapter.add("yyyyyyyyyy");
arrayAdapter.add("iiiiiiiiii");
mList.setAdapter(arrayAdapter);
mList.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(SlidingDrawerBottom.this,
"Row " + arg2 + "clicked", Toast.LENGTH_LONG).show();

}
});
mSlider = (SlidingDrawer) findViewById(R.id.slidingDrawer1);
mSlider.lock();
mSlider.setOnDrawerOpenListener(new OnDrawerOpenListener() {
public void onDrawerOpened() {
mSlider.lock();
mArrowImage.setBackgroundResource(R.drawable.red);

Toast.makeText(getApplicationContext(), // <- Line changed
"opened", Toast.LENGTH_LONG).show();

}
});
mSlider.setOnDrawerCloseListener(new OnDrawerCloseListener() {

public void onDrawerClosed() {
mArrowImage.setBackgroundResource(R.drawable.red_down);
}
});
}

public void gotoListView(View V) {
mList.setVisibility(View.VISIBLE);
mTextView.setVisibility(View.GONE);
if (!mSlider.isOpened()) {
// if the mSlider is not open then open it and display
mSlider.animateOpen();
}
}

public void gotoTextView(View v) {
mTextView.setVisibility(View.VISIBLE);
mList.setVisibility(View.GONE);
if (!mSlider.isOpened()) {
mSlider.animateOpen();
}

}

public void gotoArrowTopBottom(View v) {
// only mapview should be displayed all the views should be hidden
mSlider.animateClose();
mTextView.setVisibility(View.GONE);
mList.setVisibility(View.GONE);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

//menu code
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}


Monday 21 January 2013

Android: how to apply Styles in android



Get the colored icons from the
http://android-holo-colors.com/


activity_main.xml

<LinearLayout 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:orientation="vertical">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        style="@style/ButtonAppTheme" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        style="@style/EditTextAppTheme">

        <requestFocus />
    </EditText>

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckBox"
        style="@style/CheckBoxAppTheme" />
    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ToggleButton"
        style="@style/ToggleAppTheme" />

</LinearLayout>



Friday 18 January 2013

Android: set layout_weight dynamically

LinearLayout hzbt=(LinearLayout)findViewById(R.id.horizonbottom);
         hztp.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1f));

weight=1f

Android: Coverting Bitmap to URI

    String path = Images.Media.insertImage(context.getContentResolver(), objImage,null, null);
            Uri image1= Uri.parse(path);

Android: Video player with controls

Main.java

 VideoView videoView = (VideoView) findViewById(R.id.videoView1);
      //Use a media controller so that you can scroll the video contents
      //and also to pause, start the video.
      MediaController mediaController = new MediaController(this);
      mediaController.setAnchorView(videoView);
      videoView.setMediaController(mediaController);
      videoView.setVideoURI(Uri.parse("http://apps.thestagingurl.com/video/sample.3gp"));
      videoView.start();

activity_main.xml

<VideoView
        android:id="@+id/videoView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="16dp" />

Friday 4 January 2013

Android: Date limits on Date Picker Dialog

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

public class DatePickerRestriction extends Activity {

    private TextView tvDisplayDate;
    private DatePicker dpResult;   
   
    private int mYear;
    private int mMonth;
    private int mDay;
    static final int DATE_DIALOG_ID = 1;

 //these variables are used to set max years need to set on Date Picker dialog...
    int minYear1 = 2000;
    int minMonth1 = 0;//0-january , 1-february , 2-march..
    int minDay1 =1;
   
    int minYear = minYear1;
    int minMonth = minMonth1;
    int minDay = minDay1;

    //these are the minimum dates to set Datepicker..
    private int year;
    private int month;
    private int day;   
   
    public String dateOutput=null;
   
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
         final Calendar c = Calendar.getInstance();
            mYear = c.get(Calendar.YEAR);
            mMonth = c.get(Calendar.MONTH);
            mDay = c.get(Calendar.DAY_OF_MONTH);
           
            setCurrentDateOnView();
       
        Button pickDate = (Button) findViewById(R.id.btnChangeDate);
        pickDate.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                        showDialog(DATE_DIALOG_ID);
                }
        });
      
    }

    // display current date
    public void setCurrentDateOnView() {
       
        System.out.println("----------setCurrentDateOnView()-----------");

        tvDisplayDate = (TextView) findViewById(R.id.tvDate);
        dpResult = (DatePicker) findViewById(R.id.dpResult);

        final Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);

        // set current date into textview
        tvDisplayDate.setText(new StringBuilder()
                // Month is 0 based, just add 1
                .append(month + 1).append("-").append(day).append("-")
                .append(year).append(" "));

        // set current date into datepicker
        dpResult.init(year, month, day, null);
    }
   

    //this was the main part in program to Restrict the DatePicker Dialog  to only its current date.
    @Override
    protected Dialog onCreateDialog(int id) {
        System.out.println("----------onCreateDialog()-----------");
        DatePickerDialog _date = null;
               
        switch (id) {
        case DATE_DIALOG_ID:
                _date =  new DatePickerDialog(this,  datePickerListener,
                        year, mMonth, mDay){
           @Override
           public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
           {  
               System.out.println("----------onDateChanged()-----------"+mYear+"--"+year);
               System.out.println("----------onDateChanged()-----------"+mMonth+"--"+monthOfYear);
               System.out.println("----------onDateChanged()-----------"+mDay+"--"+dayOfMonth);
              
              
              /* These lines of commented code used for only setting the maximum date on Date Picker..
               *
               * if (year > mYear && year)
                   view.updateDate(mYear, mMonth, mDay);

                   if (monthOfYear > mMonth && year == mYear )
                   view.updateDate(mYear, mMonth, mDay);

                   if (dayOfMonth > mDay && year == mYear && monthOfYear == mMonth)
                   view.updateDate(mYear, mMonth, mDay);*/
              
              
               //these below lines of code used for setting the maximum as well as minimum dates on Date Picker Dialog..
               if (year < minYear)
                   view.updateDate(minYear, minMonth, minDay);

                   if (monthOfYear < minMonth && year == minYear  )
                   view.updateDate(minYear, minMonth, minDay );



                   if (dayOfMonth < minDay && year == minYear && monthOfYear == minMonth)
                   view.updateDate(minYear, minMonth, minDay);


                   if (year > mYear)
                   view.updateDate(mYear, mMonth, mDay);

                   if (monthOfYear > mMonth && year == mYear)
                   view.updateDate(mYear, mMonth, mDay);

                   if (dayOfMonth > mDay && year == mYear && monthOfYear == mMonth)
                   view.updateDate(mYear, mMonth, mDay);

                  dateOutput = String.format("Date Selected: %02d/%02d/%04d",
                                              dayOfMonth, monthOfYear+1, year);
                  // Log.d("Debug", dateOutput);

                  Toast.makeText(DatePickerRestriction.this,dateOutput,   Toast.LENGTH_SHORT).show();
           }
       };     
        }
         return _date;
    }
   
       
    private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

        // when dialog box is closed, below method will be called.
        public void onDateSet(DatePicker view, int selectedYear,
                int selectedMonth, int selectedDay) {           
           
            System.out.println("----------onDateSet()-----------");                               
           
            year = selectedYear;
            month = selectedMonth;
            day = selectedDay;

            // set selected date into textview
            tvDisplayDate.setText(new StringBuilder().append(month + 1).append("-").append(day).append("-").append(year).append(" "));

            // set selected date into datepicker also
            dpResult.init(year, month, day, null);
        }
    };

}


activity_main.xml

<?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" >

    <Button
        android:id="@+id/btnChangeDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Change Date" />

    <TextView
        android:id="@+id/lblDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Current Date (M-D-YYYY): "
        android:textAppearance="?android:attr/textAppearanceLarge" />
 
    <TextView
        android:id="@+id/tvDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <DatePicker
        android:id="@+id/dpResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>