Advertisement
mmayoub

School, 22.10.2017, first application

Oct 22nd, 2017
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.23 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:background="#00999F"
  8.     android:id="@+id/llyBackground">
  9.  
  10.     <Button
  11.         android:id="@+id/btnSayHello"
  12.         android:layout_width="match_parent"
  13.         android:layout_height="wrap_content"
  14.         android:text="say hello"
  15.         android:textColor="#ff00ff"
  16.         android:textSize="28sp"
  17.         android:layout_margin="16dp"
  18.         />
  19.  
  20.     <LinearLayout
  21.         android:layout_width="match_parent"
  22.         android:layout_height="wrap_content"
  23.         android:orientation="horizontal"
  24.         android:layout_margin="16dp">
  25.  
  26.         <Button
  27.             android:id="@+id/btnRed"
  28.             android:layout_width="match_parent"
  29.             android:layout_height="wrap_content"
  30.             android:layout_weight="1"
  31.             android:text="Red" />
  32.  
  33.         <Button
  34.             android:id="@+id/btnGreen"
  35.             android:layout_width="match_parent"
  36.             android:layout_height="wrap_content"
  37.             android:layout_weight="1"
  38.             android:text="Green" />
  39.  
  40.         <Button
  41.             android:id="@+id/btnBlue"
  42.             android:layout_width="wrap_content"
  43.             android:layout_height="wrap_content"
  44.             android:text="Blue"
  45.  
  46.             />
  47.     </LinearLayout>
  48.  
  49.     <Button
  50.         android:id="@+id/btnQuit"
  51.         android:layout_width="match_parent"
  52.         android:layout_height="wrap_content"
  53.         android:layout_margin="16dp"
  54.         android:text="quit"
  55.         android:textColor="#0099ff"
  56.         android:textSize="22sp" />
  57.  
  58.     <TextView
  59.         android:id="@+id/tvText"
  60.         android:layout_width="match_parent"
  61.         android:layout_height="match_parent"
  62.         android:layout_margin="16dp"
  63.         android:background="#009922"
  64.         android:gravity="center"
  65.         android:text="dogma" />
  66. </LinearLayout>
  67.  
  68.  
  69. MainActivity.java
  70. ------------------
  71. package com.example.mohamadpc.myfirstapplication;
  72.  
  73. import android.graphics.Color;
  74. import android.os.Bundle;
  75. import android.support.v7.app.AppCompatActivity;
  76. import android.view.View;
  77. import android.widget.Button;
  78. import android.widget.TextView;
  79.  
  80. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  81.     Button b1, b2;
  82.     TextView text1;
  83.  
  84.     Button btnRed, btnBlue, btnGreen;
  85.  
  86.     @Override
  87.     protected void onCreate(Bundle savedInstanceState) {
  88.         super.onCreate(savedInstanceState);
  89.         setContentView(R.layout.activity_main);
  90.  
  91.         b1 = (Button) findViewById(R.id.btnSayHello);
  92.         b2 = (Button) findViewById(R.id.btnQuit);
  93.         text1 = (TextView) findViewById(R.id.tvText);
  94.  
  95.         btnRed = (Button) findViewById(R.id.btnRed);
  96.         btnGreen = (Button) findViewById(R.id.btnGreen);
  97.         btnBlue = (Button) findViewById(R.id.btnBlue);
  98.  
  99.         b1.setOnClickListener(this);
  100.         b2.setOnClickListener(this);
  101.         text1.setOnClickListener(this);
  102.  
  103.         btnRed.setOnClickListener(this);
  104.         btnGreen.setOnClickListener(this);
  105.         btnBlue.setOnClickListener(this);
  106.  
  107.         findViewById(R.id.llyBackground).setBackgroundColor(Color.YELLOW);
  108.     }
  109.  
  110.     @Override
  111.     public void onClick(View v) {
  112.         switch (v.getId()) {
  113.             case R.id.btnSayHello:
  114.                 text1.setText("Welcome to android");
  115.                 text1.setTextSize(10);
  116.                 break;
  117.             case R.id.btnQuit:
  118.                 this.finish();
  119.                 break;
  120.             case R.id.tvText:
  121.                 text1.setTextSize(text1.getTextSize() + 1);
  122.                 break;
  123.  
  124.             case R.id.btnBlue:
  125.                 text1.setBackgroundColor(Color.BLUE);
  126.                 text1.setTextColor(Color.WHITE);
  127.                 break;
  128.  
  129.             case R.id.btnRed:
  130.                 text1.setBackgroundColor(Color.RED);
  131.                 text1.setTextColor(Color.YELLOW);
  132.                 break;
  133.  
  134.             case R.id.btnGreen:
  135.                 text1.setBackgroundColor(Color.GREEN);
  136.                 text1.setTextColor(Color.BLUE);
  137.                 break;
  138.  
  139.         }
  140.  
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement