set edittext enabled/disabled - android

I've an interface. In this interface I've a spinner with 2 values: Browser and Chart. When I select Browser I want another editText called eTxtUrl is enabled. When I select Chart I want this EditText is disabled.
This is my code:
spinnerTipoView =(Spinner) findViewById(R.id.spinnerTipoView);
nomeTabellaGen=nomeTabella;
spinnerTipoViewArray=new ArrayList<String>();
if(addingRecord==true||tableDataChart==null)
spinnerTipoViewArray=CpmAnalysisParams.tipoViewAdding();
else
spinnerTipoViewArray=CpmAnalysisParams.tipoView();
spinnerTipoViewArrayAdapter=new ArrayAdapter<String>(this, R.layout.spinner, spinnerTipoViewArray);
spinnerTipoViewArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerTipoView.setAdapter(spinnerTipoViewArrayAdapter);
if(addingRecord==true||tableDataChart==null)
spinnerTipoView.setOnTouchListener(SpinnerTipoViewOnTouch);
spinnerTipoView.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if(arg0.getItemAtPosition(arg2).toString().toUpperCase().equals("CHART")){
try {
eTxtUrl=(EditText) findViewById(R.id.editTextUrl);
eTxtUrl.setEnabled(false);
eTxtUrl.setFocusable(false);
spinnerCategoria=(CustomSpinner)findViewById(R.id.spinnerCategoria);
spinnerCategoria.setEnabled(true);
spinnerTabella=(CustomSpinner)findViewById(R.id.spinnerTabella);
spinnerTabella.setEnabled(true);
spinnerAscissa=(CustomSpinner)findViewById(R.id.spinnerAscissa);
spinnerAscissa.setEnabled(true);
buildSpinnerAreaDisplay();
buildSpinnerCategoria();
if(nomeTabellaGen!=""){
buildSpinnerTabella(nomeTabellaGen);
}
else
buildSpinnerTabella("");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if(arg0.getItemAtPosition(arg2).toString().equals("Seleziona tipo view")){
buildSpinnerAreaDisplay();
buildGrafConfigSpinnerDisabled();
}
else{
try {
eTxtUrl=(EditText) findViewById(R.id.editTextUrl);
eTxtUrl.setEnabled(true);
eTxtUrl.setFocusable(true);
spinnerCategoria=(CustomSpinner)findViewById(R.id.spinnerCategoria);
spinnerCategoria.setOnTouchListener(null);
spinnerCategoria.setEnabled(false);
spinnerTabella=(CustomSpinner)findViewById(R.id.spinnerTabella);
spinnerTabella.setEnabled(false);
spinnerTabella.setOnTouchListener(null);
spinnerAscissa=(CustomSpinner)findViewById(R.id.spinnerAscissa);
spinnerAscissa.setEnabled(false);
spinnerAscissa.setOnTouchListener(null);
buildSpinnerAreaDisplay();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// TODO Auto-generated method stub
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
I don't know what is wrong because when I select browser, then chart, then browser another time, my edit text is still disabled.
Thanks in advance.

To enable editing - have you tried using:
eTxtUrl.setFocusableInTouchMode(true)
instead of (or after) that line:
eTxtUrl.setFocusable(true);

Related

need help on condition in android

this is my code, If add the if statement in line No 105 then program crashes, if i removed this then my code is working. But without this i can't solve my code. Please can anyone tell me what could be the problem or what am i doing wrong?
public class SMSDetails extends Activity {
Spinner examSpinnerSMS,yearSpinnerSMS,boardSpinnerSMS;
private String[] examinationStrings;
private String[] yearStrings;
private String[] boardStrings;
int index;
private String mSelectedItemExam,mSelectedItemYear,mSelectedItemBoard;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_smsdetails);
//initialize view
examSpinnerSMS = (Spinner)findViewById(R.id.spnrExamination);
yearSpinnerSMS=(Spinner)findViewById(R.id.spnrYear);
boardSpinnerSMS=(Spinner)findViewById(R.id.spnrBoard);
//initialize data source
examinationStrings = getResources().getStringArray(R.array.Examination);
yearStrings = getResources().getStringArray(R.array.YearArray);
boardStrings = getResources().getStringArray(R.array.Board);
//initialize view
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, examinationStrings);
ArrayAdapter<String> adapter1= new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, yearStrings);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, boardStrings);
//bind adapter and view
examSpinnerSMS.setAdapter(adapter);
yearSpinnerSMS.setAdapter(adapter1);
boardSpinnerSMS.setAdapter(adapter2);
//work with the spinners
examSpinnerSMS.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View view,int arg2, long arg3) {
//index = examSpinnerSMS.getSelectedItemPosition();
mSelectedItemExam=arg0.getSelectedItem().toString();
//Toast.makeText(getApplicationContext(),"You have selected "+mSelectedItemExam,Toast.LENGTH_LONG).show();
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
);
yearSpinnerSMS.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View view,int arg2, long arg3) {
//index = examSpinnerSMS.getSelectedItemPosition();
mSelectedItemYear=arg0.getSelectedItem().toString();
//Toast.makeText(getApplicationContext(),"You have selected "+mSelectedItemYear,Toast.LENGTH_LONG).show();
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
);
boardSpinnerSMS.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View view,int arg2, long arg3) {
index = examSpinnerSMS.getSelectedItemPosition();
mSelectedItemBoard=arg0.getSelectedItem().toString();
//Toast.makeText(getApplicationContext(),"You have selected "+mSelectedItemBoard,Toast.LENGTH_LONG).show();
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
);
//Log.i("problem", "problem");
//problem is here
if (mSelectedItemExam.equals("Select One")||mSelectedItemYear.equals("Select One")|| mSelectedItemBoard.equals("Select One")) {
Toast.makeText(getApplicationContext(), "Enter all the value", Toast.LENGTH_SHORT).show();
}
//Log.i("problem", "problem");
}
public void Submit(View view) {
String fm = mSelectedItemExam+" "+mSelectedItemBoard+" "+mSelectedItemYear;
//Log.i("problem", "problem");
try {
sendSMS("5556", fm);
Toast.makeText(getApplicationContext(), "SMS sent",
Toast.LENGTH_LONG).show();
}
catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again.",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
private void sendSMS(String phoneNumber, String message)
{
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.smsdetails, menu);
return true;
}
}
do you mean that you need to add an if statement in this lines?:
catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again.",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
Please add the statement where it is not compiling.
mSelectedItemExam is probably null because none has been selected at the time you're doing
mSelectedItemExam.equals("Select One")
Learn how to use the Logcat and next time post a stracktrace :)

How to work with multi spinner in one activity in android ?

In my application i am using 2 spinners.I am using onitemselectedListener on both spinners but in my second spinner listener is not working.
if someone can suggest me proper solution....
my code is....
public class Expense extends Activity {
Spinner datype,distance;
public void onCreate(Bundle b){
super.onCreate(b);
setContentView(R.layout.expense);
datype=(Spinner)findViewById(R.id.da_type);
distance=(Spinner)findViewById(R.id.da_distance);
List<String>data1=new ArrayList<String>();
data1.add("Local");
data1.add("Ex-Station Double Side");
data1.add("Ex-Station Single Side");
data1.add("Out-Station Double Side");
data1.add("Out-Station Single Side");
ArrayAdapter<String>adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,data1);
adapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
datype.setAdapter(adapter);
datype.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
if(datype.getSelectedItem().toString().equals("Local")){
mainlayout.setVisibility(View.GONE);
}
else {
mainlayout.setVisibility(View.VISIBLE);
}
if(datype.getSelectedItem().toString().equals("Local")){
try{
Statement smt=mycon.connection().createStatement();
rs=smt.executeQuery("DCR_GETEXPENSE '"+PA_ID+"'");
while(rs.next()){
daAmt.setText(rs.getString("DA_L"));
}
}catch(Exception e){
e.printStackTrace();
}
}
else if(datype.getSelectedItem().toString().equals("Ex-Station Double Side")||datype.getSelectedItem().toString().equals("Ex-Station Single Side")){
try{
Statement smt=mycon.connection().createStatement();
rs=smt.executeQuery("DCR_GETEXPENSE '"+PA_ID+"'");
while(rs.next()){
daAmt.setText(rs.getString("DA_EX"));
}
}catch(Exception e){
e.printStackTrace();
}
}
else{
try{
Statement smt=mycon.connection().createStatement();
rs=smt.executeQuery("DCR_GETEXPENSE '"+PA_ID+"'");
while(rs.next()){
daAmt.setText(rs.getString("DA_NS"));
}
}catch(Exception e){
e.printStackTrace();
}
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
station=new ArrayList<String>();
try{
CallableStatement stmt=mycon.connection().prepareCall("DCRDOCTORGRID '"+PA_ID+"','','','','','','',''");
stmt.execute();
rs=stmt.getResultSet();
rs.close();//i'st table
stmt.getMoreResults();
rs=stmt.getResultSet();
rs.close();//2'nd table
stmt.getMoreResults();
rs=stmt.getResultSet();
while(rs.next()){
station.add(rs.getString("STATION_NAME"));
}
}catch(Exception e){
e.printStackTrace();
}
ArrayAdapter<String>adapter2=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,station);
adapter2.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
distance.setAdapter(adapter2);
distance.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
if(distance.getSelectedItem().toString().equals("Zero Kilometer")){
Toast.makeText(getApplicationContext(),"0000",Toast.LENGTH.Short).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
In second spinner my toast is not displaying when i select that value from spinner
please help me

How two build two line of text and one image in one item?

i am using java android .
i have a problem in my app and i need some code that can make my item has two lines of texts and one image .
please save me and help .
here is the code for what i tried please make sure that you can help and give your opinion and how i can solve it cause i stucked here.
the main activity
private void setUpView() {
// TODO Auto-generated method stub
etInput = (EditText)this.findViewById(R.id.editText_input);
Pinput = (EditText)this.findViewById(R.id.editText1);
btnAdd = (Button)this.findViewById(R.id.button_add);
lvItem = (ListView)this.findViewById(R.id.listView_items);
itemArrey = new ArrayList<String>();
itemAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,itemArrey);
lvItem.setAdapter(itemAdapter);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addItemList();
}
});
etInput.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_ENTER){
addItemList();
}
return false ;
}
});
Pinput.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(keyCode== KeyEvent.KEYCODE_NUM) {
addItemList();
}
return;
}
});
}
protected void addItemList() {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
if (isInputValid(Pinput )) {
itemArrey.add(Pinput.getText().toString());
itemArrey.add(etInput.getText().toString());
Pinput.setText("");
etInput.setText("");
itemAdapter.notifyDataSetChanged();
} if (isInputValid(etInput )) {
itemArrey.add(Pinput.getText().toString());
itemArrey.add(etInput.getText().toString());
Pinput.setText("");
etInput.setText("");
itemAdapter.notifyDataSetChanged();
}
}
protected boolean isInputValid(EditText etInput2 ) {
// TODO Auto-generatd method stub
if (etInput2.getText().toString().trim().length()<1) {
return false;
} else {
return true;
}
}
}
If you want a custom view in your ListView I recommend doing one xml layout by yourself and replace android.R.layout.simple_list_item_1 in your Adapter call with the new created one.
This would be the easiest way but as I think that you might need more freedom to create/modify the content of the row, I strongly suggest to create your own Adapter extending from BaseAdapter. In the getView() method of the adapter you can easily inflate and set up the layout like you want for that row.
Okie... you tag android-listview and list view item. So I think you need list view with image and and Text.
For that you have to use Custom List View.
See this
May be this will help you. and not then pls make your que more clear. :)

