0% found this document useful (0 votes)
31 views

Lab 8 - Intents, List, Timer: Code

This document contains code for three Android activities: 1) A main activity that gets user input via an edit text and button click, then sends the input to a second activity via an intent extra. 2) A second activity that receives the intent extra from the first activity and displays the received text. 3) A third activity that uses a spinner (drop-down list) to select a department, and buttons to presumably pass the selection to another activity.

Uploaded by

himanshu tiwari
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)
31 views

Lab 8 - Intents, List, Timer: Code

This document contains code for three Android activities: 1) A main activity that gets user input via an edit text and button click, then sends the input to a second activity via an intent extra. 2) A second activity that receives the intent extra from the first activity and displays the received text. 3) A third activity that uses a spinner (drop-down list) to select a department, and buttons to presumably pass the selection to another activity.

Uploaded by

himanshu tiwari
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/ 8

Lab 8 - Intents, List, Timer

Code:
package com.example.myapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
EditText e1;
String name;
Button bt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1 = (EditText) findViewById(R.id.editText);
bt = (Button) findViewById(R.id.button);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
name = e1.getText().toString();
Intent i = new Intent(MainActivity.this, Main2Activity.class);
i.putExtra("name_key", name);
startActivity(i);
}
});
}
}

package com.example.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class Main2Activity extends AppCompatActivity {
TextView t1;
String name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
t1 = (TextView) findViewById(R.id.textView2);
Intent i = getIntent();
name=i.getStringExtra("name_key");
t1.setText(name);
}
}
Output:
Code:
package com.example.him2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView;
listView = (ListView) findViewById(R.id.view2);
ArrayList<String> myfam = new ArrayList<String>();
myfam.add("rob");
myfam.add("rob1");
myfam.add("rob2");
myfam.add("rob3");
myfam.add("himan");
myfam.add("himansh");
myfam.add("himanshu");
ArrayAdapter<String> arrayAdapter = new
ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
myfam);
listView.setAdapter(arrayAdapter);
}
}
Output:
Code:
package com.example.myapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
public class MainActivity extends AppCompatActivity {
EditText e1;
String name;
Button bt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] users = { "CSE", "EEE", "ECE", "MECH", "CIVIL"
};
Spinner spin = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new
ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
users);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_
dropdown_item);
spin.setAdapter(adapter);
e1 = (EditText) findViewById(R.id.editText);
bt = (Button) findViewById(R.id.button);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
name = e1.getText().toString();
Intent i = new Intent(MainActivity.this,
Main2Activity.class);
i.putExtra("name_key", name);
startActivity(i);
}
});
}
}

Output:

You might also like