Search This Blog

Monday, November 30, 2009

Yahoo News Search_Part1(Activity & GUI)

I had been thinking to find an idea for my next application, something simple but kinda real and usefull at the same time.
and finally came up with this...News Search Application, it's simple...you enter what you are interested to find out more about and then the application will use Yahoo News Search WebService to see what can be found for you. how does it sound? sounds like fun to me....
so let's get down to it...
Our GUI will be so simple, a text box, a search button and a box to show the result of search , Like this :





for having that our layout file should be something like this :

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:gravity="center">


<EditText android:text=" "
android:id="@+id/EditText01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="400dip"
android:gravity="top"
android:minWidth="230dip"></EditText>

<Button android:text="@string/searchBtn"
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"></Button>

</LinearLayout>



<TextView android:text=" " android:id="@+id/TextView01"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_margin="10dip"
android:background="@color/white"
android:textColor="@color/black"
android:padding="5dip"
android:fadeScrollbars="false"
android:scrollbars="vertical" ></TextView>



</LinearLayout>



it's pretty simple and only thing that might seem a bit confusing are those gravity attributes of textbox and button, I know...
actually I had some struggles to align the textbox and button and finally that's what i came up with...I've always had some problems with designing GUIs though ;).


The next thing we would probably need to be worried about is how to handle network communication, i mean it's not a good idea to handle network communication inside our main activity since it will block our main thread, therefore we gotta have another thread
dealing with network and fetch the result of search using Yahoo WebService, but how should our main thread interact with this new thread... i reckon the best way would be like having the second thread running , then when user presses the search button we will send
the search criteria for that thread, it will then call the WebService and send the result back to our main activity where we can show that in the viewbox with appropriate format , but the whole interaction between our activity and the other thread gotta be Asynchronous, otherwise there is no point using separate thread. Thankfully Android has provided something very handy for Asynchronous thread interactions which is called Handler, the concept is pretty simple : a thread can define a Handler to recieve Asynchronous messages from other threads then each thread that want to send messages can get that Handler and send messages whenever appropriate without the first thread having to wait for the second one...
was it simple? ;) i did my best to put it in a simple way,although sometimes we need something more than words to define simplicity....
here is what our Activity class will look like...



package com.news.search;

import com.yahoo.search.NewsSearchResult;
import com.yahoo.search.xmlparser.XmlParserNewsSearchResults;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.method.ArrowKeyMovementMethod;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener{

private MyHandler handler = new MyHandler();
private FetcherThread thread;
private TextView view;
EditText field;


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

thread = new FetcherThread(this.handler);
thread.start();

this.view = (TextView)findViewById(R.id.TextView01);
this.view.setFocusable(true); //Remember to mention this
this.view.setFocusableInTouchMode(true);
this.view.setMovementMethod(ScrollingMovementMethod.getInstance());
this.field = (EditText)findViewById(R.id.EditText01);
findViewById(R.id.Button01).setOnClickListener(this);

}

@Override
public void onSaveInstanceState(Bundle instanceState){

}

@Override
public void onRestart(){
super.onRestart();
}

@Override
public void onStart(){
super.onStart();
}

@Override
public void onResume(){
super.onResume();

}

@Override
public void onPause(){
super.onPause();

}

@Override
public void onStop(){
super.onStop();

}

@Override
public void onDestroy(){
this.thread.cancelThread();
super.onDestroy();

}

@Override
public void onClick(View v) {
if(this.thread.lastRequest_finished){
this.thread.startOperation(this.field.getText().toString());
this.view.setText(" \n\nPLEASE WAIT...");
}
}


class MyHandler extends Handler {

public void handleMessage(Message msg){

Bundle bundle = msg.getData();
NewsSearchResult myResult[] = (NewsSearchResult[])bundle.getSerializable("result");

if(myResult == null || myResult.length == 0){
view.setText("\n *NO MATCH COULD BE FOUND* ");
return;
}

StringBuffer buffer = new StringBuffer();
for(NewsSearchResult temp : myResult){
buffer.append("TITLE :"+temp.getTitle()+"\n");
buffer.append("SUMMARY : "+temp.getSummary()+"\n\t----------\n");
}


view.setText(buffer.toString());
view.requestFocus();

}

}

}





although we dont need activity life cycle methods implemented for this application, i put them there purposely just to remind us the lifecycle of activities
sorry if it sounds a bit ridiculous , maybe i'm just a little bit paranoid about the activity lifecycle but i feel like we should not forget it...
any way...that's all we needed in our Activity class....it seemed to be more complicated at first, didn't it? ;)

you might be thinking about why i used setFocusable() and setMovementMethod(), well initially i did not , but i realized that i couldn't scroll the viewbox content
using arrow keys(at least using emulator, i'm not sure about real android devices though) so i added those three line of codes and got the problem solved.

i'm gonna talk about FetcherThread in my next post and you will see how smoothly these two threads are working together without any blocking problem.

No comments: