0% found this document useful (0 votes)
3 views3 pages

Slip 6

The document contains two Android application examples. The first example demonstrates sending a message from MainActivity to SecondActivity using an Intent, while the second example showcases a ListView that displays a list of fruits and shows a Toast message when an item is clicked. Both examples utilize XML layout files for their user interfaces.

Uploaded by

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

Slip 6

The document contains two Android application examples. The first example demonstrates sending a message from MainActivity to SecondActivity using an Intent, while the second example showcases a ListView that displays a list of fruits and shows a Toast message when an item is clicked. Both examples utilize XML layout files for their user interfaces.

Uploaded by

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

Q1===========================================================

xml------------------------------------

<?xml version="1.0" encoding="utf-8"?>


<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/sendButton"
android:text="Send Hello!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="100dp"/>

</android.support.constraint.ConstraintLayout>

second.xml-------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>


<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/messageText"
android:text="Message will appear here"
android:textSize="24sp"
android:gravity="center"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>

</android.support.constraint.ConstraintLayout>

main.java-------------------------------------------------------

package com.example.hellointentapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {

private Button sendButton;

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

sendButton = findViewById(R.id.sendButton);

sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,
SecondActivity.class);
intent.putExtra("message", "Hello!");
startActivity(intent);
}
});
}
}

second.java-----------------------------------------------------------------

package com.example.hellointentapp;

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

public class SecondActivity extends Activity {

private TextView messageText;

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

messageText = findViewById(R.id.messageText);

// Get message from Intent


Intent intent = getIntent();
String message = intent.getStringExtra("message");

messageText.setText(message);
}
}

Q2=================================================================================
======================

xml------------------------------------------

<?xml version="1.0" encoding="utf-8"?>


<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</android.support.constraint.ConstraintLayout>

main.java-------------------------------------------------

package com.example.listviewapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {

private ListView listView;


private String[] items = {"Apple", "Banana", "Cherry", "Mango", "Orange",
"Pineapple"};

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

listView = findViewById(R.id.listView); // Directly access using R.id

// Set up ListView with Adapter


ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, items);
listView.setAdapter(adapter);

// Handle Item Click


listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String selectedItem = items[position];
Toast.makeText(MainActivity.this, "You clicked: " + selectedItem,
Toast.LENGTH_SHORT).show();
}
});
}
}

You might also like