Wednesday, 20 June 2012

JUnit Documentation

JUnit Documentation

JUNIT

JUnit is a simple Java testing framework to write tests for you Java application.

1.1. Unit Testing

A unit test is a piece of code written by a developer that executes a specific functionality in the code under test. Unit tests ensure that code is working as intended and validate that this is still the case after code changes.

1.2. Unit Testing with JUnit

JUnit 4.x is a test framework which uses annotations to identify methods that are tests. JUnit assumes that all test methods can be executed in an arbitrary order. Therefore tests should not depend on other tests.
To write a test with JUnit
·         Annotate a method with @org.junit.Test
·         Use a method provided by JUnit to check the expected result of the code execution versus the actual result
You can use Eclipse or the class "org.junit.runner.JUnitCore" to run the test.

2. Installation of JUnit

If you use Eclipse you can use the integrated JUnit in Eclipse for your testing.
If you want to control the used JUnit library explicitly, download JUnit4.x.jar from the JUnit website at http://www.junit.org/ . The download contains the "junit-4.*.jar" which is the JUnit library. Add this library to your Java project and add it to the classpath.

3. Using JUnit

3.1. Preparation

Create a new project in ellipse. We want to create the unit tests in a separate folder. The creation of a separate folder for tests is not mandatory. But it is a good practice to keep the code separated from the regular code.
Create a new source folder test via right-clicking on your project, select "Properties" and choose the "Java Build Path". Select the "Source" tab.



Press "Add folder" then press "Create new folder". Create the folder "test".


Alternatively you can add a new source folder by right-clicking on a project and selecting New Source Folder.

3.2. Create a Java class

Right Click the "src" folder, create the following  java class.

public class Multiply {
            public int multiply (int x, int y) {
                        return x / y;
            }
}

 

 

 

3.3. Create a JUnit test

Right click on your new class in the Package Explorer and select New JUnit Test Case. Select "New JUnit 4 test" and set the source folder to "test", so that your test class gets created in this folder.



Press "Next" and select the methods which you want to test.





If the JUnit library in not part of your classpath, Eclipse will prompt you to do so.





Create a test with the following code.

import static org.junit.Assert.*;
import org.junit.Test;
public class MultiplyTest {
            @Test
            public void testMultiply() {
            Multiply tester = new Multiply();
            assertEquals("Result", 50, tester.multiply(10, 5));
}

}

3.4. Run your test via Eclipse

Right click on your new test class and select Run-As JUnit Test.
The result of the tests will be displayed in the JUnit View.



The test should be failing (indicated via a red bar).
This is because our multiplier class is currently not working correctly (it does a division instead of multiplication). Fix the bug and re-run test to get a green bar.
If you have several tests you can combine them into a test suite. Running a test suite will execute all tests in that suite.
To create a test suite, select your test classes right click on it New Other JUnit Test Suite.





Select "Next" and select the methods for which you want to create a test.
Change the code to the following to make your test suite run your test. If you develop another test later you can add it to @Suite.SuiteClasses.
 
package mypackage;
 
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
 
@RunWith(Suite.class)
@Suite.SuiteClasses( { MyClassTest.class })
public class AllTests {
}
 

Monday, 18 June 2012

Jclouds dependencies


Jclouds compendium for component context
Jclouds core for Contect builder

Saturday, 16 June 2012

Pwd validation and DOB enter in android

Pwd validation and DOB enter in android


package com.quiz;

import java.util.Calendar;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.format.DateFormat;
import android.text.format.Time;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class QuizSettingsActivity extends QuizActivity {
SharedPreferences mGameSettings;
static final int DATE_DIALOG_ID = 0;
static final int PASSWORD_DIALOG_ID = 1;
private int mYear;
    private int mMonth;
    private int mDay;
    private TextView dob;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
Spinner spinner = (Spinner) findViewById(R.id.Spinner_Gender);
   ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
           this, R.array.planets_array, android.R.layout.simple_spinner_item);
   adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
   spinner.setAdapter(adapter);
