0% found this document useful (0 votes)
5 views26 pages

Android Data Passing and UI Examples

The document contains multiple Java code snippets for Android applications, each demonstrating different functionalities such as passing data between activities, using intents, toggle buttons, alert dialogs, login/signup forms, web views, and date/time pickers. Each experiment is labeled with a name, PRN, batch, roll number, and experiment number. The code examples include both Java files and XML layout files, showcasing various UI components and their interactions.

Uploaded by

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

Android Data Passing and UI Examples

The document contains multiple Java code snippets for Android applications, each demonstrating different functionalities such as passing data between activities, using intents, toggle buttons, alert dialogs, login/signup forms, web views, and date/time pickers. Each experiment is labeled with a name, PRN, batch, roll number, and experiment number. The code examples include both Java files and XML layout files, showcasing various UI components and their interactions.

Uploaded by

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

Name: Siddique Mahamadlatif Sanadi PRN:2023011032082 Batch:B4 RollNo:234

ExperimentNo:4

// [Link]

[Link];

[Link];

[Link];

[Link];

[Link];

[Link];

publicclassMainActivityextendsAppCompatActivity{

EditTexteditText;

ButtonbuttonSend;

@Override

protectedvoidonCreate(BundlesavedInstanceState){

[Link](savedInstanceState);

setContentView([Link].activity_main);

editText = findViewById([Link]);

buttonSend=findViewById([Link]);

[Link](v->{

Stringmessage=[Link]().toString();

Intentintent=newIntent([Link],[Link]);

[Link]("key_message",message);

startActivity(intent);

});

}
//[Link]

[Link];

[Link];

[Link];

[Link];

publicclassSecondActivityextendsAppCompatActivity{

TextViewtextView;

@Override

protectedvoidonCreate(BundlesavedInstanceState){

[Link](savedInstanceState);

setContentView([Link].activity_second);

textView=findViewById([Link]);

Stringmessage=getIntent().getStringExtra("key_message");

[Link]("Received Message: " + message);

//activity_main.xml

<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayout

xmlns:android="[Link]

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:gravity="center"
android:padding="16dp">

<EditText

android:id="@+id/editText"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Enter Message" />

<Button
android:id="@+id/buttonSend"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="SendtoSecondActivity"a

ndroid:layout_marginTop="20dp"/>

</LinearLayout>

//activity_second.xml

<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayout

xmlns:android="[Link]

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:te

xt="Received Message"android:textSize="24sp"/>

</LinearLayout>
Output:
Name: Siddique Mahamadlatif Sanadi PRN:2023011032082 Batch:B4 RollNo:234
ExperimentNo:5

// [Link]

[Link];

[Link];

[Link];import

[Link];

[Link];

[Link];

publicclassMainActivityextendsAppCompatActivity{

ButtonbuttonExplicit,buttonImplicit;

@Override

protectedvoidonCreate(BundlesavedInstanceState){

[Link](savedInstanceState);

setContentView([Link].activity_main);

buttonExplicit=findViewById([Link]);

buttonImplicit=findViewById([Link]);

[Link](v->{

Intentintent=newIntent([Link],[Link]);

startActivity(intent);

});

[Link](v->{

Intent intent = new Intent(Intent.ACTION_VIEW);

[Link]([Link]("[Link]

startActivity(intent);
});

//[Link]

[Link];

[Link];

[Link];

publicclassSecondActivityextendsAppCompatActivity{

@Override

protectedvoidonCreate(BundlesavedInstanceState){

[Link](savedInstanceState);

setContentView([Link].activity_second);

}
}

//activity_main.xml

<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayout

xmlns:android="[Link]

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:gravity="center"andr

oid:padding="16dp">

<Button

android:id="@+id/buttonExplicit"andr

oid:layout_width="wrap_content"
android:layout_height="wrap_content"

android:text="Go to Second

Activity"android:layout_marginBottom=

"20dp"/>

<Buttonandroid:id="@+id/buttonImplicit"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="OpenGoogleWebsite"/>

</LinearLayout>

//activity_second.xml

<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayout

xmlns:android="[Link]

android:layout_width="match_parent"

android:layout_height="match_parent"

android:gravity="center"

android:orientation="vertical">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="This is Second

Activity"android:textSize="24sp"/>

</LinearLayout>
Output:
Name: Siddique Mahamadlatif Sanadi PRN:2023011032082 Batch:B4 RollNo:234

ExperimentNo:7

//[Link]

[Link];

[Link];
[Link];
[Link];
[Link];

publicclassMainActivityextendsAppCompatActivity{

ToggleButtontoggleButton;

@Override
protectedvoidonCreate(BundlesavedInstanceState){
[Link](savedInstanceState);
setContentView([Link].activity_main);

toggleButton = findViewById([Link]);

[Link]((buttonView,isChecked)->{
if(isChecked){
//ToggleButtonisON
[Link]([Link],"ToggleisON",Toast.LENGTH_SHORT).show();
}else{
//ToggleButtonisOFF
[Link]([Link],"ToggleisOFF",Toast.LENGTH_SHORT).show();
}
});
}
}

//activity_main.java

<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayout
xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"android:padding="16d
p">

<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="ON"
android:textOff="OFF"/>
</LinearLayout>
Output:
Name: Siddique Mahamadlatif Sanadi PRN:2023011032082 Batch:B4 RollNo:234

ExperimentNo:6

// [Link]

[Link];
[Link];
[Link];
[Link];
[Link];
[Link];

publicclassMainActivityextendsAppCompatActivity{

ButtonbuttonAlert;

@Override
protectedvoidonCreate(BundlesavedInstanceState){
[Link](savedInstanceState);
setContentView([Link].activity_main);

buttonAlert=findViewById([Link]);

[Link](v -> {
[Link]=[Link]([Link]);
[Link]("Exit App");
[Link]("Areyousureyouwanttoexit?");

[Link]("Yes",(dialog,which)-
>[Link]([Link],"YouclickedYes",Toast.LENGTH_SHORT).show()
);
[Link]("No",(dialog,which)-
>[Link]([Link],"YouclickedNo",Toast.LENGTH_SHORT).show()
);
[Link]();
});
}
}

// activity_main.xml

<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayout
xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"android:padding="16d
p">

<Button
android:id="@+id/buttonAlert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Alert Dialog"/>
</LinearLayout>
Output:
Name: Siddique Mahamadlatif Sanadi PRN:2023011032082 Batch:B4 RollNo:234

ExperimentNo:8
// [Link]

[Link];

import [Link];
[Link];
[Link].*;
import [Link];

publicclassMainActivityextendsAppCompatActivity{

EditTextusernameSignup,passwordSignup,usernameLogin,passwordLogin;
Button signupBtn, loginBtn, switchToSignupBtn, switchToLoginBtn;
LinearLayout signupLayout, loginLayout;

StringstoredUsername="",storedPassword="";

@Override
protectedvoidonCreate(BundlesavedInstanceState){
[Link](savedInstanceState);
setContentView([Link].activity_main);

//Loginform
usernameLogin=findViewById([Link]);
passwordLogin = findViewById([Link]);
loginBtn = findViewById([Link]);
switchToSignupBtn=findViewById([Link]);
loginLayout = findViewById([Link]);

//Signupform
usernameSignup = findViewById([Link]);
passwordSignup = findViewById([Link]);
signupBtn = findViewById([Link]);
switchToLoginBtn=findViewById([Link]);
signupLayout = findViewById([Link]);

// Switch to signup
[Link](v->{
[Link]([Link]);
[Link]([Link]);
});

// Switch to login
[Link](v->{
[Link]([Link]);
[Link]([Link]);
});

// Signup button logic


[Link](v->{
storedUsername=[Link]().toString();storedPassword
= [Link]().toString();
[Link](this,"Signupsuccessful!",Toast.LENGTH_SHORT).show();
});
// Login button logic
[Link](v->{
StringenteredUser=[Link]().toString();
StringenteredPass=[Link]().toString();

if([Link](storedUsername)&&[Link](storedPassword)){
[Link](this, "Login successful!", Toast.LENGTH_SHORT).show();
}else{
[Link](this,"Invalidcredentials!",Toast.LENGTH_SHORT).show();
}
});
}
}

// activity_main.xml

<?xmlversion="1.0"encoding="utf-8"?>
<ScrollViewxmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"android:l
ayout_height="wrap_content"android:orientatio
n="vertical" android:padding="20dp">

<!--LoginLayout-->
<LinearLayout
android:id="@+id/loginLayout"
android:layout_width="match_parent"a
ndroid:layout_height="wrap_content"an
droid:orientation="vertical">

<EditText
android:id="@+id/usernameLogin"
android:hint="Username"
android:layout_width="match_parent"an
droid:layout_height="wrap_content"/>

<EditText
android:id="@+id/passwordLogin"
android:hint="Password"
android:inputType="textPassword"
android:layout_width="match_parent"an
droid:layout_height="wrap_content"/>

<Button
android:id="@+id/loginBtn"
android:text="Login"
android:layout_width="match_parent"and
roid:layout_height="wrap_content"/>
<Button
android:id="@+id/switchToSignupBtn"and
roid:text="Go to
Signup"android:layout_width="match_par
ent"android:layout_height="wrap_conten
t"/>
</LinearLayout>

<!--SignupLayout-->
<LinearLayout
android:id="@+id/signupLayout"
android:layout_width="match_parent"a
ndroid:layout_height="wrap_content"an
droid:orientation="vertical"
android:visibility="gone">

<EditText
android:id="@+id/usernameSignup"androi
d:hint="Create
Username"android:layout_width="match_
parent"android:layout_height="wrap_con
tent"/>

<EditText
android:id="@+id/passwordSignup"androi
d:hint="Create
Password"android:inputType="textPasswo
rd"android:layout_width="match_parent"
and roid:layout_height="wrap_content"/>

<Button
android:id="@+id/signupBtn"
android:text="Signup"
android:layout_width="match_parent"and
roid:layout_height="wrap_content"/>

<Button
android:id="@+id/switchToLoginBtn"andr
oid:text="Go to
Login"android:layout_width="match_pare
nt"android:layout_height="wrap_content
"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
Output:
Name: Siddique Mahamadlatif Sanadi PRN:2023011032082 Batch:B4 RollNo:234

ExperimentNo:9

//[Link]

[Link];

[Link];
[Link];
[Link];
[Link];

publicclassMainActivityextendsAppCompatActivity{

WebViewwebView;

@Override
protectedvoidonCreate(BundlesavedInstanceState){
[Link](savedInstanceState);
setContentView([Link].activity_main);

webView=findViewById([Link]);
[Link](newWebViewClient());//Toopenlinkinsideapp
[Link]("[Link]
}
}

//activity_main.xml

<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayout
xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<WebView
android:id="@+id/webView"
android:layout_width="match_parent"android:la
yout_height="match_parent"/>
</LinearLayout>

//[Link]

<uses-permissionandroid:name="[Link]"/>

<application...>
<activityandroid:name=".MainActivity">
<intent-filter>
<actionandroid:name="[Link]"/>
<categoryandroid:name="[Link]"/>
</intent-filter>
</activity>
</application>
//Output:
Name: Siddique Mahamadlatif Sanadi PRN:2023011032082 Batch:B4 RollNo:234

ExperimentNo:10

//[Link]

[Link];

[Link];
[Link];
[Link];
[Link].*;
[Link];
[Link];

publicclassMainActivityextendsAppCompatActivity{
Button dateBtn, timeBtn;
TextViewresult;

@Override
protected void onCreate(Bundle b) {
[Link](b);
setContentView([Link].activity_main);

dateBtn=findViewById([Link]);
timeBtn=findViewById([Link]);
result=findViewById([Link]);

Calendarcalendar=[Link]();

[Link](v -> {
inty=[Link]([Link]);
intm=[Link]([Link]);
intd=[Link](Calendar.DAY_OF_MONTH);
new DatePickerDialog(this, (view, year, month, day) -> {
[Link]("SelectedDate:"+day+"/"+(month+1)+"/"+year);
},y,m,d).show();
});

[Link](v->{
inth=[Link](Calendar.HOUR_OF_DAY);
int min = [Link]([Link]);
new TimePickerDialog(this,(view,hour,minute) -> {
[Link]("SelectedTime:"+hour+":"+minute);
},h,min,false).show();
});
}
}

//activity_main.xml

<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"android:orientation="vertical"android:gravity="center"android:paddi
ng="20dp">

<Button android:id="@+id/dateBtn" android:text="Pick


Date"android:layout_width="wrap_content"android:layout_height="wrap_content"/>
<Button android:id="@+id/timeBtn" android:text="Pick
Time"android:layout_width="wrap_content"android:layout_height="wrap_content"
id:layout_width="wrap_content"android:layout_height="wrap_content"
id:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margi
nTop="10dp" />

<TextViewandroid:id="@+id/resultText"
android:layout_width="wrap_content"android:layout_height="wrap_content"andr
android:layout_width="wrap_content"android:layout_height="wrap_content"andr
oid:text="Selected value will appear here"android:layout_marginTop="20dp"/>
</LinearLayout>

Output:
Name: Siddique Mahamadlatif Sanadi PRN:2023011032082 Batch:B4 RollNo:234

ExperimentNo:11

//[Link]

[Link];

import [Link];
[Link];
[Link];
[Link];
[Link].*;
[Link];
[Link];

publicclassMainActivityextendsAppCompatActivity{
Button btn;
TextViewresult;

@Override
protected void onCreate(Bundle b) {
[Link](b);
setContentView([Link].activity_main);

btn=findViewById([Link]);
result=findViewById([Link]);

[Link](v->{
PopupMenupopup=newPopupMenu([Link],btn);
MenuInflater inflater = [Link]();
[Link]([Link].popup_menu,[Link]());
[Link](item -> {
[Link]("Youselected:"+[Link]());
return true;
});
[Link]();
});
}
}

//activity_main.xml

<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"android:orientation="vertical"android:gravity="center"android:paddi
ng="20dp">

<Button android:id="@+id/popupBtn" android:text="Show Popup


Menu"android:layout_width="wrap_content"android:layout_height="wrap_content"/>

<TextViewandroid:id="@+id/textView"
android:layout_width="wrap_content"android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Selecteditemwillappearhere"/>
</LinearLayout>
//res/menu/popup_menu.xml

<menuxmlns:android="[Link]
[Link]
<itemandroid:id="@+id/item1"android:title="Option1"/>
<itemandroid:id="@+id/item2"android:title="Option2"/>
<itemandroid:id="@+id/item3"android:title="Option3"/>
</menu>

Output:
Name: Siddique Mahamadlatif Sanadi PRN:2023011032082 Batch:B4 RollNo:234

ExperimentNo:12

//[Link]

[Link];

[Link];
[Link];
[Link].*;
[Link];

publicclassMainActivityextendsAppCompatActivity{

EditTextnameBox,idBox;
ButtoninsertBtn,updateBtn,deleteBtn;
DBHelper db;

@Override
protected void onCreate(Bundle b) {
[Link](b);
setContentView([Link].activity_main);

nameBox=findViewById([Link]);
idBox = findViewById([Link]);
insertBtn= findViewById([Link]);
updateBtn=findViewById([Link]);
deleteBtn=findViewById([Link]);

db = new DBHelper(this);

[Link](v->{
booleaninserted=[Link]([Link]().toString(),[Link]().toString());
[Link](this, inserted ? "Inserted" : "Failed", Toast.LENGTH_SHORT).show();
});

[Link](v->{
booleanupdated=[Link]([Link]().toString(),[Link]().toString());
[Link](this, updated ? "Updated" : "Failed", Toast.LENGTH_SHORT).show();
});

[Link](v->{
Integerdeleted=[Link]([Link]().toString());
[Link](this,deleted>0?"Deleted":"Failed",Toast.LENGTH_SHORT).show();
});
}
}
//[Link]

[Link];

[Link];
[Link];import
[Link].*; import
[Link];

publicclassDBHelperextendsSQLiteOpenHelper{
public DBHelper(Context ctx) {
super(ctx,"StudentDB",null,1);
}

publicvoidonCreate(SQLiteDatabasedb){
[Link]("CREATETABLEstudent(idTEXTPRIMARYKEY,nameTEXT)");
}

publicvoidonUpgrade(SQLiteDatabasedb,intoldV,intnewV){
[Link]("DROP TABLE IF EXISTS student");
onCreate(db);
}

publicbooleaninsertData(Stringid,Stringname){
SQLiteDatabasedb=[Link]();
ContentValues cv = new ContentValues();
[Link]("id",id);
[Link]("name",name);
longresult=[Link]("student",null,cv);
return result != -1;
}

publicbooleanupdateData(Stringid,Stringname){
SQLiteDatabasedb=[Link]();
ContentValues cv = new ContentValues();
[Link]("name",name);
intresult=[Link]("student",cv,"id=?",newString[]{id});return
result > 0;
}

publicintdeleteData(Stringid){
SQLiteDatabase db = [Link]();
[Link]("student","id=?",newString[]{id});
}
}
//activity_main.xml

<LinearLayout xmlns:android="[Link]
android:orientation="vertical"
android:gravity="center"android:layout_width="match_parent"android:layout_height="match_paren
t"android: padding="20dp">

<EditText android:id="@+id/idBox" android:hint="Enter


ID"android:layout_width="match_parent"android:layout_height="wrap_content"/>

<EditText android:id="@+id/nameBox" android:hint="Enter


Name"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marg
inTop="10dp" />

<Button android:id="@+id/insertBtn"
android:text="Insert"android:layout_width="match_parent"android:layout_height="wrap_content"androi
d:layout_marginTop="20dp" />

<Button android:id="@+id/updateBtn"
android:text="Update"android:layout_width="match_parent"android:layout_height="wrap_content"andr
oid:layout_marginTop="10dp" />

<Button android:id="@+id/deleteBtn"
android:text="Delete"android:layout_width="match_parent"android:layout_height="wrap_content"andro
id:layout_marginTop="10dp" />
</LinearLayout>
Output:

You might also like