Open top menu



 In this blog we will explain how to enable Move to Sdcard Button and allow android application  move to Sdcard , you can installed application on the sdcard. show picture...


 In Android, you can go to device Settings -->Applications ->ManageApplications -> and then click on your app to view the app details. You will find a ‘Move to SD Card’ button which is available for some apps and for some apps it will be disabled (not click-able) for other apps. By default it is disabled for your application.




  1. preferExternal :- this request that your application may installed on the external storage, but the system does not guarantee that your application will be installed on the external storage. 
  2. auto :- this indicate that your application may be installed on the external storage, but you don’t have a preference of install location.  


manifest,xml
..................................................................................................................................................................
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidbeginner.testcode"
    android:versionCode="1"
    android:installLocation="preferExternal"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />
    
    <uses-permission android:name="android.permission.INTERNET"/>
      
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        
        <activity
                 android:name="com.androidbeginner.testcode.MainActivity"
                 android:label="@string/app_name" 
                 android:hardwareAccelerated="false">            
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            
        </activity>
        
        
    </application>

</manifest>
...................................................................................................................................................................


Read more


In this blog we will add external Stylish Text  Fonts in android.

1.First you have to download text fonts.
2.Create fonts inside assets foder.
3.you can past the text font (.ttf extension) file.






MainActivity.java
...................................................................................................................................................................

package com.androidbeginner.testcode;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;

@SuppressLint("ResourceAsColor")
public class MainActivity extends Activity {

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setTitle("HINDI FONT");
setContentView(R.layout.activity_main);

TextView textstyle1 = (TextView) findViewById(R.id.textView);
TextView textstyle2 = (TextView) findViewById(R.id.textView1);
TextView textstyle3 = (TextView) findViewById(R.id.textView2);
Typeface fonttextstyle1 = Typeface.createFromAsset(getAssets(),"fonts/Face Your Fears.ttf");
Typeface fonttextstyle2 = Typeface.createFromAsset(getAssets(),"fonts/DS-DIGIT.TTF");
Typeface fonttextstyle3 = Typeface.createFromAsset(getAssets(),"fonts/CircleD_Font_by_CrazyForMusic.ttf");
textstyle1.setTypeface(fonttextstyle1);
textstyle2.setTypeface(fonttextstyle2);
textstyle3.setTypeface(fonttextstyle3);


}


}
................................................................................................................................................................

activity_main.xml.
...................................................................................................................................................................

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/logoweb" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="androidbeginnerpoint"
        android:textColor="#ecaa00"
        android:layout_margin="5dp"
        android:textSize="30dp" />
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="androidbeginnerpoint"
        android:textColor="#ecaa00"
        android:layout_margin="5dp"
        android:textSize="30dp" />
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="androidbeginnerpoint"
        android:textColor="#ecaa00"
        android:layout_margin="5dp"
        android:textSize="30dp" />

</LinearLayout>
...................................................................................................................................................................


try this code....
Read more


In this blog we will explain how to used hindi fonts in android. Explain below


1.Download an external hindi font that you like to use in your application.
2.You can also download from here
3.Create a new folder “fonts” under assets and copy the downloaded .ttf font to your assets/font               folder.Load .ttf to your TextView.

try this code....





string,xml.
...................................................................................................................................................................<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Test Code</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
   
    <string name="Red">कर्पूरगौरं करुणावतारं संसारसारं भुजगेन्द्रहारं |
सदा वसन्तं ह्रदयाविन्दे भंव भवानी सहितं नमामि ॥
जय शिव ओंकारा हर ॐ शिव ओंकारा |
ब्रम्हा विष्णु सदाशिव अद्धांगी धारा ॥

ॐ जय शिव ओंकारा......
एकानन चतुरानन पंचांनन राजे |
हंसासंन ,गरुड़ासन ,वृषवाहन साजे॥

ॐ जय शिव ओंकारा......
दो भुज चारु चतुर्भज दस भुज अति सोहें |
तीनों रुप निरखता त्रिभुवन जन मोहें॥
ॐ जय शिव ओंकारा......
अक्षमाला ,बनमाला ,रुण्ड़मालाधारी |
चंदन , मृदमग सोहें, भाले शशिधारी ॥

ॐ जय शिव ओंकारा......
श्वेताम्बर,पीताम्बर, बाघाम्बर अंगें
सनकादिक, ब्रम्हादिक ,भूतादिक संगें

ॐ जय शिव ओंकारा......
कर के मध्य कमड़ंल चक्र ,त्रिशूल धरता |
जगकर्ता, जगभर्ता, जगसंहारकर्ता ॥

ॐ जय शिव ओंकारा......
ब्रम्हा विष्णु सदाशिव जानत अविवेका |
प्रवणाक्षर मध्यें ये तीनों एका ॥

ॐ जय शिव ओंकारा......
काशी में विश्वनाथ विराजत नन्दी ब्रम्हचारी |
नित उठी भोग लगावत महिमा अति भारी ॥

ॐ जय शिव ओंकारा......
त्रिगुण शिवजी की आरती जो कोई नर गावें |
कहत शिवानंद स्वामी मनवांछित फल पावें ॥

ॐ जय शिव ओंकारा.....
जय शिव ओंकारा हर ॐ शिव ओंकारा|
ब्रम्हा विष्णु सदाशिव अद्धांगी धारा॥
ॐ जय शिव ओंकारा......

 </string>
...................................................................................................................................................................


activity_main.xml.
...................................................................................................................................................................
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/logoweb" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Red"
        android:textColor="#ecaa00"
        android:layout_margin="5dp" />

</LinearLayout>
...................................................................................................................................................................

MainActivity.Java.
...................................................................................................................................................................
package com.androidbeginner.testcode;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("ResourceAsColor")
public class MainActivity extends Activity {

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setTitle("HINDI FONT");
setContentView(R.layout.activity_main);

TextView hinditext = (TextView) findViewById(R.id.textView);
Typeface fontHindi = Typeface.createFromAsset(getAssets(),"fonts/Ananda Lipi Bold Cn Bt.ttf");
hinditext.setTypeface(fontHindi);

}


}
...................................................................................................................................................................





Read more



In this example we will explain how to handle/detect android Mobile Back Button in android programmatically.

/*This allows the system to properly navigate backward when the user presses the Back button.              However, there are a few cases in which your app should manually specify the Back behavior
   in order to provide*/





MainActivity.java
................................................................................................................................................................
package com.androidbeginner.testcode;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

@SuppressLint("ResourceAsColor")
public class MainActivity extends Activity {

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setTitle("Used Back Button");
setContentView(R.layout.activity_main);

}

@Override
public void onBackPressed() {
Toast.makeText(getApplicationContext(), "On press Button :--Open PreviousActivity",   Toast.LENGTH_LONG).show();
super.onBackPressed();
}
}
..................................................................................................................................................................

Back Button Navigation for fragment.

...................................................................................................................................................................
getSupportFragmentManager().beginTransaction()
    .add(detailFragment, "detail")
    // Add this transaction to the back stack
    .addToBackStack()
    .commit();
..................................................................................................................................................................


Back Button Navigation for webview.

...................................................................................................................................................................
@Override
public void onBackPressed() {
   if (mWebView.canGoBack()) {
       mWebView.goBack();
       return;
   }

   // Otherwise defer to system default behavior.
   super.onBackPressed();
}
...................................................................................................................................................................




Read more


In this blog explain How to display and set back arrow button on ActionBar in android. Explain Below....

Basically, we are used this button because Onclick back arrow Button redirect Back Activity in android .

                /* 
* display Home Back Button on actionbar  
*/
getActionBar().setDisplayHomeAsUpEnabled(true);
/* 
* display Home Back Button Enable  
*/
getActionBar().setHomeButtonEnabled(true);








MainActivity.java
...................................................................................................................................................................

