0% found this document useful (0 votes)
22 views4 pages

Captcha - Mobile Programming

The document contains code for an Android application that generates a random captcha code when a text view is clicked. It displays the code in a second text view. A button is clicked to check if a user entered code in an edit text matches the generated code. If it matches, a welcome message is displayed. If not, the code is cleared and an error message is shown.

Uploaded by

MUKILAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views4 pages

Captcha - Mobile Programming

The document contains code for an Android application that generates a random captcha code when a text view is clicked. It displays the code in a second text view. A button is clicked to check if a user entered code in an edit text matches the generated code. If it matches, a welcome message is displayed. If not, the code is cleared and an error message is shown.

Uploaded by

MUKILAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

package com.example.

jahez1;

import java.util.Random;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {


TextView tv1,tv2;
EditText et1,et2;
Button b1;
int cap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView)findViewById(R.id.textView1);
tv2 = (TextView)findViewById(R.id.textView2);
tv1.setOnClickListener(new OnClickListener()
{

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Random rn = new Random();
cap = rn.nextInt();
tv2.setText(Integer.toString(cap));
}
});
et1 = (EditText)findViewById(R.id.editText2);

b1 = (Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int c = Integer.parseInt(et2.getText().toString());
Log.i("captcha entered", Integer.toString(c));
Log.i("captcha generated", Integer.toString(cap));
if(c != cap){
tv2.setText("");
Toast.makeText(MainActivity.this,"Enter correct captcha",
Toast.LENGTH_LONG).show();
}
else{
String u = et1.getText().toString();
Toast.makeText(MainActivity.this, "Welcome"+ u,
Toast.LENGTH_LONG).show();
}
}

});
}

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

Android_main.xml

<RelativeLayout 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:gravity="fill_vertical"
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=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_marginLeft="18dp"
android:text="@string/captcha" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="34dp"
android:layout_marginTop="172dp"
android:layout_toRightOf="@+id/textView1"
android:text="@string/hello_world" />

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="56dp"
android:text="@string/etcpt" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView1"
android:layout_alignLeft="@+id/textView1"
android:layout_marginBottom="49dp"
android:text="@string/un" />

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView3"
android:layout_alignBottom="@+id/textView3"
android:layout_toRightOf="@+id/textView1"
android:inputType="text"
android:ems="10" >

<requestFocus />
</EditText>

<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView4"
android:layout_alignLeft="@+id/editText1"
android:ems="10"
android:inputType="text" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView4"
android:layout_centerHorizontal="true"
android:layout_marginTop="49dp"
android:text="@string/button" />

</RelativeLayout>

You might also like