I want to go back and forth a string-array and put it on my text view, but when coming back program stops, what is my problem?
String[] q = new String[20];
...
int a =0;
q[0]="a";
q[1]="b";
q[2]="c";
...
public void back(View v) {
a--;
u.setText(q[a]);
}
public void next(View v) {
a++;
u.setText(q[a]);
}
public void back(View v) {
if(a > 0){
a--;
u.set Text(q[a]);
}
}
public void next(View v) {
if(a < q.length()){
a++;
u.set Text(q[a]);
}
}
in method next() "q.length" would do, q.length() I'am afraid will not.
Related
I want to use viewpager inside recycler view and set data on pager from api.
For more details, i'm adding my code in question.
My Code is
public void onBindViewHolder(final worksheetAdapter.CustomViewHolder holder, final int position) {
final int a = position; rec_worksheet.setTag(position);
Set text on TextView
holder.question.setText(userlist.get(position).get("ques"));
holder.question.startAnimation(animFadein);
holder.tvans1.setText(userlist.get(position).get("ans1"));
holder.tvans1.startAnimation(animFadein);
holder.tvans2.setText(userlist.get(position).get("ans2"));
holder.tvans2.startAnimation(animFadein);
holder.tvans3.setText(userlist.get(position).get("ans3"));
holder.tvans3.startAnimation(animFadein);
holder.tvans4.setText(userlist.get(position).get("ans4"));
holder.tvans4.startAnimation(animFadein);
holder.submitbtn.setVisibility(View.GONE);
if(position == userlist.size() - 1){
holder.submitbtn.setVisibility(View.VISIBLE);
}
holder.queNo.setText("Question No "+String.valueOf(position+1));
holder.tvTotalQue.setText("Total Ques. "+String.valueOf(userlist.size()));
holder.tvSubName.setText(userlist.get(position).get("subject_name"));
holder.submitbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
submit();
}
});
Check if first answer is equals to correct answer
holder.llAns1.setTag(position);
holder.llAns1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.getTag(position);
holder.llAns1.setBackgroundResource(R.drawable.que_correct_back);
holder.llAns2.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns3.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns4.setBackgroundResource(R.drawable.round_strock_green);
allAns.add(userlist.get(position).get("ans1"));
correctans = userlist.get(position).get("correctans");
if(holder.tvans1.getText().toString().equals(correctans)){
CorrectAns.add(userlist.get(position).get("ans1"));
}else {
try {
wrongAns.add(userlist.get(position).get("ans1"));
CorrectAns.remove(position);
}catch (IndexOutOfBoundsException e){}
}
}
});
Check if secound answer is equals to correct answer
holder.llAns2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.llAns2.setTag(position);
holder.llAns2.setBackgroundResource(R.drawable.que_correct_back);
holder.llAns1.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns3.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns4.setBackgroundResource(R.drawable.round_strock_green);
correctans = userlist.get(position).get("correctans");
if(holder.tvans2.getText().equals(correctans)){
CorrectAns.add(userlist.get(position).get("ans2"));
}else {
try {
wrongAns.add(userlist.get(position).get("ans2"));
CorrectAns.remove(correctans);
}catch (IndexOutOfBoundsException e){}
}
}
});
Check if third answer is equals to correct answer
holder.llAns3.setTag(position);
holder.llAns3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.llAns3.setBackgroundResource(R.drawable.que_correct_back);
holder.llAns2.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns1.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns4.setBackgroundResource(R.drawable.round_strock_green);
correctans = userlist.get(position).get("correctans");
if(holder.tvans3.getText().equals(correctans)){
CorrectAns.add(userlist.get(position).get("ans3"));
}else {
try {
wrongAns.add(userlist.get(position).get("ans3"));
CorrectAns.remove(correctans);
}catch (IndexOutOfBoundsException e){}
}
}
});
Check if forth answer is equals to correct answer
holder.llAns4.setTag(position);
holder.llAns4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.llAns4.setBackgroundResource(R.drawable.que_correct_back);
holder.llAns3.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns2.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns1.setBackgroundResource(R.drawable.round_strock_green);
correctans = userlist.get(position).get("correctans");
if(holder.tvans4.getText().equals(correctans)){
CorrectAns.add(userlist.get(position).get("ans4"));
Log.d("corrAns1",String.valueOf(CorrectAns));
}else {
try {
wrongAns.add(userlist.get(position).get("ans1"));
CorrectAns.remove(correctans);
}catch (IndexOutOfBoundsException e){}
Log.d("corrAns2",String.valueOf(CorrectAns));
}
}
});
}
You cannot use a viewpager inside a recyclerview. If you want to use horizontal swiping inside a recyclerview then you will have to use a recyclerview inside a recyclerview. This child recyclerview will be have a horizontal layout.
This question precisely addresses that.
For the 2nd issue:
Add the following lines
holder.llAns2.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns1.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns3.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns4.setBackgroundResource(R.drawable.round_strock_green);
below this line:
if(position == userlist.size() - 1){
holder.submitbtn.setVisibility(View.VISIBLE);
}
for 3rd issue:
//add this below your adapter class
private ArrayList selectedOptions;
in your onbindviewholder add this below holder.llAns4.setBackgroundResource(R.drawable.round_strock_green)
if(selectedOptions.get(position) != null){
switch (selectedOptions.get(position)){
case 1:
holder.llAns1.setBackgroundResource(R.drawable.que_correct_back);
break;
case 2:
holder.llAns2.setBackgroundResource(R.drawable.que_correct_back);
break;
case 3:
holder.llAns3.setBackgroundResource(R.drawable.que_correct_back);
break;
case 4:
holder.llAns4.setBackgroundResource(R.drawable.que_correct_back);
break;
}
}
I have answered 3 things now. Please ask new stackoverflow question for further issues.
I'm a beginner in Android Studio, and I want to make a C button in calc with 2 functions.
How do I do that on one tap the C button erases only one number, and on hold erase all numbers in TextView?
findViewById(R.id.btnClear).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtScreen.setText(""); // Clear the screen
// Reset all the states and flags
lastNumeric = false;
stateError = false;
lastDot = false;
You can set an onClickListener and onLongClickListener to achieve this.
cButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String currentText = textView.getText().toString();
if(currentText.length >= 2){
currentText = currentText.substring(0, currentText.length - 2);
}else{
currentText = "";
}
textView.setText(currentText);
}
});
cButton.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
textView.setText("");
return true;
}
});
Have you looked in to the onClick() and onLongPress() methods?
cancelButton.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
myTextView.setText("");
return true;
}
});
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
removeOneCharacter();
}
});
private void removeOneCharacter()
{
String textViewValue = myTextView.getText().toString();
if (textViewValue != null && textViewValue.length >= 2)
{
myTextView.setText(textViewValue.substring(0, textViewValue.length() - 2);
}
}
The onClick() method removes on char from the TextView at a time. The onClick() method however removes the entire String from the TextView but only considering if there is already 2 or more characters to prevent an Exception from occurring due to the upper-bound of the substring.
An improvement here could be to add another if function that checks the character length within the Long press and performs a clear if there is only one character remaining.
How can i change background color of my layout by clicking on a Button ?
This is my code :
Button color_change;
LinearLayout layout;
int value = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
color_change = (Button)findViewById(R.id.color_btn);
layout = (LinearLayout)findViewById(R.id.LL);
color_change.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (value == 1) {
layout.setBackgroundColor(Color.RED);
}
else if (value == 2) {
layout.setBackgroundColor(Color.BLUE);
}
else if (value == 3) {
layout.setBackgroundColor(Color.MAGENTA);
}
else if (value == 4) {
layout.setBackgroundColor(Color.DKGRAY);
value = 0;
}
value++;
}
});
But i want replace if else condition with other code , because this code is complex and i want short code.
So any one suggest me, how can i short my code ?
You can try this
put all color into a array and in onclick method get particular color from the array and set it like
int color[]=new int[]{Color.BLUE,Color.RED,Color.GRAY};
color_change.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (value <color.length) {
layout.setBackgroundColor(color[value]);
}
value++;
}
});
There is no magic. Use the switch instead of else ifs. For the added clarity, you might consider having constants or something as values to value, so that you don't have to deal with hard-coded integers.
You could use an array to store the data:
int[] colors = {Color.RED, Color.BLUE, Color.BLACK};
int index = value % colors.length;
layout.setBackgroundColor(colors[index]);
value++;
You will use like this...
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
switch(value)
{
case 1:layout.setBackgroundColor(Color.RED);
break;
case 2:layout.setBackgroundColor(Color.BLUE);
break;
case 3: layout.setBackgroundColor(Color.MAGENTA);
break;
case 4:layout.setBackgroundColor(Color.DKGRAY);
value = 0;
break;
}
value++;
}
});
A list of colors you want to use
List<int> colorsList = new List<int>();
colorsList.add(Color.RED);
colorsList.add(Color.WHITE);
colorsList.add(Color.BLUE);
colorsList.add(Color.GREEN);
//here you can add other colors to list
Iterator<int> colorIterator = colorsList.iterator();
function to get next color
int getNextColor()
{
if(colorIterator.hasNext())
{
return colorIterator.next();
}
else
{
colorIterator = colorsList.iterator();
return colorIterator.next();
}
}
and here is your listener
color_change.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int color = getNextColor();
layout.setBackgroundColor(color);
}
});
I have one Linearlayout - totalincome and another TableLayout normalincometable, which should appear just below the totalincome. normalincometable will be invisible when the program runs. When the user clicks on "totalincome" the table should display. If the user clicks on "totalincome again", the table should disappear. I have tried this code, but It didnt work.
totalincome.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
int x =0;
// TODO Auto-generated method stub
if (x==0)
{
normalincometable.setVisibility(View.VISIBLE);
x=1;
}
else
{
normalincometable.setVisibility(View.GONE);
x=0;
}
});
}
From this code, I can make the table visible in first click but It doesnt disappear in next click. Are there any options ?
Try this:
#Override
public void onClick(View v) {
if(normalincometable.getVisibility() == View.VISIBLE) {
normalincometable.setVisibility(View.GONE);
} else {
normalincometable.setVisibility(View.VISIBLE);
}
}
You have declared int x =0; inside onClick method. So, when ever onClick is called, it assigns 0 to "x". Declare it outside at class scope.
because you have define x in the button click code so whenever button click it set to 0. define x outside the button click scope.
try this way:put x variable outside the button onclick() or defined x globally
int x =0;
totalincome.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (x==0)
{
normalincometable.setVisibility(View.VISIBLE);
x=1;
}
else
{
normalincometable.setVisibility(View.GONE);
x=0;
}
});
}
Use like this:
int x =0;
totalincome.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (x==0)
{
normalincometable.setVisibility(View.VISIBLE);
x=1;
}
else
{
normalincometable.setVisibility(View.GONE);
x=0;
}
});
}
Try this
Boolean isFirstTimeClicked=true;
totalincome.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (isFirstTimeClicked)
{
normalincometable.setVisibility(View.VISIBLE);
}
else
{
normalincometable.setVisibility(View.GONE);
}
isFirstTimeClicked=!isFirstTimeClicked;
});
}
and in your code you have declared int x =0; inside onClick method. So, when ever onClick is called, it assigns 0 to "x". Declare it outside at class scope.
Easiest Method is
button.setVisibility(View.VISIBLE == button.getVisibility() ? View.GONE:View.VISIBLE);
How to change the android button after 2 clicks ?
the first time to change button i will use this code
{
public void onClick(View v) {
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed));
}
}
I want to change the button view again after pressing it one more time
how can i do that ?
Perhaps do it like this:
int count = 0;
public void onClick(View v) {
count++;
if(count == 2){
count = 0;
b.setBackgroundDrawable(getResources()
.getDrawable(R.drawable.menubuttonpressed));
}
}
This will set the background after every 2nd click on your button (view).
private int clickCount =0;
public void onClick(View v) {
if (clickCount==0) {
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed));
} else {
// do something else
}
clickCount++;
}
Well, one way is to keep a counter.
numberOfClicks = 0;
...
public void onClick(View v) {
...
if(numberOfClicks==0)
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed0));
else if(numberofClicks==1)
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed1));
...
numberofClicks++;
}