Layout App
Main Activity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Intents: facilitates communication bet. different components of an app,
// as well as bet. different applications.
// types of intents:
// 1- Explicit Intents
Button btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
goToSecondActivity();
}
});
// 2- Implicit Intents
Button btn2 = findViewById(R.id.openBrowser);
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openWebPage();
}
});
}
public void goToSecondActivity(){
Intent intent = new Intent(this, SecondActivity.class );
startActivity(intent);
}
public void openWebPage(){
Uri webpage = Uri.parse("https://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
startActivity(intent);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Intents: facilitates communication bet. different components of an app,
// as well as bet. different applications.
// types of intents:
// 1- Explicit Intents
Button btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
goToSecondActivity();
}
});
// 2- Implicit Intents
Button btn2 = findViewById(R.id.openBrowser);
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openWebPage();
}
});
}
public void goToSecondActivity(){
Intent intent = new Intent(this, SecondActivity.class );
startActivity(intent);
}
public void openWebPage(){
Uri webpage = Uri.parse("https://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
startActivity(intent);
}
}
Second Activity
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second2);
}
}
Main XML
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to Second Activity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/openBrowser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="26dp"
android:layout_marginBottom="91dp"
android:text="Go to Google"
app:layout_constraintBottom_toTopOf="@+id/btn"
app:layout_constraintStart_toStartOf="@+id/btn" />
Second Activity XML
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="292dp"
android:layout_marginEnd="4dp"
android:text="Welcome to Second Activity"
android:textSize="32sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Comments
Post a Comment