Switch and passing data between activities android/java - java

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?

Related

Android : Saving and Restoring Activity data when switching Portrait and Landscape

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());
}
}

Managing onClickListeners in more then one xml file

I created an app that adds forms dynamically when a user clicks the add butting and deletes it when he clicks the cancel button. I have two cancel buttons, one in the Field.xml file and the other in the activity_school_search_setup file.
They both have the same ID but the one in the Field.xml file does not delete the field. It appears as though the onclicklistener does not function for the delete button.
Main Java class file
import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.view.View;
import android.widget.Toast;
public class SchoolSearchSetup extends AppCompatActivity implements View.OnClickListener {
private EditText searchSchoolID;
private Button searchSchoolButtonID;
private ListView listOfSchoolsID;
private Button openNewschoolID;
private LinearLayout schoolSetupLayout;
private Button addNewClass;
private EditText classNameEditText;
private Button deleteButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_school_search_setup);
findAllViewsID();
initializeListenners();
}
public void findAllViewsID(){
classNameEditText = findViewById(R.id.classNameText);
addNewClass = findViewById(R.id.addNewClassButton);
schoolSetupLayout = findViewById(R.id.schoolSetupLayout);
searchSchoolID = findViewById(R.id.searchSchoolID);
searchSchoolButtonID = findViewById(R.id.searchSchoolButtonID);
listOfSchoolsID = findViewById(R.id.listOfSchoolsID);
openNewschoolID = findViewById(R.id.openNewschoolID);
deleteButton = findViewById(R.id.delete_button);
}
public void initializeListenners(){
openNewschoolID.setOnClickListener(SchoolSearchSetup.this);
addNewClass.setOnClickListener(SchoolSearchSetup.this);
deleteButton.setOnClickListener(SchoolSearchSetup.this);
}
public void addNewClass(){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.field, null);
schoolSetupLayout.addView(rowView, schoolSetupLayout.getChildCount() -1);
}
#Override
public void onClick(View view) {
switch(view.getId()){
case R.id.openNewschoolID:
displaySchoolSetUpForms();
break;
case R.id.addNewClassButton:
addNewClass();
break;
case R.id.delete_button:
schoolSetupLayout.removeView((View) view.getParent());
}
}
private void displaySchoolSetUpForms() {
schoolSetupLayout.setVisibility(View.VISIBLE);
}
}
Here is the main XML activity file
activity.school_search_setup.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="wrap_content"
tools:context="com.example.demeainc.demea.MainActivity"
android:orientation="vertical"
android:background="#color/backgroundColor">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<ImageView
android:id="#+id/backArrowClassView"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/ic_arrow_class"
android:layout_marginLeft="5dp"/>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="50dp"
android:text="Search your school."
android:gravity="center"
android:textSize="25dp" />
<android.support.v7.widget.CardView
android:foreground="?attr/selectableItemBackground"
android:clickable="true"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp">
<EditText
android:id="#+id/searchSchoolID"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawableLeft="#drawable/ic_search_black_24dp"
android:hint="Search"/>
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp">
<Button
android:id="#+id/searchSchoolButtonID"
android:layout_width="200dp"
android:layout_height="60dp"
android:layout_marginLeft="60dp"
android:text="Search"
android:textColor="#ffff"
android:background="#color/colorPrimary"
android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp">
<android.support.v7.widget.CardView
android:layout_width="300dp"
android:layout_height="wrap_content">
<ListView
android:id="#+id/listOfSchoolsID"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#android:color/transparent"
android:cacheColorHint="#color/ligtherDarkGrey"
android:divider="#CCCCCC"
android:dividerHeight="2dp"
android:paddingLeft="2dp" >
</ListView>
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical">
<Button
android:id="#+id/openNewschoolID"
android:layout_width="200dp"
android:layout_height="60dp"
android:layout_marginLeft="60dp"
android:background="#color/colorPrimary"
android:text="Open New School"
android:textColor="#ffff"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="#+id/schoolSetupLayout"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<TextView
android:id="#+id/schoolSetupText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="School Setup"
android:textSize="20dp"
android:layout_marginLeft="50dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/classNameText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:hint="Class name, e.g Grade one" />
<Button
android:id="#+id/delete_button"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#android:drawable/ic_delete"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<Button
android:id="#+id/addNewClassButton"
android:layout_marginTop="15dp"
android:layout_marginLeft="50dp"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Add new class"
android:textColor="#ffff"
android:background="#color/colorPrimary"/>
<Button
android:id="#+id/nextButton"
android:layout_marginTop="25dp"
android:layout_marginLeft="8dp"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Next"
android:textColor="#ffff"
android:background="#color/green"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Here is the field.xml file
<?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/schoolSetupLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/classNameText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:hint="Class name, e.g Grade one" />
<Button
android:id="#+id/delete_button"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#android:drawable/ic_delete"/>
</LinearLayout>
</LinearLayout>
You either should add a clickListener on
View rowView = inflater.inflate(R.layout.field, null);
Or add android:onClick within an element of field.xml if you want some click event to happen on it.
initializeListenners(); is only called once, during the initial layout, not on dynamic view additions.
Implement OnClickListener for Field.xml button after you inflate that layout.
Here is example:
public void addNewClass(){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.field, null);
final Button btnDelete = rowView.findViewById(R.id.delete_button);
btnDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Your stuff
}
});
schoolSetupLayout.addView(rowView, schoolSetupLayout.getChildCount() -1);
}
Hope that help :)

Dynamically generated delete button does not respond to an OnclickListener(Android JAVA)

I created an app that dynamically adds an edittext field to the view by clicking an Add button and deletes it by clicking a delete button. I created a separate xml file called field.xml that is called up when a new edittext field is added to the view. But the delete button does not remove this edittext field when clicked. What am I missing in the code I have tried most options but to no avail.
Here is the main java file
SchoolSearchSetup.java
import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.view.View;
import android.widget.Toast;
public class SchoolSearchSetup extends AppCompatActivity implements View.OnClickListener {
private EditText searchSchoolID;
private Button searchSchoolButtonID;
private ListView listOfSchoolsID;
private Button openNewschoolID;
private LinearLayout schoolSetupLayout;
private Button addNewClass;
private EditText classNameEditText;
private Button deleteButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_school_search_setup);
findAllViewsID();
initializeListenners();
}
public void findAllViewsID(){
classNameEditText = findViewById(R.id.classNameText);
addNewClass = findViewById(R.id.addNewClassButton);
schoolSetupLayout = findViewById(R.id.schoolSetupLayout);
searchSchoolID = findViewById(R.id.searchSchoolID);
searchSchoolButtonID = findViewById(R.id.searchSchoolButtonID);
listOfSchoolsID = findViewById(R.id.listOfSchoolsID);
openNewschoolID = findViewById(R.id.openNewschoolID);
deleteButton = findViewById(R.id.delete_button);
}
public void initializeListenners(){
openNewschoolID.setOnClickListener(SchoolSearchSetup.this);
addNewClass.setOnClickListener(SchoolSearchSetup.this);
deleteButton.setOnClickListener(SchoolSearchSetup.this);
}
public void addNewClass(){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.field, null);
schoolSetupLayout.addView(rowView, schoolSetupLayout.getChildCount() -1);
}
#Override
public void onClick(View view) {
switch(view.getId()){
case R.id.openNewschoolID:
displaySchoolSetUpForms();
break;
case R.id.addNewClassButton:
addNewClass();
break;
case R.id.delete_button:
schoolSetupLayout.removeView((View) view.getParent());
}
}
private void displaySchoolSetUpForms() {
schoolSetupLayout.setVisibility(View.VISIBLE);
}
}
The main XML file
activity.school_search_setup.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="wrap_content"
tools:context="com.example.demeainc.demea.MainActivity"
android:orientation="vertical"
android:background="#color/backgroundColor">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<ImageView
android:id="#+id/backArrowClassView"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/ic_arrow_class"
android:layout_marginLeft="5dp"/>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="50dp"
android:text="Search your school."
android:gravity="center"
android:textSize="25dp" />
<android.support.v7.widget.CardView
android:foreground="?attr/selectableItemBackground"
android:clickable="true"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp">
<EditText
android:id="#+id/searchSchoolID"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawableLeft="#drawable/ic_search_black_24dp"
android:hint="Search"/>
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp">
<Button
android:id="#+id/searchSchoolButtonID"
android:layout_width="200dp"
android:layout_height="60dp"
android:layout_marginLeft="60dp"
android:text="Search"
android:textColor="#ffff"
android:background="#color/colorPrimary"
android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp">
<android.support.v7.widget.CardView
android:layout_width="300dp"
android:layout_height="wrap_content">
<ListView
android:id="#+id/listOfSchoolsID"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#android:color/transparent"
android:cacheColorHint="#color/ligtherDarkGrey"
android:divider="#CCCCCC"
android:dividerHeight="2dp"
android:paddingLeft="2dp" >
</ListView>
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical">
<Button
android:id="#+id/openNewschoolID"
android:layout_width="200dp"
android:layout_height="60dp"
android:layout_marginLeft="60dp"
android:background="#color/colorPrimary"
android:text="Open New School"
android:textColor="#ffff"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="#+id/schoolSetupLayout"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<TextView
android:id="#+id/schoolSetupText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="School Setup"
android:textSize="20dp"
android:layout_marginLeft="50dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/classNameText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:hint="Class name, e.g Grade one" />
<Button
android:id="#+id/delete_button"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#android:drawable/ic_delete"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<Button
android:id="#+id/addNewClassButton"
android:layout_marginTop="15dp"
android:layout_marginLeft="50dp"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Add new class"
android:textColor="#ffff"
android:background="#color/colorPrimary"/>
<Button
android:id="#+id/nextButton"
android:layout_marginTop="25dp"
android:layout_marginLeft="8dp"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Next"
android:textColor="#ffff"
android:background="#color/green"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Here is the Field.xml file for the edit text to be added. Both the field.xml and the activity.school_search_setup.xml share the same IDs.
Field.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/schoolSetupLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/classNameText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:hint="Class name, e.g Grade one" />
<Button
android:id="#+id/delete_button"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#android:drawable/ic_delete"/>
</LinearLayout>
</LinearLayout>
Have you tried using
setVisibility(View.GONE);
Update:
Try this code block:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button btn = new Button(this);
btn.setId(i);
final int id_ = btn.getId();
btn.setText("button " + id_);
btn.setBackgroundColor(Color.rgb(80, 80, 90));
linear.addView(btn, params);
btn1 = ((Button) findViewById(id_));
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(view.getContext(),
"Button clicked index = " + id_, Toast.LENGTH_SHORT)
.show();
}
});

