Saturday 30 August 2014

How to Restart Android ADB(Android Debug Bridge) ?

Method 1:

To restart ADB(Android Debug Bridge) by command line:
adb kill-server
adb start-server
Method 2:
To restart ADB(Android Debug Bridge) in Eclipse:
  1. Window > Show View > Other... > Android/Devices
  2. When the view is showing: View Menu of "Devices" > Reset ADB
Refer below Screen


How to Enable USB Debugging in Android 4.3 and above versions ?

Steps for Enabling USB debug mode in android version 4.3 and above......

Step 1: Go to Settings.


Step 2: Click About.

Step 3: Open Software information.

Step 4: Tap Build number continuously until a pop-up hint informs you that you have entered the developer mode.

Step 5: Return to Settings.

Step 6: Find and open Developer options.

Step 7: Select USB debugging and click OK.


After these seven steps, the USB debugging is enabled successfully.

Tuesday 8 July 2014

Solution for AJAX Repeat Calls

The AJAX Repeat Calls(caching) is due to the same URL that's being called repeatedly.
If you change the URL dynamically then this issue can be resolved.
  1. Something like by adding a query String with the current time with the request                            ( or )
  2. any random generated number
You can change the URL without affecting the result.

Friday 9 May 2014

Date Format Conversion in Java


package com.javapages4all;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author Rajeswararao (JavaPages4All)
 *
 */
public class DateFormatConversion {

public static void main(String[] args) {
Format format;
Date date = new Date();
System.out.println(date); // Sat May 10 00:31:18 IST 2014

format = new SimpleDateFormat("dd/MM/yy");
System.out.println(format.format(date)); // 10/05/14

format = new SimpleDateFormat("MM/dd/yy");
System.out.println(format.format(date)); // 05/10/14

format = new SimpleDateFormat("dd-MMM-yy");
System.out.println(format.format(date)); // 10-May-14

format = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
System.out.println(format.format(date)); // 2014.05.10.00.27.08

format = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");
System.out.println(format.format(date)); // Sat, 10 May 2014 00:27:08 +0530

format = new SimpleDateFormat("EEEE, dd MMMM yyyy HH:mm:ss zzzz");
System.out.println(format.format(date)); //Saturday, 10 May 2014 00:27:08 India Standard Time
}
}

File Download from Web by using Java

package com.javapages4all;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

/**
 *
 * @author Rajeswararao (JavaPages4All)
 *
 */
public class FileDownload {

public static void main(String args[]) throws IOException {
BufferedInputStream buffInputStream=null;
FileOutputStream fileOutputStream=null;
BufferedOutputStream buffOutputStream=null;

try {
buffInputStream= new BufferedInputStream(new URL("File Url").openStream());
fileOutputStream = new FileOutputStream("D://JavaPage4All.pdf");
buffOutputStream = new BufferedOutputStream(fileOutputStream,1024);
byte data[] = new byte[1024];
int length;
while ((length = buffInputStream.read(data)) > 0) {
buffOutputStream.write(data, 0, length);
}

} catch (Exception e) {
e.printStackTrace();
}finally{
if(buffOutputStream != null)
buffOutputStream.close();
if(fileOutputStream != null)
fileOutputStream.close();
if(buffInputStream != null)
buffInputStream.close();
}
}
}

Thursday 1 May 2014

Why we are using XML Design for Android Apps?


  1. XML layout design is easy to understand as well keep application user interface separate from the application code.
  2. There are many devices in the market having different-different display sizes and Android provides an easier task to create UI for all using XML and keeping them separate.
  3. Also if you want to develop an multilingual application then XML is the best one.
  Bottom line:
       Only use the Java code for layout if you really need otherwise you have still best option XML.

Android Activity Creation with XML Design


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

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

Android Activity Creation without XML Design


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

public class HelloAndroid extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       TextView tv = new TextView(this);
       tv.setText("Hello, Android");
       setContentView(tv);
   }
}

How to Get Thunderbird Email Messages Into Outlook

  1. Open Thunder bird. Mail
  2. Select the messages you want to export to Outlook. How many you can export will depend on the size of each message and its attachments as well as the capability of your Internet connection and provider.                                                                                                                      
  3. After selecting the messages, right click and select "Forward as Attachments." A new mail message will open up with the email messages you selected as attachments. Enter the email address you are using with Outlook and press send.
  4. Open the email containing the email message attachments and move this active window down a bit on your screen so you can see your main inbox area in Outlook.                                
  5. Select All Attachments that contains ThunderBird mails as Attachments then Drag it into your Inbox or any Folder . Finally you got it. 

How to Delete All Lines Containing a Unwanted Words from Text File?


  1. Go to Search menu > Find... > Select "Mark" Tab. Enter Search Text in Find what Search Box or Activate regular expressions. Search for ^<Path> (^ is for line start). Don't forget to check "Bookmark lines" and Press "Mark All"
    ==> All Rows you want to keep got a Bookmark
  2. Go to Menu "Search - Bookmark - Remove Bookmarked lines"
    ==> All Bookmarked lines are deleted.

How to delete lines except lines which containing wanted word?


  1. Go to Search menu > Find... > Select "Mark" Tab. Enter a Text in Find what search box or Activate regular expressions. Search for ^<Path> (^ is for line start). Don't forget to check "Bookmark lines" and Press "Mark All"
    ==> All Rows you want to keep got a Bookmark
  2. Go to Menu "Search - Bookmark - Inverse Bookmark"
    ==> All Line you want to delete are bookmarked.
  3. Go to Menu "Search - Bookmark - Remove Bookmarked lines"
    ==> All Bookmarked lines are deleted.   

Saturday 11 January 2014

Android Fundamentals


  1. Activity
  2. Service
  3. Broadcast Receiver
  4. Content Provider.
Activity
An activity represents a single screen with a user interface.
The user can interact with the activity i.e. click the button.

service is a component that runs in the background to perform long-running operations or to perform work for remote processes. A service does not provide a user interface.

Example:
       Listening Music is the service. Because no user interaction is neccessary to play music. Once you start it will playing all the songs.
content provider manages a shared set of application data. You can store the data in the file system, an SQLite database, on the web, or any other persistent storage location your application can access. 

Example:
       Consider that you are going to develop message sending application. For choosing the contacts you need to add contacts one by one in your application. 
       Instead of that you just share the contact which is in your phone contacts.

broadcast receiver is a component that responds to system-wide broadcast announcements.

            Example:
                      It reacts some thing, when the phone receive any messages or phone etc.

Thursday 9 January 2014

MD5 Encryption in Android


MD5 Encryption in Android

1.Md5.java

package com.JavaPages4All.md5encryption;

import java.security.MessageDigest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Md5 extends Activity

{
 EditText textview;
 Button get;
 TextView textlbl;
 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.md5);

  textview=(EditText)findViewById(R.id.textview);
  get=(Button)findViewById(R.id.get);
  textlbl=(TextView)findViewById(R.id.textlbl);
  get.setOnClickListener(new OnClickListener()
  {
   @Override
   public void onClick(View arg0)
   {
    String strpassword=textview.getText().toString();
    if(strpassword!=null && strpassword.trim().length()!=0)
    {
     String md5=getMD5Encryption(strpassword);
     textlbl.setText(md5);
    }
   }
  });
 }
 public String getMD5Encryption(String strpassword)
 {
  try
  {
   // Create MD5 Hash

   MessageDigest digest = java.security.MessageDigest.getInstance("MD5");

   digest.update(strpassword.getBytes());
   byte messageDigest[] = digest.digest();
   // Create Hex String
   StringBuffer hexString = new StringBuffer();
   for (int i = 0; i < messageDigest.length; i++)
   {
    String h = Integer.toHexString(0xFF & messageDigest[i]);
    while (h.length() < 2)
     h = "0" + h;
    hexString.append(h);
   }
   return hexString.toString();
  } catch (Exception e)
  {
   e.printStackTrace();
  }
  return "";
 }
}
2.md5.xml

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

      <EditText
          android:id="@+id/textview"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:ems="10"
          android:hint="Enter String"
          android:inputType="textPersonName" >
      
        <requestFocus />
       </EditText>
          
    <Button
        android:id="@+id/get"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Get MD5" />

    <TextView
        android:id="@+id/textlbl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Md5 string"
        android:textColor="#ff00ff"
        android:textSize="20sp" />

</LinearLayout>

Hide the Title Bar and Status Bar in Android



Hide the Title Bar and Status Bar in Android

1.Hide the Title Bar Using AndroidManifest.xml

If you want to hide the title bar for particular Activity 
use the below code in AndroidManifest.xml within the <activity> tag.

 android:theme="@android:style/Theme.Black.NoTitleBar"

Example:
     <activity
            android:name=" MainScreen"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Black.NoTitleBar " >

 Hide the Title Bar and Status Bar Using XML code.

If you want to hide the Title Bar and Status Bar for particular activity, use the below code in AndroidManifest.xml within the <activity> tag.

android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"

Example:
     <activity
            android:name=MainScreen"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen " >

Hide the Title Bar and Status Bar for entire Application Using XML code.

 If you want to hide the Title Bar and Status Bar for entire application,just use the above code in <application> tag.

Example:

 for hiding title bar:
        <application
         android:icon="@drawable/icon"
         android:label="@string/app_name"
         android:theme="@android:style/Theme.Black.NoTitleBar" >

 for hiding title and status bar:
        <application
         android:icon="@drawable/icon"
         android:label="@string/app_name"
         android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
                Working in Java file using java code

Hide the Title Bar Using java code.

If you want to hide the title bar for particular activity, use this java code above the setContentView() method.

requestWindowFeature(Window.FEATURE_NO_TITLE);

Android Spinner View Text Not Visible


Android Spinner View Text Not Visible 

(White background on Text Also showing White Color)