setOnitemSelectedListener is not triggering with the programmatically created spinner Android

My need is to
create a dynamic spinner on a button click
select the spinner values and set the values into an edit text field.
For this I have created a dynamic spinner programatically. Put that code inside button click listener. Its working fine upto here.
but the setOnitemSelectedListener of the dynamic spinner is not at all working.. there are no errors in the Logcat... please help me..
------------ These are the methods inside onCreate ------------
Spinner spnOutHospitalList = new Spinner(Referance.this);
// list button on click event
btnList = (Button) findViewById(R.id.btn_list);
btnList.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// function to create spinner dynamically
createDynamicSpinner();
}
});
// Out Hospital List Spinner on item click listener
spnOutHospitalList.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long arg3) {
// TODO Auto-generated method stub
outHospitalName = hospitalNameListArray.get(position);
outHospital.setText(outHospitalName);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
------------ These are the functions outside onCreate but inside the Activity------------
// to create spinner dynamically
private void createDynamicSpinner() {
// TODO Auto-generated method stub
spnOutHospitalList.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
loadOutHospitalListSpinner();
spnOutHospitalList.performClick();
}
// to load out hospital/ clinic data into spinner
private void loadOutHospitalListSpinner() {
// TODO Auto-generated method stub
try {
if (getFirstRun()) {
sampleDB = dbAdapter.getDatabase();
setRunned();
}
else {
sampleDB = dbAdapter.getWritableDatabase();
}
Cursor c1 = sampleDB.rawQuery("select DISTINCT EPR_OUT_HOSPITAL from EMR_PT_REFERNCE",null);
System.out.println("count is " + c1.getCount());
if (c1 != null && c1.getCount() != 0) {
hospitalNameListArray.clear();
if (c1.moveToFirst()) {
do {
hospitalNameListArray.add(c1.getString(c1.getColumnIndex("EPR_OUT_HOSPITAL")));
} while (c1.moveToNext());
}
}
c1.close();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, hospitalNameListArray);
// dropdownlist
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnOutHospitalList.setAdapter(dataAdapter);
}
catch (Exception e) {
// TODO: handle exception
System.out.println("CAT LIST ERROR IS: " + e.getMessage());
}
}
Try this
Write setOnItemSelectedListener inside createDynamicSpinner
private void createDynamicSpinner() {
//Remove this line from top in your code and add here
Spinner spnOutHospitalList = new Spinner(Referance.this);
spnOutHospitalList.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//Pass Spinner Object to this function to load data in it!
loadOutHospitalListSpinner(spnOutHospitalList);
spnOutHospitalList.performClick();
spnOutHospitalList.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long arg3) {
// TODO Auto-generated method stub
outHospitalName = hospitalNameListArray.get(position);
outHospital.setText(outHospitalName);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}

