i am using spinner to display month List.
but
i wanna to set a images to overlaying month list? but i am nt founded any Exact solution.
Spinner.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Spinner
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true" />
<ImageView android:id="#+id/myimgview" android:layout_width="fill_parent" android:layout_height="fill_parent"></ImageView>
</LinearLayout>
Your Activity Class must look like this :
package my.co.home;
import android.app.Activity;
import android.content.Intent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
public class ViewsActivity extends Activity
{
String[] presidents = {
"January",
"February",
"March",
"December"
};
Spinner s1;
ImageView ivl;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//startActivity(new Intent(this, SpinnerActivity.class));
s1 = (Spinner) findViewById(R.id.spinner1);
ivl=(ImageView) findViewById(R.id.myimgview);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, presidents);
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0,
View arg1, int arg2, long arg3)
{
int index = s1.getSelectedItemPosition();
//Depending on index you display the image..For example i have taken for //two indexes
if(index==1)
ivl.setBackgroundDrawable(getResources().getDrawable(R.drawable.images));
else if(index==2)
ivl.setBackgroundDrawable(getResources().getDrawable(R.drawable.images1));
}
public void onNothingSelected(AdapterView<?> arg0) {}
});
}
}
Related
I'm trying to make a simple note-taking app. I tried to display the notes in the listview. But when I add more content to the note it displays the entire content in the main menu of the list view. How to make only the title of the list visible in the Listview and the content visible only when we open the list item?
MainActitvity.java
package com.example.notes;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
public class MainActivity extends AppCompatActivity {
static ArrayList<String> notes=new ArrayList<>();
static ArrayAdapter arrayAdapter;
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater menuInflater=getMenuInflater();
menuInflater.inflate(R.menu.add_note_menu,menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
super.onOptionsItemSelected(item);
if(item.getItemId()==R.id.add_note){
Intent intent=new Intent(getApplicationContext(),NoteEditorActivity.class);
startActivity(intent);
return true;
}
return false;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listview=(ListView) findViewById(R.id.listView);
SharedPreferences sharedPreferences=getApplicationContext().getSharedPreferences("com.robpercival.notes", Context.MODE_PRIVATE);
HashSet<String> set=(HashSet<String>)sharedPreferences.getStringSet("notes",null);
if(set==null) {
notes.add("Example note");
}
else{
notes=new ArrayList(set);
}
notes.add("Note 1");
arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,notes) ;
listview.setAdapter(arrayAdapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int i, long l) {
Intent intent=new Intent(getApplicationContext(),NoteEditorActivity.class);
intent.putExtra("noteId",i);
startActivity(intent);
}
});
listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int i, long l) {
final int itemToDelete=i;
new AlertDialog.Builder(MainActivity.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("are you sure?")
.setMessage("do you want to delete?")
.setPositiveButton("yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
notes.remove(itemToDelete);
arrayAdapter.notifyDataSetChanged();
SharedPreferences sharedPreferences=getApplicationContext().getSharedPreferences("com.robpercival.notes", Context.MODE_PRIVATE);
HashSet<String> set=new HashSet<>(MainActivity.notes);
sharedPreferences.edit().putStringSet("notes",set).apply();
}
})
.setNegativeButton("no",null)
.show();
return true;
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="#drawable/note1"
tools:context=".MainActivity">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
note_editor_activity.java
package com.example.notes;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import com.google.android.material.textfield.TextInputLayout;
import java.util.HashSet;
public class NoteEditorActivity extends AppCompatActivity {
int noteId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_editor);
EditText editText=(EditText) findViewById(R.id.editText);
Intent intent=getIntent();
noteId=intent.getIntExtra("noteId",-1);
if(noteId!=-1){
editText.setText(MainActivity.notes.get(noteId));
}
else{
MainActivity.notes.add("");
noteId=MainActivity.notes.size()-1;
MainActivity.arrayAdapter.notifyDataSetChanged();
}
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
MainActivity.notes.set(noteId,String.valueOf(charSequence));
MainActivity.arrayAdapter.notifyDataSetChanged();
SharedPreferences sharedPreferences=getApplicationContext().getSharedPreferences("com.robpercival.notes", Context.MODE_PRIVATE);
HashSet<String> set=new HashSet<>(MainActivity.notes);
sharedPreferences.edit().putStringSet("notes",set).apply();
}
#Override
public void afterTextChanged(Editable editable) {
}
});
}
}
acitivity_node-editor.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:background="#drawable/olo"
tools:context=".NoteEditorActivity">
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:gravity="start|top"
android:inputType="textMultiLine"
android:padding="0dp"
tools:layout_editor_absoluteX="118dp"
tools:layout_editor_absoluteY="91dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
add_note_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/add_note" android:title="Add note"></item>
</menu>
Sorry, about the former one, I didn't explain properly using another format use two edittext one for title and one for the body.Save them in different preferences using the body key as the title
Pass the title preference to the listview.
I am trying to make a list view search for Android. I have found many tutorials that do just that where
a search-bar is placed at the top and if you type in the box the results get filtered.
In my app I want to click on given items after filtering has been completed, I have implemented setOnItemClickListener. The issue is that after filtering the position of each class that I want to open changes and the incorrect pages open. I was unable to find a solution....
Here is the jave code:
package com.equations.search;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
public class EquationsSearch extends Activity {
// List view
private ListView lv;
// Listview Adapter
ArrayAdapter<String> adapter;
// Search EditText
EditText inputSearch;
// ArrayList for Listview
ArrayList<HashMap<String, String>> productList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.equations_search);
// Listview Data
final String products[] = { "Dell Inspiron", "HTC One X",
"HTC Wildfire S", "HTC Sense", "HTC Sensation XE" };
lv = (ListView) findViewById(R.id.list_view);
inputSearch = (EditText) findViewById(R.id.inputSearch);
// Adding items to listview
adapter = new ArrayAdapter<String>(this,
R.layout.equations_search_list, R.id.product_name, products);
lv.setAdapter(adapter);
/**
* Enabling Search Filter
* */
inputSearch.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
// When user changed the Text
EquationsSearch.this.adapter.getFilter().filter(cs);
}
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
public void afterTextChange(Editable arg0) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// if(position == 1)
String openClass = products[position];
if (openClass.equals("HTC Wildfire S")) {
// code specific to first list item
Intent myIntent = new Intent(view.getContext(), A6262.class);
startActivityForResult(myIntent, 0);
}
}
});
}
}
and here is the xml equations_search.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- Editext for Search -->
<EditText android:id="#+id/inputSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Search products.."
android:inputType="textVisiblePassword"/>
<!-- List View -->
<ListView
android:id="#+id/list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
and equations_search_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" >
<!-- Single ListItem -->
<!-- Product Name -->
<TextView android:id="#+id/product_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold"/>
</LinearLayout>
Thank you in advance.
Rather than products[position], use adapter.getItem(position). When the ListView is not in filter mode, those two things will be the same. But, in filter mode, getItem() will take the filtering into account.
The ListAdapter will change the relative position of the items it currently shows due to the filtering. You should always use getItem(position) to retrieve the correct item.
I am very new to programming. I have an app that has several views. The Main view shows a list such as Breakfast, Lunch & Dinner. When a an item is selected, example Lunch, a list of lunch menu items is displayed such as Hamburger, Cheeseburger, French Fries... (this list is created from the string-array lunch_menu that is stored in \values\lunch.xml) as the user selects the items they want, it is stored in a new array called myNewList and is displayed whe the users presses the lunchList button. All of the items are displayed that the user selected. So far, So good. I created a android:onClick="shareMyList" in the selecteditems.xml and the button works, but does not populate my list. I think what i need is to some how convert it to a string, this is where I need help.
Here is my Problem now.... I have my Share button, that when pressed, I would like it to automatically open the default Messaging app and populate the list from the selected items ListView.
package com.mycompany.lunch;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class LunchListMenu extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maincoarse);
final ListView lv=(ListView)findViewById(R.id.listView1);
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this, R.array.lunch_menu,android.R.layout.simple_list_item_1);
lv.setAdapter(adapter);
final ArrayList<String> myNewList = new ArrayList<String>();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String item=lv.getItemAtPosition(arg2).toString();
String itemordered;
itemordered = item + " added to list";
Toast.makeText(getApplicationContext(), itemordered, Toast.LENGTH_SHORT).show();
myNewList.add(item);
}
});
// List View Button
Button btnLunchList = (Button) findViewById(R.id.lrList);
btnLunchList.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.selecteditems);
ListView selecteditems = (ListView) findViewById(android.R.id.list);
ArrayAdapter<String> newadapter = new ArrayAdapter<String>(LunchListMenu.this, android.R.layout.simple_list_item_1, myNewList);
selecteditems.setAdapter(newadapter);
}
});
}
public void shareMyList(View v){
// Share Selected Items Button
Button btnShareItems = (Button) findViewById(R.id.shareMyList);
btnShareItems.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT, "I'm being sent!!");
startActivity(Intent.createChooser(share, "Share Text"));
}
});
}
}
Here is the Lunch Menu Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/main_background"
android:paddingLeft="10.0dip"
android:paddingTop="0.0dip"
android:paddingRight="10.0dip"
android:paddingBottom="10.0dip"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/lrList"
android:layout_width="72dip"
android:layout_height="72dip"
android:layout_gravity="right"
android:background="#drawable/list" />
<ImageView
android:id="#+id/LunchMenuTitle"
android:contentDescription="#string/LunchMenu"
android:layout_width="0dip"
android:layout_height="72dip"
android:layout_weight="0.96"
android:background="#drawable/lunch"
android:paddingLeft="10.0dip"
android:paddingRight="10.0dip" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="10.0dip"
android:background="#drawable/head"
android:orientation="horizontal" />
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#FFCC00"
android:dividerHeight="2dp" >
</ListView>
<LinearLayout
android:orientation="horizontal"
android:background="#drawable/head"
android:layout_width="fill_parent"
android:layout_height="10.0dip" />
</LinearLayout>
And Here is my selecteditems.xml Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#drawable/main_background"
android:paddingLeft="10.0dip"
android:paddingTop="0.0dip"
android:paddingRight="10.0dip"
android:paddingBottom="10.0dip"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/shareMyList"
android:layout_width="72dip"
android:layout_height="72dip"
android:layout_gravity="right"
android:onClick="shareMyList"
android:background="#drawable/share" />
<ImageView
android:id="#+id/selectedItemsTitle"
android:contentDescription="#string/LunchTitle"
android:layout_width="0dip"
android:layout_height="72dip"
android:layout_weight="0.96"
android:background="#drawable/title"
android:paddingLeft="10.0dip"
android:paddingRight="10.0dip" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="10.0dip"
android:background="#drawable/head"
android:orientation="horizontal" />
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#FFCC00"
android:dividerHeight="2dp"
android:padding="10dip"
android:textColor="#ffffff"
android:textSize="20dip"
android:textStyle="bold" />
</LinearLayout>
This may be the most kludgiest way of doing this, but it works.
package com.mycompany.lunch;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class LunchListMenu extends Activity {
String itemsordered;
/** Called when the activity is first created. */
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maincoarse);
final ListView lv=(ListView)findViewById(R.id.listView1);
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this, R.array.lunch_menu,android.R.layout.simple_list_item_1);
lv.setAdapter(adapter);
final ArrayList<String> myNewList = new ArrayList<String>();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String item=lv.getItemAtPosition(arg2).toString();
String itemordered;
itemordered = item + " added to list";
Toast.makeText(getApplicationContext(), itemordered, Toast.LENGTH_SHORT).show();
myNewList.add(item);
}
});
// List View Button
Button btnLunchList = (Button) findViewById(R.id.lrList);
btnLunchList.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.selecteditems);
ListView selecteditems = (ListView) findViewById(android.R.id.list);
ArrayAdapter<String> newadapter = new ArrayAdapter<String>(LunchListMenu.this, android.R.layout.simple_list_item_1, myNewList);
selecteditems.setAdapter(newadapter);
// Get sdCard location so we can Create Dir and File
File sdCard = Environment.getExternalStorageDirectory();
File lunch = new File(sdCard,"Lunch");
lunch.mkdirs();
File file = new File(lunch, "Lunch.txt");
PrintWriter out = null;
try {
out = new PrintWriter(new FileWriter(file));
} catch (IOException e) {
e.printStackTrace();
}
// Write each string in the array
StringBuilder text = new StringBuilder();
for (String s : myNewList) {
out.println(s);
}
out.close();
// read File
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append(',');
text.append(' ');
}
}
catch (IOException e) {
}
itemsordered = text;
}
});
}
public void shareMyList(View v){
// Share Selected Items Button
Button btnShareItems = (Button) findViewById(R.id.shareMyList);
btnShareItems.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", itemsordered);
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
}
});
}
}
I'm having problems using s.requestFocus() when s is a spinner. Is there a special treatment to get it to work when it's a spinner ?
Thanks
try this code..
spinner.setFocusable(true);
spinner.setFocusableInTouchMode(true);
I'm guessing you aren't literally just looking to "give focus" to the spinner. When you give focus to an EditText, the keyboard pops up, so you may be expecting the spinner selection to "open up" on focus - but it doesn't work that way (don't ask me why). Use s.performClick() to do this - it will act just as if the user clicked on the spinner control.
use this
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class dynamic_spinner_main extends Activity {
private Spinner m_myDynamicSpinner;
private EditText m_addItemText;
private ArrayAdapter<CharSequence> m_adapterForSpinner;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_spinner);
///////////////////////////////////////////////////////////////
//grab our UI elements so we can manipulate them (in the case of the Spinner)
// or add listeners to them (in the case of the buttons)
m_myDynamicSpinner = (Spinner)findViewById(R.id.dynamicSpinner);
m_addItemText = (EditText)findViewById(R.id.newSpinnerItemText);
Button addButton = (Button)findViewById(R.id.AddBtn);
Button clearButton = (Button)findViewById(R.id.ClearBtn);
////////////////////////////////////////////////////////////////
//create an arrayAdapter an assign it to the spinner
m_adapterForSpinner = new ArrayAdapter(this, android.R.layout.simple_spinner_item);
m_adapterForSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
m_myDynamicSpinner.setAdapter(m_adapterForSpinner);
m_adapterForSpinner.add("gr");
m_myDynamicSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
Intent mIntent=new Intent(dynamic_spinner_main.this,sampleLocalization.class);
mIntent.putExtra("lang", m_myDynamicSpinner.getItemIdAtPosition(position));
System.out.println("Spinner value...."+m_myDynamicSpinner.getSelectedItem().toString());
startActivity(mIntent);
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
////////////////////////////////////////////////////////////////
//add listener for addButton
addButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
addNewSpinnerItem();
}
});
////////////////////////////////////////////////////////////////
//add listener for addButton
clearButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
clearSpinnerItems();
}
});
}
private void addNewSpinnerItem() {
CharSequence textHolder = "" + m_addItemText.getText();
m_adapterForSpinner.add(textHolder);
}
private void clearSpinnerItems() {
m_adapterForSpinner.clear();
m_adapterForSpinner.add("dummy item");
}
}
main_spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText android:layout_height="wrap_content"
android:layout_margin="4px"
android:id="#+id/newSpinnerItemText"
android:layout_width="fill_parent"></EditText>
<Button android:layout_height="wrap_content"
android:id="#+id/AddBtn"
android:layout_margin="4px"
android:layout_width="fill_parent"
android:text="Add To Spinner"></Button>
<Button android:layout_height="wrap_content"
android:id="#+id/ClearBtn"
android:layout_margin="4px"
android:layout_width="fill_parent"
android:text="Clear Spinner Items"></Button>
<Spinner android:layout_height="wrap_content"
android:id="#+id/dynamicSpinner"
android:layout_margin="4px"
android:layout_width="fill_parent"></Spinner>
</LinearLayout>
Spinner isn't focusable by default (source). So you have to make it focusable either in xml
<Spinner
android:id="#+id/spinner"
android:focusable="true"
android:layout_width="wrap_conten"
android:layout_height="wrap_content" />
or in code
findViewById(R.id.spinner).setFocusable(true);
Am new to this Android SDK platform,so could you people help me out to solve my issue.
so lets discuss brief about my issue, am developing small application in which am using sppiner to list all country states and zip codes so my question is when i select one country state in a list it should automatically populate and display its corresponding zip code
below is my main.xml file
android:layout_centerHorizontal="true"
android:layout_marginTop="2dp"
android:padding="12dip"
android:layout_height="40dip"
android:layout_marginRight="20dip"
/>
here is my Activity class
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
public class EriePremium extends Activity {
/** Called when the activity is first created. */
#SuppressWarnings("unchecked")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner s = (Spinner) findViewById(R.id.spinner);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.States, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
Spinner s1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter adapter1 = ArrayAdapter.createFromResource(this, R.array.Zip, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter1);
}
}
and my array list array.xml file
Alabama
Alaska
American Samoa
Arizona
Arkansas
California
Colorado
Connecticut
Delaware
District of Columbia
Florida
Georgia
Guam
Hawaii
Idaho
Illinois
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
Awaiting for response
Thanks in advance,
Madhu
This is tested and (works bi-directionally)
EriePremium.java
package com.example.statezip;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
public class EriePremium extends Activity
{
public Spinner zipspinner,statespinner;
protected void onCreate(Bundle mbfSplash)
{
super.onCreate(mbfSplash);
setContentView(R.layout.main);
setupZip();
setupState();
}
public void setupZip()
{
zipspinner = (Spinner) findViewById(R.id.zipspinner);
// List<String> ziplist = new ArrayList<String>();
ArrayAdapter<CharSequence> zipAdapter = ArrayAdapter.createFromResource(this, R.array.zip, android.R.layout.simple_spinner_item);
zipAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
zipspinner.setAdapter(zipAdapter);
zipspinner.setOnItemSelectedListener(new zipOnClickListener());
}
private void setupState()
{
statespinner = (Spinner) findViewById(R.id.statespinner);
// List<String> statelist = new ArrayList<String>();
ArrayAdapter<CharSequence> stateAdapter = ArrayAdapter.createFromResource(this, R.array.state, android.R.layout.simple_spinner_item);
stateAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
statespinner.setAdapter(stateAdapter);
statespinner.setOnItemSelectedListener(new stateOnClickListener());
}
public class stateOnClickListener implements OnItemSelectedListener
{
#Override public void onItemSelected(AdapterView<?> parent, View v, int pos,long id)
{
zipspinner.setSelection(pos);//parent.getItemAtPosition(pos)
}
#Override public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
}
public class zipOnClickListener implements OnItemSelectedListener
{
#Override public void onItemSelected(AdapterView<?> parent, View v, int pos,long id)
{
statespinner.setSelection(pos);//parent.getItemAtPosition(pos)
}
#Override public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
}
}//class
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:gravity="top|left" >
<TextView
android:id="#+id/title"
android:layout_width="130dp"
android:layout_height="50dp"
android:gravity="center"
android:text="#string/title"
android:textSize="30dp" />
<Spinner
android:id="#+id/statespinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/title"
android:entries="#array/state"
android:prompt="#string/state" />
<Spinner
android:id="#+id/zipspinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/statespinner"
android:entries="#array/zip"
android:prompt="#string/zip" />
</RelativeLayout>
TemoActivity.java
strings.xml
<string name="zip">zip</string>
<string name="state">state</string>
<string name="title">state/zip</string>
<string-array name="state">
<item>Alabama Alaska</item>
<item>American Samoa</item>
<item>Arizona</item>
<item>Arkansas</item>
<item>California</item>
<item>Colorado</item>
<item>Connecticut</item>
<item>Delaware District of Columbia</item>
<item>Florida</item>
<item>Georgia</item>
.....
< /string-array>
<string-array name="zip">
<item>1001</item>
<item>1002</item>
<item>1003</item>
<item>1004</item>
<item>1005</item>
<item>1006</item>
<item>1007</item>
<item>1008 1009</item>
<item>1010</item>
<item>1011</item>
......
< /string-array>