Some Buttons are not clickable in Landscape mode - android

Problem is shown in picture , please go light on me , i'm not that experienced in android
but trying my best to do so...
Tried this in my Main.java:
package com.faisal.my_calculator;
import android.content.SharedPreferences;
// all remaining imports here.
public class Main extends ActionBarActivity implements View.OnClickListener {
// Buttons
Button btnOne, btnTwo, btnThree, btnFour, btnFive, btnSix, btnSeven,..., btnTan, btnExit;
case R.id.three:
if (y != 0) {
y = 0;
etDisp.setText("");
}
str = str.append(btnThree.getText());
etDisp.setText(str);
break;
.
.
.
.
case R.id.add:
if (TextUtils.isEmpty(etDisp.getText().toString())) {
return;
}
operator = "+";
if (x == 0) {
x = Double.parseDouble(etDisp.getText().toString());
etDisp.setText("");
} else if (y != 0) {
y = 0;
etDisp.setText("");
} else {
y = Double.parseDouble(etDisp.getText().toString());
etDisp.setText("");
x = x + y;
etDisp.setText(Double.toString(x));
}
break;
I tried for:
appconfigchanges solution = it is not even in my AndroidManifest.xml,
Now to the layout part : In emulator all things are fully spaced on screen but when i run app below(2nd screen comes as a result not like on Emulator which i worked hard and adjusted its full screen) more when i run it on my LG_OG in landscape only starting three rows come on the screen o.O ....? need help here as well ...!
If anything needed more i will post it....!(now almost all code i have posted) Again, be patient to your brother :)

Please don't handle any exception if it is not realy needed to handle. Remove your try catch while initializing the UI and setting clicklistener to buttons and check where your code is failing.
Second you don't need to handle UI changes on orientation change as it will be handled by android itself. This will be done like -on orientation change your activity will be recreated and your oncreate will be again called so set layout and initialize all the UI element in oncrete itself and remove unnecessary code from onConfigurationChanged method.

Always check for the null value in suspected code
if(btnDiv == null)
{
//condition
}

Add following code just before try
if(btnDiv == null)
{
throw new NullPointerException("Button are not initialized");
}
if exception is thrown then your buttons are not initialized.

Yes , what a pain it was , as debugger is the best friend of programmer , i debugged it and it directly took me to the buttons which i didn't make in Landscape but were present in portrait so Eclipse doesn't like this and was causing my different buttons to stay idle but after putting those buttons in landscape xml now everything works all awesome...!
Now just layout make-up is remained as two buttons are running here and there when i run it.! :p
Thanks guys for your time...!

Related

onClick not grasping the int increase within another onClick?

So I have this code :
back --; //when it starts doing that back's value is 5
if (back == 0) {
back_button.setClickable(false);
}
if (back != 0) {
back_button.setClickable(true);
}
My back_button never sets itself to Clickable(true) after it set to Clickable(false), even when back wasn't equal to 0 caused by another button(back++;).
Why isn't my back_button onClick realizing that?
Actually view.setClickable() doesn't works at runtime that's why your toggling of back_button doesn't works at runtime .
Solution: Use view.setEnabled(boolean enabled) for your back_button.
Refer to :setEnabled() vs setClickable(), what is the difference?

Android soft keyboard reverts to default keyboard on orientation change

