Saturday, August 3, 2013

Detect Software Keyboard is Visible on Android Device

Solution 1:

  View activityRootView = fragmentView.findViewById(R.id.root_view);
   activityRootView.getViewTreeObserver().addOnGlobalLayoutListener( new                      OnGlobalLayoutListener() {
   @Override
    public void onGlobalLayout() {

 Rect r = new Rect();
        activityRootView.getWindowVisibleDisplayFrame(r);
 
        int screenHeight =  activityRootView.getRootView().getHeight();
Log.e("screenHeight",String.valueOf(screenHeight));
        int heightDiff = screenHeight - (r.bottom - r.top);
        Log.e("heightDiff",String.valueOf(heightDiff));
boolean visible = heightDiff > screenHeight / 3;
   
 Log.e("visible",String.valueOf(visible));

if(visible)
                          { if true ,do your stuff here !}
else
                           { else, do your stuff here }

}
});


Solution 2:

View activityRootView = fragmentView.findViewById(R.id.root_view);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int heightDiff = activityRootView.getRootView().getHeight() -                                                                                                                                      activityRootView.getHeight();
if (heightDiff > 150) 
{ // if more than 150 pixels, its probably a keyboard...
do your stuff , if keyboard visible
}
else 
{

}
}

});

Thursday, July 26, 2012

Play video in android




Project Name     :  Video

Package Name   :  selva.video

Version              :  1.5 ( Supports 1.5 and above versions)


main.xml

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

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

    <VideoView
        android:id="@+id/videoView1"
        android:layout_width="fill_parent"
        android:layout_height="174dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="117dp"
        android:text="Play" />

</RelativeLayout>



VideoActivity.class

package selva.video;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.VideoView;

public class VideoActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button play=(Button) findViewById(R.id.button1);
     
       
        final VideoView vv = (VideoView) findViewById(R.id.videoView1);       
        Uri video = Uri.parse("android.resource://" + getPackageName() + "/" 
                + R.raw.video);  







  // here i used video.mp4.. You can add audio file under res/raw/
       // create raw folder under res folder then add video

        vv.setVideoURI(video);
       
        play.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                   vv.start(); 
            }
        });
       
    
    }
}



video.mp4 






















OUTPUT : 























click play button. You will hear the audio but  video is not visible. 

You will check it out in real device.



 

Creating Tabs in Android




click here to download source code

Project Name     :  Tab1

Package Name   :  selva.tab

Version              :  2.1 ( Supports 2.1 and above versions)


main.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
       <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp">
                    <TabWidget
                       android:id="@android:id/tabs"
                       android:layout_width="fill_parent"
                       android:layout_height="wrap_content" />
                   <FrameLayout
                      android:id="@android:id/tabcontent"
                      android:layout_width="fill_parent"
                      android:layout_height="fill_parent"
                      android:padding="5dp" />
       </LinearLayout>
</TabHost>



tab1xml.xml

<?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:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="32dp"
        android:text="Hi.. welcome to tab1 !!!"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="100dp"
        android:text="You can add your form widgets and control here !!" />

</RelativeLayout> 



tab2xml.xml  

<?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:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="32dp"
        android:text="Hi.. welcome to tab2 !!!"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="100dp"
        android:text="You can add your form widgets and control here !!" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="150dp"
        android:text="You are in tab2 !!!!!!!!!!"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>



 select.xml    ( res/drawable/select.xml)


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected, use grey -->
    <item android:drawable="@drawable/lightaccept"
          android:state_selected="true" />
    <!-- When not selected, use white-->
    <item android:drawable="@drawable/accept" />
   
</selector>



Tab1Activity.class


 package selva.tab;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;