---------------------------------------------------------------------------------------------------------------------------------


<Spinner
     android:id="@+id/courselist"     android:layout_width="0dp"
     android:layout_height="wrap_content"
     android:layout_weight="1" />
Spinner spnrcourse=(Spinner)findViewById(R.id.courselist);
String[] courselist = {"Java", "Cpp", "Oracle","Android" };
spnrcourse.setAdapter(new ArrayAdapter(getApplicationContext(), 
android.R.layout.simple_dropdown_item_1line, courselist));

Replace getApplicationContext() to getBaseContext()
                              (OR)
Replace getApplicationContext() to this (if above is not working)
                              (OR)
Replace getApplicationContext() to getActivity() (if you are Using Fragment) 


Wednesday 8 January 2014

Mutable and Immutable Object in Java



package raja.JavaPages4All;

import java.util.Date;

/**
 * @author Raja(JavaPages4All)
 */
public class TestPojo 
{
 private String blogName;
 private Date date;

 public TestPojo(String blogName,Date date)
 {
  this.blogName=blogName;
  this.date = date;
 }

 public String getBlogName()
 {
  return this.blogName;
 }

 public Date getDate()
 {
  return this.date;
 }
}



package raja.JavaPages4All;

import java.util.Date;

public class ImmutableTest 
{

 /**
  * @author Raja(JavaPages4All)
  */
 public static void main(String[] args) 
 {
  /*
   * String Class is Immutable.
   * Date Class is Mutable. 
   */
  String str="JavaPages4All";
  Date myDate = new Date();

  TestPojo pojo =new TestPojo(str,myDate);

  /**
   * Getting Values from TestPojo Reference Object(When First Time) 
   */
  System.out.println(pojo.getBlogName());
  System.out.println( pojo.getDate() );

  /**
   * String str Object value changing so it is Creating Newly(Immutable).
   * Date myDate Object value changing so it is Updated the 
   * Previous Object myDate Value(Mutable)
   */
  str="Raja";
  myDate.setMonth( myDate.getMonth() + 1 );

  /**
   * Getting Values from TestPojo Reference Object
   * (When Second Time object Values Changed)
   */
  System.out.println(pojo.getBlogName()+"  "+str);
  System.out.println( pojo.getDate() );
 }

}




Mutable vs Immutable in Java


Mutable Objects: When you have a reference to an instance of an object, the contents of that instance can be altered

Immutable Objects: When you have a reference to an instance of an object, the contents of that instance cannot be altered

(OR)
Immutable objects are simply objects whose state (the object's data) cannot change after construction

Immutability and Instances

To demonstrate this behaviour, we'll use java.lang.String as the immutable class and java.awt.Point as the mutable class.
package raja.JavaPages4all;

import java.awt.Point;

public class Immutability  
{
 /**
  * @author Raja(JavaPages4All)
  * @param args
  */
 public static void main(String[] args) 
 {
  Point pt= new Point( 0, 0 );
  System.out.println( pt);
  pt.setLocation( 1.0, 0.0 );
  System.out.println( pt);

  String str= new String( "old String" );
  System.out.println( str);
  str.replaceAll( "old", "new" );
  System.out.println( str);
 }
}
In case you can't see what the output is, here it is:
 java.awt.Point[0.0, 0.0]
 java.awt.Point[1.0, 0.0]
 old String
 old String

We are only looking at a single instance of each object, but we can see that the contents of pt has changed, but the contents of str did not. To show what happens when we try to change the value of str, we'll extend the previous example.
package raja.JavaPages4all;

import java.awt.Point;

public class Immutability  
{
 /**
  * @author Raja(JavaPages4All)
  * @param args
  */
 public static void main(String[] args) 
 {
  Point pt= new Point( 0, 0 );
  System.out.println( pt);
  pt.setLocation( 1.0, 0.0 );
  System.out.println( pt);

  String str= new String( "old String" );
  System.out.println( str );
  str= new String( "new String" );
  System.out.println( str );
 }
}
The output from this is:
 old String
 new String
Now we find that the value displayed by the str variable has changed. We have defined immutable objects as being unable to change in value, so what is happening? Let's extend the example again to watch the str variable closer.
package raja.JavaPages4all;


public class Immutability  
{
 /**
  * @author Raja(JavaPages4All)
  * @param args
  */
 public static void main(String[] args) 
 {
  //Comparison
  String str = new String( "old String" );
  String myCache = str ;
  System.out.println( "String Comparison: " + str.equals( myCache ) );
  System.out.println( "Object Comparison:  " + ( str == myCache ) );

  str = "not " + str ;
  System.out.println( "String Comparison: " + str.equals( myCache ) );
  System.out.println( "Object Comparison:  " + ( str == myCache ) );
 }
}
The result from executing this is:
 String Comparison: true
 Object Comparison: true
 String Comparison: false
 Object Comparison: false
What this shows is that variable str is referencing a new instance of the String class. The contents of the object didn't change; we discarded the instance and changed our reference to a new one with new contents.