I'm building an android soft keyboard and I can't seem to fix this bug - I have an Arabic and QWERTY keyboard and when I rotate my device on the QWERTY keyboard (or even Arabic shift), it's as if my program has "restarted" and it becomes the Arabic Keyboard without shift.
The onSaveInstanceState(Bundle savedInstanceState) does not work because my application does not extend Activity but InputMethodService.
I put the following in my android manifest
android:configChanges="keyboard|keyboardHidden|orientation"
android:windowSoftInputMode="stateUnchanged|adjustResize">
I tried using
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.i(MYDEBUG, "Config Changed " + currentKeyboard.equals(qwerty));
}
However, currentKeyboard.equals(qwerty)) always results to false and I made sure it was true just before the orientation change.
Any help would be much appreciated.
I think its the applications fault. If the application is restarting on orientation changes, then your input connection is being torn down and rebuilt to a new edit text. This means the keyboard would see it as a new connection and will start in the default state. To test this, write a test app that turns off restarts on configuration change and see if it still happens to keyboards in that app.
Alright, after mind boggling thinking I realized the issue was with the line of code super.onConfigurationChanged(newConfig);
When removed, my code would change orientation but my keyboard would not be resized. Since I love the re-sizing feature of the parent, I made an array that contains all the keyboards and after changing orientation, I would update the array so that the keyboard array would have the right sizes.
#Override
public void onConfigurationChanged(Configuration newConfig) {
int currentKeyboard = 0;
boolean isShifted = kv.isShifted();
for (int i = 0; i < keyboard.length; i++) {
if(kv.getKeyboard().equals(keyboard[i])){
currentKeyboard = i;
break;
}
}
super.onConfigurationChanged(newConfig);
initializeKeyboardArray();
setKeyboard(keyboard[currentKeyboard]);
kv.setShifted(isShifted);
}
private void initializeKeyboardArray(){
keyboard = new Keyboard[7];
keyboard[ARABIC] = arabic;
keyboard[ARABIC_SHIFT] = arabicShift;
keyboard[ARABIC_SYMBOLS] = arabicSymbols;
keyboard[ARABIC_SYMBOLS_SHIFT] = arabicSymbolsShift;
keyboard[QWERTY] = qwerty;
keyboard[QWERTY_SYMBOLS] = qwertySymbols;
keyboard[QWERTY_SYMBOLS_SHIFT] = qwertySymbolsShift;
}
Not sure if this is a roundabout way of solving the problem or if this is the right way so if anyone knows a better way - please let me know.

How to make textView that is set dynamically appear when Tab is run?

Let me try to explain my question in detail.
I have 5 tabs and my Tab class is run after the Splash screen of my application, and the first tab will be displayed.
In my first tab, I have a class that contains a TextView that will display certain text under some condition (related to profile keyed in by the user in another class).
TextView duration = (TextView)view.findViewById(R.id.duration);
duration.setText(durationNumber);
if(week <= 12)
{
durationNumber = "Less than 12 weeks";
}
else
{
durationNumber = "More than 12 weeks";
}
When I run my application, the text does not appear, unless i navigate to other tabs and return to the first tab. I googled around and couldn't find a solution. I've tried using SharedPreferences but it didn't work. Perhaps I did it wrongly.
Why is this happening and how can I fix this?
What is the initial value of durationNumber? Is it null before it is set in the if-else loop? If it is null then the TextView will not appear (as there is no value to display).
You will have to set the TextView text again after the if-else loop. Try this...
TextView duration = (TextView)view.findViewById(R.id.duration);
if(week <= 12) {
durationNumber = "Less than 12 weeks";
} else {
durationNumber = "More than 12 weeks";
}
duration.setText(durationNumber);

Android: Long Click on a GridView does not work

