How to make invisible images visible? - android

I am developing a card game. I divide 13 cards to each client using Server,
when I divide 13 cards to 1st player, 9 cards are invisible and remaining 4 are visible.
Now I want when I click this one Image, the remaining 9 cards gets visible?
How to do this?
- the code is this:
String str=" "c,a", "c,k", "c,q", "c,j", "c,10", "c,9", "c,8", "c,7", "c,6", "c,5", "c,4", "c,3", "c,2"";
drawCards(str);
private void drawCards(String drawString) {
String[] separated = msgLog.split("\\,");
for (int i = 2; i < separated.length - 1; i += 2) {
String symbol = separated[i];
String num = separated[i + 1];
String resourceName = symbol + num;
//symbol and number is used for get image from xml file
int resID = getResources().getIdentifier(resourceName, "id", getPackageName());
im = (ImageView) findViewById(resID);
Context context = im.getContext();
cardID = context.getResources().getIdentifier(resourceName, "drawable", context.getPackageName());
//9 card invisible
if ( i > 10) {
im.setVisibility(View.GONE);
}
/* elseif(x.getVisibility() == VISIBLE)
{
x.setVisibility(INVISIBLE);
}*/
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(im.getLayoutParams());
lp.setMargins(counter * 5, 0, 0, 0);//left,right,top,bottom
im.setLayoutParams(lp);
im.setImageResource(cardID);
im.setOnClickListener(this);
counter = counter + 8;
}
}
public void onClick(View v) {
final String IdAsString = v.getResources().getResourceName(v.getId());
pieceToast = Toast.makeText(getApplicationContext(), idServer, Toast.LENGTH_SHORT);
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
pieceToast.show();
}
});
}
When I click this card how to hide card make visible

You can take the help from below given link:
https://stackoverflow.com/questions/41285814/how-to-make-visible-and-invisible-an-image-by-clicking-a-button-in-android-studi
If you still have question, let me know

You could play around with the visibility and use the methode setVisibility(View.VISIBLE) and setVisibility(View.GONE)

Related

How to update whole output row on change of given Input?

I am having trouble with my output screen!
When I click on Click button with these inputs output screen looks like attached image, which is so far great!
But if I change my input, the program gives new answer by adding more rows with previous answers! I want only new answers on screen to be shown!
Also without updating input if I click on button same way screen adds up new rows!
I am including a picture of this also..
I used this code given below,
clickbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
aI = Double.parseDouble(Ia.getText().toString());
bI = Double.parseDouble(Ib.getText().toString());
cI = Double.parseDouble(Ic.getText().toString());
bisection();
}
});
private void bisection() {
if ((f(aI) < 0 && f(bI) > 0) || (f(aI) > 0 && f(bI) < 0)) {
for (int i = 0; i < cI; i++) {
View tableRow = LayoutInflater.from(this).inflate(R.layout.table_item, null, false);
TextView iteration = (TextView) tableRow.findViewById(R.id.index);
TextView a = (TextView) tableRow.findViewById(R.id.a);
TextView b = (TextView) tableRow.findViewById(R.id.b);
TextView x = (TextView) tableRow.findViewById(R.id.x);
TextView fx = (TextView) tableRow.findViewById(R.id.fx);
double root = (aI+bI)/2;
iteration.setText(" " + (i + 1));
a.setText(Double.toString(aI));
b.setText(Double.toString(bI));
x.setText(Double.toString(root));
fx.setText(Double.toString(f(root)));
tableLayout.addView(tableRow);
if(f(aI)*f(root) < 0){
bI = root;
}else if (f(aI)*f(root) >0) {
aI = root;
}
}
} else {
Toast.makeText(getApplicationContext(), R.string.popUpMsg,Toast.LENGTH_SHORT).show();
}
}
public static double f(double x){
return ((Math.pow(x,3))-x-4);
}
I have already found almost same problem has been solved in a post previously asked by someone else but I couldn't fix mine! Help me. Thanks!
In your button click listener method, add following line of code before calling bisection().
tableLayout.removeAllViews();
Whenever, you will click the button, previous output will be removed before calculating new input values.
Your code would look like this:
clickbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
aI = Double.parseDouble(Ia.getText().toString());
bI = Double.parseDouble(Ib.getText().toString());
cI = Double.parseDouble(Ic.getText().toString());
tableLayout.removeAllViews(); // Remove previous output
bisection();
}
});
first way; You have to use
removeAllViews();
Before adding. To do this, you have to have a layout variable around. ex;
linearLayout.removeAllViews();
Another way is adding the Textviews in the beginning with VIEW.INVISIBLE or VIEW.GONE
Whichever suits you.

