Hello i want to add a autocompletetextview in popup. Such that when i type city name it should come as suggestion.. Is this possible in PopUpWindow. Was trying this.
View addresspopupView =addresslayoutInflater.inflate(R.layout.addressinput, null);
AutoCompleteTextView city = (AutoCompleteTextView) addresspopupView.findViewById(R.id.city);
String[] CITY ={"Mumbai", "Ahemadabad", "Kolkata", "Chennai", "Thane"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_dropdown_item_1line, CITY);
city.setAdapter(adapter);
PopupWindow addresspopupWindow = new PopupWindow(addresspopupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
addresspopupWindow.showAtLocation(addresspopupView, Gravity.CENTER, 0, 0);
logcat
FATAL EXCEPTION: main
android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W#424c2b38 is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:706)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:356)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:234)
at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:153)
at android.view.Window$LocalWindowManager.addView(Window.java:559)
at android.widget.PopupWindow.invokePopup(PopupWindow.java:1013)
at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:922)
at android.widget.ListPopupWindow.show(ListPopupWindow.java:595)
at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1093)
at android.widget.AutoCompleteTextView.updateDropDownForFilter(AutoCompleteTextView.java:956)
at android.widget.AutoCompleteTextView.onFilterComplete(AutoCompleteTextView.java:938)
at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:285)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
Here is my addressInput.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:elevation="#dimen/dimen_4dp"
android:orientation="vertical"
android:padding="#dimen/dimen_4dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/addressPopUptitle"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_horizontal"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/addressLine1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:hint="Address Line 1" />
<EditText
android:id="#+id/addressLine2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Address Line 2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/landMark"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:hint="Landmark" />
<EditText
android:id="#+id/locality"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Locality" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!--<EditText-->
<!--android:id="#+id/city"-->
<!--android:layout_width="0dp"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_marginRight="2dp"-->
<!--android:layout_weight="1"-->
<!--android:hint="City" />-->
<AutoCompleteTextView
android:id="#+id/city"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:hint="City" />
<EditText
android:id="#+id/pin"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="PIN" />
</LinearLayout>
<EditText
android:id="#+id/state"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="State" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/summitAddrPopUp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="SUMMIT"
android:textColor="#color/green" />
<Button
android:id="#+id/cancelAddrPopUp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="CANCEL"
android:textColor="#color/red" />
</LinearLayout>
OnCreateView:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
Log.d("ClientBookingScreen", "Inside OnCreateView of ClientBookingScreen");
rootView = inflater.inflate(R.layout.bookingrequest, container, false);
intialize(rootView);
intialSetUp();
loadconfigs();
initializeAddressPopUp();
settingClickListener();
return rootView;
}
The methods in onCreateView() :
private void intialize(View rootView) {
//Setting toolbar
myCalendar = Calendar.getInstance(TimeZone.getTimeZone("Asia/Calcutta"));
pickUpDate = (TextView) rootView.findViewById(R.id.pickUpDateInput);
pickUpTime = (TextView) rootView.findViewById(R.id.pickUpTimeInput);
returnDate = (TextView) rootView.findViewById(R.id.returnDateInput);
returnTime = (TextView) rootView.findViewById(R.id.returnTimeInput);
pickUpAddress = (TextView) rootView.findViewById(R.id.pickUpAddressInput);
pickUpAddress.setMovementMethod(new ScrollingMovementMethod());
returnAddress = (TextView) rootView.findViewById(R.id.returnAddressInput);
returnAddress.setMovementMethod(new ScrollingMovementMethod());
description = (EditText) rootView.findViewById(R.id.descriptionInput);
description.setMovementMethod(new ScrollingMovementMethod());
book = (Button) rootView.findViewById(R.id.bookButton);
//Spinners
dutyType = (Spinner) rootView.findViewById(R.id.dutyTypeInput);
vehicleCategory = (Spinner) rootView.findViewById(R.id.vehicleCategoryInput);
vehicleType = (Spinner) rootView.findViewById(R.id.vehicleTypeInput);
//creating reflection of spinner to limit his height
createReflectionOfSpinner(dutyType);
createReflectionOfSpinner(vehicleCategory);
createReflectionOfSpinner(vehicleType);
isDrawerOpened = false;
//CheckBox
btcCheckBox = (CheckBox) rootView.findViewById(R.id.btccheckBox);
}
private void loadconfigs() {
categoryMap = new HashMap<String, ArrayList<String>>();
guestAddressMap = new HashMap<String, String>();
guestaddressLabel = new ArrayList<String>();
categoryName = new ArrayList<String>();
dutyTypeName = new ArrayList<String>();
new LoadConfigs().execute();
}
private void initializeAddressPopUp() {
//Address Input Popup
addresslayoutInflater = (LayoutInflater) getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
addresspopupView = addresslayoutInflater.inflate(R.layout.addressinput, null);
addressLine1 = (EditText) addresspopupView.findViewById(R.id.addressLine1);
addressLine2 = (EditText) addresspopupView.findViewById(R.id.addressLine2);
landmark = (EditText) addresspopupView.findViewById(R.id.landMark);
locality = (EditText) addresspopupView.findViewById(R.id.locality);
city = (AutoCompleteTextView) addresspopupView.findViewById(R.id.city);
String[] CITY ={"Mumbai", "Ahemadabad", "Kolkata", "Chennai", "Thane"};
// AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.actv_country);
ArrayAdapter adapter = new ArrayAdapter(addresspopupView.getContext(), android.R.layout.simple_dropdown_item_1line, CITY);
city.setAdapter(adapter);
pin = (EditText) addresspopupView.findViewById(R.id.pin);
state = (EditText) addresspopupView.findViewById(R.id.state);
title = (TextView) addresspopupView.findViewById(R.id.addressPopUptitle);
addressSummit = (Button) addresspopupView.findViewById(R.id.summitAddrPopUp);
addressCancel = (Button) addresspopupView.findViewById(R.id.cancelAddrPopUp);
//AddressLabelList Popoup
addressLabelInflater = (LayoutInflater) getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
listOfaddressTitle = addressLabelInflater.inflate(R.layout.addresslabellist, null);
addressLabelListView = (ListView) listOfaddressTitle.findViewById(R.id.addressLabelListView);
selectedListok = (Button) listOfaddressTitle.findViewById(R.id.addressLabelListButton);
}
private void settingClickListener() {
pickUpDate.setOnClickListener(this);
pickUpTime.setOnClickListener(this);
returnDate.setOnClickListener(this);
returnTime.setOnClickListener(this);
pickUpAddress.setOnClickListener(this);
returnAddress.setOnClickListener(this);
addressSummit.setOnClickListener(this);
addressCancel.setOnClickListener(this);
book.setOnClickListener(this);
btcCheckBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
Toast.makeText(getActivity(),
"CHECKED", Toast.LENGTH_LONG).show();
isBTC = true;
} else {
Toast.makeText(getActivity(),
"UNCHECKED", Toast.LENGTH_LONG).show();
isBTC = false;
}
}
});
//on item listener for Spinners
dutyType.setOnItemSelectedListener(this);
vehicleCategory.setOnItemSelectedListener(this);
vehicleType.setOnItemSelectedListener(this);
}
Any suggesstion please...
Let you try this,
Change your onCreateView codings as like below.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View addresspopupView =inflater.inflate(R.layout.addressinput, container, false);
AutoCompleteTextView city = (AutoCompleteTextView) addresspopupView.findViewById(R.id.city);
String[] CITY ={"Mumbai", "Ahemadabad", "Kolkata", "Chennai", "Thane"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_dropdown_item_1line, CITY);
city.setAdapter(adapter);
PopupWindow addresspopupWindow = new PopupWindow(addresspopupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
addresspopupWindow.showAtLocation(addresspopupView, Gravity.CENTER, 0, 0);
return addresspopupView;
}
EDITED :
Copy paste these lines in your oncreateView method like this.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
Log.d("ClientBookingScreen", "Inside OnCreateView of ClientBookingScreen");
rootView = inflater.inflate(R.layout.bookingrequest, container, false);
AutoCompleteTextView city = (AutoCompleteTextView) rootView.findViewById(R.id.city);
String[] CITY ={"Mumbai", "Ahemadabad", "Kolkata", "Chennai", "Thane"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_dropdown_item_1line, CITY);
city.setAdapter(adapter);
intialize(rootView);
intialSetUp();
loadconfigs();
initializeAddressPopUp();
settingClickListener();
return rootView;
}
May be the way you inflate the layout is wrong. Try the above code and let me know.
Related
i am creating an application, which i use to create a list of things that i have in SQLite database
i created a Fragment with a button that add a xml file to my LinearLayout , it works good , but i want to grab EditText and Spinner data and put them in a JSONobject but i don't know how , i don't have IDs of dynamically created views
My Fragment XML
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/itemsContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="50dp"
android:orientation="vertical"
android:paddingTop="20dp">
</LinearLayout>
</ScrollView>
<android.support.design.widget.FloatingActionButton
android:id="#+id/addItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="16dp"
app:srcCompat="#drawable/fab_add"
/>
</RelativeLayout>
My field (addView) XML
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:weightSum="3"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="#+id/add"
android:onClick="onIncrease"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:background="#drawable/ic_add_circle_black_24dp" />
<Button
android:id="#+id/remove"
android:onClick="onDecrease"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:background="#drawable/ic_remove_circle_black_24dp" />
</LinearLayout>
<EditText
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:inputType="number"
android:hint="#string/number"
/>
<Spinner
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
Fragment.java File
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_newfactor,container,false);
fab = v.findViewById(R.id.addItem);
ln = v.findViewById(R.id.itemsContainer);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.field, null);
final EditText editText = rowView.findViewById(R.id.number);
Button add = rowView.findViewById(R.id.add);
Button remove = rowView.findViewById(R.id.remove);
Button clear = rowView.findViewById(R.id.clear);
Spinner spinner = rowView.findViewById(R.id.spinner);
list = new ArrayList<>();
dbConnector = new DbConnector(getContext(),null,null,1);
Cursor c = dbConnector.get().rawQuery("SELECT * FROM product",null);
while (c.moveToNext()){
int id = c.getInt(c.getColumnIndex("id"));
String name = c.getString(c.getColumnIndex("name"));
String desc = c.getString(c.getColumnIndex("description"));
String price = c.getString(c.getColumnIndex("price"));
list.add(new SpinnerObject(id,name,price));
}
spinnerAdapter = new ir.animelist.localshop.Adaptors.SpinnerAdapter(getContext(),android.R.layout.simple_list_item_1,list);
spinner.setAdapter(spinnerAdapter);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int num = Integer.parseInt(editText.getText().toString());
int num_num = num + 1;
editText.setText(num_num + "");
}
});
remove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int num = Integer.parseInt(editText.getText().toString());
if(num > 0){
int num_num = num - 1;
editText.setText(num_num + "");
}
}
});
clear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ln.removeView((View) v.getParent());
}
});
ln.addView(rowView, ln.getChildCount() - 1);
}
});
return v;
}
You need to use the recyclerview to display your list items. Using recyclerview you can get view like -
View view = recyclerview.findViewHolderForAdapterPosition(pos).itemView;
Spinner spinner = view.findViewById(R.id.mySpinner);
This will return you a spinner of whichever position you have passed and afterwards get data from it.
Similarly, you can get view of edittext by using -
EditText edtTxt = view.findViewById(R.id.myEdtTxt);
To see how to get data from database and display that in recyclerview, check the example here - https://github.com/incipientinfo/db-queries
I am trying to change the text and check box response of the clicked list item but I am not able to do so. As I not able to get the click list item.
The fragment is a part of view pager.
Here is my code,
public class ParticipantsFragment extends Fragment {
private ParticipantAdapter mAdapter;
SwipeRefreshLayout refreshLayout;
private String mParticipantId, mEventId, mGender;
ParticipantsAsyncTask task;
public ParticipantsFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mEventId = getArguments().getString(Config.BUNDLE_KEY_EVENT_ID);
mParticipantId = getArguments().getString(Config.BUNDLE_KEY_PARTICIPANT_ID);
mGender = getArguments().getString(Config.BUNDLE_KEY_GENDER);
Log.v("fragh", mEventId + " " + mParticipantId);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.participant_list, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.no_participant);
task = new ParticipantsAsyncTask();
task.execute();
ListView participantListView = (ListView) rootView.findViewById(R.id.participant_list);
refreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_refresh_layout_participant_list);
participantListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.v("Clicked Participant",mAdapter.getItem(position).getParticipantName());
EditText notes = (EditText) rootView.findViewById(R.id.participant_list_item_notes);
String text = notes.getText().toString();
Log.v("Participant Text",text);
CheckBox checkBox = (CheckBox) rootView.findViewById(R.id.participant_list_item_checkbox);
String check;
if(checkBox.isChecked())
{
check = "1";
}
else
{
check = "0";
}
}
});
Button submit = (Button) rootView.findViewById(R.id.submit_participant);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
mAdapter = new ParticipantAdapter(getContext(), new ArrayList<Participant>());
participantListView.setAdapter(mAdapter);
participantListView.setEmptyView(textView);
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
task = new ParticipantsAsyncTask();
task.execute();
refreshLayout.setRefreshing(false);
}
});
return rootView;
}
private class ParticipantsAsyncTask extends AsyncTask<Void, Void, List<Participant>> {
private ProgressDialog progress;
#Override
protected void onPreExecute() {
progress = new ProgressDialog(getContext());
progress.setMessage("Gathering Data...");
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setIndeterminate(true);
progress.setCancelable(false);
progress.show();
}
#Override
protected void onPostExecute(List<Participant> data) {
// Clear the adapter of previous participant data
mAdapter.clear();
// If there is a valid list of {#link Event}s, then add them to the adapter's
// data set. This will trigger the ListView to update.
if (data != null && !data.isEmpty()) {
mAdapter.addAll(data);
}
}
#Override
protected List<Participant> doInBackground(Void... params) {
List<Participant> result;
if (!mGender.isEmpty()) {
HashMap<String, String> map = new HashMap<String, String>();
map.put(Config.KEY_EVENT_ID, mEventId);
map.put(Config.KEY_PARTICIPANT_ID, mParticipantId);
map.put(Config.KEY_GENDER, mGender);
result = QueryUtils.extractParticipantData(map, Config.FEVER_GENDER_FILTER_PARTICIPANT_URL);
} else {
HashMap<String, String> map = new HashMap<String, String>();
map.put(Config.KEY_EVENT_ID, mEventId);
map.put(Config.KEY_PARTICIPANT_ID, mParticipantId);
Log.v("law", mEventId + ", " + mParticipantId);
result = QueryUtils.extractParticipantData(map, Config.FEVER_ALL_PARTICIPANT_URL);
}
progress.dismiss();
return result;
}
}
#Override
public void onDestroyView() {
super.onDestroyView();
mAdapter.clear();
}
}
participant_list.xml
<?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">
<TextView
android:id="#+id/no_participant"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:gravity="center_horizontal"
android:text="#string/no_participant_found"
android:textColor="#color/primaryText"
android:textSize="24sp"
android:visibility="gone" />
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe_refresh_layout_participant_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<ListView
android:id="#+id/participant_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#color/divider"
android:dividerHeight="1dp"
android:descendantFocusability="beforeDescendants"
android:drawSelectorOnTop="true"
android:headerDividersEnabled="true" />
</android.support.v4.widget.SwipeRefreshLayout>
<Button
android:id="#+id/submit_participant"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit"
android:textSize="20sp"
android:background="#color/colorAccent"/>
</LinearLayout>
participant_list_item.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:layout_width="match_parent"
android:layout_height="80dp"
android:clickable="true"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="horizontal"
android:padding="8dp">
<TextView
android:id="#+id/participant_list_item_id"
android:layout_width="0dp"
android:textStyle="bold"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="2"
android:textSize="20sp"
tools:text="M78" />
<TextView
android:id="#+id/participant_list_item_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_weight="5"
android:textSize="20sp"
android:fontFamily="sans-serif"
tools:text="Billu Barber" />
<EditText
android:id="#+id/participant_list_item_notes"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_weight="6"
android:textSize="12sp"
android:background="#drawable/participant_list_notes"
android:hint="#string/participant_notes"
android:inputType="textMultiLine"
android:scrollbars="none"
android:imeOptions="actionDone"
android:maxLength="50"
android:padding="4dp" />
<CheckBox
android:id="#+id/participant_list_item_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp" />
</LinearLayout>
My guess is that at the AdapterView.OnItemClickListener you are using rootView to access the cell properties. Like:
EditText notes = (EditText) rootView.findViewById(R.id.participant_list_item_notes);
...
CheckBox checkBox = (CheckBox) rootView.findViewById(R.id.participant_list_item_checkbox);
You should use the parameter View view from onItemClick like that:
EditText notes = (EditText) view.findViewById(R.id.participant_list_item_notes);
...
CheckBox checkBox = (CheckBox) view.findViewById(R.id.participant_list_item_checkbox);
Let me know if it solves your problem.
Edit:
If the problem is having the item clicked, check https://stackoverflow.com/a/20755698/2174489
I am trying to change the text and check box response of the clicked list item but I am not able to do so. As I not able to get the clicked list item.
The fragment is a part of view pager.
Here is my code,
public class ParticipantsFragment extends Fragment {
private ParticipantAdapter mAdapter;
SwipeRefreshLayout refreshLayout;
private String mParticipantId, mEventId, mGender;
ParticipantsAsyncTask task;
public ParticipantsFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mEventId = getArguments().getString(Config.BUNDLE_KEY_EVENT_ID);
mParticipantId = getArguments().getString(Config.BUNDLE_KEY_PARTICIPANT_ID);
mGender = getArguments().getString(Config.BUNDLE_KEY_GENDER);
Log.v("fragh", mEventId + " " + mParticipantId);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.participant_list, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.no_participant);
task = new ParticipantsAsyncTask();
task.execute();
ListView participantListView = (ListView) rootView.findViewById(R.id.participant_list);
refreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_refresh_layout_participant_list);
participantListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.v("Clicked Participant",mAdapter.getItem(position).getParticipantName());
EditText notes = (EditText) rootView.findViewById(R.id.participant_list_item_notes);
String text = notes.getText().toString();
Log.v("Participant Text",text);
CheckBox checkBox = (CheckBox) rootView.findViewById(R.id.participant_list_item_checkbox);
String check;
if(checkBox.isChecked())
{
check = "1";
}
else
{
check = "0";
}
}
});
Button submit = (Button) rootView.findViewById(R.id.submit_participant);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
mAdapter = new ParticipantAdapter(getContext(), new ArrayList<Participant>());
participantListView.setAdapter(mAdapter);
participantListView.setEmptyView(textView);
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
task = new ParticipantsAsyncTask();
task.execute();
refreshLayout.setRefreshing(false);
}
});
return rootView;
}
private class ParticipantsAsyncTask extends AsyncTask<Void, Void, List<Participant>> {
private ProgressDialog progress;
#Override
protected void onPreExecute() {
progress = new ProgressDialog(getContext());
progress.setMessage("Gathering Data...");
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setIndeterminate(true);
progress.setCancelable(false);
progress.show();
}
#Override
protected void onPostExecute(List<Participant> data) {
// Clear the adapter of previous participant data
mAdapter.clear();
// If there is a valid list of {#link Event}s, then add them to the adapter's
// data set. This will trigger the ListView to update.
if (data != null && !data.isEmpty()) {
mAdapter.addAll(data);
}
}
#Override
protected List<Participant> doInBackground(Void... params) {
List<Participant> result;
if (!mGender.isEmpty()) {
HashMap<String, String> map = new HashMap<String, String>();
map.put(Config.KEY_EVENT_ID, mEventId);
map.put(Config.KEY_PARTICIPANT_ID, mParticipantId);
map.put(Config.KEY_GENDER, mGender);
result = QueryUtils.extractParticipantData(map, Config.FEVER_GENDER_FILTER_PARTICIPANT_URL);
} else {
HashMap<String, String> map = new HashMap<String, String>();
map.put(Config.KEY_EVENT_ID, mEventId);
map.put(Config.KEY_PARTICIPANT_ID, mParticipantId);
Log.v("law", mEventId + ", " + mParticipantId);
result = QueryUtils.extractParticipantData(map, Config.FEVER_ALL_PARTICIPANT_URL);
}
progress.dismiss();
return result;
}
}
#Override
public void onDestroyView() {
super.onDestroyView();
mAdapter.clear();
}
}
participant_list.xml
<?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">
<TextView
android:id="#+id/no_participant"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:gravity="center_horizontal"
android:text="#string/no_participant_found"
android:textColor="#color/primaryText"
android:textSize="24sp"
android:visibility="gone" />
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe_refresh_layout_participant_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<ListView
android:id="#+id/participant_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#color/divider"
android:dividerHeight="1dp"
android:descendantFocusability="beforeDescendants"
android:drawSelectorOnTop="true"
android:headerDividersEnabled="true" />
</android.support.v4.widget.SwipeRefreshLayout>
<Button
android:id="#+id/submit_participant"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit"
android:textSize="20sp"
android:background="#color/colorAccent"/>
</LinearLayout>
participant_list_item.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:layout_width="match_parent"
android:layout_height="80dp"
android:clickable="true"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="horizontal"
android:padding="8dp">
<TextView
android:id="#+id/participant_list_item_id"
android:layout_width="0dp"
android:textStyle="bold"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="2"
android:textSize="20sp"
tools:text="M78" />
<TextView
android:id="#+id/participant_list_item_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_weight="5"
android:textSize="20sp"
android:fontFamily="sans-serif"
tools:text="Billu Barber" />
<EditText
android:id="#+id/participant_list_item_notes"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_weight="6"
android:textSize="12sp"
android:background="#drawable/participant_list_notes"
android:hint="#string/participant_notes"
android:inputType="textMultiLine"
android:scrollbars="none"
android:imeOptions="actionDone"
android:maxLength="50"
android:padding="4dp" />
<CheckBox
android:id="#+id/participant_list_item_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp" />
</LinearLayout>
Found the answer atlast.
Don't use setOnItemClickListener for item click. Use item view click inside adapter method by using setOnClickListener(). Do things in onClick()
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View listItemView = convertView;
if (listItemView == null)
{
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.participant_list_item, parent, false);
}
listItemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Toast.makeText(mContext, "click item",Toast.LENGTH_LONG).show();
}
});
Participant currentParticipant = getItem(position);
if(currentParticipant.getParticipantGender().equals("F"))
{
TextView idView = (TextView) listItemView.findViewById(R.id.participant_list_item_id);
idView.setTextColor(getColor(mContext, R.color.colorPrimary));
idView.setText(currentParticipant.getParticipantId());
}
else if(currentParticipant.getParticipantGender().equals("M"))
{
TextView idView = (TextView) listItemView.findViewById(R.id.participant_list_item_id);
idView.setTextColor(getColor(mContext, R.color.colorAccent));
idView.setText(currentParticipant.getParticipantId());
}
TextView nameView = (TextView) listItemView.findViewById(R.id.participant_list_item_name);
nameView.setText(currentParticipant.getParticipantName());
final EditText notesView = (EditText) listItemView.findViewById(R.id.participant_list_item_notes);
notesView.setText(currentParticipant.getParticipantNotes());
CheckBox checkedView = (CheckBox) listItemView.findViewById(R.id.participant_list_item_checkbox);
if (currentParticipant.getParticipantCheck().equals("1"))
{
checkedView.setChecked(true);
}
else
{
checkedView.setChecked(false);
}
return listItemView;
}
Remove this from participant_list_item.xml
android:descendantFocusability="blocksDescendants"
Thank you every one for the help.
<CheckBox
android:id="#+id/participant_list_item_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:focusable="false"
android:clickable="false"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp" />
Try to replace your CheckBox and add this two line
android:focusable="false"
android:clickable="false"
Try this should work in participant_list_item.xml and remove from listview
android:descendantFocusability="blocksDescendants"
you have to do it like this
private EditText notes;
private CheckBox checkBox;
and put these above onclick
notes = (EditText) rootView.findViewById(R.id.participant_list_item_notes);
String text = notes.getText().toString();
Log.v("Participant Text",text);
checkBox = (CheckBox) rootView.findViewById(R.id.participant_list_item_checkbox);
edittext and check box are the view in the listview
hope this will help you
android:clickable=true
Make sure that you have added clickable property to true.
In participantslist.xml file:
<ListView
android:id="#+id/participant_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#color/divider"
android:dividerHeight="1dp"
android:descendantFocusability="beforeDescendants"
android:drawSelectorOnTop="true"
android:headerDividersEnabled="true"
android:clickable=true/>
And let me know if this suggestion was wrong?
I used Fragmentpageradapter and out of this one fragment used autocompletetextview but i can't able to write anything or does not show keyboard on click on autocompletetextview.
Source :
layout_autocompleteTextview :
<RelativeLayout
android:id="#+id/rel_seacrh"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:visibility="gone"
android:background="#color/colorPrimary"
android:padding="10dp">
<AutoCompleteTextView
android:id="#+id/edt_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:text=""
android:imeOptions="actionGo|flagNoExtractUi"
android:background="#drawable/search_background"
android:hint="Search Category" />
<requestFocus/>
Fragment that i am used the autocomplete textview
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = null;
manager = getActivity().getSupportFragmentManager();
// Context c = getActivity().getApplicationContext();
this.context = container.getContext();
rootView = inflater.inflate(R.layout.activity_home, container, false);
categoryGridView = (GridView) rootView.findViewById(R.id.grid_category_item);
rel_search = (RelativeLayout) rootView.findViewById(R.id.rel_seacrh);
edt_search = (AutoCompleteTextView) rootView.findViewById(R.id.edt_search);
adapterCategory = new CategoryListAdapter(this.context, categoryData.getCategoryItems());
this.categoryGridView.setAdapter(adapterCategory);
this.categoryGridView.setOnItemClickListener(new CategoryItemClickListener());
for (int i=0; i<categoryData.getCategoryItems().size(); i++)
{
item.add(categoryData.getCategoryItems().get(i).getName());
}
//Create adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_dropdown_item_1line, item);
edt_search.setThreshold(1);
//Set adapter to AutoCompleteTextView
edt_search.setAdapter(adapter);
edt_search.setOnItemSelectedListener(this);
edt_search.setOnItemClickListener(this);
return rootview;
}
}
Image :
Have you tried this in manifest file,
android:windowSoftInputMode="stateAlwaysVisible"
(SOLVED) -- Removed layout params on each view ---
I am new to Android and I am trying to add rows in a TableLayout from the items that I entered in text fields.
(EDITED) Here's my layout for this Fragment:
<?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="fill_parent"
android:tag="contacts">
<LinearLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/contactName"
android:hint="Name"
android:paddingTop="5dip" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:id="#+id/contactRelationship"
android:hint="Relationship"
android:paddingTop="5dip" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPostalAddress"
android:ems="10"
android:id="#+id/contactAddress"
android:hint="Address"
android:paddingTop="5dip" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/contactPhoneNo"
android:hint="Phone Number"
android:paddingTop="5dip" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/contactPhoneTypeSpinner"
android:layout_toEndOf="#+id/contactPhoneNo"
android:layout_toRightOf="#+id/contactPhoneNo"
android:paddingTop="5dip" />
</RelativeLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="107dp"
android:inputType="textMultiLine"
android:ems="10"
android:id="#+id/contactSpecialNotes"
android:paddingTop="5dip"
android:hint="Write special notes here" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Contact"
android:id="#+id/addContact" />
<TableLayout
android:id="#+id/contactsTableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dip"></TableLayout>
</LinearLayout>
</ScrollView>
(EDITED)Here's the code for this Fragment:
public static class ContactsFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_section_contacts,
container, false);
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Spinner phoneTypeSpinner = (Spinner) this.getView().findViewById(R.id.contactPhoneTypeSpinner);
Button contactButton = (Button) this.getView().findViewById(R.id.addContact);
final TableLayout tableLayout = (TableLayout) this.getView().findViewById(R.id.contactsTableLayout);
final EditText txtContactName = (EditText) this.getView().findViewById(R.id.contactName);
final EditText txtContactRelationship = (EditText) this.getView().findViewById(R.id.contactRelationship);
final EditText txtContactAddress = (EditText) this.getView().findViewById(R.id.contactAddress);
final EditText txtContactPhoneNo = (EditText) this.getView().findViewById(R.id.contactPhoneNo);
Spinner contactPhoneTypeSpinner = (Spinner) this.getView().findViewById(R.id.contactPhoneTypeSpinner);
EmployeeDataSource datasource = new EmployeeDataSource(getActivity());
datasource.open();
//--- Gender Spinner ---
List<PhoneType> list = datasource.getPhoneTypeList();
ArrayAdapter<PhoneType> adapter = new ArrayAdapter<PhoneType>(getActivity(),
android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
phoneTypeSpinner.setAdapter(adapter);
phoneTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
contactButton.setOnClickListener(new View.OnClickListener() {
int rowId = 0;
#Override
public void onClick(View v) {
TableRow tableRow = new TableRow(getActivity().getApplicationContext());
tableRow.setId(rowId++);
tableRow.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
TextView tvContactName = new TextView(getActivity().getApplicationContext());
tvContactName.setText(txtContactName.getText().toString());
tvContactName.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
tableRow.addView(tvContactName);
TextView tvRelationship = new TextView(getActivity().getApplicationContext());
tvRelationship.setText(txtContactRelationship.getText().toString());
//tvRelationship.setLayoutParams(new ViewGroup.LayoutParams(
// ViewGroup.LayoutParams.FILL_PARENT,
// ViewGroup.LayoutParams.WRAP_CONTENT));
tableRow.addView(tvRelationship);
TextView tvContactNo = new TextView(getActivity().getApplicationContext());
tvContactNo.setText(txtContactPhoneNo.getText().toString());
//tvContactNo.setLayoutParams(new ViewGroup.LayoutParams(
// ViewGroup.LayoutParams.FILL_PARENT,
// ViewGroup.LayoutParams.WRAP_CONTENT));
tableRow.addView(tvContactNo);
tableLayout.addView(tableRow, new TableLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
}
});
}
}
I could see in the debugger that the values from my views were added in my tableRow but I don't see the table appearing in my screen, what am I missing or doing wrong?
Any help would be greatly appreciated.
Thanks!
I guess the height of screen is increased. Try using Scrollview on top of this layout.
I think you have to change RelativeLayout property android:height="wrap_content" instead of android:height="fill_parent" and also do for TableLayout
May this work for you...
Try this:
Edited:
public static class ContactsFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_section_contacts,
container, false);
Spinner phoneTypeSpinner = (Spinner) rootView.findViewById(R.id.contactPhoneTypeSpinner);
Button contactButton = (Button) rootView.findViewById(R.id.addContact);
final TableLayout tableLayout = (TableLayout) rootView.findViewById(R.id.contactsTableLayout);
final EditText txtContactName = (EditText) rootView.findViewById(R.id.contactName);
final EditText txtContactRelationship = (EditText) rootView.findViewById(R.id.contactRelationship);
final EditText txtContactAddress = (EditText) rootView.findViewById(R.id.contactAddress);
final EditText txtContactPhoneNo = (EditText) rootView.findViewById(R.id.contactPhoneNo);
Spinner contactPhoneTypeSpinner = (Spinner) rootView.findViewById(R.id.contactPhoneTypeSpinner);
EmployeeDataSource datasource = new EmployeeDataSource(getActivity());
datasource.open();
//--- Gender Spinner ---
List<PhoneType> list = datasource.getPhoneTypeList();
ArrayAdapter<PhoneType> adapter = new ArrayAdapter<PhoneType>(getActivity(),
android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
phoneTypeSpinner.setAdapter(adapter);
phoneTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
contactButton.setOnClickListener(new View.OnClickListener() {
int rowId = 0;
#Override
public void onClick(View v) {
TableRow tableRow = new TableRow(getActivity().getApplicationContext());
tableRow.setId(rowId++);
TextView tvContactName = new TextView(getActivity().getApplicationContext());
tvContactName.setText(txtContactName.getText().toString());
tableRow.addView(tvContactName);
TextView tvRelationship = new TextView(getActivity().getApplicationContext());
tvRelationship.setText(txtContactRelationship.getText().toString());
tableRow.addView(tvRelationship);
TextView tvContactNo = new TextView(getActivity().getApplicationContext());
tvContactNo.setText(txtContactPhoneNo.getText().toString());
tableRow.addView(tvContactNo);
tableLayout.addView(tableRow, new TableLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
}
});
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}