I would like to display all the registerEventName inside the spinner in order to let the user to select from it. But how to get the data from firebase and display inside the spinner? Hope someone can help me on this.
XML File
For example i want to change EditText of eventTitle to spinner.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/createEventLayout"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:weightSum="10"
android:background="#89bcd4"
android:padding="10dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="4"
android:text="Title of event"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/eventTitle"
android:layout_width="0dp"
android:inputType="text"
android:maxLines="1"
android:lines="1"
android:layout_weight="6"
android:imeOptions="actionNext"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:weightSum="10"
android:background="#dce2e6"
android:padding="10dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="4"
android:text="#string/createEventDesc"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/eventDes"
android:layout_width="0dp"
android:layout_weight="6"
android:lines="1"
android:inputType="text"
android:maxLines="1"
android:imeOptions="actionNext"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:weightSum="10"
android:background="#89bcd4"
android:padding="10dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="4"
android:text="#string/createEventLocation"
android:layout_height="wrap_content" />
<EditText
android:maxLines="1"
android:lines="1"
android:id="#+id/eventLocation"
android:layout_width="0dp"
android:layout_weight="6"
android:inputType="text"
android:imeOptions="actionNext"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:weightSum="10"
android:background="#dce2e6"
android:padding="10dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="4"
android:text="Employee email"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/eventAttendee"
android:layout_width="0dp"
android:layout_weight="6"
android:hint="Place more attendee by comma seprated"
android:maxLines="1"
android:lines="1"
android:inputType="textEmailAddress"
android:imeOptions="actionNext"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:background="#89bcd4"
android:padding="10dp"
android:layout_height="wrap_content">
<TextView
android:id="#+id/startAt"
android:layout_width="0dp"
android:layout_weight="3"
android:text="#string/createEventStart"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="7"
android:layout_height="wrap_content">
<DatePicker
android:id="#+id/startDate"
android:layout_width="wrap_content"
android:calendarViewShown="false"
android:datePickerMode="spinner"
android:layout_weight="4"
android:layout_height="wrap_content" />
<TimePicker
android:id="#+id/startTime"
android:layout_width="wrap_content"
android:layout_weight="4"
android:timePickerMode="spinner"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:background="#dce2e6"
android:padding="10dp"
android:layout_height="wrap_content">
<TextView
android:id="#+id/endAt"
android:layout_width="0dp"
android:layout_weight="3"
android:text="#string/createEventEnd"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="0dp"
android:orientation="vertical"
android:layout_weight="7"
android:layout_height="wrap_content">
<DatePicker
android:id="#+id/endDate"
android:layout_width="wrap_content"
android:calendarViewShown="false"
android:datePickerMode="spinner"
android:layout_height="wrap_content" />
<TimePicker
android:id="#+id/endTime"
android:timePickerMode="spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:weightSum="10"
android:background="#89bcd4"
android:padding="10dp"
android:layout_height="wrap_content">
<Button
android:maxLines="1"
android:id="#+id/createEvent"
android:layout_width="0dp"
android:layout_weight="5"
android:text="Create Event"
android:layout_height="wrap_content" />
<Button
android:id="#+id/cancelEvent"
android:layout_width="0dp"
android:layout_weight="5"
android:text="Cancel"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Event List Adapter Java File
package com.example.edward.neweventmanagementsystem.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import com.example.edward.neweventmanagementsystem.R;
import com.example.edward.neweventmanagementsystem.Model.ScheduledEvents;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Khushvinders on 21-Oct-16.
*/
public class EventListAdapter extends BaseAdapter {
private Context context;
private List<ScheduledEvents> scheduledEvents;
private LayoutInflater inflater;
FirebaseDatabase database;
public EventListAdapter(Context context, List<ScheduledEvents> scheduledEvents){
this.context = context;
this.scheduledEvents = scheduledEvents;
inflater = LayoutInflater.from(this.context);
}
#Override
public int getCount() {
return scheduledEvents.size();
}
#Override
public Object getItem(int i) {
return scheduledEvents.get(i);
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
EventHolder eventHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.event_view_layout, parent, false);
eventHolder = new EventHolder(convertView);
convertView.setTag(eventHolder);
} else {
eventHolder = (EventHolder) convertView.getTag();
}
ScheduledEvents scheduledEvents = (ScheduledEvents) getItem(position);
eventHolder.eventTitle.setText(scheduledEvents.getEventSummery());
eventHolder.eventDes.setText(scheduledEvents.getDescription());
eventHolder.eventAttendee.setText(scheduledEvents.getAttendees());
eventHolder.eventStart.setText(scheduledEvents.getStartDate());
eventHolder.eventEnd.setText(scheduledEvents.getEndDate());
eventHolder.eventLocation.setText(scheduledEvents.getLocation());
return convertView;
}
private class EventHolder {
TextView eventDes, eventAttendee, eventStart, eventEnd, eventLocation, eventTitle;
public EventHolder(View item) {
eventTitle = (TextView) item.findViewById(R.id.eventTitle);
eventDes = (TextView) item.findViewById(R.id.eventDes);
eventAttendee = (TextView) item.findViewById(R.id.eventAttendee);
eventStart = (TextView) item.findViewById(R.id.eventStart);
eventEnd = (TextView) item.findViewById(R.id.eventEnd);
eventLocation = (TextView) item.findViewById(R.id.eventLocation);
}
}
}
Sample of firebase database
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());
}
}
I have a simple LinearLayout with several buttons, whos state color/text change based on the state of an underlying service, thats working fine.
However the buttons, are only clickable on the right corner ???
The button allSystemServicesToggleButton which i have included the implementation for in this post and only be clicked on the right side/right corner???
Here is my fragment xml layout & Actual screen shot with “Show Layout bounds” set to true:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_height="wrap_content"
android:text="All System Services"
android:textColor="#000000"
android:textSize="20sp"
android:layout_weight="1"
android:layout_width="0dp"
android:singleLine="true"
android:onClick="onClick"/>
<Button
android:id="#+id/allSystemServicesToggleButton"
android:layout_height="wrap_content"
android:text="#string/stopped"
android:layout_weight="1"
android:layout_width="0dp"
android:backgroundTint="#color/stoppedServiceColor"
android:enabled="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_height="wrap_content"
android:text="#string/shutdown_all_services"
android:textColor="#000000"
android:textSize="20sp"
android:layout_weight="1"
android:layout_width="0dp"
android:singleLine="true"
android:onClick="onClick"/>
<Button
android:id="#+id/shutdownAllServicesToggleButton"
android:layout_height="wrap_content"
android:text="#string/shutdown"
android:layout_weight="1"
android:layout_width="0dp"
android:enabled="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="Networks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32sp"
android:textColor="#000000"/>
<View
android:id="#+id/viewServicesDivider1"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#808080"
android:layout_gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:text="Bluetooth Service"
android:textColor="#000000"
android:textSize="20sp"
android:singleLine="true"/>
<Button
android:id="#+id/btServicesToggleButton"
android:layout_height="wrap_content"
android:text="#string/stopped"
android:layout_weight="1"
android:layout_width="0dp"
android:backgroundTint="#color/stoppedServiceColor"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!--TODO: get requirements for showing paired devices & pairing devices-->
<TextView
android:id="#+id/textPairedText"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="Paired Bluetooth Devices"
android:textColor="#000000"
android:singleLine="true"
android:textSize="20sp"
android:layout_width="0dp"
/>
<TextView
android:id="#+id/textViewNumberOfConnectedDevices"
android:layout_height="wrap_content"
android:text="0"
android:layout_width="0dp"
android:layout_weight="1" />
<Button
android:id="#+id/btDevicesToggleButton"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="2"
android:text="Pair"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:text="MQTT Service"
android:textColor="#000000"
android:textSize="20sp"
android:singleLine="true"/>
<Button
android:id="#+id/MQTTserviceToggleButton"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:backgroundTint="#color/stoppedServiceColor"
android:text="#string/stopped" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="Location Services"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32sp"
android:textColor="#000000"/>
<View
android:id="#+id/viewServicesDivider3"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#808080"
android:layout_gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:text="GPS"
android:textColor="#000000"
android:textSize="20sp"
android:singleLine="true"/>
<Button
android:id="#+id/gpsServiceToggleButton"
android:layout_height="wrap_content"
android:text="#string/stopped"
android:layout_weight="1"
android:layout_width="0dp"
android:backgroundTint="#color/stoppedServiceColor"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="Command Services"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32sp"
android:textColor="#000000"/>
<View
android:id="#+id/viewServicesDivider4"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#808080"
android:layout_gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:text="Voice Recognition"
android:textColor="#000000"
android:textSize="20sp"
android:singleLine="true"/>
<Button
android:id="#+id/voiceRecognitionToggleButton"
android:layout_height="wrap_content"
android:text="#string/stopped"
android:layout_weight="1"
android:layout_width="0dp"
android:backgroundTint="#color/stoppedServiceColor"
/>
</LinearLayout>
Relevant fragment java:
package x.core.fragments;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.speech.tts.TextToSpeech;
import android.support.v4.app.Fragment;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import x.core.Application.NgfrApp;
import x.core.R;
import x.core.helpers.Util;
import x.core.services.BluetoothService;
import x.core.services.LocationService;
import x.core.services.MqttBrokerService;
import x.core.services.ServicesStateBroadcastReceiver;
import x.core.services.SpeechRecognitionService;
import x.core.services.UIService;
public class ServicesFragment extends Fragment implements View.OnClickListener {
private static final String TAG = "ServicesFragment";
public static ServicesFragment newInstance() {
return new ServicesFragment();
}
private static Button btServicesToggleButton;
private static Button mqttServicesToggleButton;
private static Button gpsServiceToggleButton;
private static Button voiceServiceToggleButton;
private static Button allServiceToggleButton;
private static String stopped = null;
private static String running = null;
private static int runningColorId, stoppedColorId = -1;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_services, container, false);
btServicesToggleButton = rootView.findViewById(R.id.btServicesToggleButton);
mqttServicesToggleButton = rootView.findViewById(R.id.MQTTserviceToggleButton);
gpsServiceToggleButton = rootView.findViewById(R.id.gpsServiceToggleButton);
voiceServiceToggleButton = rootView.findViewById(R.id.voiceRecognitionToggleButton);
allServiceToggleButton = rootView.findViewById(R.id.allSystemServicesToggleButton);
stopped = getResources().getString(R.string.stopped);
running = getResources().getString(R.string.running);
runningColorId = getResources().getColor(R.color.runningServiceColor);
stoppedColorId = getResources().getColor(R.color.stoppedServiceColor);
allServiceToggleButton.setEnabled(true);
allServiceToggleButton.setClickable(true);
allServiceToggleButton.setOnClickListener(this);
return rootView;
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.allSystemServicesToggleButton:
if (ServicesStateBroadcastReceiver.BT_SERVICE_STATE_VALUE==false || ServicesStateBroadcastReceiver.MQTT_STATE_VALUE==false || ServicesStateBroadcastReceiver.NGFR_GPS_SERVICE_STATE_VALUE==false || ServicesStateBroadcastReceiver.VOICE_SERVICE_STATE_VALUE==false)
{
Toast.makeText(NgfrApp.getContext(),NgfrApp.getContext().getResources().getString(R.string.restarting_services),Toast.LENGTH_SHORT).show();
//restartingServices();
}
else
{
Toast.makeText(NgfrApp.getContext(),NgfrApp.getContext().getResources().getString(R.string.all_already_running),Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
}
}
MainActivity.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/"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<android.support.design.widget.TabLayout
android:id="#+id/activity_main_tabLyout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed" />
<android.support.v4.view.ViewPager
android:id="#+id/activity_main_viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
MainActivity.java, I only included relevant code:
public class MainActivity extends AppCompatActivity {
private static String TAG = "Main";
private static final int CHECK_BT_CODE = 1;
private static final int CHECK_TTS_CODE = 2;
//global boolean flags that will communicate the state of the system at all times
//bluetooth related flags
public boolean isBleSupported = false;
public boolean isBluetoothEnabled = false;
public boolean accessBluetoothManager= false;
public boolean nearbyDevices = false;
//configuration data related
public boolean isConfigurationLoadedCorrectly = false;
//text to speech related
public boolean isTextToSpeechSupported = false;
private Context context = null;
private ServicesStateBroadcastReceiver servicesStateBroadcastReciever = null;
private ViewPager mainViewPager;
private TabLayout tabLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "Activity started!!");
context = this;
setContentView(R.layout.activity_main);
MainActivityViewPager adapter = new MainActivityViewPager(getSupportFragmentManager());
mainViewPager = (ViewPager) findViewById(R.id.activity_main_viewPager);
mainViewPager.setAdapter(adapter);
tabLayout = (TabLayout) findViewById(R.id.activity_main_tabLyout);
tabLayout.setupWithViewPager(mainViewPager );
}
}
The adapter for my fragments, FragmentStatePagerAdapter:
package x.core.views;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import x.BiometricsFragment;
import x.ServicesFragment;
public class MainActivityViewPager extends FragmentStatePagerAdapter {
public MainActivityViewPager(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment returnFragment;
switch(position) {
case 0:
returnFragment = ServicesFragment.newInstance();
break;
case 1:
returnFragment = BiometricsFragment.newInstance();
break;
default:
return null;
}
return returnFragment;
}
#Override
public int getCount() {
return 2;
}
public CharSequence getPageTitle(int position) {
CharSequence title;
switch (position) {
case 0:
title = "Services";
break;
case 1:
title = "Biometrics";
break;
default:
return null;
}
return title;
}
}
Thanks
only for corner clicking use this kind of logic
<FrameLayout
android:layout_width="50dp"
android:layout_height="50dp">
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#color/colorAccent" />
<TextView
android:id="#+id/tvtttt"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_gravity="right"
android:background="#F00" />
</FrameLayout>
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 :)
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();
}
});