Advertisement
mmayoub

School, simple app, 14.01.18

Jan 14th, 2018
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.16 KB | None | 0 0
  1. // activity_main.xml
  2. --------------------
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     android:orientation="vertical"
  7.     android:id="@+id/llyBack">
  8.  
  9.     <Button
  10.         android:id="@+id/btnClickMe"
  11.         android:layout_width="match_parent"
  12.         android:layout_height="wrap_content"
  13.         android:background="@color/myColor"
  14.         android:textSize="32sp"
  15.         android:text="click me"/>
  16.  
  17.     <TextView
  18.         android:id="@+id/tvMessgae"
  19.         android:layout_width="match_parent"
  20.         android:layout_height="wrap_content"
  21.         android:textSize="28sp"
  22.         android:text="Hello"
  23.         android:gravity="center"
  24.         android:layout_marginTop="16dp"
  25.         />
  26. </LinearLayout>
  27.  
  28. // activity_main2.xml
  29. ---------------------
  30. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  31.     android:layout_width="match_parent"
  32.     android:layout_height="match_parent"
  33.     android:background="@color/myColor"
  34.     android:orientation="vertical">
  35.  
  36. <Button
  37.     android:layout_width="match_parent"
  38.     android:layout_height="wrap_content"
  39.     android:text="Back to Main Activity"
  40.     android:textSize="24sp"
  41.     android:id="@+id/btnBackToMain"/>
  42. </LinearLayout>
  43.  
  44. MainActivity.java
  45. -----------------
  46. package com.example.mohamadpc.myapplication140118;
  47.  
  48. import android.content.Intent;
  49. import android.graphics.Color;
  50. import android.os.Bundle;
  51. import android.support.v7.app.AppCompatActivity;
  52. import android.view.View;
  53. import android.widget.Button;
  54. import android.widget.LinearLayout;
  55. import android.widget.TextView;
  56.  
  57. import java.util.Random;
  58.  
  59. // step 3
  60. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  61.     // step 1
  62.     Button btnClick;
  63.     TextView tvMessage;
  64.     LinearLayout llyBack;
  65.     Random rnd = new Random();
  66.  
  67.     @Override
  68.     protected void onCreate(Bundle savedInstanceState) {
  69.         super.onCreate(savedInstanceState);
  70.         setContentView(R.layout.activity_main);
  71.  
  72.         // step 2
  73.         btnClick = findViewById(R.id.btnClickMe);
  74.         tvMessage = findViewById(R.id.tvMessgae);
  75.         llyBack = findViewById(R.id.llyBack);
  76.  
  77.         tvMessage.setText("Wellcome");
  78.         // step 5
  79.         btnClick.setOnClickListener(this);
  80.         tvMessage.setOnClickListener(this);
  81.         llyBack.setOnClickListener(this);
  82.     }
  83.  
  84.     // step 4
  85.     @Override
  86.     public void onClick(View view) {
  87.         // step 6
  88.         if (view.getId() == R.id.btnClickMe) {
  89.             // when button clicked
  90.             setColor();
  91.         } else if (view.getId()==R.id.tvMessgae){
  92.             // when text view is clicked
  93.             tvMessage.setText("");
  94.         } else {
  95.             // click on background
  96.             Intent i;
  97.              i = new Intent(this,Main2Activity.class);
  98.              startActivity(i);
  99.              // finish();
  100.         }
  101.     }
  102.  
  103.     private void setColor() {
  104.         int red = rnd.nextInt(256);
  105.         int green = rnd.nextInt(256);
  106.         int blue = rnd.nextInt(256);
  107.  
  108.         int myColor = Color.rgb(red, green, blue);
  109.  
  110.         llyBack.setBackgroundColor(myColor);
  111.         tvMessage.setText(myColor + "");
  112.     }
  113. }
  114.  
  115. //Main2Activity.java
  116. --------------------
  117. package com.example.mohamadpc.myapplication140118;
  118.  
  119. import android.os.Bundle;
  120. import android.support.v7.app.AppCompatActivity;
  121. import android.view.View;
  122.  
  123. public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
  124.  
  125.     @Override
  126.     protected void onCreate(Bundle savedInstanceState) {
  127.         super.onCreate(savedInstanceState);
  128.         setContentView(R.layout.activity_main2);
  129.  
  130.         findViewById(R.id.btnBackToMain).setOnClickListener(this);
  131.     }
  132.  
  133.     @Override
  134.     public void onClick(View view) {
  135.         switch (view.getId()) {
  136.             case R.id.btnBackToMain:
  137.                 /* create a new activity
  138.                 Intent i;
  139.                 i = new Intent(this, MainActivity.class);
  140.                 startActivity(i);
  141.                 */
  142.  
  143.                 finish();
  144.                 break;
  145.  
  146.         }
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement