Advertisement
mmayoub

School, Intent example, 28.01.18

Jan 28th, 2018
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.40 KB | None | 0 0
  1. activity.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.     <EditText
  27.         android:id="@+id/etName"
  28.         android:layout_width="match_parent"
  29.         android:layout_height="wrap_content"
  30.         android:textSize="28sp"
  31.         android:hint="your name . . ."
  32.         android:gravity="center"
  33.         android:layout_marginTop="16dp"
  34.         />
  35. </LinearLayout>
  36.  
  37. MainActivity.java
  38. ------------------
  39. package com.example.mohamadpc.myapplication140118;
  40.  
  41. import android.content.Intent;
  42. import android.graphics.Color;
  43. import android.os.Bundle;
  44. import android.support.v7.app.AppCompatActivity;
  45. import android.view.View;
  46. import android.widget.Button;
  47. import android.widget.EditText;
  48. import android.widget.LinearLayout;
  49. import android.widget.TextView;
  50.  
  51. import java.util.Random;
  52.  
  53. // step 3
  54. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  55.     // step 1
  56.     Button btnClick;
  57.     TextView tvMessage;
  58.     LinearLayout llyBack;
  59.     Random rnd = new Random();
  60.     EditText etName;
  61.     int myColor;
  62.  
  63.     @Override
  64.     protected void onCreate(Bundle savedInstanceState) {
  65.         super.onCreate(savedInstanceState);
  66.         setContentView(R.layout.activity_main);
  67.  
  68.         // step 2
  69.         btnClick = findViewById(R.id.btnClickMe);
  70.         tvMessage = findViewById(R.id.tvMessgae);
  71.         llyBack = findViewById(R.id.llyBack);
  72.         etName = findViewById(R.id.etName);
  73.  
  74.         tvMessage.setText("Wellcome");
  75.         // step 5
  76.         btnClick.setOnClickListener(this);
  77.         tvMessage.setOnClickListener(this);
  78.         llyBack.setOnClickListener(this);
  79.     }
  80.  
  81.     // step 4
  82.     @Override
  83.     public void onClick(View view) {
  84.         // step 6
  85.         if (view.getId() == R.id.btnClickMe) {
  86.             // when button clicked
  87.             setColor();
  88.         } else if (view.getId() == R.id.tvMessgae) {
  89.             // when text view is clicked
  90.             tvMessage.setText("");
  91.         } else {
  92.             // click on background
  93.             Intent i;
  94.             i = new Intent(this, Main2Activity.class);
  95.  
  96.             String name = etName.getText().toString();
  97.  
  98.             i.putExtra("UserName", name);
  99.             i.putExtra("Color", myColor);
  100.  
  101.             startActivity(i);
  102.             //finish();
  103.         }
  104.     }
  105.  
  106.     private void setColor() {
  107.         int red = rnd.nextInt(256);
  108.         int green = rnd.nextInt(256);
  109.         int blue = rnd.nextInt(256);
  110.  
  111.         myColor = Color.rgb(red, green, blue);
  112.  
  113.         llyBack.setBackgroundColor(myColor);
  114.         tvMessage.setText(myColor + "");
  115.     }
  116. }
  117.  
  118. activity_main2.xml
  119. -------------------
  120. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  121.     android:layout_width="match_parent"
  122.     android:layout_height="match_parent"
  123.     android:background="@color/myColor"
  124.     android:orientation="vertical">
  125.  
  126.     <Button
  127.         android:id="@+id/btnBackToMain"
  128.         android:layout_width="match_parent"
  129.         android:layout_height="wrap_content"
  130.         android:text="Back to Main Activity"
  131.         android:textSize="24sp" />
  132.  
  133.     <TextView
  134.         android:id="@+id/tvWellcome"
  135.         android:layout_width="match_parent"
  136.         android:layout_height="match_parent"
  137.         android:gravity="center"
  138.         android:text="dogma"
  139.         android:textSize="32sp" />
  140. </LinearLayout>
  141.  
  142. Main2Activity.java
  143. -------------------
  144. package com.example.mohamadpc.myapplication140118;
  145.  
  146. import android.content.Intent;
  147. import android.os.Bundle;
  148. import android.support.v7.app.AppCompatActivity;
  149. import android.view.View;
  150. import android.widget.TextView;
  151.  
  152. public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
  153.     TextView tvWellcome;
  154.  
  155.     @Override
  156.     protected void onCreate(Bundle savedInstanceState) {
  157.         super.onCreate(savedInstanceState);
  158.         setContentView(R.layout.activity_main2);
  159.  
  160.         findViewById(R.id.btnBackToMain).setOnClickListener(this);
  161.         tvWellcome = findViewById(R.id.tvWellcome);
  162.  
  163.         // get data from previous screen
  164.         Intent i;
  165.         i = this.getIntent();
  166.  
  167.         String name = i.getStringExtra("UserName");
  168.         int color = i.getIntExtra("Color", 255);
  169.  
  170.         tvWellcome.setText("Wellcome " + name);
  171.         tvWellcome.setBackgroundColor(color);
  172.  
  173.  
  174.     }
  175.  
  176.     @Override
  177.     public void onClick(View view) {
  178.         switch (view.getId()) {
  179.             case R.id.btnBackToMain:
  180.                 /*
  181.                 // create a new activity
  182.                 Intent i;
  183.                 i = new Intent(this, MainActivity.class);
  184.                 startActivity(i);
  185.                 */
  186.  
  187.                 finish();
  188.                 break;
  189.  
  190.         }
  191.     }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement