RadioGroup.setEnabled(false) doesn't work as expected - android

I have used setEnabled(false) to set it unable, but it doesn't work and after this method, the value of RadioGroup.isEnabled() is false. The value was changed.
The code is from Android Programming Guide.
PS: The Spinner component use the setEnabled(false) as well.
Code is as follows:
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioGroup;
public class TestRadioGroup extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.radiogroup);
final RadioGroup testRadioGroup = (RadioGroup) findViewById(R.id.testRadioGroup);
final Button changeEnabledButton = (Button) findViewById(R.id.changeEnabledButton);
changeEnabledButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
changeEnabled(testRadioGroup);
}
});
final Button changeBgColorButton = (Button) findViewById(R.id.changeBackgroundColorButton);
changeBgColorButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
changeBgColor(testRadioGroup);
}
});
}
protected void changeBgColor(RadioGroup testRadioGroup) {
// TODO Auto-generated method stub
testRadioGroup.setBackgroundColor(Color.BLUE);
}
protected void changeEnabled(RadioGroup testRadioGroup) {
// TODO Auto-generated method stub
if (testRadioGroup.isEnabled()) {
testRadioGroup.setEnabled(false);
} else {
testRadioGroup.setEnabled(true);
}
}
}

Iterate and disable each button belonging to the radio group via a loop, for example:
for (int i = 0; i < testRadioGroup.getChildCount(); i++) {
testRadioGroup.getChildAt(i).setEnabled(false);
}

Views can be compose by multiple touchable elements. You have to disable them all, like this:
for(View lol : your_spinner.getTouchables() ) {
lol.setEnabled(false);
}
If it is a simple one since it also returns itself:
Find and return all touchable views that are descendants of this view, possibly including this view if it is touchable itself.
View#getTouchables()

You can't use the following code;
for(View lol : your_spinner.getTouchables() ) {
lol.setEnabled(false);
}
Once the views has disabled, there is no touchable child/descentant views anymore.

Related

Toast giving incorrect answer from Radio box references

I am pretty new to this and are not entirely sure why I am having this problem. From my code:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends Activity {
RadioButton wavelength1;
RadioButton wavelength2;
double ray;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wavelength1 = (RadioButton) findViewById(R.id.lowave);
wavelength2 = (RadioButton) findViewById(R.id.hiwave);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
If (wavelength1.isChecked());{
ray = 0.7125;
}
If (wavelength2.isChecked());{
ray = 0.4436;
}
Toast.makeText(MainActivity.this, String.valueOf(ray), Toast.LENGTH_LONG).show();
}
private void If(boolean checked) {
// TODO Auto-generated method stub
}
});
}
}
When I click on either radio boxes, the same output is given in the toast (as 0.4436) when they should be different. I am really at a loss to know what is going on. The app is aimed at API 10 and I am using Eclipse. (I am sure I am missing something).
Remove semicolor(;) at the end of if statement
If (wavelength1.isChecked()); < -- remove this
And also
use small (i) if it is method If();
remove this method
private void If(boolean checked) {
}

