Practical 14 MAD
Practical 14 MAD
Program on ListView
Xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Java code:
package com.example.prac14;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
String[] programmingLanguages = {
"Android", "Java", "Php", "Hadoop", "Sap", "Python",
"Ajax", "C++", "Ruby", "Rails", "Html"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = findViewById(R.id.listView);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, programmingLanguages);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
Toast.makeText(MainActivity.this, programmingLanguages[position],
Toast.LENGTH_SHORT).show();
}
});
}
}
Output:
Program on GridView:
Xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="10dp">
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"/>
</LinearLayout>
Java code:
package com.example.prac14_2;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private String[] buttonLabels = {
"1", "2", "3", "4", "5",
"6", "7", "8", "9", "10",
"11", "12", "13", "14", "15"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridView = findViewById(R.id.gridView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, buttonLabels) {
@Override
public View getView(int position, View convertView, ViewGroup
parent) {
Button button;
if (convertView == null) {
button = new Button(MainActivity.this);
} else {
button = (Button) convertView;
}
button.setText(getItem(position));
return button;
}
};
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
Toast.makeText(MainActivity.this, "Button " + buttonLabels[position]
+ " clicked", Toast.LENGTH_SHORT).show();
}
});
}
}
Output:
Program on scroll view:
Xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:padding="10dp"
android:textColor="@android:color/black"/>
</ScrollView>
</LinearLayout>
Java code:
package com.example.prac14_3;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.textView);
String longText ="### Understanding Android Activities ###\n\n" +
"An Activity is one of the most essential components of an Android
application. " +
"It represents a single screen with a user interface, acting as an entry
point for user interactions. " +
"For example, a login screen, a settings screen, or a home screen are
all different Activities.\n\n" +
"### Activity Lifecycle ###\n" +
"Every Activity in Android goes through multiple states in its
lifecycle, " +
"which are managed by the Android system. The key lifecycle
methods are:\n\n" +
"1. **onCreate()** - Called when the Activity is first created. It is
used to initialize UI components and data.\n" +
"2. **onStart()** - Called when the Activity becomes visible to the
user.\n" +
"3. **onResume()** - Called when the Activity starts interacting with
the user.\n" +
"4. **onPause()** - Called when another Activity comes into the
foreground, partially hiding the current one.\n" +
"5. **onStop()** - Called when the Activity is completely hidden.\n"
+
"6. **onDestroy()** - Called when the Activity is finishing or being
destroyed by the system.\n\n" +
"### Intents and Activity Navigation ###\n" +
"Activities can communicate with each other using **Intents**.
Intents are messages that allow you to navigate between Activities " +
"or even interact with external applications. There are two types of
Intents:\n\n" +
"- **Explicit Intents**: Used to start a specific Activity within the
same application.\n" +
"- **Implicit Intents**: Used to request an action from another app
(e.g., opening a web page or sharing text).\n\n" +
"Example of starting a new Activity using an Explicit Intent:\n\n" +
"`Intent intent = new Intent(CurrentActivity.this,
NewActivity.class);`\n" +
"`startActivity(intent);`\n\n" +
"### Handling Configuration Changes ###\n" +
"When the device configuration changes (e.g., screen rotation,
language change), " +
"Android may destroy and recreate the Activity. To prevent data loss,
you can use:\n\n" +
"- `onSaveInstanceState(Bundle outState)`: Saves Activity state.\n" +
"- `onRestoreInstanceState(Bundle savedInstanceState)`: Restores
state after recreation.\n\n" +
"### Conclusion ###\n" +
"Activities are a fundamental part of Android development.
Understanding their lifecycle, navigation, and state management " +
"helps in building efficient and user-friendly applications.\n\n" +
"---\n" +
"Scroll down for more details... Keep learning Android development!"
;
textView.setText(longText);
}
}
Output: