Tuesday, 10 December 2013

Web View

WebView is a view which can display web pages in your activity.

Create a webview in layout file.

<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="200dp" />

The button created in the below image is of no use.


Make the changes in the Activity file.

package com.codingredefined.webview;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
WebView mywebView;
mywebView=(WebView)findViewById(R.id.webview);
mywebView.getSettings().setJavaScriptEnabled(true);
mywebView.loadUrl("http://www.google.com");
mywebView.setWebViewClient(new WebViewClient());
mywebView.setInitialScale(1);
mywebView.getSettings().setUseWideViewPort(true);
final Activity MyActivity=this;
mywebView.setWebChromeClient(new WebChromeClient()
{
public void onProgressChanged(WebView view,int progress)
{
MyActivity.setTitle("Loading...");
MyActivity.setProgress(progress*100);
if(progress==100)
{
MyActivity.setTitle("WebView");
}
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}



This will be the output.


Friday, 6 December 2013

Progress Bar

Now these are the steps to create a Progress Bar in your app...

Create a Button and a Progress Bar in main layout file.

<TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Progress Bar" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="60dp"
        android:onClick="startProgress"
        android:text="Start Progress Bar" />

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="30dp"
        android:indeterminate="false"
        android:max="10"
        android:padding="4dp" />


Code your activity file.

package com.codingredefined.progressbar;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ProgressBar;

public class MainActivity extends Activity
{
private Handler handler;
private ProgressBar progress;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progress=(ProgressBar)findViewById(R.id.progressBar1);
handler=new Handler();
}

public void startProgress(View view)
{
Runnable runnable=new Runnable()
{
@Override
public void run()
{
for(int i=0;i<=10;i++)
{
final int value=i;
try
{
Thread.sleep(1500);
}
catch(Exception e)
{}
handler.post(new Runnable()
{
@Override
public void run()
{
progress.setProgress(value);
}
});
}
}
};
new Thread(runnable).start();
}

}



The following output will be obtained.


Custom Dialog

So here we are with a simple method to create a own designed dialog box.

Create a button to activate the dialog box.

<TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Click Button"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/buttonShowCustomDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:text="Button" />


Now create a new layout file named "custom_dialog.xml" to design your dialog box.

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="5dp"
    />

<TextView
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#DC143C"
    android:layout_toRightOf="@+id/image"
    />
   
<Button
    android:id="@+id/dialogButtonOK"
    android:layout_width="100px"
    android:layout_height="wrap_content"
    android:text="OK"
    android:layout_marginTop="5dp"
    android:layout_marginRight="5dp"
    android:layout_below="@+id/image"
    />


Now make changes in the main activity file.

package com.codingredefined.customdialog;

import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity
{
Button button;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
button=(Button)findViewById(R.id.buttonShowCustomDialog);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
final Dialog dialog=new Dialog(context);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");
TextView text=(TextView)dialog.findViewById(R.id.text);
text.setText("Android Custom Dialog");
ImageView image=(ImageView)dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
Button dialogButton=(Button)dialog.findViewById(R.id.dialogButtonOK);
dialogButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
dialog.dismiss();
}
});
dialog.show();
}
});
}

}


Following output will be obtained.