How to start two different Intents in alistView.setOnItemClickListener(new OnItemClickListener() {}

I am new to android and a little bit confused. i have a listView with image and text. Where, if I click on a Image it should start an activity, and if I click on text another activity.
Cod:
in
onCrete(){
listView = getListView();}
myBaseAdapterItemActivity = new MyBaseAdapterItemActivity(
ItemActivity.this, placeNameList);
setListAdapter(myBaseAdapterItemActivity);
myBaseAdapterItemActivity.notifyDataSetChanged();
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view,
int position, long id) {
// One Activity I can start without any problem
// In xml File I set for image clicable to true.
// What I want to do is like this
if(view.getId() == R.id.imageId)
{
Intent intent = new Intent(this, ImageActivity.class);
startActivity(intent);
}
else if(view.getID == R.id.textId)
{
Intent intent = new Intent(this, TextActivity.class);
startActivity(intent);
}
}}
And whenever I click on Image it does not not either in textView.
Any Idea
It has two solutions:
1) Instead on writing onItemClickListener for list you can do findviewbyid the textview and imageview in your custom adapter in getview method and then set onclick listeners on both of them.
2) You can use getChildAt method.... and check which child is your imageview and which is your textview. This is a work around so not much guaranteed.
Try with the below code.
Your adapter should be like below code. then your text and image click will create new activity.
Hi the code should be like below Hope this helps you.
package com.example.listwithclick;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
ListView listView1;
Activity activity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity=this;
listView1=(ListView)findViewById(R.id.listView1);
listView1.setAdapter(new MyAddapter(MainActivity.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.main, menu);
return true;
}
class MyAddapter extends BaseAdapter {
Context rContext;
private LayoutInflater rInflater;
public MyAddapter(Context c) {
rInflater = LayoutInflater.from(c);
rContext = c;
}
public MyAddapter(Activity imagebinding) {
// TODO Auto-generated constructor stub
activity = imagebinding;
rContext = imagebinding;
rInflater = LayoutInflater.from(imagebinding);
rContext = imagebinding;
rInflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 10;
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(final int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
convertView = rInflater.inflate(R.layout.child, null);
final MyDat mydat = new MyDat();
mydat.textview = (TextView) convertView.findViewById(R.id.textView1);
mydat.textview.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(rContext, "text", 1000).show();
Intent image= new Intent(rContext,TextActivity.class);
startActivity(image);
}
});
mydat.imageView1=(ImageView)convertView.findViewById(R.id.imageView1);
mydat.imageView1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(rContext, "image ", 1000).show();
Intent image= new Intent(rContext,ImageActivity.class);
startActivity(image);
}
});
return convertView;
}
class MyDat {
TextView textview;
ImageView imageView1;
}
}
}
In your MyBaseAdapterItemActivity, setOnClickListener((OnCLickListener)mContext) to the ImageVIew and TextView.
In your activity, extends OnClickListener.
write your startActivity(Intent) in the OnClick(View v) depending on v.getId()
In list item xml, for set android:onClick="onFirstLinkClick" and similarly for the image view also,
and the use following method in your activity
public void onFirstLinkClick(View V) {
}

SharedPreferences ToggleButton not saving

*I've been going nuts with this problem the last few days.I'm trying to use SharedPreferences to allow my users to save the selected option of the in-game audio via the ToggleButton but everytime when I run my app and hit my "Done" button to go back to the main_activity screen and then click on settings again it never saves.I've done some research on this by googling,my book and this site but by many methods I've tried the result is the same.
I'm new to android development let alone app development so as much as I learned these past few weeks this has been a major roadblock for me and I apologize if this question has been covered already on this site but I'm at a real loss on what I'm doing wrong here.
Here is my code from my settings activity.
package com.fullfrontalgames.numberfighter;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ToggleButton;
public class Settings extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
Button Notifications = (Button) findViewById(R.id.Notifications);
Button Done = (Button) findViewById(R.id.done);
Button AccountSettings = (Button) findViewById(R.id.AccountSettings);
final ToggleButton AT = (ToggleButton) findViewById(R.id.AudioToggle);
AT.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
{
if ((AT.isChecked()))
{
SharedPreferences appPrefs =
getSharedPreferences("com.fullfrontalgames.numberfighter.Settings_preferences",
MODE_PRIVATE);
SharedPreferences.Editor editor = appPrefs.edit();
editor.putBoolean("atpref", AT.isChecked()); //value to store
editor.commit();
}
}
}
});
SharedPreferences appPrefs =
getSharedPreferences("com.fullfrontalgames.numberfighter.Settings_preferences",
MODE_PRIVATE);
boolean atpref = appPrefs.getBoolean("atpref", true); //default is true
AT.setChecked(atpref);
Done.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent Intent = new Intent(Settings.this,activity_main.class);
Intent.setFlags(android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(Intent);
}
});
Notifications.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.fullfrontalgames.numberfighter.Notifications"));
}
});
AccountSettings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.fullfrontalgames.numberfighter.AccountSettings"));
}
});
}
public void onToggleClicked(View view) {
// Is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
if (on) {
view.setSoundEffectsEnabled(true);
} else {
view.setSoundEffectsEnabled(false);
}
}
}
If you are trying to save the ToggleButton's state when it is false, simply remove the if-statement in your OnClickListener:
if ((AT.isChecked()))

ListView OnItemClickListener Not working When ListView is inflated to onCreateInputView()

I am developing a Keyboard. When i am inflating a ListView in onCreateInputView() and returning same View(or parent View) to it. I have implemented setOnItemClickListener for the ListView i am not getting callback to it.
What might be the issue?
android framework doesn't work for listview for keyboard(InputMethodService) ?
FY Reference i am attaching the code please have look at it.
NOTE: i am able to get ontouchlistner of it.
package com.listkeyboard;
import java.util.ArrayList;
import android.content.Context;
import android.content.res.Configuration;
import android.inputmethodservice.InputMethodService;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class ListKeyBoardIME extends InputMethodService {
private LinearLayout mainLayout;
#Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
}
#Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
System.out.println("onCreate");
}
#Override
public View onCreateCandidatesView() {
// TODO Auto-generated method stub
System.out.println("onCreateCandidatesView");
return super.onCreateCandidatesView();
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("onDestroy");
}
#Override
public void onFinishInput() {
// TODO Auto-generated method stub
super.onFinishInput();
System.out.println("onFinishInput");
}
#Override
public void onFinishInputView(boolean finishingInput) {
// TODO Auto-generated method stub
super.onFinishInputView(finishingInput);
System.out.println("onFinishInputView");
}
#Override
public View onCreateInputView() {
// TODO Auto-generated method stub
System.out.println(" onCreateInputView");
LayoutInflater layoutInflater = (LayoutInflater) ListKeyBoardIME.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mMainKeyboardLayout = layoutInflater.inflate(R.layout.keyboard_list_view, null);
mainLayout = (LinearLayout)mMainKeyboardLayout.findViewById(R.id.main_layout);
ListView listView = (ListView)mainLayout.findViewById(R.id.list);
ArrayList<Character> alphabets = new ArrayList<Character>();
for (char ch = 'a'; ch <= 'z'; ch++) {
alphabets.add(ch);
if (ch == 'n') {
break;
}
}
ArrayAdapter<Character> adapter = new ArrayAdapter<Character>(ListKeyBoardIME.this, android.R.layout.simple_list_item_1,alphabets);
listView.setAdapter(adapter);
listView.setItemsCanFocus(false);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(getApplicationContext(), "on itemclicked clicked" , Toast.LENGTH_SHORT).show();
}
});
setInputView(mainLayout);
return mainLayout;
}
#Override
public void onStartCandidatesView(EditorInfo info, boolean restarting) {
// TODO Auto-generated method stub
super.onStartCandidatesView(info, restarting);
System.out.println("onStartCandidatesView ");
}
#Override
public void onStartInput(EditorInfo attribute, boolean restarting) {
// TODO Auto-generated method stub
super.onStartInput(attribute, restarting);
System.out.println("onStartInput ");
}
#Override
public void onStartInputView(EditorInfo info, boolean restarting) {
// TODO Auto-generated method stub
super.onStartInputView(info, restarting);
System.out.println("onStartInputView");
}
}
I am also face the same problem & after lot of searching when no answer work for me, i implement below code in my list adapter & its work for me. If anyone find any other better answer please let us know.
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});

How to set numeric value in EditText?

I want to make a simple calculator My code is below:
package som.dev.android.calc;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class CalcApp extends Activity {
/** Called when the activity is first created. */
Button addValues, subValues, equalsValue;
EditText inputValue;
int inputNum = 0;
public int value1;
public int value2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// initializing Views
inputValue = (EditText) findViewById(R.id.editText1);
addValues = (Button) findViewById(R.id.addBtn);
subValues = (Button) findViewById(R.id.subBtn);
equalsValue = (Button) findViewById(R.id.equalsBtn);
// adding onClick Listeners
addValues.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
pressed = true;
value1 = Integer.parseInt(inputValue.getText().toString());
inputValue.setText("");
}
});
equalsValue.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
value2 = Integer.parseInt(inputValue.getText().toString());
int result = value1+value2;
inputValue.setText(result);// There an exception occurs....
}
});
}
}
But it does not sets result to Edittext in my app and an exception occurs and message comes Unfortunately app is colsed some thing like that so please any one tell me the solution of this problem
use this inputValue.setText(""+result);instead of inputValue.setText(result);// There an exception occurs....
just conversion integer into string
The reason you get an exception is that the EditText thinks you are setting a resource id. You need to convert the integer to a string yourself Integer.toString(result) before using it in the EditText Object. http://developer.android.com/reference/android/widget/TextView.html
change Following Line:-
inputValue.setText(result);
to
inputValue.setText(String.Valueof(result));
then your error is solved.

Categories

Resources