If a toggle button is checked or not - android

Well I am trying to see if a checkbox is checked or not, but I get errors.
Defining the checkbox code:
public class Preferences extends PreferenceActivity {
CheckBoxPreference togglePref;
...
}
CheckBox Code:
public void checkNotify() {
if (togglePref.isChecked()==(true)) {
...
}
}
OnCreate code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//SharedPreferences settings = getSharedPreferences("EasySettingsPreferences", MODE_PRIVATE);
//boolean notify = settings.getBoolean("notify", false);
checkNotify();
rootView = new LinearLayout(this);
rootView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
rootView.setOrientation(LinearLayout.VERTICAL);
togglePref = new CheckBoxPreference(this);
textView = new TextView(this);
textView.setText(R.string.app_name);
titleView = new LinearLayout(this);
titleView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 26));
titleView.setBackgroundResource(R.drawable.pattern_carbon_fiber_dark);
titleView.addView(textView);
preferenceView = new ListView(this);
preferenceView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
preferenceView.setId(android.R.id.list);
PreferenceScreen screen = createPreferenceHierarchy();
screen.bind(preferenceView);
preferenceView.setAdapter(screen.getRootAdapter());
rootView.addView(titleView);
rootView.addView(preferenceView);
this.setContentView(rootView);
setPreferenceScreen(screen);
}
Logcat Picture:
logcat pic http://img710.imageshack.us/img710/8529/unledxq.png
Debugger Picture
debugger pic http://img847.imageshack.us/img847/1192/unled1rn.png
Please help me if you can. Thanks!

I guess that you never initialise togglePref. To be sure we need to see the onCreate(). (I will update my answer if my guess was wrong...)
edit
I was right. You call checkNotify() before your even initialized the togglePref variable. Check your logic if it really makes sense to call that method before everything else or if it is fine if you call it later.
A tip: You can simplify your if statement:
// yours:
if (togglePref.isChecked()==(true)) {
// simplified:
if (togglePref.isChecked()) {

In your onCreate() method, you're calling checkNotify() before togglePref has been initialized. You want to move that initialization up (or move checkNotify() down.)

Related

Android: Showing a toast message when the checkbox is checked for the dynamically created checkboxes

When an ImageButton is clicked a checkBox is created dynamically in my app.
The problem I'm facing is with toast messages not showing or let's say the listener I am using for the checkboxes to show a toast message when the checkbox is checked is working only for the current/latest checkbox that has been created. Not for the checkboxes that had been created previously.
Problem,
I saw that the problem could be with the variable (toDoCheckBox) I am using to save the newly created checkbox instance and every time it's getting overriding dynamically as I create newer checkboxes. So for the previous checkboxes, there is no reference pointing to them in order for the listener to work. (the problem could be this or not, i don't know for sure) Even if this is the problem i don't know how to solve it
What i tried is that,
creating the instance of checkbox on both onCreate as well as the createCheckBox method so that I could assign the listener in both the places.
on the onCreate()
on the createCheckbox()
Only the latest created checkbox works and shows the toast messages
Can someone help me or guide me with the problem,
I want the toast message to show for every checkboxes
i posted only the code that could be of some use
private CheckBox toDoCheckBox;
private EditText defaultEditText;
private List<CheckBox> listCheckBoxes;
private int previousViewId = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.defaultEditText = findViewById(R.id.defaultEditText);
toDoCheckBox = new CheckBox(this);
this.listCheckBoxes = new ArrayList<>();
this.checkBoxListener = new View.OnClickListener(){
#Override
public void onClick(View view){
if(toDoCheckBox.isChecked()){
toDoCheckBox.setChecked(true);
Toast.makeText(MainActivity.this,"You did a good work, keep it up!",Toast.LENGTH_SHORT).show();
}
}
};
toDoCheckBox.setOnClickListener(checkBoxListener);
}
public void createCheckBox() {
//CREATING A CHECKBOX
toDoCheckBox = new CheckBox(this);
toDoCheckBox.setText(defaultEditText.getText().toString());
toDoCheckBox.setTextSize(20);
toDoCheckBox.setId(View.generateViewId());
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) defaultEditText.getLayoutParams();
ConstraintLayout.LayoutParams newParams = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.WRAP_CONTENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT
);
newParams.width = params.width;
newParams.height = params.height;
//Constraints
newParams.startToStart = params.startToStart;
newParams.endToEnd = params.endToEnd;
newParams.topToTop = params.topToTop;
newParams.topToBottom = params.topToBottom;
//Margins
newParams.leftMargin = params.leftMargin;
newParams.topMargin = params.topMargin;
newParams.bottomMargin = params.bottomMargin;
newParams.rightMargin = params.rightMargin;
constraintLayout.addView(toDoCheckBox, -1, newParams);
previousViewId = toDoCheckBox.getId();
defaultEditText.setVisibility(View.INVISIBLE);
createEditText();
toDoCheckBox.setOnClickListener(checkBoxListener);
this.listCheckBoxes.add(toDoCheckBox);
totalCheckBox++;
}
Try to use the local checkbox object instead of the global toDoCheckBox.

