Monday, February 17, 2014

[Android] Make android button transparent.

You can make android button transparent by inserting following code in layout xml file.

android:background="@android:color/transparent"

For example, I used it in one of my button as following.

<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Who is computer Geek?"
        android:background="@android:color/transparent" />

Sunday, February 16, 2014

CSS Basics

Making bold the whole paragraph.

<p style="font-weight: bold;">This is a bold paragraph.</p>

Making a word italic.

This is the <span style="font-style:italic;">greatest</span> web page I’ve made yet.

Using an internal style sheet.

<!doctype html>
<html>
<head>
<title>Using an internal style sheet.</title>
<style type=”text/css”>
div {
      font-weight: bold;
}
span {
      font-style: italic;
}
</style>
</head>
<body>

            <div>This is my web page.</div>
</body>
</html>

Using an external style sheet.

<link rel="stylesheet" type="text/css" href="path_to_style_sheet/style.css">

HTML Basics

Basic HTML Page


<!doctype html>
<html>
    <head>
           <title>Basic HTML Web Page Template</title>
    </head>
    <body>
           <div>Web Page Content</div>
    </body>
</html>

Creating unordered list.

<ul>
       <li>Pine</li>
       <li>Oak</li>
       <li>Elm</li>

</ul>

Creating ordered list.

<ol>
       <li>Pine</li>
       <li>Oak</li>
       <li>Elm</li>

</ol>

Creating a table.

<table>
    <tr>
          <th>Airport Code</th>
          <th>Common Name/City</th>

    </tr>
    <tr>
          <td>CWA</td>
          <td>Central Wisconsin Airport</td>
    </tr>
    <tr>
          <td>ORD</td>
          <td>Chicago O’Hare</td>
    </tr>
    <tr>
          <td>LHR</td>
          <td>London Heathrow</td>
    </tr>
</table>

Linking to another page.

<p>Link to my <a href="http://37.139.31.138/">server home page.</a></p>   

Adding Images to webpage.

<img src="path_to_image.png" alt="image description">


Friday, January 17, 2014

Threading and Asynchronous Processing In Android OS - Part 1

Some tasks in application development require threading and asynchronous processing due to complexity of the task. For example networking, complex calculations, querying data over the network, parsing data, accessing remote database require threading. Unless Android Operating system decide that application is not responding and throw Application Not Responding (ANR) event.

There are three ways to manage complex and time consuming processes. They are,
  • By using AsynTask helper class.
  • By using standard Thread class.
  • Or using the Loader class.
AsyncTask Helper Class

Create subclass of AsyncTask and implement the appropriate callback method from below list.

  • onPreExecute() - This method runs on the UI thread before beginning of background process.
  • doInBackground() - This method runs where real task is taken place. For example if you want to perform complex task, you can do inside this method.
  • publishProgress() - This method called from the doInBackground() method. This will inform UI thread about the background process progress. This method sends information to the UI process.
  • onProgressUpdate() - This method runs on the UI thread whenever the doInBackground() method calls publishProgress(). Use this method to update the progressBar.
  • onPostExecute() - Runs on the UI thread after doInBackground() method completes its operation.
Sample class,

public class SimpleAsyncActivity extends Activity {
      @Override
      public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.main);
            CountingTask tsk = new CountingTask();
            tsk.execute();
       }

       private class CountingTask extends AsyncTask<Void, Integer, Integer> {
             CountingTask() {}
            @Override
            protected Integer doInBackground(Void... unused) {
                    int i = 0;                    while (i < 100) {
                            SystemClock.sleep(250);
                            i++;
                            if (i % 5 == 0) {
                                   // update UI with progress every 5%
                                   publishProgress(i);
                            }
                   }
                   return i;
          }

          protected void onProgressUpdate(Integer... progress) {
                   TextView tv = (TextView) findViewById(R.id.counter);
                    tv.setText(progress[0] + "% Complete!");
          }

          protected void onPostExecute(Integer result) {
                   TextView tv = (TextView) findViewById(R.id.counter);
                    tv.setText("Count Complete! Counted to " + result.toString());
           }
     }
}

Sunday, January 5, 2014

[Android] Display Yes/No Alert box using java.

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("Do this action");
builder.setMessage("do you want confirm this action?");

builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int which) {
        // Do do my action here

        dialog.dismiss();
    }

});

builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        // I do not need any action here you might
        dialog.dismiss();
    }
});

AlertDialog alert = builder.create();
alert.show();

In the following line, remember to specify your class.
AlertDialog.Builder builder = new AlertDialog.Builder(this);

For example it should change like this.
AlertDialog.Builder builder = new AlertDialog.Builder(ClassName.this);

Wednesday, January 1, 2014

[Android] How to restart service?

This is two lines of codes, I use to restart my Service class. First line will stop my service. Second line will start the service again.

stopService(new Intent(this, YourService.class));
startService(new Intent(this, YourService.class));

Tuesday, December 31, 2013

[Android] Show AlertDialog with EditText.

public void showDialogWithEditText() {
  AlertDialog.Builder alert = new AlertDialog.Builder(this);

  alert.setTitle("Server Message");
  alert.setMessage("Set Server Message");

  // Set an EditText view to get user input
  final EditText input = new EditText(this);
  alert.setView(input);

  alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int whichButton) {
    String value = input.getText().toString();
    
   }
  });

  alert.setNegativeButton("Cancel",
    new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int whichButton) {
      // Canceled.
     }
    });

  alert.show();
  
 }
This method will show alertdialog with EditText.You can use "value" variable as required.