Call webservices in android on textchanged listener on one edit text and populate another edittext

I am able to call webservices now.
But In my project i want to populate the name of sponser corresponding to the sponser id .
For doing this I want to call a webservice on text changed listener of sponserid edittext or i have to monitor the focus changed of edittext.
Please guide me how to call webservice and populate another edittext.
Should i take keyevent ???
The codes i have tried so far are :
txtSpnID.setFocusable(true);
if(txtSpnID.isFocused()){
Log.v("TAG", "focus is on txtspid");
}else{
Log.v("TAG", "focus lost from txtspid");
}
This prints focus lost from txtspid on logcat.
if(txtSpnID.hasFocus()){
Log.v("TAG", "focus is on txtspid");
}else{
Log.v("TAG", "focus lost from txtspid");
}
this also prints the same
txtSpnID.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
Log.v("textfield","On text changed");
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
Log.v("textfield","before text changed");
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
Log.v("textfield text",txtSpnID.getText().toString());
Toast.makeText(Registration.this, "SponserID is"+txtSpnID.getText().toString(),
Toast.LENGTH_LONG);
if (txtSpnID.getText().length() == 0) {
Toast.makeText(Registration.this, "Enter SponserID ",
Toast.LENGTH_LONG);
} else {
String spnId = txtSpnID.getText().toString();
try {
//call webservice here and get response from the service
} catch (Exception e) {
}
}
}
});
this prints corresponding logs on logcat
but it does not show toast :(
please help me guys
you just made Toast ... but you did'nt show it ... use Toast.show() like this Toast.makeToast(.....).show();
.... remeber to call web service in different thread(to prevent ANR)

Categories

Resources