I am trying to set reminders. There is a spinner with 5 values (1,2,3,4,5). Let's say if user selects 3 from the spinner there should be 3 "Add Time" buttons displayed. Similarly if user selects 5, five "Add Time" button should be displayed.
User presses "Add Time" button and time picker will be displayed to select time for the reminders. I've created spinner through xml from res/string folder and set an array of 5 item (1,2,3,4,5). I don't know to display these "Add Time" buttons as per the selection of value from the spinner.
Would greatly appreciate any suggestions/guidance.
Thanks a lot...
package com.example.medicationreminder;
import jim.h.common.android.lib.zxing.config.ZXingLibConfig;
import jim.h.common.android.lib.zxing.integrator.IntentIntegrator;
import jim.h.common.android.lib.zxing.integrator.IntentResult;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import java.util.ArrayList;
import java.util.List;
import com.example.medicationreminder.InteractiveArrayAdapter.ViewHolder;
import android.app.ListActivity;
import android.widget.ArrayAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
public class MedInfo extends Activity implements OnClickListener{
private Button daily, weekly, mScanBarcode;
private Handler handler = new Handler();
private ZXingLibConfig zxingLibConfig;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_med_info);
mScanBarcode = (Button) findViewById(R.id.Barcodescanner);
mScanBarcode.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.med_info, menu);
return true;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v == mScanBarcode)//When scan barcode button is clicked
{
IntentIntegrator.initiateScan(MedInfo.this, zxingLibConfig);//Intent that opens camera for scanning through zxing Library
}
/* else if(v == weekly)
{
Intent weekly = new Intent(MedInfo.this,Weekly.class);
startActivity(weekly);
} */
else if(v == daily)
{
Intent daily = new Intent(MedInfo.this, DailyMed.class);
startActivity(daily);
}
}
//The method that cathes the result after scanning is done
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode)
{
case IntentIntegrator.REQUEST_CODE:
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode,
resultCode, data);
if (scanResult == null)
{
return;
}
final String result = scanResult.getContents();//scanned result
if (result != null)
{
handler.post(new Runnable()
{
#Override
public void run()
{
showScannedResult(result);//opening pop up showing scanned result
}
});
}
break;
default:
}
}
//Pop up dialog showing scanned result
private void showScannedResult(String result)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Scanned Result")
.setMessage(result)
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do things
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
//Array list for selection of Days
public class Weekly extends ListActivity {
public void onCreate1(Bundle icicle) {
super.onCreate(icicle);
// Create an array of Strings, that will be put to our ListActivity
ArrayAdapter<Model> adapter = new InteractiveArrayAdapter(this,
getModel());
setListAdapter(adapter);
}
private List<Model> getModel() {
List<Model> list = new ArrayList<Model>();
list.add(get("Monday"));
list.add(get("Tuesday"));
list.add(get("Wednesday"));
list.add(get("Thursday"));
list.add(get("Friday"));
list.add(get("Saturday"));
list.add(get("Sunday"));
// Initially select one of the items
list.get(1).setSelected(true);
return list;
}
private Model get(String s) {
return new Model(s);
}
// Spinner item selection and add button
public class spinning extends Activity implements AdapterView.OnItemSelectedListener{
LinearLayout list;
Spinner spinner;
#Override
public void onCreate(Bundle savedInstanceSate) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceSate);
setContentView(R.layout.activity_med_info);
spinner = (Spinner)findViewById(R.id.spinner1);
list = (LinearLayout)findViewById(R.id.list);
ArrayAdapter<CharSequence> adapterSpinner = ArrayAdapter.createFromResource(this,
R.array.dose_arrays, android.R.layout.simple_spinner_item);
adapterSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapterSpinner);
spinner.setOnItemSelectedListener(this);
}
public void addButton(int number){
list.removeAllViews();
for(int i = 0; i<number;i++){
Button button = new Button(getApplicationContext());
button.setText("Add Time");
list.addView(button);
}
}
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i,
long l) {
// TODO Auto-generated method stub
switch(i){
case 0:
addButton(1);
break;
case 1:
addButton(2);
break;
case 2:
addButton(3);
break;
case 3:
addButton(4);
break;
case 4:
addButton(5);
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
}
}
Medinfo.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#drawable/android"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MedInfo" >
<EditText
android:id="#+id/MedName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/radioGroup1"
android:layout_alignTop="#+id/radioGroup1"
android:ems="10"
android:hint="Name of Medicine" >
<requestFocus />
</EditText>
<Button
android:id="#+id/Barcodescanner"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/MedName"
android:layout_below="#+id/MedName"
android:layout_marginTop="14dp"
android:text="Scan Barcode" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Barcodescanner"
android:layout_below="#+id/Barcodescanner"
android:layout_marginTop="44dp"
android:text="Dose per day"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/textView1"
android:entries="#array/dose_arrays"
android:prompt="#string/dose_prompt" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_alignParentLeft="true"
android:layout_below="#+id/spinner" android:id="#+id/list">
</LinearLayout>
<CheckBox
android:id="#+id/checkMon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="114dp"
android:text="Mon" />
<CheckBox
android:id="#+id/checkTue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/checkMon"
android:layout_alignBottom="#+id/checkMon"
android:layout_toRightOf="#+id/AddTime"
android:text="Tue" />
<CheckBox
android:id="#+id/checkWed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/checkTue"
android:layout_alignBottom="#+id/checkTue"
android:layout_centerHorizontal="true"
android:text="Wed" />
<CheckBox
android:id="#+id/checkThur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/checkWed"
android:layout_marginLeft="25dp"
android:layout_toRightOf="#+id/checkWed"
android:text="Thur" />
<CheckBox
android:id="#+id/checkFri"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/checkTue"
android:layout_marginTop="27dp"
android:layout_toRightOf="#+id/radioGroup1"
android:text="Fri" />
<CheckBox
android:id="#+id/checkSat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/checkFri"
android:layout_alignBottom="#+id/checkFri"
android:layout_alignLeft="#+id/checkTue"
android:text="Sat" />
<CheckBox
android:id="#+id/checkSun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/checkSat"
android:layout_alignBottom="#+id/checkSat"
android:layout_alignLeft="#+id/checkWed"
android:text="Sun" />
<!--
<Button
android:id="#+id/AddTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textView1"
android:layout_marginTop="49dp"
android:text="Add Time" />
-->
</RelativeLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Medication Reminder</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="title_activity_med_info">MedInfo</string>
<string name="dose_prompt">Choose a Dose</string>
<string-array name="dose_arrays">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
</string-array>
<string name="check_Monday">Monday</string>
<string name="check_Tuesday">Tuesday</string>
<string name="check_Wednesday">Wednesday</string>
<string name="check_Thursday">Thursday</string>
<string name="check_Friday">Friday</string>
<string name="check_Saturday">Saturday</string>
<string name="check_Sunday">Sunday</string>
<string name="title_activity_daily_med">DailyMed</string>
<string name="title_activity_weekly">Weekly</string>
<string name="title_activity_daily__addtime">Daily_Addtime</string>
<string name="title_activity_interactive_adapter">InteractiveAdapter</string>
<string name="title_activity_interactive_array_adapter">InteractiveArrayAdapter</string>
<string name="title_activity_barcode_scanner">Barcode_scanner</string>
</resources>
try this code.
ActivityMain.class
public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener{
LinearLayout list;
Spinner spinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ativity_main);
spinner = (Spinner) findViewById(R.id.spinner);
list = (LinearLayout)findViewById(R.id.list);
ArrayAdapter<CharSequence> adapterSpinner = ArrayAdapter.createFromResource(this,
R.array.number, android.R.layout.simple_spinner_item);
adapterSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapterSpinner);
spinner.setOnItemSelectedListener(this);
}
public void addButton(int number){
list.removeAllViews();
for(int i = 0; i<number; i++){
Button button = new Button(getApplicationContext());
button.setText("Add Time");
list.addView(button);
}
}
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
switch (i){
case 0:
addButton(1);
break;
case 1:
addButton(2);
break;
case 2:
addButton(3);
break;
case 3:
addButton(4);
break;
case 4:
addButton(5);
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner"
android:layout_centerHorizontal="true" android:layout_alignParentTop="true"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_alignParentLeft="true"
android:layout_below="#+id/spinner" android:id="#+id/list">
</LinearLayout>
strings.xml
<string-array name="number">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
</string-array>
Hope, it will be helpful :)
Related
The Register Button works well, but the debugger doesn't run into the setOnItemClickListener onItemClick. If You click on a item, the activity for editing the record should be launched.
package at.bomsbg.MindX;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import at.bomsbg.MindX.sqlite.helper.MindXopenHelper;
import at.bomsbg.MindX.sqlite.helper.MindXtableAdapter;
public class Activity_cardmanager extends Activity {
MindXtableAdapter mindxtableadapt;
MindXopenHelper openHelper;
ListView nameList;
Button registerBtn;
Cursor cursor;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cardmanager);
nameList = (ListView) findViewById(R.id.title);
registerBtn = (Button) findViewById(R.id.btn_register);
mindxtableadapt = new MindXtableAdapter(this);
String[] from = { MindXopenHelper.title, MindXopenHelper.detail,
MindXopenHelper.active };
int[] to = { R.id.tv_title, R.id.tv_detail, R.id.tv_active };
cursor = mindxtableadapt.queryName();
SimpleCursorAdapter cursorAdapter =
new SimpleCursorAdapter(this,
R.layout.row, cursor, from, to);
nameList.setAdapter(cursorAdapter);
nameList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> tobjParent, View tobjView,
int tintPosition, long tlngid) {
Bundle passdata = new Bundle();
Cursor listCursor = (Cursor) tobjParent
.getItemAtPosition(tintPosition);
int nameId = listCursor.getInt(listCursor
.getColumnIndex(MindXopenHelper.KEY_ID));
passdata.putInt(MindXopenHelper.KEY_ID, nameId);
Intent passIntent = new Intent(Activity_cardmanager.this,
Activity_edit_mindxtables.class);
Toast.makeText(getApplicationContext(),
Integer.toString(nameId), 500).show();
passIntent.putExtras(passdata);
startActivity(passIntent);
}
});
registerBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent registerIntent = new Intent(Activity_cardmanager.this,
Activity_MindXtablesRecord.class);
startActivity(registerIntent);
}
});
}
#Override
public void onResume() {
super.onResume();
cursor.requery();
}
}
activity_Cardmanager.html
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
<Button
android:id="#+id/btn_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/register" />
</LinearLayout>
row.xml
I add the item clickable for the textfield. I think that is not realy necesarry.
<?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:clickable="true"
android:orientation="horizontal" >
<TextView
android:id="#+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:width="100dp" />
<TextView
android:id="#+id/tv_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:width="100dp" />
<CheckBox
android:id="#+id/tv_active"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp" />
</LinearLayout>
You have focusable/clickable items in your list view item - the checkbox.
Option 1 - Set the android:descendantFocusability="blocksDescendants" flag to your ListView and you should have fired the onItemClickListener.
Option 2 - In your Adapter you can set an onClickListener on the entire list view item object, and handle it from there.
the Problem of my solution was on row.xml
again thanX on all:
row.xml
<LinearLayout
android:clickable="true" (removed)
<TextView
android:id="#+id/tv_title"
android:onClick="Title" (added)
android:clickable="true" (added)
android:focusable="true" (added)
on my Activity_cardmanager I inserted following code after nameList.setAdapter...
nameList.setClickable(true); // 2015-04-14
nameList.setFocusable(true); // 2015-04-14
nameList.setItemsCanFocus(true); // 2015-04-14
setViewItemListener();
.
.
.
public void setViewItemListener() {
nameList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> tobjParent, View tobjView, int tintPosition, long tlngid) {
Bundle passdata = new Bundle();
// Cursor listCursor = (Cursor) tobjParent.getItemAtPosition(tintPosition);
Cursor listCursor = (Cursor) nameList.getItemAtPosition(tintPosition); //2015-04-15
int nameId = listCursor.getInt(listCursor
.getColumnIndex(MindXopenHelper.KEY_ID));
passdata.putInt(MindXopenHelper.KEY_ID, nameId);
Intent passIntent = new Intent(Activity_cardmanager.this,
Activity_edit_mindxtables.class);
Toast.makeText(getApplicationContext(),
Integer.toString(nameId), Toast.LENGTH_LONG).show();
passIntent.putExtras(passdata);
startActivity(passIntent);
}
});
}
the returnValue is true, but that is another story.
I have slider app,on sliding using left button there will be a listview, on clicking list view there should open second activity.i used Intent for that but my android app automatically close after clicking in listview.
MainActivity.java
package com.example.slide_menu;
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 com.navdrawer.SimpleSideDrawer;
public class MainActivity extends Activity {
SimpleSideDrawer slide_me;
Button left_button, right_button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
slide_me = new SimpleSideDrawer(this);
slide_me.setLeftBehindContentView(R.layout.left_menu);
slide_me.setRightBehindContentView(R.layout.right_menu);
left_button = (Button) findViewById(R.id.left_buton);
right_button = (Button) findViewById(R.id.right_buton);
left_button.setOnClickListener(new View.OnClickListener() {
private ListView l1;
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
slide_me.toggleLeftDrawer();
l1=(ListView)findViewById(R.id.listView1);
l1.setAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.dataelements)));
l1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent,
View view, int position, long id) {
// TODO Auto-generated method stub
switch(position){
case 0: Intent newActivity0 = new Intent(MainActivity.this,Lone.class);
startActivity(newActivity0);
break;
// case 1: Intent newActivity1 = new Intent(MainActivity.this,Maruti.class);
// startActivity(newActivity1);
// break;
// case 2: Intent newActivity = new Intent(this, olympiakos.class);
// startActivity(newActivity);
// break;
// case 3: Intent newActivity = new Intent(this, karaiskaki.class);
// startActivity(newActivity);
// break;
// case 4: Intent newActivity = new Intent(this, reservetickets.class);
// startActivity(newActivity);
// break;
}
}
});
}
});
right_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
slide_me.toggleRightDrawer();
}
});
}
}
left_menu
<?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:background="#ffffff"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="260dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Left Menu"
android:textColor="#000000" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
String.xml
<resources>
<string name="app_name">slide_menu</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">Slide Demo</string>
<string-array name="dataelements">
<item>one</item>
<item>two</item>
<item>three</item>
<item>four</item>
<item>five</item>
</string-array>
</resources>
Lone.java(mysecondactivityfile)
package com.example.slide_menu;
import android.app.Activity;
import android.os.Bundle;
public class Lone extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.lo);
}
}
Lo.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/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="lone scond"
android:textAppearance="?android:attr/textAppearanceLarge" />
hi i want to show the result in edittext 2 only after click the convert button
also
the second button (clear) not shown when i run the program how to show it as the screen size is small
here is the code
package converter.com;
import android.app.Activity;
import android.os.Bundle;
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;
public class now extends Activity {
/** defining variables */
String[] currencys1,currencys2;
String e,l,f,u,d;
double EURO=5,dollar=6,franc=7,LE=1,UAE=8,txtvalue;
EditText txt1,txt2;
Button convert,clear;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt1 =(EditText)findViewById(R.id.txt1);
txt2 =(EditText)findViewById(R.id.txt2);
convert=(Button)findViewById(R.id.btn1);
clear=(Button)findViewById(R.id.clear_btn);
currencys1=getResources().getStringArray(R.array.currency);
final Spinner s1=(Spinner)findViewById(R.id.sp1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, currencys1);
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
switch(arg2)
{
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
currencys2=getResources().getStringArray(R.array.currency1);
final Spinner s2=(Spinner)findViewById(R.id.sp2);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, currencys2);
s2.setAdapter(adapter1);
s2.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
txtvalue = Double.parseDouble(txt1.getText().toString());
switch(arg2)
{
case 0:
final String fromCurrencyCode = s1.getSelectedItem().toString();
final String toCurrencyCode = s2.getSelectedItem().toString();
if (fromCurrencyCode.equals(toCurrencyCode)) {
txt2.setText(txt1.getText());
}
else
{
double EURO1=txtvalue*EURO;
txt2.setText(String.valueOf(EURO1));
}
break;
case 1:
final String fromCurrencyCode1 = s1.getSelectedItem().toString();
final String toCurrencyCode1 = s2.getSelectedItem().toString();
if (fromCurrencyCode1.equals(toCurrencyCode1)) {
txt2.setText(txt1.getText());
}
else
{
double LE1=txtvalue*LE;
txt2.setText(String.valueOf(LE1));
}
break;
case 2:
final String fromCurrencyCode2 = s1.getSelectedItem().toString();
final String toCurrencyCode2 = s2.getSelectedItem().toString();
if (fromCurrencyCode2.equals(toCurrencyCode2)) {
txt2.setText(txt1.getText());
}
else
{
double franc1=txtvalue*franc;
txt2.setText(String.valueOf(franc1));
}
break;
case 3:
final String fromCurrencyCode3 = s1.getSelectedItem().toString();
final String toCurrencyCode3 = s2.getSelectedItem().toString();
if (fromCurrencyCode3.equals(toCurrencyCode3)) {
txt2.setText(txt1.getText());
}
else
{
double UAE1=txtvalue*UAE;
txt2.setText(String.valueOf(UAE1));
}
break;
case 4:
final String fromCurrencyCode4 = s1.getSelectedItem().toString();
final String toCurrencyCode4 = s2.getSelectedItem().toString();
if (fromCurrencyCode4.equals(toCurrencyCode4)) {
txt2.setText(txt1.getText());
}
else
{
double dollar1=txtvalue*dollar;
txt2.setText(String.valueOf(dollar1));
}
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
convert.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//i want to put the code here which show result only after click that button
}
});
clear.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
txt2.setText(" ");
}
});
}
}
here is the 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"
android:background="#cad1d9"
>
<TableLayout android:layout_width="match_parent" android:id="#+id/tableLayout1" android:layout_height="wrap_content">
<TextView android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="from" android:textColor="#5c6471"></TextView>
</TableLayout>
<Spinner android:id="#+id/sp1" android:layout_height="50dip" android:layout_width="match_parent"></Spinner>
<TextView android:id="#+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Amount" android:textColor="#5c6471"></TextView>
<EditText android:text="1" android:id="#+id/txt1" android:layout_width="match_parent" android:layout_height="wrap_content" android:numeric="decimal"></EditText>
<TextView android:id="#+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="To" android:textColor="#5c6471"></TextView>
<Spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:id="#+id/sp2"></Spinner>
<TextView android:id="#+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="value" android:textColor="#5c6471"></TextView>
<EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="#+id/txt2" android:text="1" android:editable="false"></EditText>
<Button android:editable="false" android:width="100px" android:gravity="center" android:id="#+id/btn1" android:text="Convert" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="#string/clear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:minWidth="400px" android:id="#+id/clear_btn"></Button>
</LinearLayout>
You can do the following to hide txt2 when you first create the activity:
txt2.setVisibility(View.GONE);
Then in the click handler for the convert button:
txt2.setVisibility(View.VISIBLE);
In the click handler for the clear button, presumably you would want to make it GONE again.
Regarding the problem of the clear button being off the screen: wrap your LinearLayout in a ScrollView (making the ScrollView the top view of the hierarchy with a single LinearLayout child). That way the user can scroll down to see all the content of your layout. You need to change the height of the LinearLayout to wrap_content:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#cad1d9"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
. . .
</LinearLayout
</ScrollView>
Main.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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spin"
android:entries="#array/num"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btn"
android:text="Click ME"
android:gravity="center"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/txtv"
/>
</LinearLayout>
Spin.java
package com.and.spin;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
public class spin extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView tv=(TextView)findViewById(R.id.txtv);
Button b=(Button)findViewById(R.id.btn);
final Spinner s=(Spinner)findViewById(R.id.spin);
b.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String spin=s.toString();
tv.setText(spin);
}
});
}
}
In this program, i'm trying to display selected options from the Spinner to the TextView. But output dsiplays android.widget.Spinner#44c0d7f8
I want output like (1,2,3,4 or 5) as the option selected in Spinner rather than android.widget.Spinner#44c0d7f8
You dont need shared preference to load values in spinner. You just need to declare array in string.xml file and load that.I am giving you my code.Just use it.:-
STEP-1:-
Declare array for spinner in your string.xml(res->values->strings.xml):--
<string-array name="country_array">
<item>Greece</item>
<item>United Kingdom</item>
<item>Italy</item>
<item>France</item>
<item>Germany</item>
<item>Turkey</item>
<item>Poland</item>
<item>India</item>
</string-array>
STEP-2:-
Declare Spinner widget in your layout xml file
<Spinner
android:id="#+id/spinCountry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:paddingLeft="8dp"
android:popupBackground="#android:color/white"
android:scrollbars="none"
android:spinnerMode="dropdown" />
STEP-3:-
Declare Spinner in your Activity
Spinner spinCountry;
spinCountry= (Spinner) findViewById(R.id.spinCountry);//fetch the spinner from layout file
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, getResources()
.getStringArray(R.array.country_array));//setting the country_array to spinner
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinCountry.setAdapter(adapter);
//if you want to set any action you can do in this listener
spinCountry.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
b.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
String spin=s.getSelectedItem().toString();
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
tv.setText(spin);
}
});
spinCountry.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView,
View selectedItemView, int position, long id) {
try {
String select_item =parentView.getItemAtPosition(position).toString();
}
catch (Exception e) {
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});
You are calling toString() method on spinner to get the selection which is wrong. You need to call getSelectedItemPosition() method to get the selection.
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);
}
});
}
}