How to fill a huge TableLayout without causing timeout ARNs? - android

I have a TableLayout with some hundreds of tablerows.
I'm filling it with a thread, which notifies to a handler when a row is created, and the handler adds the row in the UI thread. But the problem is that in some cases some users are reporting ARN due to:
Input dispatching timed out (Waiting to send key event because the
focused window has not finished processing all of the input events
that were previously delivered to it. Outbound queue length: 0. Wait
queue length: 1.)
The Android OS handles creating a ANR when it detects the UI/Main thread has been blocking for 5 seconds.
I thought using AsyncTask but is going to be deprecated in Android 11.
This is my thread:
private class PopulateTableThread extends Thread {
private Context context;
private ArrayList<Player> players;
public PopulateTableThread(Context context, ArrayList<Player> players) {
super();
this.context = context;
this.players = players;
}
public void run() {
for (int i=0; i<players.size(); i++) {
TableRow row = new TableRow(context);
row.setLayoutParams(rowParams);
row.setBackgroundResource(R.drawable.table_row_not_selected);
String strings[] = {players.get(i).getName(),
"" + players.get(i).getTeam().getName(),
"" + players.get(i).getPositionInString(context),
"" + players.get(i).getYearsOfContractFormatted(),
"" + players.get(i).getValue() + " M€",
"" + players.get(i).getSalary() + " M€",
"" + players.get(i).getAge(),
"" + players.get(i).getBaseLevel()};
for (int j = 0; j < strings.length; j++) {
TextView text = new TextView(context);
text.setBackgroundResource(R.drawable.table_cell_background);
if (j == 0) {
text.setLayoutParams(nameParams);
text.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
text.setPadding((int) getResources().getDimension(R.dimen.spacing_small), 0, (int) getResources().getDimension(R.dimen.spacing_small), 0);
text.setMaxLines(1);
text.setEllipsize(TextUtils.TruncateAt.END);
}else if (j == 1) {
text.setLayoutParams(teamNameParams);
text.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
text.setPadding((int) getResources().getDimension(R.dimen.spacing_small), 0, (int) getResources().getDimension(R.dimen.spacing_small), 0);
text.setMaxLines(1);
text.setEllipsize(TextUtils.TruncateAt.END);
} else if (j == 2 || j == 3) {
text.setLayoutParams(positionAndContractParams);
text.setGravity(Gravity.CENTER);
} else if (j == 4) {
text.setLayoutParams(valueParams);
text.setGravity(Gravity.CENTER);
} else if (j == 5) {
text.setLayoutParams(salaryParams);
text.setGravity(Gravity.CENTER);
} else if (j == 6 || j == 7) {
text.setLayoutParams(ageAndLevelParams);
text.setGravity(Gravity.CENTER);
}
if (j == 3 && strings[3].split("/")[0].equals("1")) {
text.setBackgroundResource(R.drawable.table_cell_background_danger);
}
text.setMinHeight((int) getResources().getDimension(R.dimen.cell_height));
text.setText(strings[j]);
row.addView(text);
}
ProgressBar levelProgress = new ProgressBar(new ContextThemeWrapper(context, R.style.ProgressBar), null, 0);
levelProgress.setBackgroundResource(R.drawable.table_cell_background);
levelProgress.setLayoutParams(levelBarParams);
levelProgress.setMinimumHeight((int) getResources().getDimension(R.dimen.cell_height));
levelProgress.setProgress(players.get(i).getBaseLevel());
levelProgress.setPadding((int) getResources().getDimension(R.dimen.spacing_small), 0, (int) getResources().getDimension(R.dimen.spacing_small), 0);
row.addView(levelProgress);
final int finalIndex = i;
row.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (searchProgressBar.getVisibility() == View.VISIBLE) {
return;
}
SoundEffectsManager.getInstance().play(context, SoundEffectsManager.button);
displaySignDialog(players.get(finalIndex));
}
});
Message message = new Message();
message.what = HANDLER_ROW_CREATED;
message.obj = row;
updateTableHandler.sendMessage(message);
}
Message message = new Message();
message.what = HANDLER_TABLE_FILLED;
updateTableHandler.sendMessage(message);
}
}
And this is the handler that it's updating the ui thread when the thread has created a new TableRow:
updateTableHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
switch (msg.what){
case HANDLER_ROW_CREATED:
TableRow row = (TableRow) msg.obj;
tableLayout.addView(row);
break;
case HANDLER_TABLE_FILLED:
tableLayout.setAlpha(1f);
searchProgressBar.setVisibility(View.GONE);
break;
}
}
};
How can i avoid these ARN reports?
Trying to do David Wasser solution, but with same problem:
private class PopulateTableThread extends Thread {
private Context context;
private ArrayList<Player> players;
public PopulateTableThread(Context context, ArrayList<Player> players) {
super();
this.context = context;
this.players = players;
}
public void run() {
Log.d("XXX_SeekerActivity","filling table with "+players.size()+" players");
for (int i=0; i<players.size(); i++) {
final String strings[] = {players.get(i).getName(),
"" + players.get(i).getTeam().getName(),
"" + players.get(i).getPositionInString(context),
"" + players.get(i).getYearsOfContractFormatted(),
"" + players.get(i).getValue() + " M€",
"" + players.get(i).getSalary() + " M€",
"" + players.get(i).getAge(),
"" + players.get(i).getBaseLevel()};
final int finalI = i;
runOnUiThread(new Runnable() {
#Override
public void run() {
TableRow row = new TableRow(context);
row.setLayoutParams(rowParams);
row.setBackgroundResource(R.drawable.table_row_not_selected);
for (int j = 0; j < strings.length; j++) {
TextView text = new TextView(context);
text.setBackgroundResource(R.drawable.table_cell_background);
if (j == 0) {
text.setLayoutParams(nameParams);
text.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
text.setPadding((int) getResources().getDimension(R.dimen.spacing_small), 0, (int) getResources().getDimension(R.dimen.spacing_small), 0);
text.setMaxLines(1);
text.setEllipsize(TextUtils.TruncateAt.END);
}else if (j == 1) {
text.setLayoutParams(teamNameParams);
text.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
text.setPadding((int) getResources().getDimension(R.dimen.spacing_small), 0, (int) getResources().getDimension(R.dimen.spacing_small), 0);
text.setMaxLines(1);
text.setEllipsize(TextUtils.TruncateAt.END);
} else if (j == 2 || j == 3) {
text.setLayoutParams(positionAndContractParams);
text.setGravity(Gravity.CENTER);
} else if (j == 4) {
text.setLayoutParams(valueParams);
text.setGravity(Gravity.CENTER);
} else if (j == 5) {
text.setLayoutParams(salaryParams);
text.setGravity(Gravity.CENTER);
} else if (j == 6 || j == 7) {
text.setLayoutParams(ageAndLevelParams);
text.setGravity(Gravity.CENTER);
}
if (j == 2) {
switch (players.get(finalI).getPosition()) {
case Player.PLAYER_POSITION_GOALKEEPER:
text.setBackgroundResource(R.drawable.table_cell_background_position_gk);
break;
case Player.PLAYER_POSITION_DEFENDER:
text.setBackgroundResource(R.drawable.table_cell_background_position_def);
break;
case Player.PLAYER_POSITION_MIDFIELDER:
text.setBackgroundResource(R.drawable.table_cell_background_position_mid);
break;
case Player.PLAYER_POSITION_FORWARD:
text.setBackgroundResource(R.drawable.table_cell_background_position_fw);
break;
}
}
if (j == 3 && strings[3].split("/")[0].equals("1")) {
text.setBackgroundResource(R.drawable.table_cell_background_danger);
}
text.setMinHeight((int) getResources().getDimension(R.dimen.cell_height));
text.setText(strings[j]);
row.addView(text);
}
ProgressBar levelProgress = new ProgressBar(new ContextThemeWrapper(context, R.style.ProgressBar), null, 0);
levelProgress.setBackgroundResource(R.drawable.table_cell_background);
levelProgress.setLayoutParams(levelBarParams);
levelProgress.setMinimumHeight((int) getResources().getDimension(R.dimen.cell_height));
levelProgress.setProgress(players.get(finalI).getBaseLevel());
levelProgress.setPadding((int) getResources().getDimension(R.dimen.spacing_small), 0, (int) getResources().getDimension(R.dimen.spacing_small), 0);
row.addView(levelProgress);
final int finalIndex = finalI;
row.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (searchProgressBar.getVisibility() == View.VISIBLE) {
return;
}
SoundEffectsManager.getInstance().play(context, SoundEffectsManager.button);
displaySignDialog(players.get(finalIndex));
}
});
row.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(final View view, MotionEvent motionEvent) {
if (searchProgressBar.getVisibility() == View.VISIBLE) {
return true;
}
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
delayColorizeHandler.postDelayed(new Runnable() {
#Override
public void run() {
Util.getInstance().colorizeBackgroundCheckingApiLevel(view);
}
}, Util.WAIT_TIME_BEFORE_TOUCH_COLORIZE);
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
delayColorizeHandler.removeCallbacksAndMessages(null);
Util.getInstance().clearBackgroundColorFilter(view);
break;
}
return false;
}
});
tableLayout.addView(row);
}
});
}
runOnUiThread(new Runnable() {
#Override
public void run() {
tableLayout.setAlpha(1f);
searchProgressBar.setVisibility(View.GONE);
}
});
}
}

You can't create and manipulate UI elements (TableRow, TextView, etc.) on a background thread. All this stuff needs to be done on the main (UI) thread directly. You are probably creating object locks on parts of the UI framework that is blocking the main (UI) thread and causing your ANR.
Have your background thread loop over your players and get the data for each one. Then it can post a Runnable to the main (UI) thread (once for each player) with the player data and in the Runnable (which will run on the main (UI) thread) you can create the UI elements and add them to your layout.
By doing this in a loop in a background thread, you won't block the UI thread.

Related

Recursive(Tower Hanoi) UI for android

`[I want to change the UI through the recursive function.
I want to implement UI through recursive function. When you run it, you should see the process of changing the UI according to the value, but if you insert hanoi(), the process does not come out and the value is printed at once. I think I need to change the move, but it's the same if I put sleep() in the move, and I tried Thread, Handler, AsyncTask, but the results are the same. I'd appreciate your help.
public class Disk3 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
class BackgroundThread extends Thread {
boolean running = false;
#Override
public void run() {
running = true;
while (running) {
runOnUiThread(new Runnable() {
#Override
public void run() {
move(3, 1, 3);
}
});
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
btn_move.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BackgroundThread thread = new BackgroundThread();
thread.start();
}
});
public void move(int N, int start, int to) {
if (start == 1) {
for (int i = 0; i <= 2; i++) {
if (list_tv_diskA[i].getVisibility() == View.VISIBLE) {
startTv = list_tv_diskA[i];
startTv.setVisibility(View.INVISIBLE);
Log.d("TAG", "list_tv_disk A :: " + i);
break;
}
}
}
if (start == 2) {
for (int j = 0; j <= 2; j++) {
if (list_tv_diskB[j].getVisibility() == View.VISIBLE) {
startTv = list_tv_diskB[j];
startTv.setVisibility(View.INVISIBLE);
Log.d("TAG", "list_tv_disk B :: " + j);
break;
}
}
}
if (start == 3) {
for (int k = 0; k <= 2; k++) {
if (list_tv_diskC[k].getVisibility() == View.VISIBLE) {
startTv.setText(toTv.getText());
startTv = list_tv_diskC[k];
startTv.setVisibility(View.INVISIBLE);
Log.d("TAG", "list_tv_disk C :: " + k);
break;
}
}
}
if (to == 1) {
for (int i = 2; i >= 0; i--) {
if (list_tv_diskA[i].getVisibility() == View.INVISIBLE) {
toTv = list_tv_diskA[i];
toTv.setText(startTv.getText());
toTv.setVisibility(View.VISIBLE);
Log.d("TAG", "list_tv_disk B :: " + i);
break;
}
}
}
if (to == 2) {
for (int j = 2; j >= 0; j--) {
if (list_tv_diskB[j].getVisibility() == View.INVISIBLE) {
toTv = list_tv_diskB[j];
toTv.setText(startTv.getText());
toTv.setVisibility(View.VISIBLE);
Log.d("TAG", "list_tv_disk Bda :: " + j);
break;
}
}
}
if (to == 3) {
for (int k = 2; k >= 0; k--) {
if (list_tv_diskC[k].getVisibility() == View.INVISIBLE) {
toTv = list_tv_diskC[k];
toTv.setText(startTv.getText());
toTv.setVisibility(View.VISIBLE);
Log.d("TAG", "list_tv_disk C :: " + k);
break;
}
}
}
public void Hanoi(int N, int start, int to, int via) {
if (N == 1) {
move(1, start, to);
return;
} else {
Hanoi(N - 1, start, via, to);
move(N, start, to);
Hanoi(N - 1, via, to, start);
}
}

Getting incorrect id for child item click

I am getting incorrect id for child item click.
Fragment Class
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View itemView = inflater.inflate(R.layout.fragment_checklist_groups_description, container, false);
fragmentManager = getActivity().getSupportFragmentManager();
mLinearListView = (LinearLayout) itemView.findViewById(R.id.linear_ListView);
//=========================================================================
for (int i = 0; i < 2; i++) {
V_ChecklistParentItemModel v_checklistParentItemModel = new V_ChecklistParentItemModel();
v_checklistParentItemModel.setParentGroupID("" + (i + 1));
v_checklistParentItemModel.setParentGroupName("Group Name " + i);
if (i == 0) {
v_checklistParentItemModel.setHasSubGroup(false);
ArrayList<V_ChecklistChildItemModel> tempV_checklistChildItemModelArrayList = new ArrayList<>();
for (int j = 0; j < 3; j++) {
V_ChecklistChildItemModel v_checklistChildItemModel = new V_ChecklistChildItemModel();
v_checklistChildItemModel.setChildItemQuestionID("" + (j + 1));
v_checklistChildItemModel.setChildItemQuestionName("Description of Question " + (j + 1));
v_checklistChildItemModel.setChildQuestionID("" + index);
index++;
tempV_checklistChildItemModelArrayList.add(v_checklistChildItemModel);
}
v_checklistParentItemModel.setV_checklistChildItemModelArrayList(tempV_checklistChildItemModelArrayList);
} else {
v_checklistParentItemModel.setHasSubGroup(true);
ArrayList<V_ChecklistSubGroupModel> tempV_checklistSubGroupModelArrayList = new ArrayList<>();
for (int j = 0; j < 2; j++) {
V_ChecklistSubGroupModel v_checklistSubGroupModel = new V_ChecklistSubGroupModel();
v_checklistSubGroupModel.setSubGroupID("" + (j + 1));
if (j == 0) {
v_checklistSubGroupModel.setSubGroupName("Sub Group Name 2a");
ArrayList<V_ChecklistChildItemModel> tempV_checklistChildItemModelArrayList = new ArrayList<>();
for (int k = 0; k < 2; k++) {
V_ChecklistChildItemModel v_checklistChildItemModel = new V_ChecklistChildItemModel();
v_checklistChildItemModel.setChildItemQuestionID("" + (k + 1));
v_checklistChildItemModel.setChildItemQuestionName("Description of Question " + (k + 1));
v_checklistChildItemModel.setChildQuestionID("" + index);
index++;
tempV_checklistChildItemModelArrayList.add(v_checklistChildItemModel);
}
v_checklistSubGroupModel.setV_checklistChildItemModelArrayList(tempV_checklistChildItemModelArrayList);
} else {
v_checklistSubGroupModel.setSubGroupName("Sub Group Name 2b");
ArrayList<V_ChecklistChildItemModel> tempV_checklistChildItemModelArrayList = new ArrayList<>();
for (int k = 0; k < 3; k++) {
V_ChecklistChildItemModel v_checklistChildItemModel = new V_ChecklistChildItemModel();
v_checklistChildItemModel.setChildItemQuestionID("" + (k + 1));
v_checklistChildItemModel.setChildItemQuestionName("Description of Question " + (k + 1));
v_checklistChildItemModel.setChildQuestionID("" + index);
index++;
tempV_checklistChildItemModelArrayList.add(v_checklistChildItemModel);
}
v_checklistSubGroupModel.setV_checklistChildItemModelArrayList(tempV_checklistChildItemModelArrayList);
}
tempV_checklistSubGroupModelArrayList.add(v_checklistSubGroupModel);
}
v_checklistParentItemModel.setV_checklistSubGroupModelArrayList(tempV_checklistSubGroupModelArrayList);
}
v_checklistParentItemModelArrayList.add(v_checklistParentItemModel);
}
//=========================================================================
//Adds data into first row
for (int i = 0; i < v_checklistParentItemModelArrayList.size(); i++) {
Log.v("I : ", " " + i);
LayoutInflater listInflater = null;
listInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mLinearView = listInflater.inflate(R.layout.custom_cardview_checklist_groups_description_main_parent_item, null);
final TextView mProductName = (TextView) mLinearView.findViewById(R.id.textViewName);
final RelativeLayout mImageArrowFirst = (RelativeLayout) mLinearView.findViewById(R.id.rlFirstArrow);
final LinearLayout mLinearScrollSecond = (LinearLayout) mLinearView.findViewById(R.id.linear_scroll);
//checkes if menu is already opened or not
if (isFirstViewClick == false) {
mLinearScrollSecond.setVisibility(View.GONE);
mImageArrowFirst.setBackgroundResource(R.drawable.next_disable_icon);
} else {
mLinearScrollSecond.setVisibility(View.VISIBLE);
mImageArrowFirst.setBackgroundResource(R.drawable.arw_down);
}
//Handles onclick effect on list item
mImageArrowFirst.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (isFirstViewClick == false) {
isFirstViewClick = true;
mImageArrowFirst.setBackgroundResource(R.drawable.arw_down);
mLinearScrollSecond.setVisibility(View.VISIBLE);
} else {
isFirstViewClick = false;
mImageArrowFirst.setBackgroundResource(R.drawable.next_disable_icon);
mLinearScrollSecond.setVisibility(View.GONE);
}
return false;
}
});
final String name = v_checklistParentItemModelArrayList.get(i).getParentGroupName();
mProductName.setText(name);
if (v_checklistParentItemModelArrayList.get(i).isHasSubGroup()) {
//Adds data into second row
for (int j = 0; j < v_checklistParentItemModelArrayList.get(i).getV_checklistSubGroupModelArrayList().size(); j++) {
Log.v("J : ", " " + j);
LayoutInflater inflater2 = null;
inflater2 = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mLinearView2 = inflater2.inflate(R.layout.custom_cardview_checklist_groups_description_child_parent_item, null);
TextView mSubItemName = (TextView) mLinearView2.findViewById(R.id.textViewTitle);
final RelativeLayout mLinearSecondArrow = (RelativeLayout) mLinearView2.findViewById(R.id.linearSecond);
final RelativeLayout mImageArrowSecond = (RelativeLayout) mLinearView2.findViewById(R.id.rlSecondArrow);
final LinearLayout mLinearScrollThird = (LinearLayout) mLinearView2.findViewById(R.id.linear_scroll_third);
final LinearLayout linearLayoutMain = (LinearLayout) mLinearView2.findViewById(R.id.llMain);
if (i == 0) {
mLinearSecondArrow.setBackgroundColor(getResources().getColor(R.color.main_parent));
mImageArrowSecond.setVisibility(View.GONE);
linearLayoutMain.setPadding(20, 0, 0, 0);
} else {
mLinearSecondArrow.setBackgroundColor(getResources().getColor(R.color.child_parent));
mImageArrowSecond.setVisibility(View.VISIBLE);
if (i == 1) {
linearLayoutMain.setPadding(20, 8, 0, 0);
} else {
linearLayoutMain.setPadding(15, 8, 0, 0);
}
}
//checkes if menu is already opened or not
if (isSecondViewClick == false) {
mLinearScrollThird.setVisibility(View.GONE);
mImageArrowSecond.setBackgroundResource(R.drawable.next_disable_icon);
} else {
mLinearScrollThird.setVisibility(View.VISIBLE);
mImageArrowSecond.setBackgroundResource(R.drawable.arw_down);
}
//Handles onclick effect on list item
if (i == 0) {
final int finalI1 = i;
final int finalJ1 = j;
mLinearSecondArrow.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Bundle bundle = new Bundle();
bundle.putString("groupName", v_checklistParentItemModelArrayList.get(finalI1).getParentGroupName());
bundle.putInt("itemCount", 3);
bundle.putSerializable("alldata", checklist_groupNamePojoArrayList);
pageNumber = 0;
bundle.putInt("pageNumber", pageNumber);
ChecklistGroupsQuestionsMainDialogFragment checklistGroupsQuestionsMainDialogFragment = new ChecklistGroupsQuestionsMainDialogFragment();
checklistGroupsQuestionsMainDialogFragment.setArguments(bundle);
checklistGroupsQuestionsMainDialogFragment.setTargetFragment(current, 101);
checklistGroupsQuestionsMainDialogFragment.show(fragmentManager, getResources().getString(R.string.sd_project_list_screen_name));
return false;
}
});
} else {
mImageArrowSecond.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (isSecondViewClick == false) {
isSecondViewClick = true;
mImageArrowSecond.setBackgroundResource(R.drawable.arw_down);
mLinearScrollThird.setVisibility(View.VISIBLE);
} else {
isSecondViewClick = false;
mImageArrowSecond.setBackgroundResource(R.drawable.next_disable_icon);
mLinearScrollThird.setVisibility(View.GONE);
}
return false;
}
});
}
final String catName = v_checklistParentItemModelArrayList.get(i).getV_checklistSubGroupModelArrayList().get(j).getSubGroupName();
mSubItemName.setText(catName);
//Adds items in subcategories
for (int k = 0; k < v_checklistParentItemModelArrayList.get(i).getV_checklistSubGroupModelArrayList().get(j).getV_checklistChildItemModelArrayList().size(); k++) {
Log.v("K : ", " " + k);
LayoutInflater inflater3 = null;
inflater3 = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mLinearView3 = inflater3.inflate(R.layout.custom_cardview_checklist_groups_description_child_item, null);
TextView mItemName = (TextView) mLinearView3.findViewById(R.id.textViewItemName);
final String itemName = v_checklistParentItemModelArrayList.get(i).getV_checklistSubGroupModelArrayList().get(j).getV_checklistChildItemModelArrayList().get(k).getChildItemQuestionName();
mItemName.setText(itemName);
mLinearScrollThird.addView(mLinearView3);
final int finalI = i;
final int finalJ = j;
final int finalK = k;
mLinearScrollThird.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Bundle bundle = new Bundle();
bundle.putString("groupName", v_checklistParentItemModelArrayList.get(finalI).getParentGroupName());
bundle.putInt("itemCount", v_checklistParentItemModelArrayList.get(finalI).getV_checklistSubGroupModelArrayList().get(finalJ).getV_checklistChildItemModelArrayList().size());
// bundle.putSerializable("questionData", group2bQuestionList);
bundle.putSerializable("alldata", checklist_groupNamePojoArrayList);
pageNumber = finalI + finalJ + finalK + 1;
bundle.putInt("pageNumber", pageNumber);
Log.v("====ugugugu==", "===ijhbjhbjh===");
Toast.makeText(getActivity(), "Page No. " + v_checklistParentItemModelArrayList.get(finalI).getV_checklistSubGroupModelArrayList().get(finalJ).getV_checklistChildItemModelArrayList().get(finalK).getChildQuestionID(), Toast.LENGTH_LONG).show();
ChecklistGroupsQuestionsMainDialogFragment checklistGroupsQuestionsMainDialogFragment = new ChecklistGroupsQuestionsMainDialogFragment();
checklistGroupsQuestionsMainDialogFragment.setArguments(bundle);
checklistGroupsQuestionsMainDialogFragment.setTargetFragment(current, 101);
checklistGroupsQuestionsMainDialogFragment.show(fragmentManager, getResources().getString(R.string.check_list_groups_question_screen_name));
return false;
}
});
}
mLinearScrollSecond.addView(mLinearView2);
}
mLinearListView.addView(mLinearView);
} else {
//Adds items in subcategories
for (int k = 0; k < v_checklistParentItemModelArrayList.get(i).getV_checklistChildItemModelArrayList().size(); k++) {
Log.v("K : ", " " + k);
LayoutInflater inflater3 = null;
inflater3 = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mLinearView3 = inflater3.inflate(R.layout.custom_cardview_checklist_groups_description_child_item, null);
final LinearLayout mLinearScrollThird = (LinearLayout) mLinearView3.findViewById(R.id.linear_scroll_third);
TextView mItemName = (TextView) mLinearView3.findViewById(R.id.textViewItemName);
final String itemName = v_checklistParentItemModelArrayList.get(i).getV_checklistChildItemModelArrayList().get(k).getChildItemQuestionName();
mItemName.setText(itemName);
final int finalI = i;
final int finalK = k;
mLinearScrollThird.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Bundle bundle = new Bundle();
bundle.putString("groupName", v_checklistParentItemModelArrayList.get(finalI).getParentGroupName());
bundle.putInt("itemCount", v_checklistParentItemModelArrayList.get(finalI).getV_checklistChildItemModelArrayList().size());
bundle.putSerializable("alldata", checklist_groupNamePojoArrayList);
bundle.putInt("pageNumber", 1);
Toast.makeText(getActivity(), "Page No. " + v_checklistParentItemModelArrayList.get(finalI).getV_checklistChildItemModelArrayList().get(finalK).getChildQuestionID(), Toast.LENGTH_LONG).show();
ChecklistGroupsQuestionsMainDialogFragment checklistGroupsQuestionsMainDialogFragment = new ChecklistGroupsQuestionsMainDialogFragment();
checklistGroupsQuestionsMainDialogFragment.setArguments(bundle);
checklistGroupsQuestionsMainDialogFragment.setTargetFragment(current, 101);
checklistGroupsQuestionsMainDialogFragment.show(fragmentManager, getResources().getString(R.string.check_list_groups_question_screen_name));
return false;
}
});
mLinearScrollSecond.addView(mLinearView3);
}
mLinearListView.addView(mLinearView);
}
}
//==============================================================================
return itemView;
}
I have assigned Child Question ID using setChildQuestionID(). For Group Name 0, I get 0, 1, 2 on each child click. However, for Group Name 1, I get 4 for Sub Group Name 2a & 7 for Sub Group Name 2b for each of there child item click. Ideally, it should return whatever I set for them.
Do you mean that you want to get 0, 1, 2 for child clicks in group name 1? As far as I can tell, you're going to have to re-set the value of index to 0 to get that. At least, assuming you've defined index = 0 somewhere outside your outermost for-loop (I can't see where in your code you've set the value of index). Right now in Group 0, you have:
for (int k = 0; k < 2; k++) {
V_ChecklistChildItemModel v_checklistChildItemModel = new V_ChecklistChildItemModel();
v_checklistChildItemModel.setChildItemQuestionID("" + (k + 1));
v_checklistChildItemModel.setChildItemQuestionName("Description of Question " + (k + 1));
v_checklistChildItemModel.setChildQuestionID("" + index);
index++;
If index was 0 to start with, the value of index is now greater than 0. You don't set it back to 0, so when you get to the next cycle through your top-level for loop, the value of index is already > 0. If you want to start over at index = 0 in Group 1, you'll have to set the value of index back to 0.
In other words, you are returning exactly what you set for the questionIDs, but I think you're setting a number that you didn't want to set.

Animating the Loading... textview continually

Im trying to animate the three dots of "Loading..." textview as follows,
Handler handler = new Handler();
for (int i = 100; i <= 3500; i =i+100) {
final int finalI = i;
handler.postDelayed(new Runnable() {
#Override
public void run() {
if(finalI %300 == 0){
loadigText.setText("Loading.");
}else if(finalI %200 == 0){
loadigText.setText("Loading..");
}else if(finalI %100 == 0){
loadigText.setText("Loading...");
}
}
}, i);
The problem is that ,
1. Im unable to animate it infinitely till the dialog is visible.
2. Im unable to reduce the speed of Three dots animation,
How can I be able to sort this out
Example:
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
int count = 0;
#Override
public void run() {
count++;
if (count == 1)
{
textView.setText("Loading.");
}
else if (count == 2)
{
textView.setText("Loading..");
}
else if (count == 3)
{
textView.setText("Loading...");
}
if (count == 3)
count = 0;
handler.postDelayed(this, 2 * 1000);
}
};
handler.postDelayed(runnable, 1 * 1000);

Check all the view's background in an if statement in Android?

I have many buttons(81) from the left and right part of my layout. All in all, I have 162 buttons. I put those buttons to Button[] and I handle it properly.
private int[] right_lung = { R.id.btn_right_106, R.id.btn_right_113,
R.id.btn_right_114, R.id.btn_right_115, R.id.btn_right_116, R.id.btn_right_121,
R.id.btn_right_122, R.id.btn_right_123, R.id.btn_right_124, R.id.btn_right_125,
R.id.btn_right_129, R.id.btn_right_130, R.id.btn_right_131, R.id.btn_right_132,
R.id.btn_right_133, R.id.btn_right_134, R.id.btn_right_137, R.id.btn_right_138,
R.id.btn_right_139, R.id.btn_right_140, R.id.btn_right_141, R.id.btn_right_142,
R.id.btn_right_143, R.id.btn_right_145, R.id.btn_right_146, R.id.btn_right_147,
R.id.btn_right_148, R.id.btn_right_149, R.id.btn_right_150, R.id.btn_right_151,
R.id.btn_right_152, R.id.btn_right_153, R.id.btn_right_154, R.id.btn_right_155,
R.id.btn_right_156, R.id.btn_right_157, R.id.btn_right_158, R.id.btn_right_159,
R.id.btn_right_160, R.id.btn_right_161, R.id.btn_right_162, R.id.btn_right_163,
R.id.btn_right_164, R.id.btn_right_165, R.id.btn_right_166, R.id.btn_right_167,
R.id.btn_right_168, R.id.btn_right_169, R.id.btn_right_170, R.id.btn_right_171,
R.id.btn_right_172, R.id.btn_right_173, R.id.btn_right_174, R.id.btn_right_175,
R.id.btn_right_176, R.id.btn_right_177, R.id.btn_right_178, R.id.btn_right_179,
R.id.btn_right_180, R.id.btn_right_181, R.id.btn_right_182, R.id.btn_right_183,
R.id.btn_right_184, R.id.btn_right_185, R.id.btn_right_186, R.id.btn_right_187,
R.id.btn_right_188, R.id.btn_right_189, R.id.btn_right_190, R.id.btn_right_191,
R.id.btn_right_192, R.id.btn_right_194, R.id.btn_right_195, R.id.btn_right_196,
R.id.btn_right_197, R.id.btn_right_198, R.id.btn_right_199, R.id.btn_right_200,
R.id.btn_right_205, R.id.btn_right_206, R.id.btn_right_207 };
private Button[] btn_right = new Button[right_lung.length];
private int[] left_lung = { R.id.btn_left_7, R.id.btn_left_13, R.id.btn_left_14,
R.id.btn_left_15, R.id.btn_left_16, R.id.btn_left_20, R.id.btn_left_21,
R.id.btn_left_22, R.id.btn_left_23, R.id.btn_left_24, R.id.btn_left_27,
R.id.btn_left_28, R.id.btn_left_29, R.id.btn_left_30, R.id.btn_left_31,
R.id.btn_left_32, R.id.btn_left_34, R.id.btn_left_35, R.id.btn_left_36,
R.id.btn_left_37, R.id.btn_left_38, R.id.btn_left_39, R.id.btn_left_40,
R.id.btn_left_41, R.id.btn_left_42, R.id.btn_left_43, R.id.btn_left_44,
R.id.btn_left_45, R.id.btn_left_46, R.id.btn_left_47, R.id.btn_left_48,
R.id.btn_left_49, R.id.btn_left_50, R.id.btn_left_51, R.id.btn_left_52,
R.id.btn_left_53, R.id.btn_left_54, R.id.btn_left_55, R.id.btn_left_56,
R.id.btn_left_57, R.id.btn_left_58, R.id.btn_left_59, R.id.btn_left_60,
R.id.btn_left_61, R.id.btn_left_62, R.id.btn_left_63, R.id.btn_left_64,
R.id.btn_left_65, R.id.btn_left_66, R.id.btn_left_67, R.id.btn_left_68,
R.id.btn_left_69, R.id.btn_left_70, R.id.btn_left_71, R.id.btn_left_72,
R.id.btn_left_73, R.id.btn_left_74, R.id.btn_left_75, R.id.btn_left_76,
R.id.btn_left_77, R.id.btn_left_78, R.id.btn_left_79, R.id.btn_left_80,
R.id.btn_left_81, R.id.btn_left_82, R.id.btn_left_83, R.id.btn_left_84,
R.id.btn_left_85, R.id.btn_left_86, R.id.btn_left_87, R.id.btn_left_88,
R.id.btn_left_89, R.id.btn_left_90, R.id.btn_left_91, R.id.btn_left_92,
R.id.btn_left_93, R.id.btn_left_94, R.id.btn_left_95, R.id.btn_left_98,
R.id.btn_left_99, R.id.btn_left_100, };
private Button[] btn_left = new Button[left_lung.length];
Whenever I click on the button, just like in mine sweeper game, many buttons are randomly opened. And when it is opened, I am changing its background into R.drawable.affected. The goal of the game is to open all the buttons in left and right. My question is this, how can I check if all of the buttons are set in R.drawable.affected? Because after that, I will execute a method that will congratulate the user. Thanks in advance.
EDIT:
for (int i = 0; i < btn_right.length; i++) {
final int a = i;
int counter_total_affected = 0;
btn_right[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (counter == 1) {
right_lung.add(a);
btn_right[a].setBackgroundResource(R.drawable.affected);
counter_total_affected++;
} else if (counter == 2) {
for (int i = 0; i < 2; i++) {
int n = i;
right_lung.add(a + n);
}
try {
for (int i = 0; i < 2; i++) {
int n = i;
btn_right[a + n].setBackgroundResource(R.drawable.affected);
counter_total_affected++;
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (counter == 3) {
right_lung.add(a);
btn_right[a].setBackgroundResource(R.drawable.affected);
counter_total_affected++;
} else if (counter == 4) {
for (int i = 0; i < 25; i++) {
int n = i;
right_lung.add(a + n);
}
try {
for (int i = 0; i < 25; i++) {
int n = i;
btn_right[a + n].setBackgroundResource(R.drawable.affected);
counter_total_affected++;
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (counter == ...) {
//statements...
} else if (counter_total_affected == (btn_left.length + btn_right.length)) {
//CONGRATULATORY METHOD
}
counter++;
}
}
Increment the counter each time you change background of button and compare its value with length of button array. if both are same that means, all button backgrounds are set.
Try this :
for (int i = 0; i < btn_right.length; i++) {
final int b = i;
btn_right[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if btn_right[i].getDrawable().getConstantState().equals
(getResources().getDrawable(R.drawable.affected).getConstantState()))
{
if(counter == btn_right.length){
//Congratulate user...
}
}else{
btn_right[b].setBackgroundResource(R.drawable.affected);
counter++;
}
}
}
U can set Tag:
for (int i = 0; i < btn_right.length; i++) {
final int b = i;
btn_right[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (counter == 1) {
right_affected.add(b);
btn_right[b].setBackgroundResource(R.drawable.affected);
btn_right[b].setTag('1');
} else {
//some stuff here...
}
}
}
after congratulate the user set its Tag to 0.
EDIT:
ArrayList<Integer> arrayofId =new ArrayList<Integer>();
#Override
public void onClick(View v) {
if (counter == 1) {
right_affected.add(b);
btn_right[b].setBackgroundResource(R.drawable.affected);
arrayofId.add(b);//need to convert int to Integer.
} else {
//some stuff here...
}
}
To congratulate:
for(int i=0;i<arrayofId.size();i++)
{
// you can get here id of effected buttons
}

TextView OnTouch is not working

I have table layout in which I am adding textviews dynamically.
I m setting OnTouch listener on text view. On MotionEvent.ACTION_DOWN I show popup and on MotionEvent.ACTION_UP. I am removing that popup but I want if touch is removed from the textview then also the pop should remove I have tried with MotionEvent.ACTION_MOVE ,MotionEvent.ACTION_OUTSIDE but both are not working
public void addButton_with_filter(final Test test,
TableRow.LayoutParams rowParams, Activity context) {
ArrayList<Integer> filteredquestionno;
TableLayout tl = new TableLayout(context);
tl.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
LinearLayout filterlayout = (LinearLayout) findViewById(R.id.filterlayout);
filterlayout.removeAllViewsInLayout();
filterlayout.setBackgroundResource(R.drawable.background1);
int j = 0;
int k = 0;
filteredquestionno = getLayoutFilteredQuestionNumber(test
.getQuestions());
int size = filteredquestionno.size();
if (size == 0) {
Toast.makeText(this,
"NO QUSTION IS AVAILABLE OF THIS FILTER TYPE !",
Toast.LENGTH_LONG).show();
}
int norows = (size / 10) + 1;
{
for (int i = 0; i < norows; i++) {
TableRow currentRow = new TableRow(FilterActivity.this);
// currentRow.setBackgroundResource(R.drawable.background1);
while (j < 10 && k < size) {
j++;
final TextView qindex = new TextView(FilterActivity.this);
qindex.setGravity(Gravity.CENTER);
int index = filteredquestionno.get(k);
Question question = test.getQuestionAtIndex(index);
if (question != null) {
if (submited == true) {
if (question.isSkipped()) {
qindex.setBackgroundResource(R.drawable.skipped_answer);
} else if (question.isCorrectlyAnswered()) {
qindex.setBackgroundResource(R.drawable.correct_answer);
} else if (question.isAttempted()) {
qindex.setBackgroundResource(R.drawable.wrong_answer);
} else {
qindex.setBackgroundResource(R.drawable.unattempted_question);
}
} else if (submited == false) {
if (question.isSkipped()) {
qindex.setBackgroundResource(R.drawable.skipped_answer);
} else if (question.isAttempted()) {
qindex.setBackgroundResource(R.drawable.attempted_question);
} else {
qindex.setBackgroundResource(R.drawable.unattempted_question);
}
enter code here }
} else {
qindex.setBackgroundResource(R.drawable.unattempted_question);
}
qindex.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
qindex.setTextSize(11);
if ((index + 1) < 10) {
qindex.setText("0" + (index + 1));
} else {
qindex.setText("" + (index + 1));
}
k++;
qindex.setId(k);
qindex.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent event) {
String text = (String) qindex.getText();
String t = text.trim();
int questionindex = Integer.parseInt(t);
LayoutInflater questionbutton_inflater = (LayoutInflater) getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View questionbutton_view = questionbutton_inflater
.inflate(R.layout.filterbutton, null);
final PopupWindow questiontext_popupwindow = new PopupWindow(
questionbutton_view,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
questiontext_popupwindow
.setContentView(questionbutton_view);
TextView tv = (TextView) questionbutton_view
.findViewById(R.id.texttoadd);
tv.setText(text);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
questiontext_popupwindow.showAsDropDown(qindex,
0, -125);
}
if (event.getAction() == MotionEvent.ACTION_UP) {
questiontext_popupwindow.dismiss();
test.setLastQuestionAttempted(questionindex - 1);
FilterActivity.this.finish();
}
if (event.getAction() == MotionEvent.ACTION_MOVE) {
questiontext_popupwindow.dismiss();
}
return true;
}
});
currentRow.addView(qindex, rowParams);
}
j = 0;
tl.addView(currentRow);
}
filterlayout.addView(tl);
}
Typical mistake - you create pop-up every time MotionEvent fires. So the previously created pop-up cannot be dismissed, because you lost the link to it - another time your program enters onTouch() you will not have the link to your pop-up (because it is declared in onTouch()). Make your pop-up a field of OnTouchListener. And create it ONLY ONCE.
Step-by-step:
MotionEvent.ACTION_DOWN fires - you create and show pop-up, it is on screen
MotionEvent.ACTION_UP fires - you create new pop-up and dismissing it, but not the old one

Categories

Resources