0% found this document useful (0 votes)
52 views5 pages

Factorialapp: Mainactivity - Java

This document contains code for an Android application that calculates factorials. It includes the MainActivity Java class, which handles button clicks to calculate and display the factorial of a user-entered number. It also includes the activity_main XML layout file, which defines the user interface elements like text views, edit texts and a button. Finally, it includes the strings.xml file which contains text strings used in the application.

Uploaded by

Jewel
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)
52 views5 pages

Factorialapp: Mainactivity - Java

This document contains code for an Android application that calculates factorials. It includes the MainActivity Java class, which handles button clicks to calculate and display the factorial of a user-entered number. It also includes the activity_main XML layout file, which defines the user interface elements like text views, edit texts and a button. Finally, it includes the strings.xml file which contains text strings used in the application.

Uploaded by

Jewel
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
You are on page 1/ 5

Factorialapp

MainActivity.java

package com.example.factorialapp;

import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

public class MainActivity extends Activity {

EditText t1,t2;

Button b1;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

t1=(EditText)findViewById(R.id.editText1);
t2=(EditText)findViewById(R.id.editText2);

b1=(Button)findViewById(R.id.button1);

b1.setOnClickListener(new OnClickListener() {

public void onClick(View v)

double fact=1.0,n,i;

n=Double.parseDouble(t1.getText().toString());

for(i=1.0;i<=n;i++)

fact=fact*i;

t2.setText(Double.toString(fact));

});

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

activity_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: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.factorialapp.MainActivity" >

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/heading" />

<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/editText1"
android:layout_below="@+id/textView3"
android:layout_marginRight="20dp"
android:layout_marginTop="27dp"
android:ems="10" />

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="18dp"
android:ems="10" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/editText2"
android:layout_below="@+id/editText2"
android:layout_marginRight="43dp"
android:layout_marginTop="14dp"
android:text="@string/bttn" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="22dp"
android:text="@string/fno" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="22dp"
android:text="@string/secno" />

</RelativeLayout>

String.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">Factorialapp</string>
<string name="heading">FACTORIAL</string>
<string name="fno">Enter a number</string>
<string name="bttn">SUBMITT</string>
<string name="secno">Factorial is</string>
<string name="action_settings">Settings</string>

</resources>

You might also like