@SuppressLint("ResourceAsColor")
public class MainActivity extends Activity {

 

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setTitle("ActionBar");

/* 
* display Home Back Button on actionbar  
*/
getActionBar().setDisplayHomeAsUpEnabled(true);
/* 
* display Home Back Button Enable  
*/
getActionBar().setHomeButtonEnabled(true);

setContentView(R.layout.activity_main);
 


/* 
* Redirect method 
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {

case android.R.id.home:
finish();
break;

default:
break;
}
 
return super.onOptionsItemSelected(item);
}

}
...................................................................................................................................................................

Read more


In this blog explain how to send email in android. the email send via Intent ,the intent carry data one component to another means your message ,subject and email id  etc .check uploaded screen shot.






Used Intent.
 Intent email = new Intent(Intent.ACTION_SEND);
                  email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
                  email.putExtra(Intent.EXTRA_SUBJECT, subject);
                  email.putExtra(Intent.EXTRA_TEXT, message);


activity_main.xml.

Create the class inside the res/layout folder and drop down three edittext and send button.
...................................................................................................................................................................

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/editText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_margin="5dp"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:hint="To"
        android:inputType="textMultiLine" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_margin="5dp"
        android:layout_marginTop="18dp"
        android:ems="10"
        android:hint="Subject"
        android:inputType="textMultiLine" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText2"
        android:layout_margin="5dp"
        android:layout_marginTop="18dp"
        android:ems="10"
        android:hint="Message"
        android:inputType="textMultiLine" />

    <Button
        android:id="@+id/sendBtn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText3"
        android:layout_marginTop="20dp"
        android:text="Send" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="31dp"
        android:src="@drawable/logo" />

</RelativeLayout>

...................................................................................................................................................................

MainActivity.java
...................................................................................................................................................................


package com.example.androidtestcode;

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

public class MainActivity extends Activity {

EditText editTo,editSubject,editMessage;
    Button send;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTo=(EditText)findViewById(R.id.editText1);
editSubject=(EditText)findViewById(R.id.editText2);
editMessage=(EditText)findViewById(R.id.editText3);
       
        send=(Button)findViewById(R.id.sendBtn);
       
        send.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                 String to=editTo.getText().toString();
                 String subject=editSubject.getText().toString();
                 String message=editMessage.getText().toString();
                 
               
                  Intent email = new Intent(Intent.ACTION_SEND);
                  email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
                  email.putExtra(Intent.EXTRA_SUBJECT, subject);
                  email.putExtra(Intent.EXTRA_TEXT, message);
     
               
                  email.setType("message/rfc822");      
                  startActivity(Intent.createChooser(email, "Choose an Email client side :"));
     
            }


           
        });

       
   
}


}


...................................................................................................................................................................




Read more





Generally,used four constants defined in the XMLPullParser interface.

  • START_TAG :An XML start tag was read.
  • TEXT :Text content was read; the text content can be retrieved using the getText() method.
  • .END_TAG : An end tag was read.END_DOCUMENT :No more events are availabl






student.xml.
Create an Xml file named student.xml inside the assets folder.
...................................................................................................................................................................
<?xml version="1.0"?>
<records>
 
<student>
<id>10011223</id>
<name>Sarvesh Kaushik</name>
<fee>50000</fee>
</student>

<student>
<id>10013231</id>
<name>Varun Chauhan</name>
<fee>60000</fee>
</student>

<student>
<id>10078923</id>
<name>Shashank Singh</name>
<fee>70000</fee>
</student>

</records>

..................................................................................................................................................................

create the Student class that corresponds to the Xml file

Student.Java
...................................................................................................................................................................
package com.example.androidtestcode;

public class Student {
private int id;
    private String name;
    private float fee;
 
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getFee() {
return fee;
}
public void setFee(float fee) {
this.fee = fee;
}
@Override
   public String toString() {
       return " Id= "+id + "\n Name= " + name + "\n Fee= " + fee;
   }
}

..................................................................................................................................................................

Parse the Xml file using XMLPullParser and returning all the employee in list

PullParserHandler.Java
...................................................................................................................................................................
package com.example.androidtestcode;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
 

public class PullParserHandler {
    private List<Student> students= new ArrayList<Student>();
    private Student student;
    private String text;
 
    public List<Student> getEmployees() {
        return students;
    }
 
    public List<Student> parse(InputStream isInput) {
           try {
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser  parser = factory.newPullParser();
 
            parser.setInput(isInput, null);
 
            int eventType = parser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                String tagname = parser.getName();
                switch (eventType) {
                case XmlPullParser.START_TAG:
                    if (tagname.equalsIgnoreCase("student")) {
                        // create a new instance of employee
                    student = new Student();
                    }
                    break;
 
                case XmlPullParser.TEXT:
                    text = parser.getText();
                    break;
 
                case XmlPullParser.END_TAG:
                    if (tagname.equalsIgnoreCase("student")) {                      
                    students.add(student);
                    }else if (tagname.equalsIgnoreCase("id")) {
                    student.setId(Integer.parseInt(text));
                    }  else if (tagname.equalsIgnoreCase("name")) {
                    student.setName(text);
                    } else if (tagname.equalsIgnoreCase("fee")) {
                    student.setFee(Float.parseFloat(text));
                    }
                    break;
 
                default:
                    break;
                }
                eventType = parser.next();
            }
 
        } catch (XmlPullParserException e) {e.printStackTrace();}
        catch (IOException e) {e.printStackTrace();}
 
        return students;
    }
}
..................................................................................................................................................................



activity_main.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:gravity="center"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/logo"
        android:layout_marginBottom="40dp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Student List"
        android:gravity="center"
        android:textColor="#ffffff"
        android:textStyle="bold"
        android:padding="7dp"
        android:background="#349b2d" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:dividerHeight="4dp" >
    </ListView>

</LinearLayout>
..................................................................................................................................................................



MainActivity.Java
...................................................................................................................................................................
package com.example.androidtestcode;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

ListView listView;

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

listView = (ListView) findViewById(R.id.listView1);
       
       List<Student> students = null;
       try {
           PullParserHandler parser = new PullParserHandler();
           InputStream isInput=getAssets().open("student.xml");
           students = parser.parse(isInput);
           
           ArrayAdapter<Student> adapter =new ArrayAdapter<Student>(this,android.R.layout.simple_list_item_1, students);
           listView.setAdapter(adapter);
           
       } catch (IOException e) {e.printStackTrace();}
}

}
..................................................................................................................................................................
Read more

Android XML parsing Using DOM Parser?

 In this blog parse the xml used by DOM parser. The DOM parser better then SAX parser because DOM Parser Create And Parse xml file.





parser_file.xml.
Create the file inside assets folder.
...................................................................................................................................................................


<?xml version="1.0"?>
<records>
 
<employee>
<name>Sarvesh Kaushik</name>
<salary>50000</salary>
</employee>

<employee>
<name>Varun Chauhan</name>
<salary>60000</salary>
</employee>

<employee>
<name>Shashank Singh</name>
<salary>70000</salary>
</employee>

</records>


..................................................................................................................................................................

main_activity.xml.
Create the file inside res/layout folder.
...................................................................................................................................................................


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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/logo"
        android:layout_marginBottom="60dp" />

    <TextView
        android:id="@+id/textView_parser"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Details"
        android:gravity="center" />

</LinearLayout>

..................................................................................................................................................................

MainActivity.java.
...................................................................................................................................................................

package com.example.androidtestcode;

import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

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

public class MainActivity extends Activity {

TextView tvParser;

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

tvParser = (TextView) findViewById(R.id.textView_parser);
try {

InputStream is = getAssets().open("pasrser_file.xml");

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document documentfile = docBuilder.parse(is);
Element elements=documentfile.getDocumentElement();
elements.normalize();

NodeList nodList = documentfile.getElementsByTagName("employee");
for (int i=0; i<nodList.getLength(); i++) {

Node node = nodList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element2 = (Element) node;
tvParser.setText(tvParser.getText()+"\nName : " + getValue("name", element2)+"\n");
tvParser.setText(tvParser.getText()+"Salary : " + getValue("salary", element2)+"\n");

}

}//end of for loop


} catch (Exception e) {
e.printStackTrace();
}

}
private static String getValue(String tag, Element element) {
NodeList nodeList = element.getElementsByTagName(tag).item(0).getChildNodes();
Node node = (Node) nodeList.item(0);
return node.getNodeValue();
}
}
..................................................................................................................................................................
Read more



Android XML Parsing using SAX Parser ?


In this blog we will explain how to parse xml file.Android SAX Parser provide the facility xml file parse .but parse the data on xml file.





parser_file.xml.
create a xml file inside assets folder.
...................................................................................................................................................................
<?xml version="1.0"?>
<records>
 
<employee>
<name>Sarvesh Kaushik</name>
<salary>50000</salary>
</employee>

<employee>
<name>Varun Chauhan</name>
<salary>60000</salary>
</employee>

<employee>
<name>Shashank Singh</name>
<salary>70000</salary>
</employee>

</records>
...................................................................................................................................................................
activity_main.
Create the view inside res/layout folder.
...................................................................................................................................................................
<?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:gravity="center"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/logo"
        android:layout_marginBottom="60dp" />

    <TextView
        android:id="@+id/textView_parser"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Details"
        android:gravity="center" />

</LinearLayout>

...................................................................................................................................................................
MainActivity.java.
...................................................................................................................................................................

package com.example.androidtestcode;

import java.io.InputStream;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

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

public class MainActivity extends Activity {

TextView tvParser;

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

tvParser = (TextView) findViewById(R.id.textView_parser);
try {

SAXParserFactory pasrserFacrtory = SAXParserFactory.newInstance();
SAXParser sax_parser = pasrserFacrtory.newSAXParser();

DefaultHandler handler = new DefaultHandler() {
boolean name = false;
boolean salary = false;

public void startElement(String uri, String localName,
String qName, Attributes attributes)
throws SAXException {
if (qName.equalsIgnoreCase("name")) {
name = true;
}
if (qName.equalsIgnoreCase("salary")) {
salary = true;
}
}

public void endElement(String uri, String localName,
String qName) throws SAXException {
}

public void characters(char ch[], int start, int length)
throws SAXException {
if (name) {
tvParser.setText(tvParser.getText() + "\n\n Name : "
+ new String(ch, start, length));
name = false;
}
if (salary) {
tvParser.setText(tvParser.getText() + "\n Salary : "
+ new String(ch, start, length));
salary = false;
}
}// close of characters

};// close of DefaultHandler

InputStream is = getAssets().open("pasrser_file.xml");
sax_parser.parse(is, handler);

} catch (Exception e) {
e.printStackTrace();
}

}
}
...................................................................................................................................................................


Read more

In this blog we will implement custom Number Picker step by step.
1. first create activity_main.xml class.
2. second create view gradient xml class.
3.MainActivity java class. explain below.




activity_main.xml
Create the  view main xml class inside res/layout folder and drop the two button with Horizontal LinearLayout.
...................................................................................................................................................................

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

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/edit_text_gradient"
        android:orientation="horizontal"
        android:padding="5dp"
        android:gravity="center" >

        <Button
            android:id="@+id/button_decrement"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="-" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="1"
            android:textSize="30dp" />

        <Button
            android:id="@+id/button_increment"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="+" />
    </LinearLayout>

    <Button
        android:id="@+id/pickerBtn"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:text="OK" />

</LinearLayout>
...................................................................................................................................................................

edit_text_gradient.xml
Create the UI Gradient Design xml class inside the res/drawable folder .
...................................................................................................................................................................



<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="15dp">
 <solid android:color="#FFFFFF"/>

    <corners
     android:bottomRightRadius="10dp"
     android:bottomLeftRadius="10dp"
     android:topLeftRadius="10dp"
     android:topRightRadius="10dp"/>

  <stroke android:width="2dip" android:color="#dce0e2" />
</shape>
...................................................................................................................................................................

MainActivity.java.
..................................................................................................................................................................

package com.example.androidtestcode;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
Context context;  
TextView tv;
Button increment,decrement,pickerBtn;

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

   setContentView(R.layout.activity_main);
   tv=(TextView)findViewById(R.id.textView1);
 
   increment=(Button)findViewById(R.id.button_increment);
   decrement=(Button)findViewById(R.id.button_decrement);
   pickerBtn=(Button)findViewById(R.id.pickerBtn);
 
   context=this;

   increment.setOnClickListener(new View.OnClickListener()
   {

       @Override
       public void onClick(View v)
       {
           String present_value_string = tv.getText().toString();
           int present_value_int = Integer.parseInt(present_value_string);
           present_value_int++;
           tv.setText(String.valueOf(present_value_int));        
       }
   });
 
 
   decrement.setOnClickListener(new View.OnClickListener()
   {

       @Override
       public void onClick(View v)
       {
           String present_value_string = tv.getText().toString();
           int present_value_int = Integer.parseInt(present_value_string);
           if(present_value_int>1){
           present_value_int--;
           tv.setText(String.valueOf(present_value_int));
           }
       }
   });

 
   pickerBtn.setOnClickListener(new View.OnClickListener()
   {

       @Override
       public void onClick(View v)
       {
           String present_value_string = tv.getText().toString();
           Toast.makeText(getApplication(), "present value:-"+present_value_string,                                                Toast.LENGTH_LONG).show();
         
           }
     
   });
 
}
}
.................................................................................................................................................................
Read more


ListView Header with parallax effect in android?

In this blog we will implement Parallax effect ListView header step by step below.




Step 1-
list_item.xml.
The xml class create inside res/layout/ folder.the class is create a single row listview and bind via adapter show MainActivity java class.
...................................................................................................................................................................
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#FAFAFA"
    android:gravity="center_vertical"
    android:minHeight="40dp"
    android:padding="8dp"
    android:textColor="#222" />
...................................................................................................................................................................


Step 2-
activity_main.xml.

Create class inside res/layout folder. In this class declare the layout ImageView,ListView header  textView and ListView in frame layout.
...................................................................................................................................................................


<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/ImageView_logo"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:padding="20dp"
        android:background="@drawable/logo"
        android:scaleType="fitCenter" />

    <ListView
        android:id="@+id/listView_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="#9E9E9E"
        android:dividerHeight="1dp"
        android:scrollbars="none"></ListView>

    <TextView
        android:id="@+id/ListView_header"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#cc43a5"
        android:gravity="center_vertical"
        android:paddingLeft="10dp"
        android:text="Heading1"
        android:textColor="#72cc52"
        android:textSize="20sp"
        android:textStyle="bold" />
</FrameLayout>
 ..................................................................................................................................................................


Step 3-
MainActivity.java.

The Create Class inside src folder.Then Initialize listview and create array adapter,whic will be used in listview. then you add header layout via layout inflater calling by addHeaderView() method and implement AbsListView.onScrollListener interface in your class.

...................................................................................................................................................................
 package com.example.androidtestcode;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

private TextView ListView_header;
private ListView listView;
private View ImageView;

private View Viewholder;

private int MAX_ROWS = 20;

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

listView = (ListView) findViewById(R.id.listView_item);
ImageView = findViewById(R.id.ImageView_logo);
ListView_header = (TextView) findViewById(R.id.ListView_header);

/* Inflate list header layout */
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View listHeader = inflater.inflate(R.layout.list_header, null);
Viewholder = listHeader.findViewById(R.id.Viewholder);

/* Add list view header */
listView.addHeaderView(listHeader);

/* Handle list View scroll events */
listView.setOnScrollListener(new AbsListView.OnScrollListener() {

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {

/* Check if the first item is already reached to top. */

if(listView.getFirstVisiblePosition() == 0) {

View firstChild = listView.getChildAt(0);
int topY = 0;
if (firstChild != null) {
topY = firstChild.getTop();
}

int ViewTopY = Viewholder.getTop();
ListView_header.setY(Math.max(0, ViewTopY + topY));

/*
* Set the image to scroll half of the amount that of
* ListView
*/

ImageView.setY(topY * 0.5f);
}
}
});

/* Populate the ListView with sample data */
List<String> modelList = new ArrayList<>();
for (int i = 0; i < MAX_ROWS; i++) {
modelList.add("List item " + i);
}

ArrayAdapter adapter = new ArrayAdapter(this, R.layout.list_item, modelList);
listView.setAdapter(adapter);
}
}
...................................................................................................................................................................



you can watch the video click here.
https://www.youtube.com/watch?v=rcDWjRdYaSM




try this code


Read more