Monday, 12 November 2018

Web View In Android

Hi All,

See this code for incorporating Webview.
First of all we have to place one Webview component in the activity. Then add this code in the onCreate() function.


protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.hupweblayout);
hupwebview=(WebView)findViewById(R.id.webView);
hupwebview.getSettings().setJavaScriptEnabled(true);
hupwebview.loadUrl("http://www.servetechnoresearch.com");

}

Add this line in the manifest file.

<uses-permission android:name="android.permission.INTERNET" />

Wednesday, 7 November 2018

Tabbed Activity in Android

Hi All,
Use Tabbed Activity for good user experience.



package com.example.tabdemo;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class MainActivity extends TabActivity {
TabHost tabhost;
       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              tabhost=(TabHost)findViewById(android.R.id.tabhost);
             
              TabSpec tab1=tabhost.newTabSpec("first activity");
              TabSpec tab2=tabhost.newTabSpec("second activity");
              TabSpec tab3=tabhost.newTabSpec("second activity");
             
              tab1.setIndicator("contact");
              tab1.setContent(new Intent(this,one.class));
             
              tab2.setIndicator("home");
              tab2.setContent(new Intent(this,two.class));
             
              tab3.setIndicator("list");
              tab3.setContent(new Intent(this,three.class));
             
              tabhost.addTab(tab1);
              tabhost.addTab(tab2);
              tabhost.addTab(tab3);
             
       }

       @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;
       }

       @Override
       public boolean onOptionsItemSelected(MenuItem item) {
              // Handle action bar item clicks here. The action bar will
              // automatically handle clicks on the Home/Up button, so long
              // as you specify a parent activity in AndroidManifest.xml.
              int id = item.getItemId();
              if (id == R.id.action_settings) {
                     return true;
              }
              return super.onOptionsItemSelected(item);
       }
}


------------------------------------------------------------------------------------------------------------------------------------------

Layout file


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.tabdemo.MainActivity"
    android:orientation="horizontal">

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </TabWidget>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                            </FrameLayout>
        </LinearLayout>
    </TabHost>

</LinearLayout>

Tuesday, 6 November 2018

Radio Button checking in Android

Hi All,
Please find the code below

private RadioGroup radioGroup1;
private RadioButton radioGenderButton;

public void HUP1() {

radioGroup1 = (RadioGroup) findViewById(R.id.radioGender);

        // get selected radio button from radioGroup
int selectedId = radioGroup1.getCheckedRadioButtonId();

// find the radiobutton by returned id
        radioGenderButton = (RadioButton) findViewById(selectedId);

Toast.makeText(MyAndroidAppActivity.this, radioGenderButton.getText(), Toast.LENGTH_SHORT).show();


  }




List view in Android

Hi all,

Use this for List View in Android.

Step1 :  Create a layout file and place a TextView (This is for a single item in Listview)

Step2 : Create a new Layout file and Java file. Place the List view in the Layout file and use the code below under section 2 for loading data into listview.
The first layout file is connected with the Listview in the same code.



Hi all,
To show data in listview from Android there are two steps




1. Create a Layout file with one TextView ,ie label box

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




<TextView

android:id="@+id/listviewtext"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="TextView" />




</LinearLayout>
-------------------------------------------------------------------------------------------------------------------------


2. Place one Button (For insert into db) and One ListView (To show data). After that reuse this code.

package com.example.hupnew;



import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {


public void HUP1()

{ String [] listitem;
int i=0;

ListView lstview=(ListView)findViewById(R.id.huplistview);
SQLiteDatabase hupdb;
hupdb=this.openOrCreateDatabase("imspect", MODE_PRIVATE, null);
hupdb.openDatabase("data/data/com.example.hupnew/databases/imspect", null, SQLiteDatabase.OPEN_READONLY);
String s="select * from STUD";
Cursor c=hupdb.rawQuery(s, null);
int count = c.getCount();
listitem=new String[count];
String val="";
if(c!=null)
{

if(c.moveToFirst())
{
do
{
String username=c.getString(0);
String abc=c.getString(1);

val=username + " " + abc;
//Toast.makeText(this, val, Toast.LENGTH_LONG).show();
listitem[i]=val;
i++;

}while(c.moveToNext());
}
}

//Toast.makeText(this, ""+listitem[4], Toast.LENGTH_LONG).show();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.hupviewlayout,R.id.huptext,listitem);
lstview.setAdapter(adapter);

}

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

hupdb=this.openOrCreateDatabase("imspect", MODE_PRIVATE, null);
hupdb.execSQL("CREATE TABLE IF NOT EXISTS STUD(name text,roll text,marks text)");
hupdb.execSQL("insert into STUD values('HUP','108','100')");
hupdb.execSQL("insert into STUD values('HUP','99','100')");
Toast.makeText(this, "Successfully inserted", Toast.LENGTH_LONG).show();
hupdb.close();
HUP1();
}

@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;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}



Android program to insert into SQLite DB

Hi All,
See the Android program to insert into SQLite DB.


public void hup(View abc)
{
  
    SQLiteDatabase hupdb;
    hupdb=this.openOrCreateDatabase("hup", MODE_PRIVATE, null);
    hupdb.execSQL("CREATE TABLE IF NOT EXISTS REGISTRATION(username text,password text)");
    hupdb.execSQL("insert into registration values('HUP','TAFTKTU')");
    Toast.makeText(this, "Data Inserted HUP", Toast.LENGTH_LONG).show();
    hupdb.close();    
}



NB: Here the values can be taken from text boxes using getText() function instead of 'HUP' and 'TAFTKTU'

Login logic in Android using SQLite DB

Hi All,
Use this code to impliment login logic in Android using SQLite
Use the below function 'HUP1(View view)' in the click of a Button.

public void HUP1(View view)

{
      
       SQLiteDatabasehupdb;
       hupdb=this.openOrCreateDatabase("hup", MODE_PRIVATE, null);
       hupdb.openDatabase("data/data/com.example.hupdb1/databases/hup", null, SQLiteDatabase.OPEN_READONLY);
       String s="select * from registration where username=’admin’ and password=’admin’";
       Cursor c=hupdb.rawQuery(s, null);
       if(c!=null)
       {
              if(c.moveToFirst())
              {
              do
              {
                     String username=c.getString(0);
                     String abc=c.getString(1);

                     //write Intent code here

              }while(c.moveToNext());
              }//if
}//if
//Write Toast message here showing invalid login

}