ImageView: changing set of imageviews src by generic code

I have set of 26 imageviews and set of 26 images( named as a... z), I am trying to set a random image to each imageview one by one by generic code.
Getting a runtime error.
public class MainActivity extends AppCompatActivity {
int i = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialMethod();
//this method loads the random images to imageview one by one
}
public void initialMethod() {
String temp;
TextView mytextView;
int randList[] ={26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
ImageView myImageView;
int ImageViewIdList[] = {R.id.imageA1, R.id.imageA2, R.id.imageA3, R.id.imageA4, R.id.imageA5, R.id.imageA6, R.id.imageA6, R.id.imageA8, R.id.imageA9, R.id.imageA10, R.id.imageA11, R.id.imageA12, R.id.imageA13, R.id.imageA14, R.id.imageA15, R.id.imageA16, R.id.imageA17, R.id.imageA18, R.id.imageA19, R.id.imageA20, R.id.imageA21, R.id.imageA22, R.id.imageA23, R.id.imageA24, R.id.imageA25, R.id.imageA26};
for (i = 0; i < 26; i++) {
myImageView= (ImageView)findViewById(ImageViewIdList[i]);
String ImageName = String.valueOf((char) (randList[i] + 97));
int id = getResources().getIdentifier(ImageName, "id", getPackageName());
myImageView.setImageResource(id);
}
Well first of all - always post your error. You're trying to load an ID as an image resource. Instead, put your drawable IDs in randList.
Also consider creating all those ImageViews dynamically in the code, as well as other proper coding practices (e.g. no hard coded values in code and so on, though I bet this is for a school exercise)
int randList[] = { R.drawable.image1, R.drawable.image2, ... }
imageView.setImageDrawable(randList[...]);
You have mistyped one of your commas with ' between 17 and 16
Change:
int randList[] ={26,25,24,23,22,21,20,19,18,17'16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
to:
int randList[] ={26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
try this way...
int randList[] ={26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
ImageView myImageView;
int ImageViewIdList[] = {R.id.imageA1, R.id.imageA2, R.id.imageA3, R.id.imageA4, R.id.imageA5, R.id.imageA6, R.id.imageA6, R.id.imageA8, R.id.imageA9, R.id.imageA10, R.id.imageA11, R.id.imageA12, R.id.imageA13, R.id.imageA14, R.id.imageA15, R.id.imageA16, R.id.imageA17, R.id.imageA18, R.id.imageA19, R.id.imageA20, R.id.imageA21, R.id.imageA22, R.id.imageA23, R.id.imageA24, R.id.imageA25, R.id.imageA26};
for (int i = 0; i < 26; i++) {
myImageView= (ImageView)findViewById(ImageViewIdList[i]);
String ImageName = String.valueOf((char) (randList[i] + 97));
int id = getResources().getIdentifier(ImageName, "id", getPackageName());
}
As "etan" said
I have changed the code
int id = getResources().getIdentifier(randList[i],"id", getPackageName());
to
int id = getResources().getIdentifier(randList[i],"drawable", getPackageName());
Thanks a lot etan! :)

Edit text of several buttons using a for loop

I have 16 buttons, whose names are "button1", "button2", and so on. Is there a way I can iterate through them using a for loop, by somehow appending the number value upon each iteration? Something like this:
for(int i = 1; i<17; i++ ){
Button b = (Button)findViewById(R.id.buttoni);
I know I can simply initialize each button in my onCreate() method, but I was just curious if I could do it in a way similar to my example code.
Thank you.
You can use getIdentifier :
for(int i = 1; i<17; i++ ){
int buttonId = getResources().getIdentifier("button"+i, "id", getPackageName());
Button b = (Button)findViewById(buttonId);
//Your stuff with the button
}
You can create an array of Button's and use getIdentifier method that allows you to get an identifier by its name.
final int number = 17;
final Button[] buttons = new Button[number];
final Resources resources = getResources();
for (int i = 0; i < number; i++) {
final String name = "btn" + (i + 1);
final int id = resources.getIdentifier(name, "id", getPackageName());
buttons[i] = (Button) findViewById(id);
}
In case someone is interested how to achive the same result using Java only
The solution above uses Android specific methods (such as getResources, getIdentifier) and can not be used in usual Java, but we can use a reflection and write a method that works like a getIdentifier:
public static int getIdByName(final String name) {
try {
final Field field = R.id.class.getDeclaredField(name);
field.setAccessible(true);
return field.getInt(null);
} catch (Exception ignore) {
return -1;
}
}
And then:
final Button[] buttons = new Button[17];
for (int i = 0; i < buttons.length; i++) {
buttons[i] = (Button) findViewById(getIdByName("btn" + (i + 1)));
}
NOTE:
Instead of optimizing this kind of code you should rethink your layout. If you have 17 buttons on the screen, a ListView is probably the better solution. You can access the items via index and handle onClick events just like with the buttons.

Looping through two views to check for equality

I have two identical views with a number of editTexts. In one, pre-defined answers are populated in the editTexts (but not shown to the user). In the second, the user starts with all blank editTexts, and then fills them out in an attempt to make them the same as the pre-defined answers.
So I want to loop through the user's view, checking it against the pre-defined one, until an inequality is found, in which case the method will return false.
My code is below. Inside the onCreate I have a buttonListener (when the user is ready to check answers)
btnSolution.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(checkAnswer() == true){
Toast.makeText(getBaseContext(), "all good!", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getBaseContext(), "no good", Toast.LENGTH_LONG).show();
}
}
});
the checkAnswer() method is then defined as follows
public boolean checkAnswer() {
final int ROW_COUNT = 15;
final int COL_COUNT = 10;
final String ROWS[] = {"R1","R2","R3","R4","R5","R6","R7","R8","R9","R10","R11","R12","R13","R14","R15"};
final String COLS[] = {"C1","C2","C3","C4","C5","C6","C7","C8","C9","C10"};
for(int i=0; i<ROW_COUNT; i++) {
for(int j=0; j<COL_COUNT; j++) {
String a = ROWS[i];
String b = COLS[j];
int editTextBaseId = getResources().getIdentifier("box" + a + b, "id", getPackageName());
int editTextAnswerId = getResources().getIdentifier("boxA" + a + b, "id", getPackageName());
EditText editTextBase = (EditText)findViewById(editTextBaseId);
EditText editTextAnswer = (EditText)findViewById(editTextAnswerId);
String textBase = editTextBase.getText().toString();
String textAnswer = editTextAnswer.getText().toString();
if(textBase.equals(textAnswer)) {
}
else {
return false;
}
}
}
return true;
}
Unfortunately when I try and run this I am getting a crash and the following error in my LogCat
12-17 00:05:02.075: E/SKIA(16370): FimgApiStretch:stretch failed
Any obvious errors?
That's not an error itself. I guess you're using a Samsung as your target device, if so, don't worry about it.
In the other hand, maybe it's better to compare only the strings. All those findViewById are inneficient.
Looking at your code:
EditText editTextAnswer = (EditText)findViewById(editTextAnswerId);
Do you have both views in the same layout, and the one with the answers is hidden? I mean, if you have the view with blank editTexts as the content of your activity, you can't find the editText with the answer as it's in other xml (assuming you did it as a different xml).

Set imageview background on button click?

lets say I have a timer activity. Theres an image view that displays a number of 0 - 9(these are viewed as images, NOT text).
eg. the images names are clock_0, clock_1, clock_2...clock_9.
There is a plus and minus button that should change a counter(to pick the image). If current image is clock_0, pressing the plus button will change the view to clock_1 etc. How do I do this efficiently?
EDIT
Heres what I tried to do;
set an int[],
int[] clockResource = new int[10];
clockResource[0] = R.drawable.clock_0;
clockResource[1] = R.drawable.clock_1;
clockResource[2] = R.drawable.clock_2;
clockResource[3] = R.drawable.clock_3;
clockResource[4] = R.drawable.clock_4;
clockResource[5] = R.drawable.clock_5;
clockResource[6] = R.drawable.clock_6;
clockResource[7] = R.drawable.clock_7;
clockResource[8] = R.drawable.clock_8;
clockResource[9] = R.drawable.clock_9;
assign an onclicklistener for the addbutton
case R.id.addMin:
if (addMin.isEnabled()) {
if (mintwo < 9) {
mintwo++;
} else if (mintwo == 9) {
minone++;
mintwo = 0;
}
min2.setBackgroundResource(clockResource[mintwo]);
min1.setBackgroundResource(clockResource[minone]);
}
Since they're ImageViews, use setImageDrawable or setImageResource instead.
Related SO-question.
case R.id.addMin:
if (addMin.isEnabled()) {
if (mintwo < 9) {
mintwo++;
} else if (mintwo == 9) {
minone++;
mintwo = 0;
}
min2.setImageResource(clockResource[mintwo]); //updated
min1.setImageResource(clockResource[minone]); //updated
}

Categories

Resources