Wednesday, February 19, 2014

View Switcher example

view flipper

download:  http://www.mediafire.com/download/7j0ycdg5j2mdpvy/ViewFlipperDemos.zip



package com.ui.yogeshblogspot;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ViewFlipper;

public class Main extends Activity {

ViewFlipper flipper;
Button btBackPage;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Getting View Flipper from main.xml and assigning to flipper reference
// variable
flipper = (ViewFlipper) findViewById(R.id.viewFlipper1);
btBackPage = (Button) findViewById(R.id.btBackPage);

btBackPage.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v)
{
if (flipper.isFlipping())// Checking flipper is flipping or not.
{
flipper.stopFlipping(); // stops the flipping .
}
flipper.showPrevious();// shows the next view element of ViewFlipper
}
});
}

public void flipByClick(View v) {
if (flipper.isFlipping())// Checking flipper is flipping or not.
{
flipper.stopFlipping(); // stops the flipping .
}
flipper.showNext();// shows the next view element of ViewFlipper
}

public void flipByInterval(View v) {
flipper.setFlipInterval(500);// setting the interval 500 milliseconds
flipper.startFlipping(); // views flipping starts.
}
}


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

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <!-- Calling flipByInterval() method of Activity class -->
       
         <Button
            android:id="@+id/btBackPage"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
             android:layout_weight="0.40"
            android:text="Back Flip"
            android:background="@android:color/holo_green_dark"/>

        <Button
            android:id="@+id/flipbyclick"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.40"
            android:background="@android:color/holo_green_dark"
            android:onClick="flipByClick"
            android:layout_marginLeft="10dp"
            android:text="Flip By Click" />

        <!-- calling flipByClick() method of Activity class -->
    </LinearLayout>
    <!-- ViewFlipper can contains any view elements in this case it contains 5 ImageView view Component with different Images -->

    <ViewFlipper
        android:id="@+id/viewFlipper1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <include
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            layout="@layout/dialog_text" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/img1" />
        <!-- Setting img1 from drawable folder in ImageView -->

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/img2" />
        <!-- Setting img2 from drawable folder in ImageView -->

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/img3" />
        <!-- Setting img3 from drawable folder in ImageView -->

        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/img4" />
        <!-- Setting img4 from drawable folder in ImageView -->

        <ImageView
            android:id="@+id/imageView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/img5" />
        <!-- Setting img5 from drawable folder in ImageView -->

        <ImageView
            android:id="@+id/imageView6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/img6" />
        <!-- Setting img6 from drawable folder in ImageView -->

       

    </ViewFlipper>
   
 
   
    <Button
            android:id="@+id/flipbyinterval"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.25"
            android:onClick="flipByInterval"
            android:text="Flip By Interval" />

</LinearLayout>



--------------------------------
dialog_text.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" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight=".50" >

        <Button
            android:id="@+id/imageViewSearchBanner"
            android:layout_width="fill_parent"
            android:layout_height="100dp"
            android:text="new btn" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight=".50" >

        <Button
            android:id="@+id/ivForText"
            android:layout_width="fill_parent"
            android:layout_height="100dp"
            android:text="other btn" />
    </LinearLayout>

</LinearLayout>

Tuesday, February 11, 2014

file download with asynctask & progress bar


download:  http://www.mediafire.com/download/zlvy8f0gomntnhj/DownloadFileByProgressBar.zip



package com.example.androidhive;

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class AndroidDownloadFileByProgressBarActivity extends Activity {

// button to show progress dialog
Button btnShowProgress;

// Progress Dialog
private ProgressDialog pDialog;
ImageView my_image;
// Progress dialog type (0 - for Horizontal progress bar)
public static final int progress_bar_type = 0;

// File url to download
private static String file_url = "http://api.androidhive.info/progressdialog/hive.jpg";

String download_path = "";

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

download_path = Environment.getExternalStorageDirectory().toString() + "/Download/";

// show progress bar button
btnShowProgress = (Button) findViewById(R.id.btnProgressBar);
// Image view to show image after downloading
my_image = (ImageView) findViewById(R.id.my_image);
/**
* Show Progress bar click event
* */
btnShowProgress.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// starting new Async Task
new DownloadFileFromURL().execute(file_url);
}
});
}

/**
* Showing Dialog
* */
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case progress_bar_type:
pDialog = new ProgressDialog(this);
pDialog.setMessage("Downloading file. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(true);
pDialog.show();
return pDialog;
default:
return null;
}
}

/**
* Background Async Task to download file
* */
class DownloadFileFromURL extends AsyncTask<String, String, String> {

/**
* Before starting background thread
* Show Progress Bar Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}

/**
* Downloading file in background thread
* */
@Override
protected String doInBackground(String... f_url) {
int count;
       try {
           URL url = new URL(f_url[0]);
           URLConnection conection = url.openConnection();
           conection.connect();
           // getting file length
           int lenghtOfFile = conection.getContentLength();

           // input stream to read file - with 8k buffer
           InputStream input = new BufferedInputStream(url.openStream(), 8192);
         
           // Output stream to write file
           OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory().toString() +"/Download/downloadedfile.jpg");

           byte data[] = new byte[1024];

           long total = 0;

           while ((count = input.read(data)) != -1) {
               total += count;
               // publishing the progress....
               // After this onProgressUpdate will be called
               publishProgress(""+(int)((total*100)/lenghtOfFile));
             
               // writing data to file
               output.write(data, 0, count);
           }

           // flushing output
           output.flush();
         
           // closing streams
           output.close();
           input.close();
         
       } catch (Exception e) {
        Log.e("Error: ", e.getMessage());
       }
     
       return null;
}

/**
* Updating progress bar
* */
protected void onProgressUpdate(String... progress) {
// setting progress percentage
            pDialog.setProgress(Integer.parseInt(progress[0]));
       }

/**
* After completing background task
* Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
dismissDialog(progress_bar_type);

// Displaying downloaded image into image view
// Reading image path from sdcard
String imagePath =  download_path +"downloadedfile.jpg";
// setting downloaded into image view
my_image.setImageDrawable(Drawable.createFromPath(imagePath));
}

}
}

Monday, February 10, 2014

scrollView with linearLayout



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <!-- non-scrolling top pane -->

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight=".1" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="This text will not scroll" />
    </LinearLayout>

    <!-- scrolling bottom pane -->

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight=".80" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center" >

            <TableLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="@android:color/black" >

                <TableRow
                    android:id="@+id/tableRow1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                     >

                    <Button
                        android:id="@+id/button1"
                        android:layout_width="100dp"
                        android:layout_height="100dp"
                        android:layout_marginLeft="10dp"
                        android:background="@drawable/fish_pressed"
                        android:text="Column 2" />

                    <Button
                        android:id="@+id/button2"
                        android:layout_width="100dp"
                        android:layout_height="100dp"
                        android:layout_marginLeft="10dp"
                        android:background="@drawable/fish_pressed"
                        android:text="Column 3" />
                </TableRow>

                <TableRow
                    android:id="@+id/tableRow2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="10dp" >

                    <Button
                        android:id="@+id/button3"
                        android:layout_width="100dp"
                        android:layout_height="100dp"
                        android:layout_marginLeft="10dp"
                        android:background="@drawable/gentleman_2"
                        android:text="Column 2" />

                    <Button
                        android:id="@+id/button4"
                        android:layout_width="100dp"
                        android:layout_height="100dp"
                        android:layout_marginLeft="10dp"
                        android:background="@drawable/gentleman_2"
                        android:text="Column 3" />
                </TableRow>

                <TableRow
                    android:id="@+id/tableRow3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="10dp" >

                    <Button
                        android:id="@+id/button5"
                        android:layout_width="100dp"
                        android:layout_height="100dp"
                        android:layout_marginLeft="10dp"
                        android:background="@drawable/fish_pressed"
                        android:text="Column 2" />

                    <Button
                        android:id="@+id/button6"
                        android:layout_width="100dp"
                        android:layout_height="100dp"
                        android:layout_marginLeft="10dp"
                        android:background="@drawable/fish_pressed"
                        android:text="Column 3" />
                </TableRow>

                <TableRow
                    android:id="@+id/tableRow4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="10dp" >

                    <Button
                        android:id="@+id/button7"
                        android:layout_width="100dp"
                        android:layout_height="100dp"
                        android:layout_marginLeft="10dp"
                        android:background="@drawable/gentleman_2"
                        android:text="Column 2" />

                    <Button
                        android:id="@+id/button8"
                        android:layout_width="100dp"
                        android:layout_height="100dp"
                        android:layout_marginLeft="10dp"
                        android:background="@drawable/gentleman_2"
                        android:text="Column 3" />
                </TableRow>

                <TableRow
                    android:id="@+id/tableRow5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="10dp" >

                    <Button
                        android:id="@+id/button9"
                        android:layout_width="100dp"
                        android:layout_height="100dp"
                        android:layout_marginLeft="10dp"
                        android:background="@drawable/fish_pressed"
                        android:text="Column 2" />

                    <Button
                        android:id="@+id/button10"
                        android:layout_width="100dp"
                        android:layout_height="100dp"
                        android:layout_marginLeft="10dp"
                        android:background="@drawable/fish_pressed"
                        android:text="Column 3" />
                </TableRow>

                <TableRow
                    android:id="@+id/tableRow6"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="10dp" >

                    <Button
                        android:id="@+id/button11"
                        android:layout_width="100dp"
                        android:layout_height="100dp"
                        android:layout_marginLeft="10dp"
                        android:background="@drawable/gentleman_2"
                        android:text="Column 2" />

                    <Button
                        android:id="@+id/button12"
                        android:layout_width="100dp"
                        android:layout_height="100dp"
                        android:layout_marginLeft="10dp"
                        android:background="@drawable/gentleman_2"
                        android:text="Column 3" />
                </TableRow>
            </TableLayout>
        </LinearLayout>
    </ScrollView>
   
   
     <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight=".1" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="This text will not scroll" />
    </LinearLayout>

</LinearLayout>

Tuesday, February 4, 2014

facebook wall post with image

download: http://www.mediafire.com/download/dd0qqra99yrnbs1/facebookLoginWallPostWithSdk3.0.zip




public class MainActivity extends Activity {

private static final String APP_ID = "529772120384562";
private static final String[] PERMISSIONS = new String[] { "publish_stream" };

private static final String TOKEN = "access_token";
private static final String EXPIRES = "expires_in";
private static final String KEY = "facebook-credentials";

private Facebook facebook;
private String messageToPost;

Button btFacebookShareButton;

public boolean saveCredentials(Facebook facebook) {
Editor editor = getApplicationContext().getSharedPreferences(KEY,
Context.MODE_PRIVATE).edit();
editor.putString(TOKEN, facebook.getAccessToken());
editor.putLong(EXPIRES, facebook.getAccessExpires());
return editor.commit();
}

public boolean deleteCredentials(Facebook facebook) {
Editor editor = getApplicationContext().getSharedPreferences(KEY,
Context.MODE_PRIVATE).edit();
editor.putString(TOKEN, "");
editor.putLong(EXPIRES, 0);

Log.e("------deleteCredentials() ", " ");

return editor.commit();
}

public boolean restoreCredentials(Facebook facebook) {
SharedPreferences sharedPreferences = getApplicationContext()
.getSharedPreferences(KEY, Context.MODE_PRIVATE);
facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
return facebook.isSessionValid();
}

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

facebook = new Facebook(APP_ID);
restoreCredentials(facebook);

requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.activity_main);

btFacebookShareButton = (Button) findViewById(R.id.btFacebookShareButton);

btFacebookShareButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
if (!facebook.isSessionValid()) {
loginAndPostToWall();
} else {
postToWall(messageToPost);
// loginAndPostToWall();
}

}
});

String facebookMessage = getIntent().getStringExtra("facebookMessage");
if (facebookMessage == null) {
facebookMessage = "https://play.google.com/store/apps/details?id=com.polarbit.rthunder2lite"
+ " minute = "
+ new Date().getMinutes()
+ " "
+ new Date().getSeconds();
}
messageToPost = facebookMessage;
}

@Override
public void onBackPressed() {

Log.e("------onBackPressed() ", " ");

// TODO Auto-generated method stub
super.onBackPressed();

deleteCredentials(facebook);
}

@Override
protected void onDestroy() {

Log.e("------onDestroy", " ");
// TODO Auto-generated method stub
super.onDestroy();

deleteCredentials(facebook);

}

public void doNotShare(View button) {
finish();
}

/*
* public void share(View button) { if (!facebook.isSessionValid()) {
* loginAndPostToWall(); } else { postToWall(messageToPost); //
* loginAndPostToWall(); } }
*/

public void loginAndPostToWall() {
facebook.authorize(this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH,
new LoginDialogListener());
}

public void postToWall(String message) {
Bundle parameters = new Bundle();
parameters.putString("message", message);
parameters.putString("description", "topic share");

parameters.putString("picture", "http://upload.wikimedia.org/wikipedia/commons/a/a6/Bottlenose_Dolphin_KSC04pd0178.jpg");
parameters.putString("name", "BanglaGaan");
parameters.putString("caption", "G-Series Online Music Store");
parameters.putString("description", "Listening to ......");

try {
facebook.request("me");
String response = facebook.request("me/feed", parameters, "POST");
Log.d("Tests", "got response: " + response);
if (response == null || response.equals("")
|| response.equals("false")) {
showToast("Blank response.");
} else {
showToast("Message posted to your facebook wall!");
}
// finish();
} catch (Exception e) {
showToast("Failed to post to wall!");
e.printStackTrace();
finish();
}
}

class LoginDialogListener implements DialogListener {
public void onComplete(Bundle values) {
saveCredentials(facebook);
if (messageToPost != null) {
postToWall(messageToPost);
}
}

public void onFacebookError(FacebookError error) {
showToast("Authentication with Facebook failed!");
finish();
}

public void onError(DialogError error) {
showToast("Authentication with Facebook failed!");
finish();
}

public void onCancel() {
showToast("Authentication with Facebook cancelled!");
finish();
}
}

private void showToast(String message) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT)
.show();
}

}