final EditText nick_name = (EditText) findViewById(R.id.nick_nameET);
TextView dob = (TextView)findViewById(R.id.dobstatusTV);
EditText email = (EditText) findViewById(R.id.emailET);
Button pwd_btn = (Button) findViewById(R.id.pwd);
Button dob_btn = (Button) findViewById(R.id.dob);
String strNickname = nick_name.getText().toString();
String strEmail = email.getText().toString();
/*Editor editor = mGameSettings.edit();
editor.putString("GAME_PREFERENCES_NICKNAME", strNickname);
editor.putString("GAME_PREFERENCES_EMAIL", strEmail);

editor.commit();*/
nick_name.setOnKeyListener(new View.OnKeyListener() {

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
String strNicknameToSave = nick_name.getText().toString();
Editor editor = mGameSettings.edit();
editor.putString("GAME_PREFERENCES_NICKNAME", strNicknameToSave);

editor.commit();
return true;
}
return false;
}
});
pwd_btn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(PASSWORD_DIALOG_ID);

}
});

dob_btn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

showDialog(DATE_DIALOG_ID);

}
});
final Calendar c = Calendar.getInstance();
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);

    // display the current date (this method is below)
    updateDisplay();

}
 
private void updateDisplay() {
TextView dob = (TextView)findViewById(R.id.dobstatusTV);
dob.setText(
           new StringBuilder()
                   // Month is 0 based so add 1
                   .append(mMonth + 1).append("-")
                   .append(mDay).append("-")
                   .append(mYear).append(" "));
}
private DatePickerDialog.OnDateSetListener mDateSetListener =
           new DatePickerDialog.OnDateSetListener() {

               public void onDateSet(DatePicker view, int year, 
                                     int monthOfYear, int dayOfMonth) {
                   mYear = year;
                   mMonth = monthOfYear;
                   mDay = dayOfMonth;
                   updateDisplay();
               }
           };

@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
                    mDateSetListener,
                    mYear, mMonth, mDay);
case PASSWORD_DIALOG_ID:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View layout = inflater.inflate(R.layout.password_dialog,
(ViewGroup) findViewById(R.id.root));
final EditText p1 = (EditText) layout
.findViewById(R.id.EditText_Pwd1);
final EditText p2 = (EditText) layout
.findViewById(R.id.EditText_Pwd2);
final TextView error = (TextView) layout
.findViewById(R.id.TextView_PwdProblem);
p2.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
String strPass1 = p1.getText().toString();
String strPass2 = p2.getText().toString();
if (strPass1.equals(strPass2)) {
error.setText("pwd_equal");
} else {
error.setText("pwd_not_equal");
}
}

// ... other required overrides need not be implemented
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layout);
// Now configure the AlertDialog
builder.setTitle("pwd setting button");
builder.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// We forcefully dismiss and remove the Dialog, so
// it
// cannot be used again (no cached info)
QuizSettingsActivity.this
.removeDialog(PASSWORD_DIALOG_ID);
}
});
builder.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
TextView passwordInfo = (TextView) findViewById(R.id.pwd_status_TV);
String strPassword1 = p1.getText().toString();
String strPassword2 = p2.getText().toString();
if (strPassword1.equals(strPassword2)) {
/*Editor editor = mGameSettings.edit();
editor.putString("GAME_PREFERENCES_PASSWORD",
strPassword1);
editor.commit();*/
passwordInfo.setText("pwd set");
} else {
//Log.d(DEBUG_TAG,
// "Passwords do not match. Not saving. Keeping old password (if set).");
}
// We forcefully dismiss and remove the Dialog, so
// it
// cannot be used again
//passwordInfo.setText("pwd not set");
QuizSettingsActivity.this
.removeDialog(PASSWORD_DIALOG_ID);
}
});
// Create the AlertDialog and return it
AlertDialog passwordDialog = builder.create();
return passwordDialog;
}
return null;
}

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
super.onPrepareDialog(id, dialog);
switch (id) {
case DATE_DIALOG_ID:
// Handle any DatePickerDialog initialization here
/* DatePickerDialog dateDialog = (DatePickerDialog) dialog;
int iDay,
iMonth,
iYear;
// Check for date of birth preference
if (mGameSettings.contains("GAME_PREFERENCES_DOB")) {
// Retrieve Birth date setting from preferences
long msBirthDate = mGameSettings.getLong("GAME_PREFERENCES_DOB",
0);
Time dateOfBirth = new Time();
dateOfBirth.set(msBirthDate);

iDay = dateOfBirth.monthDay;
iMonth = dateOfBirth.month;
iYear = dateOfBirth.year;
} else {
Calendar cal = Calendar.getInstance();
// Today's date fields
iDay = cal.get(Calendar.DAY_OF_MONTH);
iMonth = cal.get(Calendar.MONTH);
iYear = cal.get(Calendar.YEAR);
}
// Set the date in the DatePicker to the date of birth OR to the
// current date
dateDialog.updateDate(iYear, iMonth, iDay);
return;
case PASSWORD_DIALOG_ID:
// Handle any Password Dialog initialization here
// Since we don't want to show old password dialogs, just set new
// ones, we need not do anything here
// Because we are not "reusing" password dialogs once they have
// finished, but removing them from
// the Activity Dialog pool explicitly with removeDialog() and
// recreating them as needed.*/
return;
}
}

}

