Вы находитесь на странице: 1из 10

Android APP

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hp.myapplication">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
<activity android:name=".Main2Activity" />
<activity android:name=".Main3Activity" />
</application>

</manifest>
Three Activities in the src directoy:
MainActivity.java
package com.example.hp.myapplication;

import android.content.Intent;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{


TextView tv;
Button but, but1, but2,but3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView);
but = (Button) findViewById(R.id.button);
but1 = (Button) findViewById(R.id.button1);
but2 = (Button) findViewById(R.id.button2);
but3 = (Button) findViewById(R.id.button3);
but.setOnClickListener(this);
but1.setOnClickListener(this);
but2.setOnClickListener(this);
but3.setOnClickListener(this);
Thread t = new Thread(){
@Override
public void run(){
try{
while(!isInterrupted()){
Thread.sleep(1000);
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView tdate = (TextView) findViewById(R.id.date);
long date = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd
yyyy\nhh:mm:ss a");
String dateString = sdf.format(date);
tdate.setText(dateString);
}
});
}
}catch (InterruptedException e){
}
}
};
t.start();
}

@Override
public void onClick(View view) {
if (view.getId()==R.id.button){
tv.setText(R.string.textView_text1);
}
else if (view.getId()==R.id.button1){
tv.setText(R.string.textView_text);
}
else if(view.getId()==R.id.button2){
Intent i = new Intent(this,Main2Activity.class);
startActivity(i);
}
else if(view.getId()==R.id.button3){
Intent i = new Intent(this,Main3Activity.class);
startActivity(i);
}

}
}
Main2Activity.java
package com.example.hp.myapplication;

import android.content.Intent;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{


TextView tv;
Button but, but1, but2,but3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView);
but = (Button) findViewById(R.id.button);
but1 = (Button) findViewById(R.id.button1);
but2 = (Button) findViewById(R.id.button2);
but3 = (Button) findViewById(R.id.button3);
but.setOnClickListener(this);
but1.setOnClickListener(this);
but2.setOnClickListener(this);
but3.setOnClickListener(this);
Thread t = new Thread(){
@Override
public void run(){
try{
while(!isInterrupted()){
Thread.sleep(1000);
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView tdate = (TextView) findViewById(R.id.date);
long date = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd
yyyy\nhh:mm:ss a");
String dateString = sdf.format(date);
tdate.setText(dateString);
}
});
}
}catch (InterruptedException e){
}
}
};
t.start();
}

@Override
public void onClick(View view) {
if (view.getId()==R.id.button){
tv.setText(R.string.textView_text1);
}
else if (view.getId()==R.id.button1){
tv.setText(R.string.textView_text);
}
else if(view.getId()==R.id.button2){
Intent i = new Intent(this,Main2Activity.class);
startActivity(i);
}
else if(view.getId()==R.id.button3){
Intent i = new Intent(this,Main3Activity.class);
startActivity(i);
}

}
}
Main3Activity.java
package com.example.hp.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Main3Activity extends AppCompatActivity implements View.OnClickListener {


double fn,sn;
String operator;
EditText et;
Button
back,clear,plus,minus,multi,div,equal,dot,pnm,off,n0,n1,n2,n3,n4,n5,n6,n7,n8,n9;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
et = (EditText) findViewById(R.id.et);
back = (Button) findViewById(R.id.back);
clear = (Button) findViewById(R.id.clear);
plus = (Button) findViewById(R.id.plus);
minus = (Button) findViewById(R.id.minus);
multi = (Button) findViewById(R.id.multi);
div = (Button) findViewById(R.id.div);

equal = (Button) findViewById(R.id.equal);


dot = (Button) findViewById(R.id.dot);
pnm = (Button) findViewById(R.id.pnm);
off = (Button) findViewById(R.id.off);

n0 = (Button) findViewById(R.id.n0);
n1 = (Button) findViewById(R.id.n1);
n2 = (Button) findViewById(R.id.n2);
n3 = (Button) findViewById(R.id.n3);
n4 = (Button) findViewById(R.id.n4);
n5 = (Button) findViewById(R.id.n5);
n6 = (Button) findViewById(R.id.n6);
n7 = (Button) findViewById(R.id.n7);
n8 = (Button) findViewById(R.id.n8);
n9 = (Button) findViewById(R.id.n9);

back.setOnClickListener(this);
clear.setOnClickListener(this);
plus.setOnClickListener(this);
minus.setOnClickListener(this);
multi.setOnClickListener(this);
div.setOnClickListener(this);
equal.setOnClickListener(this);
dot.setOnClickListener(this);
pnm.setOnClickListener(this);
off.setOnClickListener(this);

n0.setOnClickListener(this);
n1.setOnClickListener(this);
n2.setOnClickListener(this);
n3.setOnClickListener(this);
n4.setOnClickListener(this);
n5.setOnClickListener(this);
n6.setOnClickListener(this);
n7.setOnClickListener(this);
n8.setOnClickListener(this);
n9.setOnClickListener(this);
}
@Override
public void onClick(View v) {
try{
String string = et.getText().toString();
switch (v.getId()){
case R.id.back:
StringBuilder stB = new StringBuilder(string);
stB.deleteCharAt(string.length()-1);
et.setText(stB.toString());
case R.id.clear:
et.setText("");
break;
case R.id.plus:
operator = "+";
fn = Double.parseDouble(et.getText().toString());
et.setText("");
break;
case R.id.minus:
operator = "-";
fn = Double.parseDouble(et.getText().toString());
et.setText("");
break;
case R.id.pnm:
et.setText(Double.parseDouble(et.getText().toString())*(-1)+"");
break;
case R.id.multi:
operator = "*";
fn = Double.parseDouble(et.getText().toString());
et.setText("");
break;
case R.id.div:
operator = "/";
fn = Double.parseDouble(et.getText().toString());
et.setText("");
break;
case R.id.equal:
sn = Double.parseDouble(et.getText().toString());
double result = 0;
switch (operator){
case "+":
result = fn+sn;
break;
case "-":
result = fn-sn;
break;
case "*":
result = fn*sn;
break;
case "/":
result = fn/sn;
break;
}
et.setText(result+"");
break;
case R.id.n7:
et.setText(string+n7.getText());
break;
case R.id.n8:
et.setText(string+n8.getText());
break;
case R.id.n9:
et.setText(string+n9.getText());
break;
case R.id.n4:
et.setText(string+n4.getText());
break;
case R.id.n5:
et.setText(string+n5.getText());
break;
case R.id.n6:
et.setText(string+n6.getText());
break;
case R.id.n1:
et.setText(string+n1.getText());
break;
case R.id.n2:
et.setText(string+n2.getText());
break;
case R.id.n3:
et.setText(string+n3.getText());
break;
case R.id.dot:
et.setText(string+dot.getText());
break;
case R.id.n0:
et.setText(string+n0.getText());
break;
case R.id.off:
finish();
}
}catch (Exception e){
Toast.makeText(Main3Activity.this,"Please type proper
input",Toast.LENGTH_SHORT).show();
}

}
}
Background, icon and layout in the res directory:
Three layout for three activity:
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/backk"
android:gravity="center"
android:orientation="vertical"
tools:context="com.example.hp.myapplication.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#92c4bdbd"
android:id="@+id/date"
android:textSize="30dp"
android:textColor="#cc000000"
android:textStyle="bold"
android:fontFamily="serif-monospace"
android:textAlignment="center"
/>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginBottom="0dp"
android:shadowColor="#3b69ce"
android:text="@string/textView_text"
android:textAlignment="center"
android:textColor="#e4e4ed"
android:textSize="35sp"
android:textStyle="bold"
/>
<TableLayout
android:layout_width="372dp"
android:layout_height="match_parent">
<TableRow
android:layout_width="match_parent">
<Button
android:id="@+id/button"
android:layout_width="372dp"
android:layout_height="50dp"
android:text="@string/button_name"
android:textSize="20sp"/>
</TableRow>
<TableRow
android:layout_width="match_parent">
<Button
android:id="@+id/button1"
android:layout_width="372dp"
android:layout_height="50dp"
android:text="@string/button_name1"
android:textSize="20sp"/>
</TableRow>
<TableRow
android:layout_width="match_parent">

<Button
android:id="@+id/button2"
android:layout_width="372dp"
android:layout_height="50dp"
android:text="@string/button_name2"
android:textSize="20sp"/>
</TableRow>
<TableRow
android:layout_width="match_parent">

<Button
android:id="@+id/button3"
android:layout_width="372dp"
android:layout_height="50dp"
android:textSize="20sp"
android:text="@string/button_name3" />
</TableRow>
</TableLayout>
</LinearLayout>
activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#a59dd9"
android:orientation="vertical"
tools:context="com.example.hp.myapplication.Main2Activity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:background="@android:drawable/screen_background_dark"
android:text="@string/textview_identity"
android:textAlignment="center"
android:textAllCaps="true"
android:textColor="@android:color/background_light"
android:textSize="35sp"
android:textStyle="bold" />

<ImageView
android:layout_width="320dp"
android:layout_height="300dp"
android:layout_marginLeft="35dp"
android:layout_marginRight="35dp"
android:scaleType="fitXY"
android:src="@drawable/kk" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="35dp"
android:layout_marginTop="15dp"
android:text="@string/textview_story"
android:textColor="#631f55"
android:textStyle="bold" />

</LinearLayout>
activity_main3.xml
<TableLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
>
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_weight="1"
android:background="#e1e0e0"
android:padding="5dp"
android:textSize="35sp" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight=".30">
<Button
android:id="@+id/back"
android:layout_width="0dp"
android:layout_height="90dp"
android:layout_weight=".30"
android:text="b"
android:textColor="#ffffff"
android:textSize="36sp" />
<Button
android:id="@+id/clear"
android:layout_width="0dp"
android:layout_weight=".30"
android:text="C"
android:layout_height="90dp"
android:textColor="#ffffff"
android:textSize="36sp" />
<Button
android:id="@+id/plus"
android:layout_width="0dp"
android:layout_weight=".30"
android:text="+"
android:layout_height="90dp"
android:textColor="#ffffff"
android:textSize="36sp" />
<Button
android:id="@+id/minus"
android:layout_width="0dp"
android:layout_weight=".30"
android:layout_height="90dp"
android:text="-"
android:textColor="#ffffff"
android:textSize="36sp" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight=".30">
<Button
android:id="@+id/n7"
android:layout_width="0dp"
android:layout_weight=".30"
android:layout_height="90dp"
android:text="7"
android:textColor="#ffffff"
android:textSize="36sp" />
<Button
android:id="@+id/n8"
android:layout_width="0dp"
android:layout_weight=".30"
android:layout_height="90dp"
android:text="8"
android:textColor="#ffffff"
android:textSize="36sp" />
<Button
android:id="@+id/n9"
android:layout_width="0dp"
android:layout_height="90dp"
android:layout_weight=".30"
android:text="9"
android:textColor="#ffffff"
android:textSize="36sp" />
<Button
android:id="@+id/multi"
android:layout_width="0dp"
android:layout_weight=".30"
android:layout_height="90dp"
android:text="*"
android:textColor="#ffffff"
android:textSize="36sp" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight=".30">
<Button
android:id="@+id/n4"
android:layout_width="0dp"
android:layout_weight=".30"
android:layout_height="90dp"
android:text="4"
android:textColor="#ffffff"
android:textSize="36sp" />
<Button
android:id="@+id/n5"
android:layout_width="0dp"
android:layout_weight=".30"
android:layout_height="90dp"
android:text="5"
android:textColor="#ffffff"
android:textSize="36sp" />
<Button
android:id="@+id/n6"
android:layout_width="0dp"
android:layout_height="90dp"
android:layout_weight=".30"
android:text="6"
android:textColor="#ffffff"
android:textSize="36sp" />
<Button
android:id="@+id/div"
android:layout_width="0dp"
android:layout_weight=".30"
android:layout_height="90dp"
android:text="/"
android:textColor="#ffffff"
android:textSize="36sp" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight=".30">
<Button
android:id="@+id/n1"
android:layout_width="0dp"
android:layout_weight=".30"
android:layout_height="90dp"
android:text="1"
android:textColor="#ffffff"
android:textSize="36sp" />
<Button
android:id="@+id/n2"
android:layout_width="0dp"
android:layout_weight=".30"
android:layout_height="90dp"
android:text="2"
android:textColor="#ffffff"
android:textSize="36sp" />
<Button
android:id="@+id/n3"
android:layout_width="0dp"
android:layout_weight=".30"
android:layout_height="90dp"
android:text="3"
android:textColor="#ffffff"
android:textSize="36sp" />
<Button
android:id="@+id/equal"
android:layout_width="0dp"
android:layout_weight=".30"
android:layout_height="90dp"
android:text="="
android:textColor="#ffffff"
android:textSize="36sp" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight=".30">
<Button
android:id="@+id/dot"
android:layout_width="0dp"
android:layout_height="90dp"
android:layout_weight=".30"
android:text="."
android:textColor="#ffffff"
android:textSize="36sp" />
<Button
android:id="@+id/n0"
android:layout_width="0dp"
android:layout_weight=".30"
android:layout_height="90dp"
android:text="0"
android:textColor="#ffffff"
android:textSize="36sp" />
<Button
android:id="@+id/pnm"
android:layout_width="0dp"
android:layout_weight=".30"
android:layout_height="90dp"
android:text="+/-"
android:textColor="#ffffff"
android:textSize="36sp" />
<Button
android:id="@+id/off"
android:layout_width="0dp"
android:layout_height="90dp"
android:layout_weight=".30"
android:text="H"
android:textColor="#ffffff"
android:textSize="36sp" />
</TableRow>
</TableLayout>
All the string resides strings.xml in the values:
<resources>
<string name="app_name">ClassApp</string>
<string name="textView_text">Hello World!</string>
<string name="textView_text1">How are you world?</string>
<string name="button_name">Change</string>
<string name="button_name1">Back</string>
<string name="button_name2">Creator</string>
<string name="button_name3">Calculator</string>
<string name="button_name4">PDF</string>
<string name="textview_identity">Owner Description</string>
<string name="textview_story">Name: Kawsar Hamid \nDegree: CSTE(NSTU), MIT(JU)
\nCertification: MCSE, CCNA, OCP, Oracle APEX Dev \nDesignation: Executive(IT)
\nEmail: kawsarhamid44@gmail.com \nkawsar.it@ibnsinapharma.com \nCell :
+8801701204403</string>
<string name="title_activity_main3">Main3Activity</string>
</resources>
All the images resides in drawable directory

Вам также может понравиться