0% found this document useful (0 votes)
30 views6 pages

Qazi Lab 3

The document describes code for an Android app with buttons that launch an activity to display text when clicked. XML and Java code is provided to create buttons and link them to launch a second activity, passing text to display.

Uploaded by

Riffaqat Hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views6 pages

Qazi Lab 3

The document describes code for an Android app with buttons that launch an activity to display text when clicked. XML and Java code is provided to create buttons and link them to launch a second activity, passing text to display.

Uploaded by

Riffaqat Hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Mobile application Development

Lab # 3

Submitted by
qazi

Submitted to

Ms. Anum Asghar

Faculty of Engineering & Computer Sciences

Department of BSIT-Morning

6th Semester

National University of Modern Languages, Islamabad

Page 1 of 6
Tasks:

Xml 1st code:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">

<Button
android:id="@+id/textOneButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text One"
android:layout_marginBottom="20dp" />

<Button
android:id="@+id/textTwoButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text Two"
android:layout_marginBottom="20dp" />

<Button
android:id="@+id/textThreeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text Three" />

</LinearLayout>
Java code :
package com.example.lab3;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button textOneButton = findViewById(R.id.textOneButton);


Button textTwoButton = findViewById(R.id.textTwoButton);
Button textThreeButton = findViewById(R.id.textThreeButton);

textOneButton.setOnClickListener(new View.OnClickListener() {
@Override
Page 2 of 6
public void onClick(View v) {
launchSecondActivity("Qazi");
}
});

textTwoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
launchSecondActivity("Jawad");
}
});

textThreeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
launchSecondActivity("Zaheer");
}
});
}

private void launchSecondActivity(String text) {


Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("text", text);
startActivity(intent);
}
}

screenshot

Page 3 of 6
Xml 2nd code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="@android:color/black"
android:padding="16dp" />

</LinearLayout>

Java code:
package com.example.lab3;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class SecondActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);

TextView textView = findViewById(R.id.textView);


Intent intent = getIntent();
if (intent != null && intent.hasExtra("text")) {
String text = intent.getStringExtra("text");
textView.setText(text);
}
}
}

Page 4 of 6
screenshots after clicks:

Page 5 of 6
Page 6 of 6

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy