Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // activity_main.xml
- --------------------
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:id="@+id/llyBack">
- <Button
- android:id="@+id/btnClickMe"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@color/myColor"
- android:textSize="32sp"
- android:text="click me"/>
- <TextView
- android:id="@+id/tvMessgae"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textSize="28sp"
- android:text="Hello"
- android:gravity="center"
- android:layout_marginTop="16dp"
- />
- </LinearLayout>
- // activity_main2.xml
- ---------------------
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@color/myColor"
- android:orientation="vertical">
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Back to Main Activity"
- android:textSize="24sp"
- android:id="@+id/btnBackToMain"/>
- </LinearLayout>
- MainActivity.java
- -----------------
- package com.example.mohamadpc.myapplication140118;
- import android.content.Intent;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.Button;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import java.util.Random;
- // step 3
- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
- // step 1
- Button btnClick;
- TextView tvMessage;
- LinearLayout llyBack;
- Random rnd = new Random();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- // step 2
- btnClick = findViewById(R.id.btnClickMe);
- tvMessage = findViewById(R.id.tvMessgae);
- llyBack = findViewById(R.id.llyBack);
- tvMessage.setText("Wellcome");
- // step 5
- btnClick.setOnClickListener(this);
- tvMessage.setOnClickListener(this);
- llyBack.setOnClickListener(this);
- }
- // step 4
- @Override
- public void onClick(View view) {
- // step 6
- if (view.getId() == R.id.btnClickMe) {
- // when button clicked
- setColor();
- } else if (view.getId()==R.id.tvMessgae){
- // when text view is clicked
- tvMessage.setText("");
- } else {
- // click on background
- Intent i;
- i = new Intent(this,Main2Activity.class);
- startActivity(i);
- // finish();
- }
- }
- private void setColor() {
- int red = rnd.nextInt(256);
- int green = rnd.nextInt(256);
- int blue = rnd.nextInt(256);
- int myColor = Color.rgb(red, green, blue);
- llyBack.setBackgroundColor(myColor);
- tvMessage.setText(myColor + "");
- }
- }
- //Main2Activity.java
- --------------------
- package com.example.mohamadpc.myapplication140118;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main2);
- findViewById(R.id.btnBackToMain).setOnClickListener(this);
- }
- @Override
- public void onClick(View view) {
- switch (view.getId()) {
- case R.id.btnBackToMain:
- /* create a new activity
- Intent i;
- i = new Intent(this, MainActivity.class);
- startActivity(i);
- */
- finish();
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement