Java Android Program to Demonstrate Alert Dialog Box
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
Button but = (Button) findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Create an alert dialog box
[Link] builder = new [Link](
[Link]);
// Set alert title
[Link]([Link]);
// Set the value for the positive reaction from the user
// You can also set a listener to call when it is pressed
[Link]([Link], null);
// The message
[Link]([Link]);
// Create the alert dialog and display it
AlertDialog theAlertDialog = [Link]();
[Link]();
});
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate([Link], menu);
return true;
}
[Link]
<RelativeLayout xmlns:android="[Link]
xmlns:tools="[Link]
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=".MainActivity" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="SHOW DIALOG" />
</RelativeLayout>
Output:
Create a Simple Application Which Shows Life Cycle of Activity.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="[Link]">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</[Link]>
[Link]
package [Link].activity_life;
import [Link];
import [Link];
import [Link];
import [Link];
@SuppressLint("NewApi")
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
notify("onCreate");
@Override
protected void onPause() {
[Link]();
notify("onPause");
@Override
protected void onResume() {
[Link]();
notify("onResume");
@Override
protected void onStop() {
[Link]();
notify("onStop");
@Override
protected void onDestroy() {
[Link]();
notify("onDestroy");
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
[Link](savedInstanceState);
notify("onRestoreInstanceState");
@Override
protected void onSaveInstanceState(Bundle outState) {
[Link](outState);
notify("onSaveInstanceState");
private void notify(String methodName) {
String name = [Link]().getName();
String[] strings = [Link]("\\.");
[Link](getApplicationContext(),
methodName + "" + strings[[Link] - 1],
Toast.LENGTH_LONG).show();
Output:
Create the simple calculator shown below also perform appropriate operation
[Link]
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#8BC34A"
android:backgroundTint="@android:color/darker_gray"
tools:context=".MainActivity">
<!-- Text View to display "gfg_myFirstApp "-->
<TextView
android:id="@+id/textView"
android:layout_width="133dp"
android:layout_height="28dp"
android:layout_marginStart="139dp"
android:layout_marginLeft="139dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="139dp"
android:layout_marginRight="139dp"
android:layout_marginBottom="559dp"
<!-- providing the green colour to the background -->
android:background="#0F9D58"
android:text="gfg_myFirstApp"
android:textAppearance="@style/[Link]"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- Text View to display our basic heading of "calculator"-->
<TextView
android:layout_width="194dp"
android:layout_height="43dp"
android:layout_marginStart="114dp"
android:layout_marginLeft="114dp"
android:layout_marginTop="58dp"
android:layout_marginEnd="103dp"
android:layout_marginRight="103dp"
android:layout_marginBottom="502dp"
android:scrollbarSize="30dp"
android:text=" Calculator"
android:textAppearance="@style/[Link].Body1"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- Edit Text View to input the values -->
<EditText
android:id="@+id/num1"
android:layout_width="364dp"
android:layout_height="28dp"
android:layout_marginStart="72dp"
android:layout_marginTop="70dp"
android:layout_marginEnd="71dp"
android:layout_marginBottom="416dp"
android:background="@android:color/white"
android:ems="10"
android:hint="Number1(0)"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- Edit Text View to input 2nd value-->
<EditText
android:id="@+id/num2"
android:layout_width="363dp"
android:layout_height="30dp"
android:layout_marginStart="72dp"
android:layout_marginTop="112dp"
android:layout_marginEnd="71dp"
android:layout_marginBottom="374dp"
android:background="@android:color/white"
android:ems="10"
android:hint="number2(0)"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- Text View to display result -->
<TextView
android:id="@+id/result"
android:layout_width="356dp"
android:layout_height="71dp"
android:layout_marginStart="41dp"
android:layout_marginTop="151dp"
android:layout_marginEnd="48dp"
android:layout_marginBottom="287dp"
android:background="@android:color/white"
android:text="result"
android:textColorLink="#673AB7"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- A button to perform 'sum' operation -->
<Button
android:id="@+id/sum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="292dp"
android:layout_marginEnd="307dp"
android:layout_marginBottom="263dp"
android:backgroundTint="@android:color/holo_red_light"
android:onClick="doSum"
android:text="+"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- A button to perform subtraction operation. -->
<Button
android:id="@+id/sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="210dp"
android:layout_marginTop="292dp"
android:layout_marginEnd="113dp"
android:layout_marginBottom="263dp"
android:backgroundTint="@android:color/holo_red_light"
android:onClick="doSub"
android:text="-"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- A button to perform division. -->
<Button
android:id="@+id/div"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="307dp"
android:layout_marginTop="292dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="263dp"
android:backgroundTint="@android:color/holo_red_light"
android:onClick="doDiv"
android:text="/"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- A button to perform multiplication. -->
<Button
android:id="@+id/mul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="356dp"
android:layout_marginEnd="307dp"
android:layout_marginBottom="199dp"
android:backgroundTint="@android:color/holo_red_light"
android:onClick="doMul"
android:text="x"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- A button to perform a modulus function. -->
<Button
android:id="@+id/button"
android:layout_width="92dp"
android:layout_height="48dp"
android:layout_marginStart="113dp"
android:layout_marginTop="356dp"
android:layout_marginEnd="206dp"
android:layout_marginBottom="199dp"
android:backgroundTint="@android:color/holo_red_light"
android:onClick="doMod"
android:text="%(mod)"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- A button to perform a power function. -->
<Button
android:id="@+id/pow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="113dp"
android:layout_marginTop="292dp"
android:layout_marginEnd="210dp"
android:layout_marginBottom="263dp"
android:backgroundTint="@android:color/holo_red_light"
android:onClick="doPow"
android:text="n1^n2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</[Link]>
[Link]
package [Link].gfg_my_first_app;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
EditText e1, e2;
TextView t1;
int num1, num2;
@Override
protected void onCreate(Bundle savedInstanceState)
{
[Link](savedInstanceState);
setContentView([Link].activity_main);
}
// a public method to get the input numbers
public boolean getNumbers()
{
// defining the edit text 1 to e1
e1 = (EditText)findViewById([Link].num1);
// defining the edit text 2 to e2
e2 = (EditText)findViewById([Link].num2);
// defining the text view to t1
t1 = (TextView)findViewById([Link]);
// taking input from text box 1
s1 = [Link]().toString();
// taking input from text box 2
s2 = [Link]().toString();
// condition to check if box is not empty
if (([Link](null) && [Link](null))
|| ([Link]("") && [Link](""))) {
String result = "Please enter a value";
[Link](result);
return false;
}
else {
// converting string to int.
num1 = [Link]([Link]().toString());
// converting string to int.
num2 = [Link]([Link]().toString());
}
return true;
}
// a public method to perform addition
public void doSum(View v)
{
// get the input numbers
if (getNumbers()) {
int sum = num1 + num2;
[Link]([Link](sum));
}
}
// a public method to perform power function
public void doPow(View v)
{
// get the input numbers
if (getNumbers()) {
double sum = [Link](num1, num2);
[Link]([Link](sum));
}
}
// a public method to perform subtraction
public void doSub(View v)
{
// get the input numbers
if (getNumbers()) {
int sum = num1 - num2;
[Link]([Link](sum));
}
}
// a public method to perform multiplication
public void doMul(View v)
{
// get the input numbers
if (getNumbers()) {
int sum = num1 * num2;
[Link]([Link](sum));
}
}
// a public method to perform Division
public void doDiv(View v)
{
// get the input numbers
if (getNumbers()) {
// displaying the text in text view assigned as t1
double sum = num1 / (num2 * 1.0);
[Link]([Link](sum));
}
}
// a public method to perform modulus function
public void doMod(View v)
{
// get the input numbers
if (getNumbers()) {
double sum = num1 % num2;
[Link]([Link](sum));
}
}
Output:
Performing the addition operation.
Java Android Program to demonstrate login form with validation.
[Link].
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends Activity {
Button b1,b2;
EditText ed1,ed2;
TextView tx1;
int counter = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
b1 = (Button)findViewById([Link]);
ed1 = (EditText)findViewById([Link]);
ed2 = (EditText)findViewById([Link].editText2);
b2 = (Button)findViewById([Link].button2);
tx1 = (TextView)findViewById([Link].textView3);
[Link]([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
if([Link]().toString().equals("admin") &&
[Link]().toString().equals("admin")) {
[Link](getApplicationContext(),
"Redirecting...",Toast.LENGTH_SHORT).show();
}else{
[Link](getApplicationContext(), "Wrong
Credentials",Toast.LENGTH_SHORT).show();
[Link]([Link]);
[Link]([Link]);
counter--;
[Link]([Link](counter));
if (counter == 0) {
[Link](false);
});
[Link](new [Link]() {
@Override
public void onClick(View v) {
finish();
});
activity_main.xml.
<?xml version = "1.0" encoding = "utf-8"?>
<RelativeLayout xmlns:android = "[Link]
xmlns:tools = "[Link] android:layout_width="match_parent"
android:layout_height = "match_parent" android:paddingLeft=
"@dimen/activity_horizontal_margin"
android:paddingRight = "@dimen/activity_horizontal_margin"
android:paddingTop = "@dimen/activity_vertical_margin"
android:paddingBottom = "@dimen/activity_vertical_margin" tools:context = ".MainActivity">
<TextView android:text = "Login" android:layout_width="wrap_content"
android:layout_height = "wrap_content"
android:id = "@+id/textview"
android:textSize = "35dp"
android:layout_alignParentTop = "true"
android:layout_centerHorizontal = "true" />
<TextView
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "Tutorials point"
android:id = "@+id/textView"
android:layout_below = "@+id/textview"
android:layout_centerHorizontal = "true"
android:textColor = "#ff7aff24"
android:textSize = "35dp" />
<EditText
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:id = "@+id/editText"
android:hint = "Enter Name"
android:focusable = "true"
android:textColorHighlight = "#ff7eff15"
android:textColorHint = "#ffff25e6"
android:layout_marginTop = "46dp"
android:layout_below = "@+id/imageView"
android:layout_alignParentLeft = "true"
android:layout_alignParentStart = "true"
android:layout_alignParentRight = "true"
android:layout_alignParentEnd = "true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/abc"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/editText2"
android:layout_below="@+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/editText"
android:layout_alignEnd="@+id/editText"
android:textColorHint="#ffff299f"
android:hint="Password" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Attempts Left:"
android:id="@+id/textView2"
android:layout_below="@+id/editText2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="25dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView3"
android:layout_alignTop="@+id/textView2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignBottom="@+id/textView2"
android:layout_toEndOf="@+id/textview"
android:textSize="25dp"
android:layout_toRightOf="@+id/textview" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="login"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/textview"
android:layout_toStartOf="@+id/textview" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="@+id/button2"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/textview"
android:layout_toEndOf="@+id/textview" />
</RelativeLayout>
Construct image switcher using setFactory().
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="[Link]">
<[Link]
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/[Link]">
<[Link]
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/[Link]" />
</[Link]>
<include layout="@layout/content_main" />
</[Link]>
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
ImageSwitcher imageSwitcher;
Button nextButton;
int imageSwitcherImages[] =
{[Link], [Link].c_sarp, [Link], [Link], [Link]};
int switcherImageLength = [Link];
int counter = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
Toolbar toolbar = (Toolbar) findViewById([Link]);
setSupportActionBar(toolbar);
imageSwitcher = (ImageSwitcher) findViewById([Link]);
nextButton = (Button) findViewById([Link]);
[Link](new [Link]() {
@Override
public View makeView() {
ImageView switcherImageView = new ImageView(getApplicationContext());
[Link](new [Link](
[Link].FILL_PARENT, [Link].FILL_PARENT
));
[Link]([Link].FIT_CENTER);
[Link]([Link]);
//[Link](100);
return switcherImageView;
});
Animation aniOut = [Link](this, [Link].slide_out_right);
Animation aniIn = [Link](this, [Link].slide_in_left);
[Link](aniOut);
[Link](aniIn);
[Link](new [Link]() {
@Override
public void onClick(View v) {
counter++;
if (counter == switcherImageLength){
counter = 0;
[Link](imageSwitcherImages[counter]);
else{
[Link](imageSwitcherImages[counter]);
});
}
Output