Advertisement
Shailrshah

[Android] Square and Cube of a Number

Nov 4th, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1. ///////////////////////
  2. // MainActivity.java //
  3. //////////////////////
  4.  
  5. package com.example.squareandcube;
  6.  
  7. import android.support.v7.app.ActionBarActivity;
  8. import android.widget.*;
  9. import android.os.Bundle;
  10. import android.view.Menu;
  11. import android.view.MenuItem;
  12. import android.view.View;
  13.  
  14.  
  15. public class MainActivity extends ActionBarActivity {
  16.    
  17.     EditText ip;
  18.     Button b;
  19.     TextView op;
  20.  
  21.     @Override
  22.     protected void onCreate(Bundle savedInstanceState) {
  23.         super.onCreate(savedInstanceState);
  24.         setContentView(R.layout.activity_main);
  25.         ip = (EditText) findViewById(R.id.ip);
  26.         b = (Button) findViewById(R.id.button);
  27.         op = (TextView) findViewById(R.id.op);
  28.        
  29.         b.setOnClickListener(new View.OnClickListener() {
  30.            
  31.             @Override
  32.             public void onClick(View arg0) {
  33.                 // TODO Auto-generated method stub
  34.                 float n = Float.parseFloat(ip.getText().toString());
  35.                 op.setText("Square: "+String.valueOf(n*n)+"\nCube: "+String.valueOf(n*n*n));
  36.             }
  37.         });
  38.        
  39.     }
  40.  
  41.  
  42.     @Override
  43.     public boolean onCreateOptionsMenu(Menu menu) {
  44.         // Inflate the menu; this adds items to the action bar if it is present.
  45.         getMenuInflater().inflate(R.menu.main, menu);
  46.         return true;
  47.     }
  48.  
  49.     @Override
  50.     public boolean onOptionsItemSelected(MenuItem item) {
  51.         // Handle action bar item clicks here. The action bar will
  52.         // automatically handle clicks on the Home/Up button, so long
  53.         // as you specify a parent activity in AndroidManifest.xml.
  54.         int id = item.getItemId();
  55.         if (id == R.id.action_settings) {
  56.             return true;
  57.         }
  58.         return super.onOptionsItemSelected(item);
  59.     }
  60. }
  61.  
  62. ///////////////////////
  63. // activity_main.xml //
  64. //////////////////////
  65.  
  66. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  67.     xmlns:tools="http://schemas.android.com/tools"
  68.     android:layout_width="match_parent"
  69.     android:layout_height="match_parent"
  70.     android:paddingBottom="@dimen/activity_vertical_margin"
  71.     android:paddingLeft="@dimen/activity_horizontal_margin"
  72.     android:paddingRight="@dimen/activity_horizontal_margin"
  73.     android:paddingTop="@dimen/activity_vertical_margin"
  74.     tools:context="com.example.squareandcube.MainActivity" >
  75.  
  76.     <Button
  77.         android:id="@+id/button"
  78.         android:layout_width="wrap_content"
  79.         android:layout_height="wrap_content"
  80.         android:layout_centerHorizontal="true"
  81.         android:layout_centerVertical="true"
  82.         android:text="@string/abc_searchview_description_submit" />
  83.  
  84.     <TextView
  85.         android:id="@+id/op"
  86.         android:layout_width="wrap_content"
  87.         android:layout_height="wrap_content"
  88.         android:layout_above="@+id/button"
  89.         android:layout_alignLeft="@+id/button"
  90.         android:layout_marginBottom="79dp" />
  91.  
  92.     <EditText
  93.         android:id="@+id/ip"
  94.         android:layout_width="wrap_content"
  95.         android:layout_height="wrap_content"
  96.         android:layout_above="@+id/button"
  97.         android:layout_centerHorizontal="true"
  98.         android:ems="10"
  99.         android:hint="@string/input"
  100.         android:inputType="numberDecimal" >
  101.  
  102.         <requestFocus />
  103.     </EditText>
  104.  
  105. </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement