Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- activity.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"
- />
- <EditText
- android:id="@+id/etName"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textSize="28sp"
- android:hint="your name . . ."
- android:gravity="center"
- android:layout_marginTop="16dp"
- />
- </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.EditText;
- 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();
- EditText etName;
- int myColor;
- @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);
- etName = findViewById(R.id.etName);
- 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);
- String name = etName.getText().toString();
- i.putExtra("UserName", name);
- i.putExtra("Color", myColor);
- startActivity(i);
- //finish();
- }
- }
- private void setColor() {
- int red = rnd.nextInt(256);
- int green = rnd.nextInt(256);
- int blue = rnd.nextInt(256);
- myColor = Color.rgb(red, green, blue);
- llyBack.setBackgroundColor(myColor);
- tvMessage.setText(myColor + "");
- }
- }
- 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:id="@+id/btnBackToMain"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Back to Main Activity"
- android:textSize="24sp" />
- <TextView
- android:id="@+id/tvWellcome"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:gravity="center"
- android:text="dogma"
- android:textSize="32sp" />
- </LinearLayout>
- Main2Activity.java
- -------------------
- package com.example.mohamadpc.myapplication140118;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.TextView;
- public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
- TextView tvWellcome;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main2);
- findViewById(R.id.btnBackToMain).setOnClickListener(this);
- tvWellcome = findViewById(R.id.tvWellcome);
- // get data from previous screen
- Intent i;
- i = this.getIntent();
- String name = i.getStringExtra("UserName");
- int color = i.getIntExtra("Color", 255);
- tvWellcome.setText("Wellcome " + name);
- tvWellcome.setBackgroundColor(color);
- }
- @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