Settings.xml


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

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/a" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:text="@string/menu"
            android:textColor="@color/white" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/b" />
    </RelativeLayout>

    <ScrollView
        android:id="@+id/scrollview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scrollbars="vertical" >

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

            <TextView
                android:id="@+id/nick_nameTV"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Nick Name" />

            <EditText
                android:id="@+id/nick_nameET"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <requestFocus />
            </EditText>

            <TextView
                android:id="@+id/emailTV"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Email" />

            <EditText
                android:id="@+id/emailET"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <requestFocus />
            </EditText>

            <TextView
                android:id="@+id/passwordTV"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Password" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/pwd"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button" />

                <TextView
                    android:id="@+id/pwd_status_TV"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Password Status" />
            </LinearLayout>

            <TextView
                android:id="@+id/dobTV"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="DOB" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/dob"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button" />

                <TextView
                    android:id="@+id/dobstatusTV"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="DOB Status" />
            </LinearLayout>

            <TextView
                android:id="@+id/genderTV"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Gender" />

            <Spinner
                android:id="@+id/Spinner_Gender"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:prompt="@string/planet_prompt"
                 >
            </Spinner>
        </LinearLayout>
    </ScrollView>

</LinearLayout>



Read an xml file in android


Read an xml file in android


package com.quiz;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.content.res.XmlResourceParser;

import android.os.Bundle;
import android.util.Log;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class QuizScoresActivity extends QuizActivity {
    /** Called when the activity is first created. */
   @Override
  public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scores);
        TabHost host = (TabHost) findViewById(R.id.tabhost1);
        host.setup();
        TabSpec allscorestab = host.newTabSpec("alltab");
        allscorestab.setIndicator(getResources().getString(R.string.UserScores));
       allscorestab.setContent(R.id.scrollviewuser);
        host.addTab(allscorestab);
   
        TabSpec allscoresfriendstab = host.newTabSpec("allscoresfriend");
        allscoresfriendstab.setIndicator(getResources().getString(R.string.FriendScores));
        allscoresfriendstab.setContent(R.id.scrollviewfriends);
        host.addTab(allscoresfriendstab);
      //  host.setCurrentTabByTag("alltab");
        TableLayout allScoresTable = (TableLayout) findViewById(R.id.tablelayout1);
        TableLayout friendScoresTable = (TableLayout) findViewById(R.id.tablelayout2);
 

        initializeHeaderRow(allScoresTable);
        initializeHeaderRow(friendScoresTable);
     
        XmlResourceParser mockAllScores = getResources().getXml(R.xml.allscores);
        XmlResourceParser mockFriendScores = getResources().getXml(R.xml.friendscores);
        try {
            processScores(allScoresTable, mockAllScores);
            processScores(friendScoresTable, mockFriendScores);
        } catch (Exception e) {
         //   Log.e(DEBUG_TAG, "Failed to load scores", e);
        }
    }
 
 
   private void initializeHeaderRow(TableLayout scoreTable) {
       // Create the Table header row
       TableRow headerRow = new TableRow(this);
     //  int textColor = getResources().getColor(R.color.logo_color);
//       float textSize = getResources().getDimension(R.dimen.help_text_size);
       addTextToRowWithValues(headerRow, getResources().getString(R.string.username));
       addTextToRowWithValues(headerRow, getResources().getString(R.string.score));
       addTextToRowWithValues(headerRow, getResources().getString(R.string.rank));
       scoreTable.addView(headerRow);
   }
 
 
 
   private void processScores(final TableLayout scoreTable, XmlResourceParser scores) throws XmlPullParserException,
   IOException {
int eventType = -1;
boolean bFoundScores = false;
// Find Score records from XML
while (eventType != XmlResourceParser.END_DOCUMENT) {
   if (eventType == XmlResourceParser.START_TAG) {
       // Get the name of the tag (eg scores or score)
       String strName = scores.getName();
       if (strName.equals("score")) {
           bFoundScores = true;
           String scoreValue = scores.getAttributeValue(null, "score");
           String scoreRank = scores.getAttributeValue(null, "Rank");
           String scoreUserName = scores.getAttributeValue(null, "username");
           insertScoreRow(scoreTable, scoreValue, scoreRank, scoreUserName);
       }
   }
   eventType = scores.next();
}
 
 
if (bFoundScores == false) {
    final TableRow newRow = new TableRow(this);
    TextView noResults = new TextView(this);
    noResults.setText(getResources().getString(R.string.no_scores));
    newRow.addView(noResults);
    scoreTable.addView(newRow);
}
}
   private void insertScoreRow(final TableLayout scoreTable, String scoreValue, String scoreRank, String scoreUserName) {
       final TableRow newRow = new TableRow(this);
      // int textColor = getResources().getColor(R.color.title_color);
       //float textSize = getResources().getDimension(R.dimen.help_text_size);
       addTextToRowWithValues(newRow, scoreUserName);
       addTextToRowWithValues(newRow, scoreValue);
       addTextToRowWithValues(newRow, scoreRank);
       scoreTable.addView(newRow);
   }
 
   private void addTextToRowWithValues(final TableRow tableRow, String text) {
       TextView textView = new TextView(this);
   //    textView.setTextSize(textSize);
    //   textView.setTextColor(textColor);
       textView.setText(text);
       tableRow.addView(textView);
   }
 
 
}

Scores.xml

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

    
    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/a" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:text="@string/menu"
            android:textColor="@color/white" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/b" />
    </RelativeLayout>

    <TabHost
        
        android:id="@+id/tabhost1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

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

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

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

            <ScrollView
                android:id="@+id/scrollviewuser"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:scrollbars="vertical" >

                <TableLayout
                    android:id="@+id/tablelayout1"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:stretchColumns="*" >
                </TableLayout>
            </ScrollView>
            <ScrollView
                android:id="@+id/scrollviewfriends"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:scrollbars="vertical" >

                <TableLayout
                    android:id="@+id/tablelayout2"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:stretchColumns="*" >
                </TableLayout>
            </ScrollView>
        </FrameLayout>
        </LinearLayout>
     
    </TabHost>
   </LinearLayout>


Place this by creating xml folder in android

allscores.xml

<?xml version="1.0" encoding="utf-8"?>
<scores>
<score username="A" score="1234" Rank="1" />
<score username="B" score="1235" Rank="2" />
<score username="C" score="1236" Rank="3" />
<score username="D" score="1237" Rank="4" />
</scores>
    

Friendscores.xml

<?xml version="1.0" encoding="utf-8"?>
<scores>
<score username="E" score="1238" Rank="11" />
<score username="F" score="1239" Rank="12" />
<score username="G" score="1230" Rank="13" />
<score username="H" score="1245" Rank="14" />
</scores>
    


Read a text file present in raw folder in android


Read a text file present in raw folder in android



package com.quiz;

import java.io.IOException;

import java.io.InputStream;
import java.io.StringWriter;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import android.os.Bundle;
import android.widget.TextView;

public class QuizHelpActivity extends QuizActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.help);
        InputStream ios = getResources().openRawResource(R.raw.help);
        TextView tv = (TextView)findViewById(R.id.textViewhelp);
        String strfine = inputStreamToString(ios);
        tv.setText(strfine);
    }

private String inputStreamToString(InputStream ios) {
// TODO Auto-generated method stub
StringWriter writer = new StringWriter();
try {
IOUtils.copy(ios, writer);




} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String theString = writer.toString();

//String content = FileUtils.readFileToString(ios);

return theString;
}
}

Help.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/index1"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/a" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:text="@string/menu"
            android:textColor="@color/white" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/b" />
    </RelativeLayout>

    <TextView
        android:id="@+id/textViewhelp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@raw/help" />
    
    </LinearLayout>

Relative layout reusability code


Relative layout reusability code


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/index1"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/a" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:text="@string/menu"
            android:textColor="@color/white" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/b" />
    </RelativeLayout>

    <TextView
        android:id="@+id/textViewhelp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@raw/help" />
 
    </LinearLayout>

Alert Dialogue on launch example in android


Alert Dialogue on launch example in android


package com.textview;

import android.app.Activity;

