I have a couple of EditTexts arranged on rows and columns.Those EditTexts contain product name,quantity and price and a TextView that shows the total in real time(calculates it each time you write on one of the EditTexts)
I've setup a a button on each row that when clicked sets visibility of the row(3EditTexts for product name,price and quantity) to GONE.
My problem is that after i set the visibility to GONE,though there are no more EditTexts it still calculates their values from before being GONE.
My question now is,what happens when the EditTexts are set to visibility.GONE ?
My app calculates in real time,so when something happens to an EditText,he calculates again..but it's like the values are still there...Isn't this supposed to be the difference between invisible and gone ?
I'll show you the way i calculate(it is called even after you press the X button to erase the EditTexts,not only when you change values inside EditTexts)
public void calculeaza() {
totaltest = 0;
prod = new String[allprod.size()];
pret = new String[allpret.size()];
cant = new String[allcant.size()];
for (int m = 0; m < allprod.size(); m++) {
prod[m] = allprod.get(m).getText().toString();
if (prod[m].matches("")) {
prod[m] = " - ";
}
}
for (int j = 0; j < allcant.size(); j++) {
cant[j] = allcant.get(j).getText().toString();
if (cant[j].matches("")) {
cant[j] = Float.toString(0);
}
}
for (int k = 0; k < allpret.size(); k++) {
pret[k] = allpret.get(k).getText().toString();
if (pret[k].matches("")) {
pret[k] = Float.toString(0);
}
}
for (int l = 0; l < allpret.size(); l++) {
Float temp = Float.parseFloat(cant[l]) * Float.parseFloat(pret[l]);
totaltest = totaltest + temp;
TextView totalf = (TextView) findViewById(R.id.total);
totalf.setText(String.format("Total: %.2f", totaltest));
}
}
Lines Straight from Android dev site..
View.GONE This view is invisible, and it doesn't take any space for layout purposes.
View.INVISIBLE This view is invisible, but it still takes up space for layout purposes.
i.e it retains EditText object even after Gone..
You can reinitialise edittext if you dont want it to retain its value...or setText = ""
Above quoted is the only difference...
Hope this helps...
I'm not really seeing in the code you posted anything I can use to answer this question, but there does appear to be some confusion as to what setVisibility does:
INVISIBLE elements are not seen on the page, but they still take up space (there's a hole where they would be)
GONE elements have no visible effect on the screen, from the user's perspective they aren't there. However they are still part of the view.
If you want to remove the object from the view, then you need to call removeView() on its parent.
It may still take up memory after it has been removed from the view, in case your code has kept references to it in any variables.
It may still take up memory after there are no further references to it, at least until the garbage collector gets around to it.
I'm hoping the rather generalized statements above help clarify the situation.
Related
I'm trying to create a function that pulls together information that is currently scattered around different parts of my project.
As part of this task, I have a layout file with something like the following content... basically a set of rows, with each row having a label (TextView) and a UI element (e.g. CheckBox, Spinner or EditText) to collect information from the user:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/MyLinearLayout.Section"
android:id="#+id/section_pressure" >
<TableLayout style="#style/MyTableLayout"
android:id="#+id/table_pressure" >
<TableRow style="#style/MyTableRow" >
<TextView
style="#style/MyTextView.Label.WithHelp"
android:tag="label_show"
android:text="#string/label_show" />
<CheckBox
style="#style/MyCheckBox"
android:id="#+id/pressure" />
</TableRow>
<TableRow style="#style/MyTableRow" >
<TextView
style="#style/MyTextView.Label.WithHelp"
android:tag="label_unit"
android:text="#string/label_unit" />
<Spinner
style="#style/MySpinnerStyle"
android:id="#+id/pressureUnit" />
</TableRow>
</TableLayout>
</LinearLayout>
I already have an array of all the android:id values for the UI elements, and from that I want to generate another array of the corresponding android:text labels.
e.g. from R.id.pressureUnit I want to find the associated R.string.label_unit from that TableRow, so that I have a central record of what label is used for each UI element... currently that information is scattered across lots of different layout files.
Is this possible programmatically?
From what I understand you want to find #string resource ID of a sibling view of a given view.
Assuming you have the xml file already inflated, you can do the following:
private int getSiblingStringId(#IdRes int viewId) {
View uiElement = findViewById(viewId);
ViewGroup parent = (ViewGroup) uiElement.getParent();
// iterate through the siblings
for (int i = 0; i < parent.getChildCount(); i++) {
View view = parent.getChildAt(i);
if (view instanceof TextView) {
String value = ((TextView) view).getText().toString();
// found
return getStringIdFromValue(value);
}
}
throw new IllegalArgumentException("no TextView sibling for " + viewId);
}
private int getStringIdFromValue(String value) {
// get all fields using reflection
Field[] fields = R.string.class.getDeclaredFields();
for (Field field : fields) {
int stringResId = getResources().getIdentifier(field.getName(), "string", getPackageName());
String s = getString(stringResId);
if (s.equals(value)) return stringResId;
}
throw new IllegalArgumentException("no matching string for " + value);
}
The above code will work inside an activity. You might need to modify it a bit for other classes. (You will need to call findViewById, getResources and getString)
Well, I would set an ID to each table row. Then, programmatically get every child view under each row by iterating over the row's children. From there, you can easily manipulate every view by simply determining which one are you at by using instanceof. But, let's assume you cannot change this, you can still be doing it but it might not be efficient.
Since I don't really know the structure of your array of ids, I just assume its an array of integers called uiIds. Then, you would have to do something like
TableLayout layout = findViewById(R.id.table_pressure);
TableRow tr = null;
View rowChild = null;
TextView tv = null;
View secondaryView = null;
int tableChildCount = layout.getChildCount();
int rowChildCount = 0;
// Since I don't really know the structure of your array of ids,
// I just assume its an array of integers called uiIds
//This will contain the corresponding text strings of every textview
List<String> textLabels = Arrays.asList(new String[uiIds.length]);
// Go over all the rows on your table
for (int i = 0; i < tableChildCount; i++) {
// I'm assuming that you only have rows as the children of your table
tr = (TableRow) layout.getChildAt(i); //This is the row
rowChildCount = tr.getChildCount();
tv = null;
secondaryView = null;
// Now go over all the children of the current row i
for (int j = 0; j < rowChildCount; j++) {
// Here I'm also assuming you only have two children on every row
rowChild = getChildAt(j);
// At this point, the view v could be any type of the ones you have
// used. Since we are interested in TextView simply do an instanceof
if ( rowChild instanceof TextView ) {
tv = (TextView) v;
} else {
// This is the other view in the row. The ones from which you alreade have its ids
// e.g. the spinner with id pressureUnit
secondaryView = rowChild; //We do not need to cast
}
}
// Now we construct the array
if (tv != null && secondaryView != null) {
// The id we just obtained is one from the ones you have already saved
// in an array called uiIds
int secondaryViewId = secondaryView.getId();
//Now, we need to find the index of this id in uiIds
for (int idx = 0; idx < uiIds.length; idx++) {
if ( uiIds[idx] == secondaryViewId ) {
//We have found a match. Then just add the text of the textview in the corresponding index of our new array
textLabels.set(idx,tv.getText().toString());
}
}
}
}
// At this point, textLabels should contain the text of the text view on every row
// such that the ith element in the array you already have corresponds to
// ith element of the array we just created.
// In other words, if uiIds[0] is equals to R.id.pressureUnit, then,
// textLabels.get(0) is equals to R.string.label_unit
I have edited the answer directly on the text editor so it might have some sintax erros. Hopefully this solves your problem or at least gives you an idea.
To get overview of your label mappings, I suggest to code more systematically, in this case to name the label just like the id.
#+id/pressure => #string/label_pressure
#+id/pressureUnit => #string/label_pressureUnit
I know this is not a direct answer to your question. However I think, if you work in this way, you don't need a central table of your mappings at all and your layouts become more readable. Android Studio makes it really easy to do this kind of changes.
Is there a way to suggest or restrict keyboard input when selecting a NumberPicker so only the number controls are shown when entering values, similar to how you can use android:inputType="number" with a EditText?
I have a series of values, from 0.0 to 100.0 in 0.1 increments that I'd like to be able to use a NumberPicker to select in Android 4.3. In order to have the numbers selectable, I've created an array of strings which corresponds to these values, as shown below:
NumberPicker np = (NumberPicker) rootView.findViewById(R.id.programmingNumberPicker);
int numberOfIntensityOptions = 1001;
BigDecimal[] intensityDecimals = new BigDecimal[numberOfIntensityOptions];
for(int i = 0; i < intensityDecimals.length; i++ )
{
// Gets exact representations of 0.1, 0.2, 0.3 ... 99.9, 100.0
intensityDecimals[i] = BigDecimal.valueOf(i).divide(BigDecimal.TEN);
}
intensityStrings = new String[numberOfIntensityOptions];
for(int i = 0; i < intensityDecimals.length; i ++)
{
intensityStrings[i] = intensityDecimals[i].toString();
}
// this will allow a user to select numbers, and bring up a full keyboard. Alphabetic keys are
// ignored - Can I somehow change the keyboard for this control to suggest to use *only* a number keyboard
// to make it much more intuitive?
np.setMinValue(0);
np.setMaxValue(intensityStrings.length-1);
np.setDisplayedValues(intensityStrings);
np.setWrapSelectorWheel(false);
As more info, I've noticed that if I dont use the setDisplayedValues() method and instead set the integers directly, the numeric keyboard will be used, but the problem here is that the number being entered is 10 times more than it should be - e.g. if you enter "15" into the control its interpreted as "1.5"
// This will allow a user to select using a number keyboard, but input needs to be 10x more than it should be.
np.setMinValue(0);
np.setMaxValue(numberOfIntensityOptions-1);
np.setFormatter(new NumberPicker.Formatter() {
#Override
public String format(int value) {
return BigDecimal.valueOf(value).divide(BigDecimal.TEN).toString();
}
});
Any suggestions on how to raise a numeric keyboard to allow a user to enter decimal numbers like this?
I have successfully achieved this, borrowing heavily from #LuksProg's helpful answer to another question. The basic idea is to search for the EditText component of the NumberPicker and then to assign the input type as numeric. First add this method (thanks again to #LuksProg):
private EditText findInput(ViewGroup np) {
int count = np.getChildCount();
for (int i = 0; i < count; i++) {
final View child = np.getChildAt(i);
if (child instanceof ViewGroup) {
findInput((ViewGroup) child);
} else if (child instanceof EditText) {
return (EditText) child;
}
}
return null;
}
Then, in my Activity's onCreate() method I call this and set the input type:
np.setMinValue(0);
np.setMaxValue(intensityStrings.length-1);
EditText input = findInput(np);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
That did the trick for me!
My answer is to improve on the basis of Alan Moore, just change the last line
input.setInputType(InputType.TYPE_CLASS_NUMBER);
to
input.setRawInputType(InputType.TYPE_CLASS_NUMBER);
then there will no more problem like personne3000 said, no more crashes.
I'm sorry my English is not good, but I hope you can understand what I mean.
I have a layout the contains 4 TextViews with ids: "Name1", "Name2", "Name3", "Name4"
I would like to iterate on them with a for loop,
is there any way to do this?
something like
for(int i = 1; i <= 4; i++)
{
findViewById(R.id."Name" + i)
}
I know that this code is far from being real, but any help?
Thank you!
Ron
No, you cannot do it like that because R.id.xyz is referencing a static int of a static class. It's not a string that can be concatenated like that. Also, your code ignores the return value of findViewById so it does nothing (though I realize you mentioned the code is far being real, but still an actual use case might help clarify what you're trying to do). R.id."Name" means nothing and will give you a compiler error.
To loop through you can do something like this:
int[] ids = {R.id.foo, R.id.bar};
then
for(int i = 0; i<ids.length; i++) {
View v = findViewById(ids[i]);
}
Sure you can do that sort of. You cannot access an member of R.id using a string literal you must type out the variable name.
R.id."Test" is no good but R.id.Test is fine.
If you examine the type of R.id.XYZ you will find that it is simply an integer. There is no reason why you cannot perform basic arithmetic on id values.
However it doesn't really make sense to do so. When you build your APK the compiler automatically creates a static class called R that contains references to the various resources and assets contained in your APK, such as layouts, drawables, sounds, etc..
Just because you know that the integer value for R.id.button1 is X there is no guarantee that the integer value for R.id.button2 will be X+1, in fact it could be anything.
If you want to create a list of TextViews to iterate over consider adding them to a List and then iterating over the list. Like so:
ArrayList<TextView> list = new ArrayList<TextView>();
list.add( (TextView) findViewById(R.id.textView1) );
list.add( (TextView) findViewById(R.id.textView2) );
list.add( (TextView) findViewById(R.id.textView3) );
list.add( (TextView) findViewById(R.id.textView4) );
int size = list.getSize();
for (int i=0; i< size; i++)
{
TextView tv = list.get(i);
// Do something with tv like set its label to i
tv.setText(Integer.toString(i));
}
i am preparing one small game like jigsaw , for that i am using 9 imageview's with 9 different images in the layout. set the images to imageview at the time of starting those are actual images, after shuffle user will do sliding the images to complete puzzle, i want to check the modified image with actual image's, weather it's equal or not if those are equal popup a message, like gameover.
i tried like this
1.by using AND operator between the images(Drawables) but unlucky.
2.Using setLevel() for the images, compare those setLevel values with getLevel values for images after sliding still Unlucky.. here the problem is if i click on imageview one time getLevel() gives correct value, if i click more than once it gives zero value. Why it happens like this..
Please help me if you find any mistake in my code, otherwise guide me with good technique..
Xml code like this
<RelativeLayout
//9 imageview's
in jave code like this
if user will click on imageview it will swap the image's
img_View11.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(img_View21.getDrawable() == default_img ||
img_View12.getDrawable() == default_img) {
System.out.println("yes");
++click;
Noof_moves.setText("Moves: " +click);
ImageView iv = (ImageView)v;
d11 = iv.getDrawable();
prev_img = prev_imgView.getDrawable();
la11 = d11.getLevel();
System.out.println("d11 value is " +la11);
prev_imgView.setImageDrawable(d11);
img_View11.setImageDrawable(prev_img);
prev_imgView = img_View11;
check();
}
else { System.out.println("no"); }
}
});
void check(){
System.out.println("in checking condition");
if((lb11==la11) && (lb12==la12) && (lb13==la13) && (lb21==la21) && (lb22==la22)
&& (lb23==la23) && (lb31==la31) && (lb32==la32) && (lb33==la33)) {
Context c = getBaseContext();
System.out.println("gameover");
Toast.makeText(c, " GameOver ", Toast.LENGTH_SHORT).show();
}else{ System.out.println(" codition not checked "); }
}
//here lb=level before sliding
// la=level after sliding..
Ok, here is what you do for the solution.
maintain a new set of drawables marking them as original drawables that you had initially.
may be like
drawable og11 = imageView11.getDrawable();
do this part before shuffling. now you have original drawables, stored in the form of drawables.
After every click , check if og11 == imageView11.getDrawable(),... and so on for all the images in the jigsaw, if they match , it matches, else, they don't.
HTH.
I have a simple solution. Set serial number Integer tags to ImageViews using View.setTag(index) before jumbling(before preparing the puzzle.) Then everytime the user makes a move, loop through all the imageviews and check if they are in order. If out of order then puzzle is not completed yet.
class PuzzleItem {
Drawable puzzlepartImage;
int correctPosition;
public PuzzleItem(Drawable d, int index) {
puzzlepartImage = d;
correctPosition = index;
}
}
ArrayList<PuzzleItem> list = new ArrayList<PuzzleItem>();
for (int i = 0; i < 9; i++) {
list.add(new PuzzleItem(drawables[i], i));
}
Collections.shuffle(list);
//Create the views from this list and add to the layout serially.
// set the last view as emptyview.
On every move:
void onClick(View v) {
/* swap drawables */
Drawable clickedDrawable = v.getDrawable();
v.setDrawable(null);
mEmptyView.setDrawable(clickedDrawable);
mEmptyView = v;
/* swap tag integers */
Integer temp = (Integer)mEmptyView.getTag();
mEmptyView.setTag(v.getTag());
v.setTag(temp);
}
After every move check for completion:
for (int i = 0; i < 9; i++) {
if (imgView[i].getTag() != i) break;
}
if (i == 9)// puzzle completed.
why don't you use a gridView? and after each change, just check the items to see that images are in desired order
When you use == operator they are checking if both the objects are referencing the same object. But in your case they are 2 different objects.
You should set a tag to both the actual image and the other image. And check if they are both the same. That should work in your if condition.
For the drawable class i did notice a method setLevel and getLevel. You might be able to use this for your requirement.
I am using a Pool to manage Bullets in my game. The only problem is when a Bullet is obtained from the pool having just been recycled because it was involved in a collision, although it's Body's location is reset using Body.setTransform() when it is initialized, the Bullet's Sprite's location (which is used to detect collisions using Sprite.collidesWith(otherSprite)) is not reset quick enough (as it's updated in a Physics thread). This means the newly created bullet causes a collision the instant it is created, resulting in a single bullet causing more than one collision.
I tried calling Bullet.sprite.setPosition(0,0) when it is initialized, but this clearly interferes, as Bullets fail to be displayed at all with that line of code in place. What should I do to prevent this problem?
Bullet Creation:
bullets[bulletCounter] = bulletPool.obtainPoolItem();
bullets[bulletCounter].getBody().setTransform(shipBody.getTransform().getPosition(),0);
bullets[bulletCounter].getBody().setLinearVelocity(shipBody.getLinearVelocity());
bullets[bulletCounter].activate();
Collision Detection:
for(int i = 0; i < BULLET_MAX; i++){
if(bullets[i] != null && bullets[i].isActive()){
for(int j = 0; j < enemies.size(); j++){
//check for collision!
if(bullets[i].getSprite().collidesWith(enemies.get(j).getSprite())){
//-snip-
break;
}
}
}
}
I'd be interested to see what you're doing around the time you invoke onHandleObtainItem to get a bullet from the pool.
Would it not make sense to set the sprite position to some offscreen value when you invoke onHandleRecycleItem, something like:
#Override
protected void onHandleRecycleItem(final Bullet pBullet) {
pBullet.disable();
pBullet.getSprite().setPosition(-1, -1);
}