public class Tab1Activity extends TabActivity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
      
      
        Resources res = getResources();
        TabHost tabHost = getTabHost();
        TabHost.TabSpec spec;
        Intent intent;

 
        intent = new Intent().setClass(this, Activity1.class);

    
        spec = tabHost.newTabSpec("artists").setIndicator("Tab 1",
                          res.getDrawable(R.drawable.select))
                      .setContent(intent);
        tabHost.addTab(spec);

  
        intent = new Intent().setClass(this, Activity2.class);
        spec = tabHost.newTabSpec("albums").setIndicator("Tab 2",
                          res.getDrawable(R.drawable.select))
                      .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this,Activity3.class);
        spec = tabHost.newTabSpec("songs").setIndicator("Tab 3",
                          res.getDrawable(R.drawable.select))
                      .setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(2);
      
      
      
    }
}



 Activity1.class


package selva.tab;

import android.app.Activity;
import android.os.Bundle;

public class Activity1 extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
         setContentView(R.layout.tab1xml);
        
    }
   
}





 

 Activity2.class

package selva.tab;

import android.app.Activity;
import android.os.Bundle;


public class Activity2 extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
         setContentView(R.layout.tab2xml);
        
    }
   
}



Activity3.class


package selva.tab;

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


public class Activity3 extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
      
         TextView textview = new TextView(this);
            textview.setText("This is the third tab");
            setContentView(textview);
       
    }
   
}







OUTPUT  :







click tab1




click tab2 


































click here to download source code




Play song in Emulator




Project Name     :  Song

Package Name   :  selva.song

Version              :  1.5 ( Supports 1.5 and above versions)


main.xml


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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="31dp"
        android:layout_marginTop="50dp"
        android:text="Play" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:layout_alignParentLeft="true"
            android:layout_marginLeft="80dp"
        android:layout_marginTop="50dp"
        android:text="stop" />

</RelativeLayout>


 SongActivity.class


package selva.song;

import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class SongActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button play=(Button) findViewById(R.id.button1);
        Button stop=(Button) findViewById(R.id.button2);

        final MediaPlayer mp=MediaPlayer.create(getApplicationContext(), R.raw.song);

        // here i used song.mp3.. You can add audio file under res/raw/
       // create raw folder under res folder then add song
 
        play.setOnClickListener(new View.OnClickListener()
        {
        
           @Override
           public void onClick(View v)
             {
                      // TODO Auto-generated method stub
        
              mp.stop();
                  try
                     {
                     mp.prepare();
                    }
                  catch (IllegalStateException e)
                     {
                       // TODO Auto-generated catch block
                       e.printStackTrace();
                     }
                  catch (IOException e)
                     {
                           // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
             mp.start();
            
         }
     });
       
     stop.setOnClickListener(new View.OnClickListener()
        {
        
         @Override
         public void onClick(View v)
         {
             // TODO Auto-generated method stub
        
             mp.stop();
    
         }
     });
    }
}



song.mp3 


























OUTPUT: 















click play button. You will hear the song.

click stop button. song playing will be stopped.




Simple Seekbar In Android



click here to download source code


Project Name     :  seekbar

Package Name   :  selva.seekbar

Version              :  1.5 ( Supports 1.5 and above versions)


main.xml

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="94dp" >

        <requestFocus />
    </EditText>

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="38dp" />

</RelativeLayout>



 SeekbarActivity.class



package selva.seekbar;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;


public class SeekbarActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        SeekBar sb=(SeekBar) findViewById(R.id.seekBar1);
        final EditText et=(EditText) findViewById(R.id.editText1);
       
        sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
        {
            @Override
            public void onStopTrackingTouch(SeekBar seekBar)
            {
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar)
            {
            }
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser)
            {
            //---change the font size of the EditText---
         
            et.setText(String.valueOf(progress));
            }
            });
       
    }
}


OUTPUT  : 


























move seekbar


























click here to download source code



Monday, July 23, 2012

Check URL is available or not



click here to download source code

package name   :  selva.ip

project Name    :   Ip

Version             :   1.5 ( Supports 1.5 and above versions)


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

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

</LinearLayout>




 IpActivity.java



