• TekArt

    TekArt is an Organisation where people develop Android App through innovative ideas. App for the next Generation....

Sunday 20 April 2014

Posted by Unknown
7 comments | 04:25
Hello guys, today we are going to see how to change the screen with viewflipper and put animation in it. First of all we create the animation files. The animation files reside in the anim folder in the "res" directory.




in_from_left.xml

<set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false">     <translate         android:fromXDelta="-100%" android:toXDelta="0%"            android:fromYDelta="0%" android:toYDelta="0%"            android:duration="1400" /> </set>

in_from_right.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
    <translate         android:fromXDelta="100%" android:toXDelta="0%"            android:fromYDelta="0%" android:toYDelta="0%"            android:duration="1400" /> </set>

out_to_left.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">       <translate android:fromXDelta="0%" android:toXDelta="-100%"         android:fromYDelta="0%" android:toYDelta="0%"         android:duration="1400"/> </set> 

out_to_right.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">       <translate android:fromXDelta="0%" android:toXDelta="100%"         android:fromYDelta="0%" android:toYDelta="0%"         android:duration="1400"/> </set>
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" >


        <ViewFlipper
            android:id="@+id/view_flipper"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
             >
           
        <!--  The child Views/Layout to flip -->
       
        <!--  Layout 1 for 1st Screen -->
            <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:orientation="vertical" >

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="45dp"
                        android:text="This Is Screen 1"
                        android:textColor="#191975"
                        android:textSize="25dp"
                        android:textStyle="bold" >
                    </TextView>

                    <ImageView
                        
                        android:id="@+id/imageView1"
                        android:layout_width="350dp"
                        android:layout_height="350dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/image1" />
                   
            </LinearLayout>
           
             <!--  Layout 2 for 2nd Screen -->
           
            <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:orientation="vertical" >

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="45dp"
                        android:text="This Is Screen 2"
                        android:textColor="#191975"
                        android:textSize="25dp"
                        android:textStyle="bold" >
                    </TextView>

                    <ImageView
                        
                        android:id="@+id/imageView3"
                        android:layout_width="350dp"
                        android:layout_height="350dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/image2" />
                   
            </LinearLayout>
            <!--  Layout 3 for 1st Screen -->
            <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:orientation="vertical" >

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="45dp"
                        android:text="This Is Screen 3"
                        android:textColor="#191975"
                        android:textSize="25dp"
                        android:textStyle="bold" >
                    </TextView>

                    <ImageView
                        
                        android:id="@+id/imageView3"
                        android:layout_width="350dp"
                        android:layout_height="350dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/image3" />
                   
            </LinearLayout>
            <!--  Layout 4 for 1st Screen -->
            <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:orientation="vertical" >

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="45dp"
                        android:text="This Is Screen 4"
                        android:textColor="#191975"
                        android:textSize="25dp"
                        android:textStyle="bold" >
                    </TextView>

                    <ImageView
                        
                        android:id="@+id/imageView3"
                        android:layout_width="350dp"
                        android:layout_height="350dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/image4" />
                   
            </LinearLayout>


        </ViewFlipper>

</LinearLayout>

MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.ViewFlipper;

public class MainActivity extends Activity {
    private ViewFlipper viewFlipper;
    private float lastX;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewFlipper = (ViewFlipper) findViewById(R.id.view_flipper);
    }

    public boolean onTouchEvent(MotionEvent touchevent) {
        switch (touchevent.getAction()) {

        case MotionEvent.ACTION_DOWN: {
            lastX = touchevent.getX();
            break;
        }
        case MotionEvent.ACTION_UP: {
            float currentX = touchevent.getX();

            if (lastX < currentX) {

                if (viewFlipper.getDisplayedChild() == 0)
                    break;

                viewFlipper.setInAnimation(this, R.anim.in_from_left);
                viewFlipper.setOutAnimation(this, R.anim.out_to_right);
                // Show The Previous Screen
                viewFlipper.showPrevious();
            }

            // if right to left swipe on screen
            if (lastX > currentX) {
                if (viewFlipper.getDisplayedChild() == 3)
                    break;

                viewFlipper.setInAnimation(this, R.anim.in_from_right);
                viewFlipper.setOutAnimation(this, R.anim.out_to_left);
                // Show the next Screen
                viewFlipper.showNext();
            }
            break;
        }
        }
        return false;
    }

}
   
 

Wednesday 16 April 2014

