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);
Related
I am unable to find the way to create a popup like info window in map . I have to use it to show the three button inside
Yes
later
No
these three options are clickable.
The pop up will show above three button in vertical manner in recycler view . I have created recycler view and pop up view but how to show it like a Tooltip with clickabe
you can use PopupWindow, its starting with API +23
<?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="wrap_content" android:background="#FFBBFFBB" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="Hello My Window" android:textSize="20sp" /> <Button android:id="#+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="Button" android:textSize="20sp" /> </LinearLayout>
class
package com.example.hellopopupwindow;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;
public class MainActivity extends Activity {
private Context mContext = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showPopupWindow(view);
}
});
}
private void showPopupWindow(View view) {
// A custom layout, as the display content
View contentView = LayoutInflater.from(mContext).inflate(
R.layout.pop_window, null);
// Set the button click event
Button button = (Button) contentView.findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext, "button is pressed",
Toast.LENGTH_SHORT).show();
}
});
final PopupWindow popupWindow = new PopupWindow(contentView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
popupWindow.setTouchable(true);
popupWindow.setTouchInterceptor(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("mengdd", "onTouch : ");
return false;
// It returns true if the words, the touch event will be blocked
// PopupWindow onTouchEvent interception is not called, so click on the external area cannot be dismiss
}
});
// If you do not set the PopupWindow background, both the external region click or Back keys are not dismiss box
// I think there is a bug API
popupWindow.setBackgroundDrawable(getResources().getDrawable(
R.drawable.selectmenu_bg_downward));
// After setting the parameter to show
popupWindow.showAsDropDown(view);
}
}
I am a beginner and I want the user to select three fields from a spinner, and using conditional statements to test all the three fields with an if condition and the and operator to decide which activity to be started using a button and intent.
activity level_menu
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/choose_txt"
android:textSize="30sp"
android:textStyle="bold"
android:layout_gravity="center"
android:textColor="#ff000000"
android:layout_marginBottom="30dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/prog"
android:textSize="32sp"
android:textStyle="bold"
android:textColor="#ff000000"
/>
<Spinner
android:id="#+id/program_spinner"
android:layout_width="match_parent"
android:layout_height="40dp"
android:entries="#array/program_list"
android:prompt="#string/programm_prompt"
android:layout_marginBottom="30sp"
android:backgroundTint="#e9ffc1"
android:background="#e9ffc1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/lev"
android:textSize="32sp"
android:textStyle="bold"
android:textColor="#ff000000"
/>
<Spinner
android:id="#+id/level_spinner"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="30sp"
android:backgroundTint="#e9ffc1"
android:background="#e9ffc1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sem"
android:textSize="32sp"
android:textStyle="bold"
android:textColor="#ff000000"/>
<Spinner
android:id="#+id/sem_spinner"
android:layout_width="match_parent"
android:layout_height="40dp"
android:backgroundTint="#e9ffc1"
android:background="#e9ffc1"
android:layout_marginBottom="20dp"></Spinner>
<Button
android:id="#+id/btnEnter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/enter"
android:textSize="25sp"
android:textStyle="bold"
android:textColor="#ff000000"
/>
level_menu.java
package inc.zibit.com.conware;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
public class Level_menu extends ActionBarActivity implements OnItemSelectedListener {
private Spinner program_spinner;
private Spinner level_spinner;
private Spinner sem_spinner;
private Button btnEnter;
ArrayAdapter<String> program_adapter;
List<String> program_list = new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.program_list)));
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level_menu);
addItemsOnSpinner2();
addItemsOnSpinner3();
addListenerOnButton();
// addListenerOnSpinnerItemSelection();
program_spinner = (Spinner) findViewById(R.id.program_spinner);
level_spinner = (Spinner) findViewById(R.id.level_spinner);
sem_spinner = (Spinner) findViewById(R.id.sem_spinner);
program_spinner.setOnItemSelectedListener(this);
level_spinner.setOnItemSelectedListener(this);
sem_spinner.setOnItemSelectedListener(this);
program_adapter=new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_spinner_item, program_list);
program_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
program_spinner.setAdapter(program_adapter);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (!(program_spinner.getSelectedItem().toString().trim().equals("Bsc. Computer Science"))) {
if (level_spinner.getSelectedItem().toString().trim().equals("Level 100")) {
if (sem_spinner.getSelectedItem().toString().trim().equals("1st Semester")) {
startActivity(new Intent(Level_menu.this, CompScience.class));
}
}
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
// add items into spinner dynamically
public void addItemsOnSpinner2() {
level_spinner = (Spinner)findViewById(R.id.level_spinner);
List<String> list = new ArrayList<String>();
list.add("Level 100");
list.add("Level 200");
list.add("Level 300");
list.add("Level 400");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
level_spinner.setAdapter(dataAdapter);
}
public void addItemsOnSpinner3() {
sem_spinner = (Spinner)findViewById(R.id.sem_spinner);
List<String> list = new ArrayList<String>();
list.add("1st Semester");
list.add("2nd Semester");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sem_spinner.setAdapter(dataAdapter);
}
public void addListenerOnButton() {
btnEnter = (Button) findViewById(R.id.btnEnter);
btnEnter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if ( v.getId()==R.id.btnEnter){
Intent intent = new Intent(Level_menu.this,CompScience.class);
startActivity(intent);
}
}
});
}
}
When i run it crashes.. what am I not doing right?
Without any logs provided, my best guess would be that NullPointerException causes your crash inside onItemSelected() callback. If any of those 3 spinners have nothing selected, you will get NPE.
I suggest you first make sure that spinner.getSelectedItem() is not null, before comparing strings.
I am trying to make a TO DO LIST. I have a EditText, Button, and ListView. On button click I want to add, what I typed into the EditText into a ListView.
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
android:id="#+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter task"
android:textSize="24dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/addTaskBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add Task"
android:layout_below="#+id/editText"
android:layout_alignParentLeft="true" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" Task"
android:id="#+id/header"
android:layout_below="#+id/addTaskBtn"
android:background="#5e5e5e"
android:textColor="#FFFFFF"
android:textSize="14dp"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/list"
android:layout_below="#+id/header"
android:layout_centerHorizontal="true" />
Main_ToDoList.java
package com.example.todolist;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class Main_ToDoList extends Activity implements OnClickListener
{
private Button btnAdd;
private EditText et;
private ListView lv;
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
btnAdd = (Button)findViewById(R.id.addTaskBtn);
btnAdd.setOnClickListener(this);
et = (EditText)findViewById(R.id.editText);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, list);
lv.setAdapter(adapter);
}
public void onClick(View v)
{
String input = et.getText().toString();
if(input.length() > 0)
{
list.add(input);
adapter.notifyDataSetChanged();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main__to_do_list, menu);
return true;
}
}
The code doesn't work, new to android developing, and just trying to create a simple To Do List. Thanks for your help!
This should do it.
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
btnAdd = (Button)findViewById(R.id.addTaskBtn);
btnAdd.setOnClickListener(this);
et = (EditText)findViewById(R.id.editText);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, list);
// set the lv variable to your list in the xml
lv=(ListView)findViewById(R.id.list);
lv.setAdapter(adapter);
}
public void onClick(View v)
{
String input = et.getText().toString();
if(input.length() > 0)
{
// add string to the adapter, not the listview
adapter.add(input);
// no need to call adapter.notifyDataSetChanged(); as it is done by the adapter.add() method
}
}
you need to find your listview and then set the adapter:
lv=(ListView)findViewById(android.R.id.yourlistview);
lv.setAdapter(adapter);
<?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:orientation="vertical" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/speed"
android:inputType="number"></EditText>"
<TextView
android:id="#+id/TextViewSpeed"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text ="Speed"
android:layout_below="#+id/speed"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="AddValue"
android:id="#+id/AddValue"
>
</Button>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ListView"
android:layout_below="#+id/AddValue"
>
</ListView>
</RelativeLayout>
This is my layout code. I wanted to add a text data from EditText to the ListView which is on same page. How to write a code to add text to the list when I click on the AddValue Button.
Thanks in advance.
use this code
addbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String val = edittext.getText().toString();
list.add(val);
((ArrayAdapter<Object>) listView.getAdapter()).notifyDataSetChanged();
}
});
Below snippet wil help you.
package org.sample;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
public class SampleActivity extends Activity
{
private Button add;
private EditText speedText;
private ArrayAdapter<String> adapter;
private ListView AddValue;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1);
AddValue=(ListView)findViewById(R.id.AddValue);
AddValue.setAdapter(adapter);
add = (Button) findViewById(R.id.add);
speedText = (EditText) findViewById(R.id.speed);
add.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
if (speedText.getText().toString().length() != 0)
{
adapter.add(speedText.getText().toString());
adapter.notifyDataSetChanged();
}
}
});
}
}
I want integrate one combo filled with currency types. please tell me any way to create select box in android. i have created spinner but i have similar different views to show on page. and it is not looking attractive. please share any code for select.
use this dynamic_spinner_main.java
package example.sampleLocalization;
import java.util.ArrayList;
//import com.matthias.dynamicSpinnerT.R;
//import com.matthias.dynamicSpinnerT.R.id;
//import com.matthias.dynamicSpinnerT.R.layout;
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");
////////////////////////////////////////////////////////////////
//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>