filling in text views from shared preferences in a scroll view

Im a bit stuck on the best way to implement this so here I am.
I have a list of shared preferences with a label and a price. My app, a sort of calculator if you will, I would like to load the shared preferences in this way but stuck on how to implement it properly so ill wrote it down in a very literal way
Load shared preferences "item1"
Load into textView1 if empty
if it is full then try textview2,
if it is full then try textview3,
etc etc
I would like a function which loads preference 1, attempts to load it into a view and if its full, try the next textview and so on until it finds and empty slot. and then try the next shared preference item
Maybe I'm looking at this wrong but without writing out tons of if/else statements to get the job done, I cant see a way that makes sense.
I've seen somewhere some code to increment the textview numbers in a loop until a case is met but cant seem to recall it anywhere. this would reduce the code a lot if this was implemented properly I'm sure.The app I'm creating will display a list of selected items from another screen,save selected to shared preferences and then load the list on the results screen to give a total based on the sums attached to the items loaded.
I have no actual code to give you as im still prototyping this section and have nothing solid to show you.
any pointers welcome or any nod in the right direction would be handy.
thanks guys
You could add TextViews dynamiclly through java.
public class DynamicAvtivity1 extends Activity {
TextView tv;
EditText et;
Button btn;
LinearLayout ll;
int i;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
i = 1;
ll = new LinearLayout(this);
ll.setOrientation(1);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT , LayoutParams.FILL_PARENT);
final LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT , LayoutParams.WRAP_CONTENT);
ll.setLayoutParams(lp);
tv = new TextView(this);
tv.setText("Enter Name");
tv.setLayoutParams(lp2);
ll.addView(tv);
et = new EditText(this);
et.setHint("Name");
et.setLayoutParams(lp2);
ll.addView(et);
btn = new Button(this);
btn.setLayoutParams(lp2);
btn.setText("Add Button");
btn.setLayoutParams(lp2);
ll.addView(btn);
btn.setOnClickListener(new View.OnClickListener() {
#SuppressLint("NewApi") #Override
public void onClick(View v) {
// TODO Auto-generated method stub
Button b = new Button(getApplicationContext());
b.setLayoutParams(lp2);
b.setText("Button " + i++);
b.setTextColor(Color.BLACK);
b.setBackground(btn.getBackground());
ll.addView(b);
}
});
setContentView(ll);
}
}

Android app Crash

Why is this:
public class HelpTab extends Activity
{
LinearLayout helpLayout; //was changed to LinearLayout helpLayout = new LinearLayout(this);
TextView helpText = new TextView(this);
Button tips = new Button(this);
Button walkThrough = new Button(this);
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
buttonCreator();
setContentView(helpLayout);
}
public void buttonCreator()
{
//Button featuress defined in here (setText("") and adding them to the layout, addView();)
}
Causing my program to crash?
I have looked at the code extensively and I can't put my finger on it, and the debugger also says source not found when it opens the new page tab thing to tell me what is happening.
Try calling setContentView(helpLayout); first and then buttonCreator();
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(helpLayout);
buttonCreator();
}
Assuming that you are trying to initialize your button inside buttonCreater() with respect to a button you have declared in your helplayout, you might run into Null Pointer Exception.

Problems with coding Preference Screen

i have a big Problem with this Code:
public class Preferences extends PreferenceActivity {
private LinearLayout rootView;
private LinearLayout titleView;
private ListView preferenceView;
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
rootView = new LinearLayout(this);
rootView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
rootView.setOrientation(LinearLayout.VERTICAL);
textView = new TextView(this);
textView.setText(R.string.tx_einstellungen);
textView.setTextColor(Color.WHITE);
textView.setTextSize(20);
titleView = new LinearLayout(this);
titleView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 45));
titleView.setBackgroundResource(R.color.background);
titleView.addView(textView);
titleView.setGravity(Gravity.CENTER);
preferenceView = new ListView(this);
preferenceView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
preferenceView.setId(android.R.id.list);
PreferenceScreen screen = createPreferenceHierarchy();
screen.bind(preferenceView);
preferenceView.setAdapter(screen.getRootAdapter());
rootView.addView(titleView);
rootView.addView(preferenceView);
this.setContentView(rootView);
setPreferenceScreen(screen);
}
private PreferenceScreen createPreferenceHierarchy() {
PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
PreferenceScreen lang_screen = getPreferenceManager().createPreferenceScreen(this);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
// Screen preference
lang_screen.setKey("language_pref");
lang_screen.setTitle("Sprache");
lang_screen.bind(preferenceView);
root.addPreference(lang_screen);
// Example of next screen toggle preference
CheckBoxPreference nextScreenCheckBoxPref = new CheckBoxPreference(this);
nextScreenCheckBoxPref.setKey("next_screen_toggle_preference");
nextScreenCheckBoxPref.setTitle("");
nextScreenCheckBoxPref.setSummary("");
lang_screen.addPreference(nextScreenCheckBoxPref);
return root;
}
The First PreferenceScreen is a nice white screen with a blue line on the top and in the line we have the Text "Einstellungen" in english "Preferences" in color white.
However when i click on "Sprache" the next prefcreen is black and there is an android Titlebar.... i want to create more "under-prefscreens" and they all have to be white with these blue line on the top.
PLEASE HELP
Thankx Alex
This is a known bug in Android 2.1u1.
If you use multi-level preference screen the Light theme is not propogated to next level screens. For higher SDK 2.2 and above it works perfectly fine. The issue is also on some devices. Unfortunately no solution yet for this apart from leaving the theme as dark.

More than one AnimationDrawable at the same time

I would like to start different animations at the same time when the app starts. I added my code in the onWindowFocusChanged function for one animated button and I haven't problems. However, when I try to animate another button, the second one don't move, fixed on the first frame.
Anyone knows how can I get over?
Some piece of code:
In the OnCreate:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button mButtonPlay = (Button) findViewById(R.id.ButtonPlay);
mButtonPlay.setBackgroundResource(R.drawable.button_play);
buttonAnimationPlay = (AnimationDrawable)mButtonPlay.getBackground();
Button mButtonOptions = (Button) findViewById(R.id.ButtonOptions);
mButtonOptions.setBackgroundResource(R.drawable.button_options);
buttonAnimationOptions = (AnimationDrawable)mButtonPlay.getBackground();
}
enter code here
And in the onWindowFocusChanged :
#Override
public void onWindowFocusChanged(boolean hasFocus)
{
if (hasFocus)
{
buttonAnimationPlay.start();
buttonAnimationOptions.start();
}
else
{
buttonAnimationPlay.stop();
buttonAnimationOptions.stop();
}
}
Well, I got it finally! There were a mistake in the code, what a crap! This is the good one:
final Button mButtonPlay = (Button) findViewById(R.id.ButtonPlay);
mButtonPlay.setBackgroundResource(R.drawable.button_play);
buttonAnimationPlay = (AnimationDrawable)mButtonPlay.getBackground();
final Button mButtonOptions = (Button) findViewById(R.id.ButtonOptions);
mButtonOptions.setBackgroundResource(R.drawable.button_play);
buttonAnimationOptions = (AnimationDrawable)mButtonOptions.getBackground();
Anyway, I've also tried the programmatically add of views and, now, it also works. For anyone would use one or the other kind, this is the other one:
RelativeLayout Relative = (RelativeLayout) findViewById(R.id.RelativeForButtons);
RelativeLayout.LayoutParams relativeParamsPlay =
new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams relativeParamsOptions =
new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
relativeParamsPlay.addRule(RelativeLayout.BELOW,R.id.Title);
relativeParamsPlay.addRule(RelativeLayout.CENTER_IN_PARENT);
final Button mButtonPlay = new Button(this);
mButtonPlay.setBackgroundResource(R.drawable.button_play);
buttonAnimationPlay = (AnimationDrawable)mButtonPlay.getBackground();
mButtonPlay.setId(1);
mButtonPlay.setText("PLAY");
Relative.addView(mButtonPlay,relativeParamsPlay);
final Button mButtonOptions = new Button(this);
mButtonOptions.setBackgroundResource(R.drawable.button_options);
buttonAnimationOptions = (AnimationDrawable)mButtonOptions.getBackground();
mButtonOptions.setId(2);
mButtonOptions.setText("OPTIONS");
relativeParamsOptions.addRule(RelativeLayout.BELOW, mButtonPlay.getId());
relativeParamsOptions.addRule(RelativeLayout.CENTER_IN_PARENT);
Relative.addView(mButtonOptions,relativeParamsOptions);
All of that is in the OnCreate function. If you want to start animations, use the onWindowFocusChanged function as explained above.
Hope it helps! :)

Categories

Resources