When I click EditText, I want spinner to open the menu. When I click on textviewplaces, I want the spinner menu to be opened. How can I do this?
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, OnItemSelectedListener {
private Spinner spinnerDevice;
private static final String[] paths = {"......"};
TextView textviewPlaces;
spinnerDevice = (Spinner)findViewById(R.id.spinnerDevice);
ArrayAdapter<String>adapterSpinnerDevice = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item,paths);
textviewPlaces = findViewById(R.id.places_textView);
adapterSpinnerDevice.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerDevice.setAdapter(adapterSpinnerLocation);
spinnerDevice.setOnItemSelectedListener(this);
XML File:
<AutoCompleteTextView
android:id="#+id/places_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="44dp"
android:layout_marginTop="31dp"
android:text="TextView"
android:drawableLeft="#drawable/location"
android:drawablePadding="2dp"
android:ems="10"
android:textStyle="bold" />
Use AutoCompleteTextView instead of it
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
getApplicationContext(), android.R.layout.simple_dropdown_item_line,
paths);
//ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String> (this, android.R.layout.select_dialog_item, paths);
final AutoCompleteTextView textView;
textView = (AutoCompleteTextView) findViewById(R.id.textView);
textView.setAdapter(arrayAdapter);
/* textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View arg0) {
textView.showDropDown();
}
}); */
Define UI element in your layout file
<AutoCompleteTextView
android:id="#+id/textView"
android:layout_width="match_content"
android:layout_height="wrap_content"
android:ems="10" />
If you want something like this image
AutoCompeleteTextView Exmaple
Arwy Shelke answer can help you, but if you want to open a real spinner you can use this code:
spinner.performClick();
Related
I'm developing with android studio.
I have a ListView that the elements in it are aligned from left to right and I want to align them from right to left, how can I do so?
the java class is:
public class ProductList extends ListActivity {
ArrayList<String> list = new ArrayList<String>(Arrays.asList("במבה אוסם", "בשר טחון", "קוקה קולה שישייה", "קפה עלית", "שמיר", "מילקי","רבעיית דניאלה"));
ArrayAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pop_productlist);
Button btnDel = (Button) findViewById(R.id.btnDel);
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, list);
/** Defining a click event listener for the button "Delete" */
View.OnClickListener listenerDel = new View.OnClickListener() {
#Override
public void onClick(View v) {
/** Getting the checked items from the listview */
SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
int itemCount = getListView().getCount();
for(int i=itemCount-1; i >= 0; i--){
if(checkedItemPositions.get(i)){
adapter.remove(list.get(i));
}
}
checkedItemPositions.clear();
adapter.notifyDataSetChanged();
}
};
/** Setting the event listener for the delete button */
btnDel.setOnClickListener(listenerDel);
/** Setting the adapter to the ListView */
setListAdapter(adapter);
}
public void StartCalck(View view){
Intent intent = new Intent(ProductList.this, SplitBuying.class);
startActivity(intent);
}
}
and the layout is:
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:choiceMode="multipleChoice"
android:layout_below="#+id/ChooseStore">
</ListView>
</LinearLayout>
Thank you !
Use RecyclerView instead of ListView.
LinearLayoutManager(Context context, int orientation, boolean reverseLayout)
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right" ><br>
<TextView
android:id="#+id/textView_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"/><br>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_toLeftOf="#+id/textView_name"/>
</RelativeLayout>
Manifest.xml
<application
....
android:supportsRtl="true"
...
</application>
you should use custom layout like
myTextView.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:gravity="right"
android:background="#bdbdbd"/>
pass this layout to adapter like
ArrayAdapter adapter = new ArrayAdapter(this,R.layout.nameOfLayout, list);
I am displaying a Listview of recorded data and when the user clicks on a specific list item, it triggers an intent and brings the user to the profile for that selected user. I worked when i first ran my project but ever since it just will not work. Thank you in advance.
public class CowListView extends ListActivity {
RegisterCowAdapter cowAdapter;
private DataBaseHelper databaseHelper;
public ListView listView;
TextView student_Id;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cow_list_item);
cowAdapter = new RegisterCowAdapter(this);
cowAdapter.open();
updateCowUI();
listView = (ListView) findViewById(android.R.id.list);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
student_Id = (TextView) view.findViewById(R.id.cowtagnolist);
String studentId = student_Id.getText().toString();
Intent objIndent = new Intent(getApplicationContext(), CowProfile.class);
Toast.makeText(CowListView.this, "Clicked", Toast.LENGTH_SHORT).show();
objIndent.putExtra("student_Id", Integer.parseInt(studentId));
startActivity(objIndent);
}
});
}
private void updateCowUI() {
Cursor cursor = cowAdapter.getAllCowData();
startManagingCursor(cursor);
String[] from = new String[]{RegisterCowAdapter.COWTAGNO, RegisterCowAdapter.COWDOB};
int[] to = new int[]{R.id.cowtagnolist, R.id.cowdoblist};
SimpleCursorAdapter reminders = new SimpleCursorAdapter(
this,
R.layout.cow_list_item,
cursor,
from,
to
);
this.setListAdapter(reminders);
}
}
private void updateCowUI() {
Cursor cursor =cowAdapter.getAllCowData();
startManagingCursor(cursor);
String[] from = new String[]{RegisterCowAdapter.COWTAGNO, RegisterCowAdapter.COWDOB};
int[] to = new int[]{R.id.cowtagnolist, R.id.cowdoblist};
SimpleCursorAdapter reminders = new SimpleCursorAdapter(
this,
R.layout.cow_list_item,
cursor,
from,
to
);
this.setListAdapter(reminders);
}
}
My XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:longClickable="true">
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</ListView>
<TextView
android:id="#+id/cowtagnolist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/black"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:descendantFocusability="blocksDescendants"
>
</TextView>
<TextView
android:id="#+id/cowdoblist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="17dp"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:descendantFocusability="blocksDescendants"
android:textColor="#color/black">
</TextView>
</LinearLayout>
Might be one of two things.
You have implemented OnClickListener but you have not set the ListView to listen to that specific class nor have you called any method in your onCreate that will set the ListView's listener to that class.Secondly I think you might need to implement OnItemClickListener and not onCLickListener since thats what is on the ListView, an Item.Im confused though why do you have onClick and then you set the listener inside this method? How will it know in the first place that the view has been clicked if it is not listening from the beginning i.e. in the onCreate or a method called in the onCreate ? Your logic seems abit off
I am working with AutoCompleteTextView. I have added few items through Activity. After AutoCompleteTextView , I've given a "submit" button. I want to sync this button with AutoCompleteTextView . I want to click submit button when any item is selected and after clicking button, it should switch to that item's activity. Currently I don't have code with me. Please suggest me with any example.
Here is the code for AutoCompleteTextView
MainActivity.java
public class MainActivity extends Activity {
public static final String[] COUNTRIES = new String[] { "Belgium", "France",
"Italy", "Germany", "Spain" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// TODO Auto-generated method stub
// In the onCreate method
final AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.actv_country);
//EditText e = (EditText) findViewById(R.id.editText1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
MainActivity.this, android.R.layout.simple_dropdown_item_1line, COUNTRIES);
textView.setAdapter(adapter);
// textView.setThreshold(0);
}
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" >
<AutoCompleteTextView
android:id="#+id/actv_country"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
</AutoCompleteTextView>
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
so I have an app with 3 fragments. One of the fragments involves adding characters to a listview and then giving them weapons and abilities. Weapons and abilities are given to a characters via a custom dialog that appears when the Add button is clicked.
This dialog contains a spinner with a predetermined list of names of characters and then 3 edit-texts that require the user to enter the number of abilities that the characters is supposed to have.
My problem is that the spinner in this dialog is not displaying its contents.
Everytime I click on the dialog I get this :
W/InputEventReceiver﹕ Attempted to finish an input event but the input event receiver has already been disposed.
Rewards.java
public class Rewards extends Fragment {
private Button button;
Context mContext;
int lesserCount;
int greaterCount;
int exaltedCount;
Dialog addCharDialog;
Dialog lesserDialog;
Dialog greaterDialog;
Dialog exaltedDialog;
Spinner SpnLesser1;
Spinner SpnLesser2;
Spinner SpnLesser3;
Spinner SpnGreater1;
Spinner SpnGreater2;
Spinner SpnExalted;
Spinner SpnChar;
ArrayList<Character> chars = new ArrayList();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_rewards, container, false);
View addCharDialogView = inflater.inflate(R.layout.character_add, container, false);
button = (Button) rootView.findViewById(R.id.btnadd);
mContext = getActivity();
// custom dialogs
createAddDialog(addCharDialogView);
createLesserDialog();
createGreaterDialog();
createExaltedDialog();
SpnLesser1 = (Spinner) rootView.findViewById(R.id.lesserspinner1);
SpnLesser2 = (Spinner) rootView.findViewById(R.id.lesserspinner2);
SpnLesser3 = (Spinner) rootView.findViewById(R.id.lesserspinner3);
SpnGreater1 = (Spinner) rootView.findViewById(R.id.greaterspinner1);
SpnGreater2 = (Spinner) rootView.findViewById(R.id.greaterspinner2);
SpnExalted = (Spinner) rootView.findViewById(R.id.exaltedspinner);
return rootView;
}
public View createAddDialog(View addCharDialogView) {
LayoutInflater inflater = LayoutInflater.from(this.getActivity());
addCharDialog = new Dialog(mContext);
addCharDialog.setContentView(R.layout.character_add);
addCharDialog.setTitle("Add a character");
SpnChar = (Spinner) addCharDialogView.findViewById(R.id.dialog_spinner);
ArrayAdapter<CharSequence> dataAdapter = ArrayAdapter.createFromResource(getActivity().getBaseContext(),R.array.item_array, R.layout.spinner_layout);
//ArrayAdapter<CharSequence> dataAdapter = ArrayAdapter.createFromResource(addCharDialogView.getContext(),R.array.item_array, R.layout.spinner_layout);
dataAdapter.setDropDownViewResource
(android.R.layout.simple_spinner_dropdown_item);
SpnChar.setAdapter(dataAdapter);
//spinner is populated but it's not displaying and won't do anything when clicked on
SpnChar.setSelection(2);
String Text = SpnChar.getSelectedItem().toString();
Log.d("2", Text);
Button dialogOkButton = (Button) addCharDialog.findViewById(R.id.dialogOK);
// if button is clicked, close the custom dialog
dialogOkButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
lesserCount = R.id.lessertext;
greaterCount = R.id.greatertext;
exaltedCount = R.id.exaltedtext;
Character c;
if (lesserCount > 0) {
lesserDialog.show();
}
if (greaterCount > 0) {
greaterDialog.show();
}
if (exaltedCount > 0) {
exaltedDialog.show();
}
}
});
Button dialogCancelButton = (Button) addCharDialog.findViewById(R.id.dialogCancel);
// if button is clicked, close the custom dialog
dialogCancelButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
addCharDialog.dismiss();
}
});
// add button listener
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
addCharDialog.show();
}
});
return addCharDialogView;
}
}
}
character_add.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!--<TextView-->
<!--android:id="#+id/dialog_label"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_width="fill_parent"-->
<!--android:hint="Please select an option"-->
<!--/>-->
<Spinner
android:id="#+id/dialog_spinner"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
<Button
android:id="#+id/dialogOK"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="OK"
android:layout_marginTop="113dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/dialogCancel"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_alignTop="#+id/dialogOK"
android:layout_toRightOf="#+id/dialogOK" />
<EditText
android:layout_width="60dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/lessertext"
android:layout_below="#+id/dialog_spinner"
android:layout_toRightOf="#+id/editText8"
android:text="0" />
<EditText
android:layout_width="60dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/greatertext"
android:layout_below="#+id/dialog_spinner"
android:layout_toRightOf="#+id/editText9"
android:text="0" />
<EditText
android:layout_width="60dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/exaltedtext"
android:layout_below="#+id/dialog_spinner"
android:layout_toRightOf="#+id/editText10"
android:text="0" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText8"
android:layout_below="#+id/dialog_spinner"
android:layout_alignParentStart="true"
android:text="L" />
<EditText
android:layout_width="50dp"
android:layout_height="wrap_content"
android:id="#+id/editText9"
android:layout_below="#+id/dialog_spinner"
android:layout_toRightOf="#+id/lessertext"
android:text="G" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText10"
android:layout_below="#+id/dialog_spinner"
android:layout_toRightOf="#+id/greatertext"
android:text="E" />
If I understand correctly, you want a spinner that can display the input the user types in? If so, you could use an Arraylist and create a method that will destroy your spinner and recreate it every time the contents changes. You could set it up as follows:
ArrayList<String> thing = new ArrayList<String>();
ViewGroup layout = null;
layout = (ViewGroup) spinner.getParent();
public void addSpinner(ArrayList thing){
layout.removeView(spinner);
layout.addView(spinnersy2);
spinner =(Spinner) findViewById(R.id.spinner1);
ArrayAdapter <String>adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, thing);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
Create a second method that will add the user input into the array.
How to take data that was typed in EditText and by clicking "submit" in that window should add it to previous activity listview items?
What I need to do is:
Creating EditText and submit button
Creating listview in same Activity
By clicking submit button it should display it in listview.
I saw this similar question here:add items to listview dynamically android
But i couldn't understand the answer.Somebody please explain how to do this.
You just do the following :
Prepare your xml like this :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/addItem"
android:hint="Add a new item to List View" />
<Button
android:id="#+id/addItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Add" />
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/editText" >
</ListView>
</RelativeLayout>
Activity looks like following :
public class MainActivity extends Activity {
EditText editText;
Button addButton;
ListView listView;
ArrayList<String> listItems;
ArrayAdapter<String> adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
addButton = (Button) findViewById(R.id.addItem);
listView = (ListView) findViewById(R.id.listView);
listItems = new ArrayList<String>();
listItems.add("First Item - added on Activity Create");
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listItems);
listView.setAdapter(adapter);
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
listItems.add(editText.getText().toString());
adapter.notifyDataSetChanged();
}
});
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_LONG)
.show();
}
});
}
}
Create String Arraylist and initialize it
ArrayList<String> str1 = new ArrayList<String>();
Add some values on it
str1.add("First Row");
str1.add("Second Row");
str1.add("Third Row");
str1.add("Fourth Row");
str1.add("Fifth Row");
Then set Adapter to ListView
adapter=new ListAdapter(this,str1);
list.setAdapter(adapter);
then add your EditText text into str1 and then called adapter.notifyDataSetChanged(); like
str1.add(edit_message.getText().toString());
adapter.notifyDataSetChanged();
Try to add this code into Button onclick()
Demo Output:
Suppose you have an arraylist
ArrayList<String> dataList = new Arraylist ();
So On clicking of button, you need to add the new data item into your data arraylist.
For this first take the value entered in edittext and store in a string.
String editedValue = yourEditText.getText.toString();
Then we need to add this in our datalist.
Like
dataList.add(editedValue);
And then just call adapter.notifyDataSetChanged()
yourAdapter.notifyDataSetChanged();
It will work.