I'd like to capture the currently connected SSID - and display it in a TextView - how can this be done?
From what I can gather I'll need to use the following:
public void run() {
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
String nameEt = info.getSSID();
}
But when I attempt to implement it - I cannot seem to display it on the screen.
JAVA:
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import java.text.DecimalFormat;
public class AddEditDevice extends Activity {
private long rowID;
private EditText nameEt;
private EditText capEt;
private EditText codeEt;
private TimePicker timeEt;
private TextView ssid;
// DecimalFormat df = new DecimalFormat ("00");
// public String minutes = df.format(min);
// #Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_country);
nameEt = (EditText) findViewById(R.id.nameEdit);
capEt = (EditText) findViewById(R.id.capEdit);
codeEt = (EditText) findViewById(R.id.codeEdit);
timeEt = (TimePicker) findViewById(R.id.timeEdit);
Bundle extras = getIntent().getExtras();
if (extras != null) {
rowID = extras.getLong("row_id");
nameEt.setText(extras.getString("name"));
capEt.setText(extras.getString("cap"));
codeEt.setText(extras.getString("code"));
String time = extras.getString("time");
String[] parts = time.split(":");
timeEt.setCurrentHour(Integer.valueOf(parts[0]));
timeEt.setCurrentMinute(Integer.valueOf(parts[1]));
timeEt.setIs24HourView(false);
}
Button saveButton = (Button) findViewById(R.id.saveBtn);
saveButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (nameEt.getText().length() != 0) {
AsyncTask<Object, Object, Object> saveContactTask =
new AsyncTask<Object, Object, Object>() {
#Override
protected Object doInBackground(Object... params) {
saveContact();
return null;
}
#Override
protected void onPostExecute(Object result) {
finish();
}
};
saveContactTask.execute((Object[]) null);
} else {
AlertDialog.Builder alert = new AlertDialog.Builder(
AddEditDevice.this);
alert.setTitle(R.string.errorTitle);
alert.setMessage(R.string.errorMessage);
alert.setPositiveButton(R.string.errorButton, null);
alert.show();
}
}
});
}
public void run() {
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
String ssid = info.getSSID();
TextView ssidTextView = (TextView) findViewById(R.id.wifiSSID);
ssidTextView.setText(ssid);
}
private void saveContact() {
DatabaseConnector dbConnector = new DatabaseConnector(this);
if (getIntent().getExtras() == null) {
dbConnector.insertContact(nameEt.getText().toString(), capEt
.getText().toString(), timeEt.getCurrentHour().toString() + ":"
+ timeEt.getCurrentMinute().toString(), codeEt.getText()
.toString());
} else {
dbConnector.updateContact(rowID, nameEt.getText().toString(), capEt
.getText().toString(), timeEt.getCurrentHour().toString() + ":"
+ timeEt.getCurrentMinute().toString(), codeEt.getText()
.toString());
}
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<EditText
android:id="#+id/nameEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/name_hint"
android:imeOptions="actionNext"
android:inputType="textPersonName|textCapWords" />
<TextView
android:id="#+id/wifiSSID"
style="#style/StyleText"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/capEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/cap_hint"
android:imeOptions="actionNext"
android:inputType="textPersonName|textCapWords" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Data Limit"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#ffffff" />
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="left"
android:text="Unlimited Data"
android:textColor="#ffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="right"
android:text="10MB"
android:textColor="#ffffff" />
</LinearLayout>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bandwidth Limit"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#ffffff" />
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="left"
android:text="Unlimited Bandwidth"
android:textColor="#ffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="right"
android:text="10kbs"
android:textColor="#ffffff" />
</LinearLayout>
<EditText
android:id="#+id/codeEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:ems="10"
android:gravity="center"
android:hint="#string/code_hint"
android:imeOptions="actionNext"
android:inputType="textUri"
android:lines="1" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="center"
android:text="Parental Controls"
android:textColor="#ffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="center"
android:text="Block Streaming"
android:textColor="#ffffff" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ToggleButton
android:id="#+id/ToggleButton03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="center"
android:text="ToggleButton" />
<ToggleButton
android:id="#+id/ToggleButton03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="center"
android:text="ToggleButton" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="center"
android:text="Block File Type/Size"
android:textColor="#ffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="center"
android:text="Banned Music/Video"
android:textColor="#ffffff" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ToggleButton
android:id="#+id/ToggleButton02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="center"
android:text="ToggleButton" />
<ToggleButton
android:id="#+id/ToggleButton02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="center"
android:text="ToggleButton" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal" >
</LinearLayout>
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceSmall" />
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="8dp"
android:layout_weight="1.0"
android:text="WiFi Time Limit"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#ffffff" />
<TimePicker
android:id="#+id/timeEdit"
android:layout_width="286dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1.0" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="horizontal" >
</LinearLayout>
<Button
android:id="#+id/saveBtn"
android:layout_width="199dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp"
android:text="#string/save_btn" />
</LinearLayout>
</ScrollView>
You aren't calling run() from anywhere, and that method is not trying to update a textview. Try doing this in your run method.
TextView ssidTextView = (TextView) findViewById(R.id.<your_textview_id>);
ssidTextView.setText(nameEt);
And thenmake sure you call run() whenever you want to update the ssid in the TextView.
Related
i'm beginner in android coding and im trying to switch activities using Intent class
but it do nothing on all cases (Putting data or inputting data) can someone resolve my problem ? here is my layout.xml :
i already added the other activity in the manifiest xml
i think the problem is in where i get data from the spinner or from the radioButton
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Formulaire d'inscription"
android:textAlignment="center"
android:textSize="30dp"
android:textColor="#FF0000"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="250px"
android:layout_height="wrap_content"
android:text="Nom" />
<EditText
android:id="#+id/txtNom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Saisir votre nom"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="250px"
android:layout_height="wrap_content"
android:text="Prenom :" />
<EditText
android:id="#+id/txtPrenom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Saisir votre prénom"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="250px"
android:layout_height="wrap_content"
android:text="Email :" />
<EditText
android:id="#+id/txtEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="Saisir votre email"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="250px"
android:layout_height="wrap_content"
android:text="Mot de passe" />
<EditText
android:id="#+id/txtPass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="Saisir le mot de passe"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="250px"
android:layout_height="wrap_content"
android:text="Confirmation :" />
<EditText
android:id="#+id/txtPassConfirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="Confirmation"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="250px"
android:layout_height="wrap_content"
android:text="Genre" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RadioGroup
android:id="#+id/radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/rbh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Homme"
android:checked="true">
</RadioButton>
<RadioButton
android:id="#+id/rbf"
android:layout_marginLeft="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Femme">
</RadioButton>
</RadioGroup>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="250px"
android:layout_height="wrap_content"
android:text="Pays :" />
<Spinner
android:id="#+id/spnPays"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/pays" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="40dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/btnSignUp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="INSCRIRE"
/>
</LinearLayout>
</LinearLayout>
and my java code is :
package com.fach.app1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public static String nomInscrit;
public static String prenomInscrit;
public static String emailInscrit;
public static String passwordInscrit;
public static String payInscrit;
public static String genreInscrit;
Button btnSign ;
EditText nom ;
EditText prenom ;
EditText email ;
EditText password ;
EditText passConf;
Spinner spnPay;
RadioGroup genre;
RadioButton gen;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSign = findViewById(R.id.btnSignUp);
nom = findViewById(R.id.txtNom);
prenom = findViewById(R.id.txtPrenom);
email = findViewById(R.id.txtEmail);
password = findViewById(R.id.txtPass);
passConf = findViewById(R.id.txtPassConfirm);
spnPay = findViewById(R.id.spnPays);
genre = findViewById(R.id.radiogroup);
int selectedId = genre.getCheckedRadioButtonId();
gen = findViewById(selectedId);
btnSign.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nomInscrit = nom.getText().toString();
prenomInscrit = prenom.getText().toString();
emailInscrit = email.getText().toString();
passwordInscrit = password.getText().toString();
payInscrit = spnPay.getSelectedItem().toString();
genreInscrit = gen.getText().toString();
if(nom.getText().equals("") || prenom.getText().equals("") || email.getText().equals("") || password.getText().equals("")){
Toast.makeText(MainActivity.this,"Veuillez remplir tous les champs", Toast.LENGTH_SHORT);
}
else if(!password.getText().equals(passConf.getText())){
Toast.makeText(MainActivity.this,"Les mots de passe doivent etre identique", Toast.LENGTH_SHORT);
}
else{
switchActivities();
}
}
});
}
private void switchActivities() {
Intent ActivityIntent = new Intent(this, DataActivity.class);
startActivity(ActivityIntent);
}
}
Thanks for reading and resolving my problem
You need to pass the data using the ActivityIntent you have created in switchActivity function amd then get that data in your second Activity from getIntent().getString(key) function.
Please refer to this,
How do I pass data between Activities in Android application?
I am try to save my roll no and name in main activity but when my screen is rotated the activity data has been lose ,
finally i fixed it , below i adding source code
consider any layout , here i am using this layout structure
In activity_xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/rollnoID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:text="Rollno : "
android:textSize="25dp" />
<EditText
android:hint="Roll Number"
android:id="#+id/rollnoID_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:ems="8"
android:inputType="number"
android:text=""
android:textSize="25dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/nameID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:text="Name : "
android:textSize="25dp" />
<EditText
android:hint="Student Name"
android:id="#+id/nameID_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:ems="8"
android:inputType="textCapWords"
android:text=""
android:textSize="25dp" />
</LinearLayout>
<Button
android:onClick="saveData"
android:id="#+id/saveButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="save this Data"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:padding="20dp"
android:orientation="vertical">
<TextView
android:gravity="center"
android:hint="~RollNo~"
android:textColor="#color/colorPrimary"
android:id="#+id/display_rollno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp" />
<TextView
android:layout_marginTop="20dp"
android:id="#+id/display_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="~Name~"
android:textColor="#color/colorPrimary"
android:textSize="30sp" />
</LinearLayout>
</LinearLayout>
In MainActivity :
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static final String KEY_ROLL = "rollno_key";
private static final String KEY_NAME = "name_key";
int rollno;
String name;
EditText roll_et , name_et;
TextView roll_tv , name_tv;
Button saveButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
roll_et = (EditText) findViewById(R.id.rollnoID_edit);
name_et = (EditText) findViewById(R.id.nameID_edit);
roll_tv = (TextView) findViewById(R.id.display_rollno);
name_tv = (TextView) findViewById(R.id.display_name);
if (savedInstanceState != null){
String save_RollNO = savedInstanceState.getString(KEY_ROLL);
roll_tv.setText(save_RollNO);
String save_Name = savedInstanceState.getString(KEY_NAME);
name_tv.setText(save_Name);
} else {
Toast.makeText(getApplicationContext(),"Entry your data",Toast.LENGTH_SHORT).show();
}
}
#Override
public void onSaveInstanceState(Bundle saveData) {
saveData.putString(KEY_ROLL , roll_et.getText().toString());
saveData.putString(KEY_NAME , name_et.getText().toString());
super.onSaveInstanceState(saveData);
}
public void saveData(View v){
roll_tv.setText(roll_et.getText().toString().trim());
name_tv.setText(name_et.getText().toString().trim());
}
}
My project won't open on my device, it say's "Unfortunately,myApp has stopped".
this is my manifest:
this is my activity java :
package bismillah.project;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class Project2Activity extends Activity {
public static int a,b,c;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//coding start here!!
//input data
EditText pek=(EditText)findViewById(R.id.editText1);
String value=pek.getText().toString();
final int pekerja=Integer.parseInt(value);
EditText lam=(EditText)findViewById(R.id.editText2);
String value1=lam.getText().toString();
final int lama=Integer.parseInt(value1);
EditText up=(EditText)findViewById(R.id.editText3);
String value2=up.getText().toString();
final int upah=Integer.parseInt(value2);
EditText jum=(EditText)findViewById(R.id.editText4);
String value3=jum.getText().toString();
final int jumlah=Integer.parseInt(value3);
//button proses
Button button1=(Button)findViewById(R.id.Button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Spinner bibit = (Spinner)findViewById(R.id.spinner1);
TextView biaya = (TextView)findViewById(R.id.textView6);
if(bibit.getSelectedItem().toString().equals("Cabai Rp.100")){
a=pekerja*(lama*upah)+(jumlah*100);
biaya.setText("Biaya Rp." + a);
if(bibit.getSelectedItem().toString().equals("Tomat Rp.150")){
a=pekerja*(lama*upah)+(jumlah*150);
biaya.setText("Biaya Rp." + a);
if(bibit.getSelectedItem().toString().equals("Timun Rp.200")){
a=pekerja*(lama*upah)+(jumlah*200);
biaya.setText("Biaya Rp." + a);
}
}
}
}
});
//button reset
Button button2=(Button)findViewById(R.id.Button2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
TextView biaya = (TextView)findViewById(R.id.textView6);
EditText pek=(EditText)findViewById(R.id.editText1);
EditText lam=(EditText)findViewById(R.id.editText2);
EditText up=(EditText)findViewById(R.id.editText3);
EditText jum=(EditText)findViewById(R.id.editText4);
pek.setText("");
lam.setText("");
up.setText("");
jum.setText("");
biaya.setText("");
}
});
//button pindah activity
Button button3=(Button)findViewById(R.id.Button3);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i =new Intent(getApplicationContext(),activity2.class);
startActivity(i);
}
});
}
}
and this is my main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/bg"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="#string/hello"
android:textColor="#color/warna"
android:textSize="20dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:text="Jumlah Pekerja :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Lama Kerja :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Upah :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Jenis Bibit :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:entries="#array/list" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Jumlah Bibit :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Large Text"
android:textColor="#color/warna2"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:text="Proses" />
<Button
android:id="#+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Reset" />
<Button
android:id="#+id/Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Hasil Panen" />
</LinearLayout>
</LinearLayout>
I tried your code and I had to delete some stuff from your XML file because it was creating some bugs. It might be related to me but here's your XML I was able to use:
But please do not change your XML file before trying the solution below. If it still doesn't work, you should consider the elements I removed.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="hello"
android:textSize="20dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:text="Jumlah Pekerja :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Lama Kerja :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Upah :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Jenis Bibit :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Jumlah Bibit :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:text="Proses" />
<Button
android:id="#+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Reset" />
<Button
android:id="#+id/Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Hasil Panen" />
</LinearLayout>
</LinearLayout>
Next I created your Project2Activity java class and activity2 (pretty much empty since you gave no info about it).
So when trying to run your application you get the following error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.application.so/com.example.application.so.Project2Activity}: java.lang.NumberFormatException: For input string: ""
and it is at :
at com.example.application.so.Project2Activity.onCreate(Project2Activity.java:24)
now if we consider this lines 23 and 24:
String value=pek.getText().toString(); // line 23
final int pekerja=Integer.parseInt(value); // line 24
The error is that you are trying to read (line 23) your editText Value at the creation of the activity, at that time, your editText is still blank which means value will be equal to "" (empty string).
What you want to do instead is to put your lines from 22 to 33 (the ones below) in a button click event, I tried putting them in your button 1 click event and your app doesn't crash any more:
EditText pek=(EditText)findViewById(R.id.editText1);
String value=pek.getText().toString();
final int pekerja=Integer.parseInt(value);
EditText lam=(EditText)findViewById(R.id.editText2);
String value1=lam.getText().toString();
final int lama=Integer.parseInt(value1);
EditText up=(EditText)findViewById(R.id.editText3);
String value2=up.getText().toString();
final int upah=Integer.parseInt(value2);
EditText jum=(EditText)findViewById(R.id.editText4);
String value3=jum.getText().toString();
final int jumlah=Integer.parseInt(value3);
So to summarize, the problem is that you are extracting empty strings from your editTexts and trying to make integers out of them. Instead try making that all reading and integer extracting process on the click of a button where your really need these inputs. just like get the inputs and the final moment.
You should pay attention to your logcat (error messages), it is quite helpful.
What you can also do is to verify that your editTexts are not empty while trying to extract strings from them and raise exception or Toast if they are empty. Nothing too complicated just something like:
if (value.equals("")):
// editText is empty so do something
You can refer to this link for further understanding of how to know when an editText is empty.
I have created my first android application I have 4 pages, I have set one page to be the menu and it has 3 buttons on it to switch between the pages. However I seem to be getting an error in my code, if someone could take a look and see where I am going wrong with this.
So this is my menu page (activity_main)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ifmdb.MainActivity"
tools:ignore="MergeRootFrame,HardcodedText" >
<TextView
android:id="#+id/txtMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_gravity="center"
android:text="#string/please_click_on_the_options_available"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/btnDOCare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/txtDOCare"
android:layout_below="#+id/txtDOCare"
android:layout_marginTop="30dp"
android:layout_gravity="center_vertical"
android:onClick="openDOCare"
android:text="#string/duty_of_care" />
<TextView
android:id="#+id/txtDOCare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/btnDock"
android:layout_marginLeft="202dp"
android:layout_marginTop="44dp"
android:layout_gravity="center"
android:text="#string/click_for_duty_of_care"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/txtVeCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtDOCare"
android:layout_below="#+id/txtMain"
android:layout_marginTop="36dp"
android:text="#string/click_for_vehicle_checklist"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/btnCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/btnDOCare"
android:layout_below="#+id/txtVeCheck"
android:layout_marginTop="37dp"
android:onClick="openChecklist"
android:text="#string/checklist" />
<TextView
android:id="#+id/txtDock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/txtDOCare"
android:layout_below="#+id/btnCheck"
android:layout_marginTop="36dp"
android:text="#string/click_for_docket"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/btnDock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/btnCheck"
android:layout_below="#+id/txtDock"
android:layout_marginTop="38dp"
android:onClick="openDocket"
android:text="#string/docket" />
</RelativeLayout>
and this is my Java class for it (Main Activity)
package com.example.ifmdb;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnHome = (Button)findViewById(R.id.btnHome);
btnHome.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(),
startActivityForResult(myIntent, 0);
}
});
}
}
This is my second page (checklist)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
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.example.ifmdb.MainActivity$PlaceholderFragment" >
<TextView
android:id="#+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/vehicle_safety_checklist"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/txtInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/please_check_each_box"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.58" >
<TextView
android:id="#+id/txtInt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:text="#string/interior"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/txtEx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/txtInt"
android:layout_alignBottom="#+id/txtInt"
android:layout_alignParentRight="true"
android:layout_marginRight="113dp"
android:text="#string/exterior"
android:textAppearance="?android:attr/textAppearanceMedium" />
<CheckBox
android:id="#+id/cboxFLev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/txtInt"
android:text="#string/fuel_level" />
<CheckBox
android:id="#+id/cboxTires"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/cboxFLev"
android:layout_alignBottom="#+id/cboxFLev"
android:layout_alignLeft="#+id/txtEx"
android:text="#string/tires_wheels" />
<CheckBox
android:id="#+id/cboxWWash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/cboxFLev"
android:text="#string/windscreen_washer" />
<CheckBox
android:id="#+id/cboxExhaust"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/cboxWWash"
android:layout_alignBottom="#+id/cboxWWash"
android:layout_alignLeft="#+id/cboxTires"
android:text="#string/exhaust" />
<CheckBox
android:id="#+id/cboxSWheel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/cboxWWash"
android:text="#string/steering_wheel" />
<CheckBox
android:id="#+id/cboxLights"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/cboxSWheel"
android:layout_alignBottom="#+id/cboxSWheel"
android:layout_alignLeft="#+id/cboxExhaust"
android:text="#string/lights_reflectors" />
<CheckBox
android:id="#+id/cboxBrakes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/cboxSWheel"
android:text="#string/brakes" />
<CheckBox
android:id="#+id/cboxELeaks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/cboxBrakes"
android:layout_alignBottom="#+id/cboxBrakes"
android:layout_alignLeft="#string/_id_cboxlights"
android:text="#string/exterior_leaks" />
<CheckBox
android:id="#+id/cboxClutch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/cboxBrakes"
android:text="#string/clutch" />
<CheckBox
android:id="#+id/cbocBody"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/cboxClutch"
android:layout_alignBottom="#+id/cboxClutch"
android:layout_alignLeft="#string/_id_cboxeleaks"
android:text="#string/body" />
<CheckBox
android:id="#+id/cboxHorn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/cboxClutch"
android:text="#string/horn" />
<TextView
android:id="#+id/txtEng"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/cbocBody"
android:layout_below="#+id/cboxHorn"
android:text="#string/engine"
android:textAppearance="?android:attr/textAppearanceMedium" />
<CheckBox
android:id="#+id/cboxHeater"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/txtEng"
android:text="#string/heater" />
<CheckBox
android:id="#+id/cboxOil"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/cboxExhaust"
android:layout_below="#+id/cboxHeater"
android:text="#string/oil_level" />
<CheckBox
android:id="#+id/cboxSBelt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/cboxOil"
android:layout_alignBottom="#+id/cboxOil"
android:layout_alignParentLeft="true"
android:text="#string/seat_belts" />
<CheckBox
android:id="#+id/cboxWLight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/cboxCool"
android:layout_alignBottom="#+id/cboxCool"
android:layout_alignParentLeft="true"
android:text="#string/warning_lights" />
<CheckBox
android:id="#+id/cboxBelt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/cboxCool"
android:layout_below="#+id/cboxCool"
android:text="#string/belts" />
<CheckBox
android:id="#+id/cboxMirrors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/cboxWLight"
android:text="#string/mirrors" />
<CheckBox
android:id="#+id/cboxLeaks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/cboxBelt"
android:layout_below="#+id/cboxBelt"
android:text="#string/engine_leaks" />
<CheckBox
android:id="#+id/cboxCool"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/cboxOil"
android:layout_below="#+id/cboxOil"
android:text="#string/coolant_level" />
<TextView
android:id="#+id/txtOther"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/cboxLeaks"
android:layout_alignParentLeft="true"
android:text="#string/other"
android:textAppearance="?android:attr/textAppearanceMedium" />
<CheckBox
android:id="#+id/cboxScrew"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtEng"
android:layout_below="#+id/cboxLeaks"
android:text="#string/loose_bolts_screws" />
<CheckBox
android:id="#+id/cboxWTri"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/cboxScrew"
android:layout_alignBottom="#+id/cboxScrew"
android:layout_alignParentLeft="true"
android:text="#string/warning_triangle" />
<CheckBox
android:id="#+id/cboxFire"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/cboxWTri"
android:text="#string/fire_extinguisher_first_aid_kit" />
<TextView
android:id="#+id/txtAddCom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="#string/additional_comments"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/txtAddComBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/cboxLights"
android:layout_below="#+id/txtAddCom"
android:layout_marginTop="29dp" />
<TextView
android:id="#+id/txtFault"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="27dp"
android:text="#string/in_the_event_of_serious_faults_do_not_drive_the_vehicle"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/txtDefects"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/txtAddComBox"
android:layout_marginTop="110dp"
android:text="#string/defects"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/txtDefectsBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/cboxScrew"
android:layout_below="#+id/txtDefects"
android:layout_marginTop="25dp" />
<Button
android:id="#+id/btnHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/cboxOil"
android:text="#string/home" />
</RelativeLayout>
</LinearLayout>
and this is that java class for that page (Checklist)
package com.example.ifmdb;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Checklist extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.checklist);
Button btnCheck = (Button) findViewById(R.id.btnCheck);
btnCheck.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
}
I have an error with a line of code
Intent myIntent = new Intent(view.getContext(),
startActivityForResult(myIntent, 0);
This is the error---
The constructor Intent(Context, void) is undefined
Syntax error, insert ")" to complete Variable Initializer
Try this code
public class MainActivity extends Activity {
private Activity mActivity = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mActivity = this;
Button btnHome = (Button)findViewById(R.id.btnHome);
btnHome.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(mActivity.getApplicationContext(),Checklist.class);
mActivity.startActivityForResult(myIntent, 0);
}
});
}
}
In the code its missing the syntax.
here is a tutorial
You can try this way. Hope it helps.
btnHome.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(getActivity(),Checklist.class);
startActivity(myIntent);
}
});
I got it fixed, you were right Atul O Holic i was missing a part of the code.
heres the working solution
package com.example.ifmdb;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnHome = (Button)findViewById(R.id.btnHome);
btnHome.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(),Checklist.class);
startActivityForResult(myIntent, 0);
}
});
}
}
I have three fragments, Player1Tun, Player2Turn and TicTacToeLayout. Player1Turn and Player2Turn are inner-switching every time a player makes a move and TicTacToeLayout contains the tic tac toe table which update everytime the fragments switches. What I'm trying to do is to send the data from either player fragments to the host activity (while the program is still running) and then from there, I'll update the table in the TicTacToeLayout. Any ideas or method I can do it?
Player1:
package As2.packageTK;
//import android.app.Activity;
import java.util.ArrayList;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class PlayerTurn2 extends Fragment{
TextView p2Name;
TextView p2Icon;
Button doneP2;
Button resetP2;
EditText row;
EditText column;
TicTacToeLayout myObject2 = new TicTacToeLayout();
ArrayList<String> player2;
Bundle extras = new Bundle();
int turn = 2;
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.playerturn2, container, false);
return view;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
extras = getArguments();
player2 = new ArrayList<String>(extras.getStringArrayList("player2"));
//Toast.makeText(getActivity(), player2.get(0), Toast.LENGTH_LONG).show();
//Toast.makeText(getActivity(), player2.get(1), Toast.LENGTH_LONG).show();
p2Name = (TextView) getActivity().findViewById(R.id.p2NameInfo);
p2Icon = (TextView) getActivity().findViewById(R.id.p2IconInfo);
row = (EditText) getActivity().findViewById(R.id.rowP2);
column = (EditText) getActivity().findViewById(R.id.columnP2);
doneP2 = (Button) getActivity().findViewById(R.id.doneP2);
//resetP2 = (Button) getActivity().findViewById(R.id.resetP2);
setPlayer(); //sets all the information of player 2, name, icon, image, etc
doneP2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
callPlayer1Fragment(); //switches with first player
}
});
}
public void callPlayer1Fragment()
{
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment Player1Frag = new PlayerTurn1();
Player1Frag.setArguments(extras);
ft.replace(R.id.fragment_container, Player1Frag);
ft.commit();
}
public boolean checkField()
{
if(row == null || column == null)
{
Toast.makeText(getActivity(), "Please input the row or column!", Toast.LENGTH_LONG).show();
return false;
}
else
return true;
}
public void setPlayer()
{
String name = player2.get(0);
if(!name.equals(""))
p2Name.setText("Player Name: " + name);
else
p2Name.setText("");
String icon = player2.get(1);
if(!icon.equals(""))
p2Icon.setText("Player Icon: " + icon);
else
p2Icon.setText("");
}
}
player 2 fragment is exactly the same code so I won't bother adding it.
TicTacToeLayout fragment class:
public class TicTacToeLayout extends Fragment {
TextView image1, image2, image3, image4, image5, image6, image7, image8, image9;
TextView[][] images;
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.tictactoe_layout, container, false);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
image1 = (TextView) getActivity().findViewById(R.id.Image1);
image2 = (TextView) getActivity().findViewById(R.id.Image2);
image3 = (TextView) getActivity().findViewById(R.id.Image3);
image4 = (TextView) getActivity().findViewById(R.id.Image4);
image5 = (TextView) getActivity().findViewById(R.id.Image5);
image6 = (TextView) getActivity().findViewById(R.id.Image6);
image7 = (TextView) getActivity().findViewById(R.id.Image7);
image8 = (TextView) getActivity().findViewById(R.id.Image8);
image9 = (TextView) getActivity().findViewById(R.id.Image9);
images = new TextView[][]{ {image1, image2, image3},
{image4, image5, image6},
{image7, image8, image9} };
toast();
}
public void toast()
{
Toast.makeText(getActivity(), images[0][0].getText().toString(), Toast.LENGTH_LONG).show();
Toast.makeText(getActivity(), images[0][1].getText().toString(), Toast.LENGTH_LONG).show();
Toast.makeText(getActivity(), images[0][2].getText().toString(), Toast.LENGTH_LONG).show();
Toast.makeText(getActivity(), images[1][0].getText().toString(), Toast.LENGTH_LONG).show();
}
public void play(int row, int column, String icon)
{
images[row-1][column-1].setText(icon);
}
}
here's the XML layout for playerturn2
<?xml version="1.0" encoding="UTF-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow
android:id="#+id/tablerow0"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/player2Num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_marginLeft="100dp"
android:gravity="center_horizontal"
android:text="Player 2"
android:textSize="20dp" />
</TableRow>
<TableRow
android:id="#+id/tablerow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/p2NameInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Player Name: "
android:textSize="20dp" />
</TableRow>
<TableRow
android:id="#+id/tablerow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/p2IconInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Player icon: "
android:textSize="20dp" />
</TableRow>
<TableRow
android:id="#+id/tablerow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/p2PicInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Player picture:"
android:textSize="20dp" />
</TableRow>
<ImageView
android:id="#+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#android:drawable/editbox_background" />
<TableRow
android:id="#+id/rowNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_marginLeft="50dp"
android:gravity="center_horizontal"
android:text="Row (1-3):"
android:textSize="20dp" />
<EditText
android:id="#+id/rowP2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:width="50dp" />
</TableRow>
<TableRow
android:id="#+id/colNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_marginLeft="50dp"
android:gravity="center_horizontal"
android:text="Col (1-3):"
android:textSize="20dp" />
<EditText
android:id="#+id/columnP2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:width="50dp" />
</TableRow>
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp" >
<Button
android:id="#+id/doneP2"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="Done"
/>
<Button
android:id="#+id/resetP2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
/>
</TableRow>
</TableLayout>
here's the layout for tictactoelayout.xml
<?xml version="1.0" encoding="UTF-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tictactoe"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="10dp" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp" >
<TextView
android:id="#+id/Image1"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="1"
android:textSize="70dp" />
<TextView
android:id="#+id/Image2"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="2"
android:textSize="70dp" />
<TextView
android:id="#+id/Image3"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="3"
android:textSize="70dp" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/Image4"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="4"
android:textSize="70dp" />
<TextView
android:id="#+id/Image5"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="5"
android:textSize="70dp" />
<TextView
android:id="#+id/Image6"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="6"
android:textSize="70dp" />
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/Image7"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="7"
android:textSize="70dp" />
<TextView
android:id="#+id/Image8"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="8"
android:textSize="70dp" />
<TextView
android:id="#+id/Image9"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="9"
android:textSize="70dp" />
</TableRow>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TextView
android:id="#+id/win"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Winner"
android:textSize="20dp" />
<EditText
android:id="#+id/winner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:ems="10" />
</RelativeLayout>
</TableLayout>
and finally the host activity's xml layout: tictactoegame.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="#+id/frag2"
android:name="As2.packageTK.TicTacToeLayout"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
My friend did the same but with a implementing an interface he created in the fragments and having the main activity implement the interface. With that, he used a method to pass datas. Maybe it's not clear but if you know what I'm trying to say then you could clear it out for all of us :)
your friend is right, communicating with other fragments is nicely explained in the docs:
http://developer.android.com/training/basics/fragments/communicating.html