Android button click doesn't work and editing textbox with it

I'm really new in coding for Android, just started to learn. I have some image buttons to change values displayed in textboxes and I don't know how to do this. I tried some tests for buttons but I have some errors which I can't repair.
Message from eclipse is:
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new OnClickListener(){})
and
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new OnClickListener(){})
for setOnClickListener and OnClickListener
I looked at this thread but still I don't know what to do
setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (SequencerActivity) back.setOnClickListener(this);
Except that, I want to change the Medium text fields with the buttons. Perhaps by the declared integers.
Code:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button b1;
Button b2;
Button b3;
Button b4;
Button b5;
Button b6;
Button b7;
Button b8;
int vs,vh,ws,wh,wth,wts;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button) findViewById(R.id.imageButton1);
b1.setOnClickListener(new OnClickListener()
{
protected void onClick(View v)
{
Toast.makeText(ApplicationContext(), "Om nom nom", Toast.LENGTH_SHORT).show();
}
});
}
xml
<RelativeLayout 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:background="#012e53"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
tools:context="pl.tmalachowski.truewindcalc.MainActivity" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="26dp"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="center"
android:orientation="vertical"
android:textColor="#bdc3c7" >
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/ves"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/podst" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="center" >
<TextView
android:id="#+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/rel"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/podst" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:gravity="center" >
<TextView
android:id="#+id/textView1"
android:layout_width="62dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/ves1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/podst" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:gravity="center" >
<TextView
android:id="#+id/textView2"
android:layout_width="77dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/ves2"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/podst" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:gravity="center" >
<TextView
android:id="#+id/textView3"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/rel1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/podst" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:gravity="center" >
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/rel2"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/podst" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:gravity="center"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/arrow_up_float" />
</LinearLayout>
I think you are using your onClickListener wrong.
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Om nom nom", Toast.LENGTH_SHORT).show();
}
});
Please try this usage. Hope it helps
Declare like this ImageButton b1; instead of Button b1;
then change to like this following code
b1=(ImageButton) findViewById(R.id.imageButton1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(ApplicationContext(), "Om nom nom", Toast.LENGTH_SHORT).show();
}
});

Display Currently Connected SSID in Android

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.

Categories