Lab 8 - Intents, List, Timer: Code

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 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