To clear EditText when a Button is Clicked

Well we usually don’t have a cross sign in an android to delete the mistype keyword when usually we type fast or we are in rush. But In our Android App we definitely can have a customized button which does the task for us.

My Assumption:
1. I have a EditText called firstName, where i mistype and want to delete it at once.
2. I create a clear button with its ID clearsearchSubmit.
3. I set OnClickListener in the clear Button.
4. So when Button is Clicked, it Checks the ID if it matches then we set the text of firstName as null i.e (“”)

firstName = (EditText) findViewById(R.id.firstName);
clear = (Button) findViewById(R.id.clearsearchSubmit);
clear.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) {  

// TODO Auto-generated method stub  

if (v.getId() == R.id.clearsearchSubmit); firstName.setText(“”); } });


This will help to clear the wrong keywords that you have typed in so instead of pressing backspace again and again you can simply click the button to clear everything.It Worked For me. Hope It Helps! Cheers!!

XML Resources in Android, using XmlResourceParser

Well i’m kinda new to android and i’m learning everything that i ca find about android app development so just yesterday i realised that i havent written any post in my blog lately so why not write about android??? well now i’m writing everything that i learn from no onwards as far as possible. well if i have a time i will do post it for sure.

So today lets start with XML parsing using XmlResourceParser. so i’ve divided it in steps and here it goes:

STEP 1. CREATE A LAYOUT WITH XML
Create an Xml file named sample.xml in a folder named xml(you can name your xml filename and folder name as you like for me its res/xml/sample.xml)

CODE:
=================================================================

<?xml version=”1.0″ encoding=”UTF-8″?>
<school>
    <class1>beginners</class1>
    <class2>beginners+</class2>
    <class9>Young</class9>
    <class12>Adult</class12>
</school>

=================================================================

well the getDepth() or the counting of XML is like this:

 <!– outside –>     0
 <root>                  1
   sometext                 1
     <foobar>         2
     </foobar>        2
 </root>              1
 <!– outside –>     0
=================================
Just dont get confused you will be clear soon.

STEP 2. NOW CREATE A LAYOUT
Now We need to create a Layout where we want to display our output. For this i have just created a header and a TextView.

CODE:
=================================================================

<TextView
        android:id=”@+id/first”
        android:layout_width=”fill_parent”
        android:layout_height=”wrap_content”
        android:text=”@string/hello_world” />

<TextView
android:id=”@+id/second”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:layout_below=”@+id/first” />

=================================================================

STEP 3: Write code in your MainActivity.java file
=================================================================

package com.ashim.xmlparseexample_1;

import java.io.IOException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.os.Bundle;
import android.app.Activity;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView displayXmlHere =(TextView)findViewById(R.id.second); //make TextView Reference as displayXmlHere
String storeHere; //make a string reference as storeHere
try{
storeHere=fromAnXml(this); //call a method fromAnXml and store everything returned to storeHere
displayXmlHere.setText(storeHere); //display everything that is in storeHere
}catch(XmlPullParserException e){ //if an Xml pulls some Parser exception
e.printStackTrace();
}catch(IOException e){ //if an IO exception shows up
e.printStackTrace();
}
}
public String fromAnXml(Activity a) throws XmlPullParserException, IOException{
StringBuffer buffer = new StringBuffer(); //buffer as a stringBuffer
Resources res =a.getResources(); //Return a Resources instance for your application’s package.
XmlResourceParser xpp =res.getXml(R.xml.sample); //Return an XmlResourceParser for the given resource ID.
xpp.next(); //Get next parsing event

int EventType = xpp.getEventType(); //returns the type of the current Events
System.out.println(“TOTAL EventType:”+EventType); //This Prints the initial value of EventType. We can see it in the LogCat to know how it works

while(EventType !=XmlPullParser.END_DOCUMENT){//check if the value of EventType is equal or not with The END_DOCUMENT if not enter inside the loop
if(EventType==XmlPullParser.START_DOCUMENT)
{
buffer.append(“START OF AN XMLn”);
}
if(EventType == XmlPullParser.START_TAG){
buffer.append(“nSTART_TAG: “+xpp.getName());
}
if(EventType == XmlPullParser.END_TAG){
buffer.append(“nEND_TAG: “+xpp.getName());
}
if(EventType ==XmlPullParser.TEXT){
buffer.append(“nText: “+xpp.getText());
}
System.out.println(“VALUE OF EventType:”+EventType);
EventType=xpp.next();
}
buffer.append(“nnEND OF AN XML”);
return buffer.toString(); //returns the StringBuffer i.e “buffer” to the above calling method
}

}

=================================================================
So This is IT. just try it once if u face any problem just comment below or ask Google Uncle. 😛