import android.app.AlertDialog;
import android.os.Bundle;
import android.text.Html;

public class TextViewActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AlertDialog.Builder ab=new AlertDialog.Builder(TextViewActivity.this);
ab.setMessage(Html.fromHtml("<b><font color=#ff0000> Html View " +"</font></b><br>Androidpeople.com"));
ab.setPositiveButton("ok", null);
ab.show();
}
}

Main.xml


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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>



Adapter Example in Android

Adapter Example in Android


DisplayTo.java

package com.success;

public class DisplayTo {
private int id;
private String name;
public DisplayTo() {
super();
}
public DisplayTo(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

}


OrderAdapter.java


package com.success;

import java.util.List;

import android.R.layout;
import android.app.Activity;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class OrderAdapter extends BaseAdapter {
private Activity activity;
private List<DisplayTo> items;

public OrderAdapter(Activity activity,
List<DisplayTo> items) {
// TODO Auto-generated constructor stubi
this.activity = activity;
this.items = items;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return items.get(arg0);
}

@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}

@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if(convertView == null )
{
LayoutInflater inflater = activity.getLayoutInflater();
convertView = inflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.textView1);
holder.id = (TextView) convertView.findViewById(R.id.textView1);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
if (position < items.size()) {
final DisplayTo producName = items.get(position);

holder.name.setText(""+producName.getId());
holder.id.setText(producName.getName());
}
return convertView;
}



private static class ViewHolder {
TextView name, id;
}
}

SuccessActivity.java

package com.success;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TableLayout;

public class SuccessActivity extends Activity {
    /** Called when the activity is first created. */
private TableLayout tab1;
     private LinearLayout lv1;
//private LinearLayout lv1;
private LinearLayout lv2;
private ListView listv;
private static final String TAG = SuccessActivity.class
.getSimpleName();
private static List<DisplayTo> displaylist= new ArrayList<DisplayTo>();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         lv1 = (LinearLayout)findViewById(R.id.linear1);
         lv2 = (LinearLayout)findViewById(R.id.linear2);
         tab1 = (TableLayout)findViewById(R.id.table1);
          listv = (ListView)findViewById(R.id.listView1);
       // listv.setAdapter(new OrderAdapter(this ,displaylist));
        
        Button button1 =(Button)findViewById(R.id.button1);
        Button button2 = (Button) findViewById(R.id.button2);
        Button button3 = (Button) findViewById(R.id.button3);
       // ListView list = (ListView) findViewById(R.id.listView1);
        button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
tab1.setVisibility(View.VISIBLE);
lv2.setVisibility(View.GONE);
}
});
        button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Log.i(TAG,"button2");
tab1.setVisibility(View.GONE);
lv2.setVisibility(View.VISIBLE);
for(DisplayTo displayTo:displaylist)
{
Log.i(TAG,displayTo.getName());
}
listv.setAdapter(new OrderAdapter(SuccessActivity.this,displaylist));
}
});
        button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText et1 = (EditText)findViewById(R.id.editText1);
EditText et2 = (EditText) findViewById(R.id.editText2);
int id = Integer.parseInt(et1.getText().toString());
String name = et2.getText().toString();
System.out.println("text in ed1" + id);
                System.out.println("text in ed2" + name);
                displaylist.add(new DisplayTo(id,name));
                
}
});
        
        
        
        
    }
}

Layouts

list_items.xml


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

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

Main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <LinearLayout 
        android:id="@+id/linear1"
        android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal">

           <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button1" />
      
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button2" />
        
    </LinearLayout>
    <TableLayout
        android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:id="@+id/table1">
        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ID" />
            
            
            
            
            <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:inputType="textPersonName" >

            <requestFocus />
        </EditText>
        
            
            
            
        </TableRow>
        <TableRow android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            
            <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Name" />
            
            <EditText
            android:id="@+id/editText2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:inputType="textPersonName" >

            <requestFocus />
        </EditText>
            
                      
        </TableRow>
   
<TableRow android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
    
        
        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="submit" />
        
   </TableRow>
    </TableLayout>
    <LinearLayout
        android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:id="@+id/linear2" >
            <TextView
            android:id="@+id/android:empty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hi" />
    
    <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
        </ListView>
    
     </LinearLayout>
</LinearLayout>

Pass a HashMap from Angular Client to Spring boot API

This example is for the case where fileData is very huge and in json format   let map = new Map<string, string>()      map.set(this.ge...