Posted by Unknown
No comments | 23:52
Hello guys, in this tutorial we are going to see how to create a timer and to do something when the timer reaches a particular time. So lets  do it.




activity_main.xml

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

    <TextView
        android:id="@+id/timerValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/pauseButton"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="37dp"
        android:textSize="40sp"
        android:textColor="#ffffff"
        android:text="00:00:000" />

    <Button
        android:id="@+id/startButton"
        android:layout_width="90dp"
        android:layout_height="45dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="38dp"
        android:text="start" />

    <Button
        android:id="@+id/pauseButton"
        android:layout_width="90dp"
        android:layout_height="45dp"
        android:layout_alignBaseline="@+id/startButton"
        android:layout_alignBottom="@+id/startButton"
        android:layout_alignParentRight="true"
        android:layout_marginRight="38dp"
        android:text="pause" />

</RelativeLayout>

MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
    Button start, pause;
    TextView time;
    private long startTime = 0L;

    private Handler customHandler = new Handler();

    long timeInMilliseconds = 0L;
    long timeSwapBuff = 0L;
    long updatedTime = 0L;
    boolean stopTimer = false;

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

        start = (Button) findViewById(R.id.startButton);
        pause = (Button) findViewById(R.id.pauseButton);
        time = (TextView) findViewById(R.id.timerValue);
        start.setOnClickListener(this);
        pause.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.startButton:
            startTime = SystemClock.uptimeMillis();
            customHandler.postDelayed(updateTimerThread, 0);
            break;

        case R.id.pauseButton:
            timeSwapBuff += timeInMilliseconds;
            customHandler.removeCallbacks(updateTimerThread);
            break;
        }
    }

    private Runnable updateTimerThread = new Runnable() {

        public void run() {
            timeInMilliseconds = SystemClock.uptimeMillis() - startTime;

            updatedTime = timeSwapBuff + timeInMilliseconds;

            int secs = (int) (updatedTime / 1000);
            int mins = secs / 60;
            secs = secs % 60;
            int milliseconds = (int) (updatedTime % 1000);
            String localtime = "" + mins + ":" + String.format("%02d", secs)
                    + ":" + String.format("%03d", milliseconds);
            time.setText(localtime);
            if (mins == 1) {
                stopTimer = true;
                Toast.makeText(MainActivity.this, "You guys are awesome",
                        Toast.LENGTH_SHORT).show();
            }
            if (!stopTimer)
                customHandler.postDelayed(this, 0);
        }

    };

}

Tuesday 15 April 2014

Posted by Unknown
1 comment | 00:25
Hey guys, in this blog we will see how to record sound in the android. The recorded sound will be saved in the "MediaRecorder" folder in your SD card. Run and test this project on the real device as it wont work on the emulator because emulators dont have mics to record.



activity_main.xml

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Record"
        android:id="@+id/record" />

</RelativeLayout>

MainActivity.java

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {
    
    MediaRecorder mRecorder;
    String filename="";
    Button record;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        record = (Button) findViewById(R.id.record);
        record.setOnClickListener(this);
    }

    private void record_and_save() {
        // TODO Auto-generated method stub
        
        
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        
        mRecorder.setAudioEncodingBitRate(64);
        mRecorder.setAudioSamplingRate(48000);
        
        filename = Environment.getExternalStorageDirectory().getAbsolutePath();
        File file = new File(filename,"MediaRecorder");
        if(!file.exists()) file.mkdirs();
        filename = file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".wav";
        
        mRecorder.setOutputFile(filename);
        
        try {
            mRecorder.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mRecorder.start();
        
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()) {
        case R.id.record : record_and_save();break;
        }
    }

}

Sunday 13 April 2014

Posted by Unknown
2 comments | 22:48

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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical" >
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Low Pitch"
        android:id = "@+id/low"
         />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Normal"
        android:id = "@+id/normal"
         />
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="High"
        android:id = "@+id/high"
        />

</LinearLayout>

MainActivity.java

import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{
    
    SoundPool sp;
    private Button low, normal, high;
    float pitch = 0.5f;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        low = (Button) findViewById(R.id.low);
        normal = (Button) findViewById(R.id.normal);
        high = (Button) findViewById(R.id.high);
        
        low.setOnClickListener(this);
        normal.setOnClickListener(this);
        high.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()) {
        case R.id.low : pitch = 0.5f;playit();break;
        
        case R.id.normal: pitch = 1.0f;playit();break;
        
        case R.id.high: pitch = 2.0f;playit();break;
        }
        
    }

    private void playit() {
        // TODO Auto-generated method stub
        sp = new SoundPool(1,AudioManager.STREAM_MUSIC,0);
        final int i = sp.load(this, R.raw.music, 0);
        sp.setOnLoadCompleteListener(new OnLoadCompleteListener() {

            @Override
            public void onLoadComplete(SoundPool arg0, int arg1, int arg2) {
                // TODO Auto-generated method stub
                sp.play(i, 1,1,0,0,pitch);    
            }
            
        });
    }
}

Saturday 12 April 2014

Posted by Unknown
No comments | 21:30
Hey Guys, Today we are going to see the usage of SoundPool in Android. So lets get started.




1) First of all create an Android Application Project.

2) Next go to "res/layout/activity_main.xml" and create a  button there.

activity_main.xml

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="play"
        android:id = "@+id/play"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

</RelativeLayout>

3) Now go to "src/your_package_name/MainActivity.java" and the code is here.

import android.app.Activity;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{
    
    SoundPool sp;
    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        btn = (Button) findViewById(R.id.play);
        btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()) {
        case R.id.play : playit();break;
        }
        
    }

    private void playit() {
        // TODO Auto-generated method stub
        sp = new SoundPool(1,AudioManager.STREAM_MUSIC,0);
        final int i = sp.load(this, R.raw.music, 0);
        sp.setOnLoadCompleteListener(new OnLoadCompleteListener() {

            @Override
            public void onLoadComplete(SoundPool arg0, int arg1, int arg2) {
                // TODO Auto-generated method stub
                sp.play(i, 1,1,0,0,1.0f);    
            }
            
        });
    }
}












Now we are done. Post your questions in the comment. I will be happy to answer those.

For more info visit on facebook https://www.facebook.com/androidcoolstuffs

Thank you



Friday 11 April 2014

Posted by Unknown
16 comments | 05:07
Hello guys, how are you today? Hope you will all in the best of your life.

Today we are going to see how to connect our android app with mysql databse using php. Just follow the video and post comments if you have any problem.




For more info visit on facebook https://www.facebook.com/androidcoolstuffs

Thank you

Wednesday 9 April 2014

Posted by Unknown
2 comments | 22:12
Hello guys, how are you today? Its super Thursday, and hope you will be in the best of your life.

Today we are going to see how to pass value from one activity to other in Android. So lets get started.



1) First of all create an Android Application Project.

2) Next, go to "res/layout/activity_main.xml" and create one editText and one "send" button there. The code looks like.

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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical" >

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id = "@+id/et" />
    
    <Button 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id = "@+id/send"
        android:text="send"/>

</LinearLayout>

3) Next go to "src/your_package_name/MainActivity.java" and here we are goin to retrieve the value written in the EditText and send the value to the "Second_Activity.java" on clicking the send button. The code looks like the following.

MainActivity.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener {
    EditText et;
    Button send;

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

        et = (EditText) findViewById(R.id.et);
        send = (Button) findViewById(R.id.send);

        send.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.send:
            Intent i = new Intent(this, Second_Activity.class);
            i.putExtra("value", et.getText().toString());
            startActivity(i);
            break;
        }
    }

}

4) Now go to "res/layout" and create an Android Xml file there, for our case lets name it "show.xml" .

show.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id = "@+id/tv"
        android:text="Demo text"
        android:textColor="#000000" />
    

</LinearLayout>

5) Next go to "src/your_package_name/Second_Activity.java" and here we are going to retrieve the value send in by MainActivity.java and set the same value  in the TextView.

Second_Activity.java

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class Second_Activity extends Activity {
    TextView tv;
    String word;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show);
        try{
        word = getIntent().getExtras().getString("value");
        Toast.makeText(this, word, Toast.LENGTH_SHORT).show();
        }catch(Exception e) {
            Toast.makeText(this, "Exception raised", Toast.LENGTH_SHORT).show();
            
        }
        tv = (TextView) findViewById(R.id.tv);
        tv.setText(word);
    
    }

}

Note - Dont forget to declare the activity in AndroidManifest.xml

<activity android:name="com.example.passing_value.Second_Activity" /> 

Now we are done. Post your questions in the comment. I will be happy to answer those.

For more info visit on facebook https://www.facebook.com/androidcoolstuffs

Thank you