Imagine I have checkbox with criteria in main activity and table in second activity. I want to link table to checkbox so for example when price and mileage is checked, only price and mileage columns are displayed. But when nothing is checked table is not appearing.I would really appreciate if someone could explain how to do it or give me a link to tutorial.
MainActivity.java:
package todo.beginner.com.carchooser2;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
import static todo.beginner.com.carchooser2.R.id.checkBoxPrice;
import static todo.beginner.com.carchooser2.R.id.checkBoxGas;
import static todo.beginner.com.carchooser2.R.id.checkBoxYear;
import static todo.beginner.com.carchooser2.R.id.checkBoxMileage;
import static todo.beginner.com.carchooser2.R.id.checkBoxCapacity;
public class MainActivity extends AppCompatActivity {
private CheckBox check1, check2, check3, check4, check5;
private static Button button_next;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerToCeckBox();
OnClickButtonListener();
}
public void OnClickButtonListener() {
button_next = (Button)findViewById(R.id.button);
button_next.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
}
);
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("Price", check1.isChecked());
startActivity(intent);
}
};
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("Year", check2.isChecked());
startActivity(intent);
}
};
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("Capacity", check3.isChecked());
startActivity(intent);
}
};
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("Gas", check4.isChecked());
startActivity(intent);
}
};
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("Mileage", check5.isChecked());
startActivity(intent);
}
};
}
public void addListenerToCeckBox() {
check1 = (CheckBox)findViewById(checkBoxCena);
check1.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox)v).isChecked()){
Toast.makeText(MainActivity.this,
"Price is chosen", Toast.LENGTH_LONG).show();
}
}
}
);
check2 = (CheckBox)findViewById(checkBoxGads);
check2.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox)v).isChecked()){
Toast.makeText(MainActivity.this,
"Year is chosen", Toast.LENGTH_LONG).show();
}
}
}
);
check3 = (CheckBox)findViewById(checkBoxTilpums);
check3.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox)v).isChecked()){
Toast.makeText(MainActivity.this,
"Engine capacity is chosen", Toast.LENGTH_LONG).show();
}
}
}
);
check4 = (CheckBox)findViewById(checkBoxDegviela);
check4.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox)v).isChecked()){
Toast.makeText(MainActivity.this,
"Gas consumption is chosen", Toast.LENGTH_LONG).show();
}
}
}
);
check5 = (CheckBox)findViewById(checkBoxNobraukums);
check5.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox)v).isChecked()){
Toast.makeText(MainActivity.this,
"Mileage is chosen", Toast.LENGTH_LONG).show();
}
}
}
);
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="todo.beginner.com.carchooser2.MainActivity">
<CheckBox
android:text="Price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/checkBoxPrice"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="67dp" />
<CheckBox
android:text="Year"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/checkBoxPrice"
android:layout_alignRight="#+id/checkBoxPrice"
android:layout_alignEnd="#+id/checkBoxPrice"
android:layout_marginTop="33dp"
android:id="#+id/checkBoxYear" />
<CheckBox
android:text="Capacity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="37dp"
android:id="#+id/checkBoxCapacity"
android:layout_below="#+id/checkBoxYear"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<CheckBox
android:text="Gas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/checkBoxCapacity"
android:layout_alignRight="#+id/checkBoxCapacity"
android:layout_alignEnd="#+id/checkBoxCapacity"
android:layout_marginTop="30dp"
android:id="#+id/checkBoxGas" />
<CheckBox
android:text="Mileage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/checkBoxGas"
android:layout_alignRight="#+id/checkBoxGas"
android:layout_alignEnd="#+id/checkBoxGas"
android:layout_marginTop="33dp"
android:id="#+id/checkBoxMileage" />
<Button
android:text="Continue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="31dp"
android:id="#+id/button" />
<TextView
android:text="Choose criteria!"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
SecondActivity.java:
package todo.beginner.com.carchooser2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
boolean hasPrice = getIntent().getBooleanExtra("Price", true);
boolean hasYear = getIntent().getBooleanExtra("Year", true);
boolean hasCapacity = getIntent().getBooleanExtra("Capacity", true);
boolean hasGas = getIntent().getBooleanExtra("Gas", true);
boolean hasMileage = getIntent().getBooleanExtra("Mileage", true);
TextView Price = (TextView) findViewById(R.id.Price); // you will need to create this id in your layout
Price.setVisibility(hasPrice ? View.VISIBLE : View.GONE);
TextView Year = (TextView) findViewById(R.id.Year); // you will need to create this id in your layout
Year.setVisibility(hasYear ? View.VISIBLE : View.GONE);
TextView Capacity = (TextView) findViewById(R.id.Capacity); // you will need to create this id in your layout
Capacity.setVisibility(hasCapacity ? View.VISIBLE : View.GONE);
TextView Gas = (TextView) findViewById(R.id.Gas); // you will need to create this id in your layout
Gas.setVisibility(hasGas ? View.VISIBLE : View.GONE);
TextView Mileage = (TextView) findViewById(R.id.Mileage); // you will need to create this id in your layout
Mileage.setVisibility(hasMileage ? View.VISIBLE : View.GONE);
}
}
activity_second.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp">
<TableRow
android:background="#607D8B"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/Name"
android:text="Car Name" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/Price"
android:text="Price" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/Year"
android:text="Year" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/Gas"
android:text="Gas" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/Mileage"
android:text="Mileage" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/Capacity"
android:text="Capacity" />
</TableRow>
<TableRow
android:background="#ECEFF1"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Audi" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2001" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="280000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2.5" />
</TableRow>
</TableLayout>
In your OnClickButtonListener, you are declaring a lot of new View.OnClickListener()s, but you are just creating the implementations then not doing anything with them. Those should all be removed. Your method should look like this:
public void OnClickButtonListener() {
button_next = (Button)findViewById(R.id.button);
button_next.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("Price", check1.isChecked());
intent.putExtra("Year", check2.isChecked());
intent.putExtra("Capacity", check3.isChecked());
intent.putExtra("Gas", check4.isChecked());
intent.putExtra("Mileage", check5.isChecked());
startActivity(intent);
}
}
);
}
See how it parallels with the code in the onCreate method in the SecondActivity?
After this change, you should see about what you expect to see.
Your first step would put be to create an Intent that has the values of the checkboxes, and pass that to the second activity:
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("name", check1.isChecked());
// same for other checkboxes
startActivity(intent);
Next, in your SecondActivity access those values:
boolean hasName = getIntent().getBooleanExtra("name", true);
When you are creating your TableRow, set the visibility based on the values:
TextView carName = (TextView) findViewById(R.id.car_name); // you will need to create this id in your layout
carName.setVisibility(hasName ? View.VISIBLE : View.GONE);
Remember, if nothing is checked then you won't see anything, so you should probably start MainActivity with all the checkboxes checked to begin with.
Related
I created a android studio button for my app and when I click on the register button it doesn't work . I don't get any errors it just doesn't work . When the user clicks the quiz button I want to go to the quiz activity
MainActivity.java
package com.littlekidsmath.yoong.mathlearningforkids;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.Toast;
import java.util.zip.Inflater;
public class MainActivity extends AppCompatActivity implements View.OnClickListener,AdapterView.OnItemSelectedListener{
Button addBtn, subBtn, multiBtn, divisionBtn,quiz;
String[] levels = {"Easy","Medium","Hard"};
SharedPreferences prefs;
Switch settings;
Spinner language;
public String[] languages = {GameActivity.ENG,GameActivity.ARABIC,"বাংলা",GameActivity.FRENCH,GameActivity.GERMAN,GameActivity.MALAY};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prefs = getSharedPreferences("MyPref",MODE_PRIVATE);
settings = (Switch) findViewById(R.id.settings);
if(prefs.getBoolean("SOUND", false)){
settings.setChecked(true);
}
settings.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
prefs.edit().putBoolean("SOUND",b).commit();
}
});
addBtn = (Button) findViewById(R.id.addtion);
subBtn = (Button) findViewById(R.id.sub);
multiBtn = (Button) findViewById(R.id.multi);
divisionBtn = (Button) findViewById(R.id.divide);
quiz = (Button) findViewById(R.id.quiz);
language = (Spinner) findViewById(R.id.language);
addBtn.setOnClickListener(this);
subBtn.setOnClickListener(this);
multiBtn.setOnClickListener(this);
divisionBtn.setOnClickListener(this);
quiz.setOnClickListener(this);
language.setOnItemSelectedListener(this);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,languages);
language.setAdapter(adapter);
int pos = 0;
//
for(int i=0;i<languages.length;i++){
if(prefs.getString(GameActivity.LANGUAGE,"").equals(languages[i])){
pos = i;
break;
}
}
language.setSelection(pos);
if(prefs.getString(GameActivity.LANGUAGE,"").equals(GameActivity.BANGLA)){
setBangla();
}else if(prefs.getString(GameActivity.LANGUAGE,"").equals(GameActivity.ARABIC)){
setArabic();
}else if(prefs.getString(GameActivity.LANGUAGE,"").equals(GameActivity.FRENCH)){
setFrence();
}else if(prefs.getString(GameActivity.LANGUAGE,"").equals(GameActivity.GERMAN)){
setGerman();
}else if(prefs.getString(GameActivity.LANGUAGE,"").equals(GameActivity.ENG)){
setEnglish();
}else if(prefs.getString(GameActivity.LANGUAGE,"").equals(GameActivity.MALAY)){
setMalay();
}
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.addtion: {
levelChooseDialog("+");
break;
}
case R.id.sub: {
levelChooseDialog("-");
break;
}
case R.id.multi: {
levelChooseDialog("X");
break;
}
case R.id.divide: {
levelChooseDialog("/");
break;
}
case R.id.quiz: {
Intent intent = new Intent(MainActivity.this, HomeScreen.class);
startActivity(intent);
}
}
}
public void levelChooseDialog(final String operator){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
View view = View.inflate(this,R.layout.level_dialog,null);
builder.setView(view);
ListView listView = (ListView) view.findViewById(R.id.listview);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,levels);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int level = 0;
if (position == 0){
level = 0;
}else if(position == 1){
level = 1;
}else {
level = 2;
}
startActivity(new Intent(MainActivity.this,LessonActivity.class).putExtra("level",level)
.putExtra("operator",operator));
}
});
builder.create().show();
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
prefs.edit().putString(GameActivity.LANGUAGE,languages[position]).commit();
if(prefs.getString(GameActivity.LANGUAGE,"").equals(GameActivity.BANGLA))
{
setBangla();
}else if(prefs.getString(GameActivity.LANGUAGE,"").equals(GameActivity.ARABIC)){
setArabic();
}else if(prefs.getString(GameActivity.LANGUAGE,"").equals(GameActivity.FRENCH)){
setFrence();
}else if(prefs.getString(GameActivity.LANGUAGE,"").equals(GameActivity.GERMAN)){
setGerman();
}else if(prefs.getString(GameActivity.LANGUAGE,"").equals(GameActivity.ENG)){
setEnglish();
}else if(prefs.getString(GameActivity.LANGUAGE,"").equals(GameActivity.MALAY)){
setMalay();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
public void setBangla(){
addBtn.setText(Languages.BANGLA[0]);
subBtn.setText(Languages.BANGLA[1]);
multiBtn.setText(Languages.BANGLA[3]);
divisionBtn.setText(Languages.BANGLA[2]);
settings.setText(Languages.BANGLA[4]);
levels[0] = Languages.BANGLA[7];
levels[1] = Languages.BANGLA[8];
levels[2] = Languages.BANGLA[9];
}
public void setArabic(){
addBtn.setText(Languages.ARABIC[0]);
subBtn.setText(Languages.ARABIC[1]);
multiBtn.setText(Languages.ARABIC[3]);
divisionBtn.setText(Languages.ARABIC[2]);
settings.setText(Languages.ARABIC[4]);
levels[0] = Languages.ARABIC[7];
levels[1] = Languages.ARABIC[8];
levels[2] = Languages.ARABIC[9];
}
public void setMalay(){
addBtn.setText(Languages.MALAY[0]);
subBtn.setText(Languages.MALAY[1]);
multiBtn.setText(Languages.MALAY[3]);
divisionBtn.setText(Languages.MALAY[2]);
settings.setText(Languages.MALAY[4]);
levels[0] = Languages.MALAY[7];
levels[1] = Languages.MALAY[8];
levels[2] = Languages.MALAY[9];
}
public void setFrence(){
addBtn.setText(Languages.FRENCH[0]);
subBtn.setText(Languages.FRENCH[1]);
multiBtn.setText(Languages.FRENCH[3]);
divisionBtn.setText(Languages.FRENCH[2]);
settings.setText(Languages.FRENCH[4]);
levels[0] = Languages.FRENCH[7];
levels[1] = Languages.FRENCH[8];
levels[2] = Languages.FRENCH[9];
}
public void setGerman(){
addBtn.setText(Languages.GERMAN[0]);
subBtn.setText(Languages.GERMAN[1]);
multiBtn.setText(Languages.GERMAN[3]);
divisionBtn.setText(Languages.GERMAN[2]);
settings.setText(Languages.GERMAN[4]);
levels[0] = Languages.GERMAN[7];
levels[1] = Languages.GERMAN[8];
levels[2] = Languages.GERMAN[9];
}
public void setEnglish(){
addBtn.setText(Languages.ENGLISH[0]);
subBtn.setText(Languages.ENGLISH[1]);
multiBtn.setText(Languages.ENGLISH[3]);
divisionBtn.setText(Languages.ENGLISH[2]);
settings.setText(Languages.ENGLISH[4]);
levels[0] = Languages.ENGLISH[7];
levels[1] = Languages.ENGLISH[8];
levels[2] = Languages.ENGLISH[9];
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorAccent"
android:gravity="center_vertical"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.littlekidsmath.yoong.mathlearningforkids.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/addtion"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawablePadding="#dimen/activity_horizontal_margin"
android:drawableTop="#drawable/add"
android:paddingBottom="25dp"
android:paddingTop="25dp"
android:text="Addition" />
<Button
android:id="#+id/sub"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawablePadding="#dimen/activity_horizontal_margin"
android:drawableTop="#drawable/minus"
android:paddingBottom="25dp"
android:paddingTop="25dp"
android:text="Subtraction" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/multi"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawablePadding="#dimen/activity_horizontal_margin"
android:drawableTop="#drawable/cancel"
android:paddingBottom="25dp"
android:paddingTop="25dp"
android:text="Multiplication" />
<Button
android:id="#+id/divide"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawablePadding="#dimen/activity_horizontal_margin"
android:drawableTop="#drawable/division"
android:paddingBottom="25dp"
android:paddingTop="25dp"
android:text="Division" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/quiz"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawablePadding="#dimen/activity_horizontal_margin"
android:paddingBottom="20dp"
android:paddingTop="20dp"
android:text="Quiz" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:padding="#dimen/activity_horizontal_margin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:text=""
android:textColor="#color/white"
android:textSize="18sp" />
<Switch
android:id="#+id/settings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingRight="#dimen/activity_horizontal_margin"
android:text="Sound"
android:textColor="#color/white"
android:textSize="18sp" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:text="Language" />
<Spinner
android:id="#+id/language"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="7dp"
android:background="#color/white"
android:padding="10dp"></Spinner>
</LinearLayout>
Can anyone help please? When click on the quiz button the application will close. I want it to go to quiz page which called HomeScreen.java
looks good to me, did u declare the second activity in the manifest file? :
<activity android:name="HomeScreen"/>
If that doesn't work, use some break points and the debugger, hope it helps.
I have created admin_content.xml layout and put ListView in that layout. There is also a button to add new list item to that ListView. This layout's java file is AdminContent.java and I created my adapter inside this file. When I click to "Add" button it goes to add_school_info.xml layout and I get the information from this layout with Bundle class and send it to my AdminContent.java file. However, when I add new school info, it only shows last entry. Here is what I have done so far:
admin_content.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/adminTab"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Informations -->
<ListView
android:id="#+id/infoList"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="10dp"
android:layout_weight="6">
</ListView>
<!-- Buttons -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="#+id/addAdmin"
android:text="#string/btn_add"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="#fff"
android:background="#color/success" />
<Button
android:id="#+id/updateAdmin"
android:text="#string/btn_update"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="#fff"
android:background="#color/primary" />
<Button
android:id="#+id/deleteAdmin"
android:text="#string/btn_search"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="#fff"
android:background="#color/info" />
<Button
android:id="#+id/cancelAdmin"
android:text="#string/btn_delete"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="#fff"
android:background="#color/danger" />
</LinearLayout>
<!-- #END Buttons -->
</LinearLayout>
AdminContent.java
package com.example.android.students;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
public class AdminContent extends Activity {
private Button addAdmin, cancelAdmin;
private ArrayList<SchoolInfos> myArrayList = new ArrayList<SchoolInfos>();
private ListView infoList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.admin_content);
addClickListener();
Bundle bundle = getIntent().getExtras();
String faculty = bundle.getString("Faculty");
String department = bundle.getString("Department");
String advisor = bundle.getString("Advisor");
myArrayList.add(new SchoolInfos(faculty, department, advisor));
infoList = (ListView) findViewById(R.id.infoList);
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, myArrayList);
infoList.setAdapter(adapter);
}
public void addClickListener() {
final Context context = this;
addAdmin = (Button) findViewById(R.id.addAdmin);
addAdmin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, AdminTab.class);
startActivity(intent);
}
});
cancelAdmin = (Button) findViewById(R.id.cancelAdmin);
cancelAdmin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, MainActivity.class);
startActivity(intent);
}
});
}
}
add_school_info.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/add_school_info"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Registration Form -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6"
android:paddingHorizontal="20dp"
android:orientation="vertical">
<!-- Faculty -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:text="#string/txt_faculty"
android:textStyle="bold"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"/>
<EditText
android:id="#+id/editFaculty"
android:text="Hey!"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
<!-- Last Name -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:text="#string/txt_department"
android:textStyle="bold"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"/>
<EditText
android:id="#+id/editDepartment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
<!-- Gender -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:text="#string/txt_advisor"
android:textStyle="bold"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"/>
<EditText
android:id="#+id/editAdvisor"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
<!-- Buttons -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/addInfo"
android:text="#string/btn_add"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="#fff"
android:background="#color/success"/>
<Button
android:id="#+id/clearInfo"
android:text="#string/btn_clear"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="#333"
android:background="#color/warning"/>
<Button
android:id="#+id/cancelInfo"
android:text="#string/btn_cancel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="#fff"
android:background="#color/danger"/>
</LinearLayout>
</LinearLayout>
<!-- #END Buttons -->
</LinearLayout>
AdminTab.java: This file is getting data from EditText fields and send it to my AdminContent.java file via Bundle class.
package com.example.android.students;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class AdminTab extends Activity {
private EditText editFaculty, editDepartment, editAdvisor;
private Button clearInfo, cancelInfo, addInfo;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_school_info);
addClickListener();
}
public void addClickListener() {
final Context context = this;
addInfo = (Button) findViewById(R.id.addInfo);
clearInfo = (Button) findViewById(R.id.clearInfo);
cancelInfo = (Button) findViewById(R.id.cancelInfo);
editFaculty = (EditText) findViewById(R.id.editFaculty);
editDepartment = (EditText) findViewById(R.id.editDepartment);
editAdvisor = (EditText) findViewById(R.id.editAdvisor);
addInfo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, AdminContent.class);
String faculty = editFaculty.getText().toString();
String department = editDepartment.getText().toString();
String advisor = editAdvisor.getText().toString();
Bundle bundle = new Bundle();
bundle.putString("Faculty", faculty);
bundle.putString("Department", department);
bundle.putString("Advisor", advisor);
intent.putExtras(bundle);
startActivity(intent);
}
});
clearInfo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
editFaculty.setText(null);
editDepartment.setText(null);
editAdvisor.setText(null);
}
});
cancelInfo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, MainActivity.class);
startActivity(intent);
}
});
}
}
Lastly, this is my SchoolInfo.java class which is to put data to my ArrayList.
package com.example.android.students;
public class SchoolInfos {
private String faculty, department, advisor;
public SchoolInfos() {
}
public SchoolInfos(String faculty, String department, String advisor) {
this.faculty = faculty;
this.department = department;
this.advisor = advisor;
}
public String getFaculty() {
return faculty;
}
public void setFaculty(String faculty) {
this.faculty = faculty;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getAdvisor() {
return advisor;
}
public void setAdvisor(String advisor) {
this.advisor = advisor;
}
#Override
public String toString() {
return "SchoolInfos{" +
"faculty='" + faculty + '\'' +
", department='" + department + '\'' +
", advisor='" + advisor + '\'' +
'}';
}
}
The problem is that, it seems when I add a new entry with add_school_info.xml layout, it actually doesn't show my whole ArrayList. it only shows last entry.
Add this value & method in AdminContent.Java
static final int PICK_ADMIN_DATA = 1; // The request code
ArrayAdapter adapter; // declare globally
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_ADMIN_DATA) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
}
}
}
Change this method
addAdmin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, AdminTab.class);
startActivity(intent);
}
});
To
addAdmin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, AdminTab.class);
startActivityForResult(intent, PICK_ADMIN_DATA);
}
});
In AdminTab.Java set result to intent.
addInfo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
String faculty = editFaculty.getText().toString();
String department = editDepartment.getText().toString();
String advisor = editAdvisor.getText().toString();
Bundle bundle = new Bundle();
bundle.putString("Faculty", faculty);
bundle.putString("Department", department);
bundle.putString("Advisor", advisor);
intent.putExtras(bundle);
setResult(Activity.RESULT_OK,intent);
finish();
}
}
Then get result from AdminContent.Java OnActivityResult method
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_ADMIN_DATA) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
String faculty = bundle.getString("Faculty");
String department = bundle.getString("Department");
String advisor = bundle.getString("Advisor");
myArrayList.add(new SchoolInfos(faculty, department, advisor));
adapter.notifyDataSetChanged();
}
}
}
Hope it helps.!
So, content of second activity does not appear when app is running, although content is showing in xml design. Programming in Java on Android Studio. In similar article answer didn't help. I also tried just to put one element in second activity, same result. Thanks in advance!
This is code from MainActivity.java:
package todo.beginner.com.carchooser2;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
import static todo.beginner.com.carchooser2.R.id.checkBoxPrice;
import static todo.beginner.com.carchooser2.R.id.checkBoxGas;
import static todo.beginner.com.carchooser2.R.id.checkBoxYear;
import static todo.beginner.com.carchooser2.R.id.checkBoxMileage;
import static todo.beginner.com.carchooser2.R.id.checkBoxCapacity;
public class MainActivity extends AppCompatActivity {
private CheckBox check1, check2, check3, check4, check5;
private static Button button_next;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerToCeckBox();
OnClickButtonListener();
}
public void OnClickButtonListener() {
button_next = (Button)findViewById(R.id.button);
button_next.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("todo.beginner.com.SecondActivity");
startActivity(intent);
}
}
);
}
public void addListenerToCeckBox() {
check1 = (CheckBox)findViewById(checkBoxCena);
check1.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox)v).isChecked()){
Toast.makeText(MainActivity.this,
"Price is chosen", Toast.LENGTH_LONG).show();
}
}
}
);
check2 = (CheckBox)findViewById(checkBoxGads);
check2.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox)v).isChecked()){
Toast.makeText(MainActivity.this,
"Year is chosen", Toast.LENGTH_LONG).show();
}
}
}
);
check3 = (CheckBox)findViewById(checkBoxTilpums);
check3.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox)v).isChecked()){
Toast.makeText(MainActivity.this,
"Engine capacity is chosen", Toast.LENGTH_LONG).show();
}
}
}
);
check4 = (CheckBox)findViewById(checkBoxDegviela);
check4.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox)v).isChecked()){
Toast.makeText(MainActivity.this,
"Gas consumption is chosen", Toast.LENGTH_LONG).show();
}
}
}
);
check5 = (CheckBox)findViewById(checkBoxNobraukums);
check5.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox)v).isChecked()){
Toast.makeText(MainActivity.this,
"Mileage is chosen", Toast.LENGTH_LONG).show();
}
}
}
);
}
}
This is from activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="todo.raitis.com.carchooser.MainActivity">
<CheckBox
android:text="Price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/checkBoxPrice"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="67dp" />
<CheckBox
android:text="Year"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/checkBoxPrice"
android:layout_alignRight="#+id/checkBoxPrice"
android:layout_alignEnd="#+id/checkBoxPrice"
android:layout_marginTop="33dp"
android:id="#+id/checkBoxYear" />
<CheckBox
android:text="Engine Capacity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="37dp"
android:id="#+id/checkBoxCapacity"
android:layout_below="#+id/checkBoxYear"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<CheckBox
android:text="Gas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/checkBoxCapacity"
android:layout_alignRight="#+id/checkBoxCapacity"
android:layout_alignEnd="#+id/checkBoxCapacity"
android:layout_marginTop="30dp"
android:id="#+id/checkBoxGas" />
<CheckBox
android:text="Mileage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/checkBoxGas"
android:layout_alignRight="#+id/checkBoxGas"
android:layout_alignEnd="#+id/checkBoxGas"
android:layout_marginTop="33dp"
android:id="#+id/checkBoxMileage" />
<Button
android:text="Next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="31dp"
android:id="#+id/button" />
<TextView
android:text="Choose criteria!"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
This is from SecondActivity.java:
package todo.beginner.com.carchooser2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
And this is from activity_second.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp">
<TableRow
android:background="#607D8B"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Car Name" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Price" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Year" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Gas" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Mileage" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Capacity" />
</TableRow>
<TableRow
android:background="#ECEFF1"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Audi" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2001" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="280000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2.5" />
</TableRow>
</TableLayout>
You are using the intent incorrectly. It should be:
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
Don't think you're starting the activity correctly. Try changing Intent intent = new Intent("todo.beginner.com.SecondActivity"); to Intent intent = new Intent(MainActivity.this, SecondActivity.class); in your MainActivity.
Okay This is what I'm trying to make. 3 Activities. MainActivity,ActivityA & ActivityB from Main activity I'm going to ActivityA or B by Intent (Using Button). There is an EditText & a Button on each Activities A&B.. I just want to Toast the String typed on the textbox to MainActivity when I press the button on activities A or B. I provided everything below. Please someone explain what did I do wrong in the code.
Main Activity.java
package com.training.threeactivities;
import android.app.Activity;
import android.content.DialogInterface;
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.Toast;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button gotoA = (Button) findViewById(R.id.gotoA);
gotoA.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentA = new Intent(MainActivity.this, ActivityA.class);
startActivity(intentA);
}
});
Button gotoB = (Button) findViewById(R.id.gotoB);
gotoB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentB = new Intent(MainActivity.this, ActivityB.class);
startActivity(intentB);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
// check if the request code is same as what is passed here it is 2
if (requestCode == 1) {
String MsgA = intent.getStringExtra("FrmA");
Toast.makeText(MainActivity.this, MsgA, Toast.LENGTH_LONG).show();
} else if (requestCode == 2) {
String MsgB = intent.getStringExtra("FrmB");
Toast.makeText(MainActivity.this, MsgB, Toast.LENGTH_LONG).show();
}
}
}
activity_main.xml
<TextView
android:id="#+id/txtm"
android:paddingTop="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="MAIN ACTIVITY"
android:textSize="30dp" />
<Button
android:id="#+id/gotoA"
android:layout_width="350dp"
android:layout_height="150dp"
android:layout_centerHorizontal="true"
android:layout_below="#+id/txtm"
android:layout_marginTop="10dp"
android:textSize="50dp"
android:text="GOTO A" />
<Button
android:id="#+id/gotoB"
android:layout_below="#+id/gotoA"
android:layout_centerHorizontal="true"
android:layout_width="350dp"
android:layout_height="150dp"
android:layout_marginTop="10dp"
android:textSize="50dp"
android:text="GOTO B" />
ActivityA.java
public class ActivityA extends AppCompatActivity {
EditText txtA;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
Button btnA = (Button) findViewById(R.id.btnA);
btnA.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtA = (EditText) findViewById(R.id.txtA);
String MsgFrmA = txtA.getText().toString().trim();
Intent intentA = new Intent();
intentA.putExtra("FrmA", MsgFrmA);
setResult(1, intentA);
finish();
}
});
}
}
activity_a.xml
<TextView
android:paddingTop="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="ACTIVITY A"
android:textSize="30dp" />
<EditText
android:id="#+id/txtA"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_below="#+id/fa"
android:layout_centerHorizontal="true"
android:layout_marginTop="150dp"
android:background="#ffffff"
android:hint="Type The Message" />
<Button
android:id="#+id/btnA"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_below="#+id/txtA"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="SEND" />
ActivityB.java
public class ActivityB extends AppCompatActivity {
EditText txtB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
Button btnB = (Button) findViewById(R.id.btnB);
btnB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtB = (EditText) findViewById(R.id.txtB);
String MsgFrmB = txtB.getText().toString().trim();
Intent intentB = new Intent();
intentB.putExtra("FrmB", MsgFrmB);
setResult(2, intentB);
finish();
}
});
}
}
activity_b.xml
<TextView
android:paddingTop="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="ACTIVITY B"
android:textSize="30dp" />
<EditText
android:id="#+id/txtB"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_below="#+id/fa"
android:layout_centerHorizontal="true"
android:layout_marginTop="150dp"
android:background="#ffffff"
android:hint="Type The Message" />
<Button
android:id="#+id/btnB"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_below="#+id/txtB"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="SEND" />
use startActivityForResult from your main activity
If you want to get a result back from an activity, you should call
startActivityForResult(Intent, int)
But in your main activity, you have started the activity with "startActivity" which will not listen for any result from the activity.
Refer this link to know more about this.
I have 3 buttons in total. And it seemed that, if I don't press the first button, and instead second or third button right after I startup, my application will forcestop.
My code for part1 : http://pastebin.com/udkEdF3E
package project.ernest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Part1 extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button a = (Button) findViewById(R.id.Disclaimer);
a.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent b = new Intent(getBaseContext(), Part2.class);
Part1.this.startActivity(b);
final Button c = (Button) findViewById(R.id.Orientation);
c.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent d = new Intent(getBaseContext(), Part3.class);
Part1.this.startActivity(d);
final Button e = (Button) findViewById(R.id.Course);
e.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent f = new Intent(getBaseContext(), Part4.class);
Part1.this.startActivity(f);
}
});
};
});
};
});
};
}
my code for main.xml : http://pastebin.com/SxfxfJ4a
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/myColor" android:weightSum="1">
<LinearLayout android:orientation="vertical" android:id="#+id/linearLayout1" android:weightSum="1" android:layout_width="wrap_content" android:layout_height="404dp">
<TextView android:text="#string/HospName" android:id="#+id/textView1" android:textSize="20mm" android:layout_width="324dp" android:layout_height="142dp"></TextView>
<Button android:text="#string/Disclaimer" android:layout_weight="0.37" android:textSize="5mm" android:layout_height="75dp" android:layout_width="fill_parent" android:id="#+id/Disclaimer" android:drawingCacheQuality="high" android:onClick="#string/DisclaimerHandler"></Button>
<Button android:text="#string/Orientation" android:textSize="5mm" android:layout_height="75dp" android:layout_weight="0.37" android:layout_width="fill_parent" android:id="#+id/Orientation" android:onClick="#string/OrientationHandler"></Button>
<Button android:text="#string/Course" android:textSize="5mm" android:layout_height="75dp" android:layout_weight="0.37" android:layout_width="fill_parent" android:id="#+id/Course" android:onClick="#string/CourseHandler"></Button>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:orientation="horizontal" android:layout_weight="0.19" android:layout_height="wrap_content" android:id="#+id/linearLayout2" android:weightSum="1">
<Button android:layout_weight="0.23" android:text="#string/Search" android:textSize="1.9mm" android:layout_height="75dp" android:layout_width="75dp" android:onClick="#string/SearchHandler" android:id="#+id/Search"></Button>
<Button android:layout_weight="0.23" android:text="#string/Contact" android:textSize="1.9mm" android:layout_height="75dp" android:layout_width="75dp" android:onClick="#string/ContactHandler" android:id="#+id/Contact"></Button>
<Button android:onClick="#string/LinkHandler" android:layout_height="75dp" android:layout_width="75dp" android:layout_weight="0.23" android:textSize="1.9mm" android:text="#string/Link" android:id="#+id/Link"></Button>
<Button android:layout_weight="0.23" android:text="#string/Copyright" android:textSize="1.9mm" android:layout_height="75dp" android:layout_width="75dp" android:onClick="#string/CopyrightHandler" android:id="#+id/Copyright"></Button>
</LinearLayout>
</LinearLayout>
The error I get is either:
java.lang.IllegalStateException: Could not find a method CourseHandler(View) in activity class project.ernest.Part1 or...
java.lang.IllegalStateException: Could not find a method OrientationHandler(View) in activity class project.ernest.Part1 or...
Please help me out!
You've placed initialization of buttons 2 and 3 inside the first button's onClickListener(), means they won't be initialized before you click the first button. Fix your code.
this is the format.
public class Part1 extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button a = (Button) findViewById(R.id.Disclaimer);
a.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent b = new Intent(getBaseContext(), Part2.class);
Part1.this.startActivity(b);
}
});
final Button c = (Button) findViewById(R.id.Orientation);
c.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent d = new Intent(getBaseContext(), Part3.class);
Part1.this.startActivity(d);
}
});
final Button e = (Button) findViewById(R.id.Course);
e.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent f = new Intent(getBaseContext(), Part4.class);
Part1.this.startActivity(f);
}
});
}
}