tutorial: JSON Get data from MySql
I followed this tutorial and everything went good. Even though the error not thrown, my app doesn't crash. But how to disable button after view the last array in JSON?
public void onClick(View v) {
if (v == nextBtn) {
if(TRACK<marks.length()){
TRACK++;
}
showData();
}
if (v == prevBtn) {
if(TRACK>0){
TRACK--;
}
showData();
}
}
EDIT : 1
Try below code:
public void onClick(View v) {
if (v == nextBtn) {
if(TRACK<marks.length()){
if(TRACK==0)
{
//Enable previous button because you are incrementing the count
prevBtn.setEnabled(true);
}
TRACK++;
if(TRACK==(marks.length()-1))
{
//Disable next button
v.setEnabled(false);
}
}
showData();
}
if (v == prevBtn) {
if(TRACK>0){
if(TRACK==(marks.length()-1))
{
//Enable next button because you are decrementing the count
nextBtn.setEnabled(true);
}
TRACK--;
if(TRACK==0)
{
//Disable previous button
v.setEnabled(false);
}
}
showData();
}
}
EDIT 2:
for making prevButton disabled by default do it like this, Where you are doing findViewById()
prevButton = ()findViewById(); // Your findViewById code..!!
prevButton.setEnabled(false);
Hope this helps..!!
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.
I have android apps:
Send data to Textview (working)
check if data is there getdata from text view(working)
Else if data not in Textview check again ??
Thanks.
button1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
//enter text to TextView (tv1) need some time
EntertextToTextView();
//label1
if (tv1.getText().toString() != "")
{
tv2.setText(tv1.getText().toString());
}
else {
// goto label1;
}
}
}
});
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);
}
});
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++;
}