I have a longClick method on a grid view that does not work for the the 0 index position of the underlying ArrayList. However, if I add only one 'SentenceButton' I can remove it but not if I add many 'SentenceButton's.
The primary code is as follows in the SentenceButton class:
public boolean onLongClick(View view) {
boolean result = false;
SentenceButton btnSentence = (SentenceButton)view;
ArrayList<SentenceButton> temp = Utility.getSentenceArray();
if(temp.contains((SentenceButton)view));
result = Utility.getSentenceArray().remove(btnSentence);
Utility.getSentenceArray().trimToSize();
SBActivity.getmSentenceAdapter().notifyDataSetChanged();
return result;
}
This part of the code is made to simply remove an errant entry.
This does not seem to work, ie. remove, the first element of Utility.getSentenceArray and only works intermittently on the other 'SentenceButtons'.
Strangely, the play sound and adding to the Sentence view from the fragment I am using as a content view only seems to work twice in a row and then seems to hang until I rotate the device. The relevant code follows in the SoundButton class:
public void onClick(View view) {
// To Do: add to current sentence
SoundThread sndThread = new SoundThread();
sndThread.start();
Utility.getSentenceArray().add(new SentenceButton(mContext, (SoundButton)view));
SBActivity.getmSentenceAdapter().notifyDataSetChanged();
}
This adds the content view 'SoundButton' as a 'SentenceButton' in the SentenceButton array. Even more strangely to me, if I rotate the device enough it seems to add the 'SoundButton' as a 'SentenceButton'. It even plays the 'SoundButton' as the rotation happens but not before. It as if the onClick of the SoundButton is queued somewhere.
The rotation code is as follows:
#Override
public void onConfigurationChanged(Configuration newConfig)
{
if (getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_LANDSCAPE)
{
setFragment(getmFrag());
mSentenceGridView.invalidate();
}
else if (getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_PORTRAIT)
{
setFragment(getmFrag());
mSentenceGridView.invalidate();
}
super.onConfigurationChanged(newConfig);
}
Could someone help me with this. Definitely a noob here and of course will add any relevant info asked of me.
I am trying to write a soundboard to help my friend's relative communicate better.
Many thanks in advance and especially to a tutorial on a working soundboard.
I am writing this with a Kindle Fire as my testing device.
I discovered part of the problem was not having a proper id for the buttons I am creating

Problem when moving TableRow within TableLayout

I am trying to dynmically move several rows around at once within a specific view based on user input.
The use-case is that the user picks an option and based on the option, certain rows are show/hidden and the rows need to be rearranged to make sense to the user.
The TableLayout is instantiated in XML, but the TableRow and it's layout(s) are instantiated in code. I have tried several different methods, with varying levels of success.
Code which works for hiding/disabling properly:
private void updateTimeRow(TargetType goalTarget)
{
TableRow timeRow = (TableRow)this.findViewById(timeId + 100);
EditText timeText = (EditText)this.findViewById(timeId);
if (goalTarget == TargetType.daysRowedWeek
|| goalTarget == TargetType.metersDay
|| goalTarget == TargetType.metersSeason
|| goalTarget == TargetType.metersWeek)
{
timeRow.setVisibility(View.GONE);
timeRow.setEnabled(false);
timeText.setEnabled(false);
}
else if (goalTarget == TargetType.splitForDistance
|| goalTarget == TargetType.unknown)
{
setTimeText(currentTarget.recalcTime().toString());
timeRow.setVisibility(View.VISIBLE);
timeRow.setEnabled(false);
timeText.setEnabled(false);
}
else
{
timeRow.setVisibility(View.VISIBLE);
timeRow.setEnabled(true);
timeText.setEnabled(true);
}
}
Based on questions answered here, I tried removing/re-adding rows, with this piece of code:
int rowNum = -1;
tableLayout.removeView(timeRow);
switch (goalTarget)
{
case unknown:
case timeForDistance:
rowNum = 2;
break;
case splitForDistance:
rowNum = 4;
break;
case splitForTime:
rowNum = 3;
break;
}
tableLayout.addView(timeRow, rowNum);
Two things break here:
The disable code fails (whether this code is run before of after the enable/disable code above
The rows in my layout get all screwed up.
So, how do I move the rows around dynamically, while properly hiding/showing/enabling/disabling the different rows?
After a lot of frustrating trial and error, I finally figured out the only way to do this with solid results:
I had to adjust the way my UI was updating, to a 3-step process:
- Update the values for the Inputs (enforces formatting, re-calcs, etc)
- Re-layout the rows
- Enable/disable the different inputs based on the selections
The nuts and bolts is that anytime the goalTarget changes I have to remove all of the TableRows from the TableLayout and add
them back in the correct order, with the appropriate row numbers as part of the addView() call.
This is definitely not the prettiest way I could see it being done, but apparently the TableLayout doesn't like you just trying
to move things around one at a time.

Categories

Resources