package selva.ip;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class IpActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
       
                 try
                   {

                           URL url = new URL("http://androidprogramz.in/" );
                         //URL url = new URL("http://www.nofoundwebsite.com/" );
                             executeReq(url);
                             Toast.makeText(getApplicationContext(), "Webpage is available!", Toast.LENGTH_SHORT).show();
        
                   }
                 catch(Exception e)
                     {
                             Toast.makeText(getApplicationContext(), "oops! webpage is not available!", Toast.LENGTH_SHORT).show();
                     }


    }
   
    private void executeReq(URL urlObject) throws IOException
    {
        HttpURLConnection conn = null;
        conn = (HttpURLConnection) urlObject.openConnection();
        conn.setReadTimeout(30000);//milliseconds
        conn.setConnectTimeout(3500);//milliseconds
        conn.setRequestMethod("GET");
        conn.setDoInput(true);

        // Start connect
        conn.connect();
        InputStream response =conn.getInputStream();
        Log.d("Response:", response.toString());
    }

}



Output :









change unavailable url  in  IpActivity.java

then run it.














































click here to download source code



Sliding panel from bottom to top



click here to download source code

package name  :  selva.sliding

project  name   :  Sliding3

version             :  1.5 ( Supports 1.5 and above versions)

main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="bottom"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
  <Button
        android:id="@+id/show1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="150dp"  
        android:background="@drawable/collapse"/>
 

        <selva.sliding.Sliding
            android:id="@+id/sliding1"
            android:orientation="vertical"
            android:layout_marginLeft="120dp" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
         
            android:gravity="left"
            android:padding="1px"
            android:background="#ffffff">
                <CheckBox android:id="@+id/check1"
                        android:layout_width="wrap_content"
                        android:textColor="#FFFFFF"
                        android:layout_height="wrap_content"
                        android:text="Option1" />
                <CheckBox android:id="@+id/check2"
                        android:layout_width="wrap_content"
                        android:textColor="#FFFFFF"
                        android:layout_height="wrap_content"
                         android:text="Option2" />
                <CheckBox android:id="@+id/check3"
                        android:textColor="#FFFFFF"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                         android:text="Option3" />
        </selva.sliding.Sliding>
   
</LinearLayout>
 


 Sliding2Activity.java


package selva.sliding;


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

public class Sliding2Activity extends Activity
{
      CheckBox c1,c2,c3;
        int key=0;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final Sliding popup = (Sliding) findViewById(R.id.sliding1);
        popup.setVisibility(View.GONE);

                final Button btn=(Button)findViewById(R.id.show1);
                btn.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View arg0) {
                                if(key==0){
                                        key=1;
                                        popup.setVisibility(View.VISIBLE);
                                        btn.setBackgroundResource(R.drawable.collapse);
                                }
                                else if(key==1){
                                        key=0;
                                        popup.setVisibility(View.GONE);
                                        btn.setBackgroundResource(R.drawable.collapse);
                                }
                        }
                });

        }
    }
 

 Sliding.java

package selva.sliding;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.widget.LinearLayout;
public class Sliding extends LinearLayout
{
private Paint innerPaint, borderPaint ;
public Sliding(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public Sliding(Context context) {
super(context);
init();
}
private void init() {
innerPaint = new Paint();
innerPaint.setARGB(225, 75, 75, 75); //gray
innerPaint.setAntiAlias(true);
borderPaint = new Paint();
borderPaint.setARGB(255, 255, 255, 255);
borderPaint.setAntiAlias(true);
borderPaint.setStyle(Style.STROKE);
borderPaint.setStrokeWidth(2);
}
public void setInnerPaint(Paint innerPaint) {
this.innerPaint = innerPaint;
}
public void setBorderPaint(Paint borderPaint) {
this.borderPaint = borderPaint;
}
@Override
protected void dispatchDraw(Canvas canvas) {
RectF drawRect = new RectF();
drawRect.set(0,0, getMeasuredWidth(), getMeasuredHeight());
canvas.drawRoundRect(drawRect, 5, 5, innerPaint);
canvas.drawRoundRect(drawRect, 5, 5, borderPaint);
super.dispatchDraw(canvas);
}
}

  Output :






click icon 














































 













































click here to download source code