Change image and text after click - java

Hello guys I'm working on simple application, which basically has an button function, which when pressed, displays the progress bar and then switch the image. I have problem, that this code switched the image immediately after clicked the button. I need to switch the image at the end of progress bar.
This is the code I have so far:
package com.example.testapp;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
ImageView image;
Button btnStartProgress;
ProgressDialog progressBar;
private int progressBarStatus = 0;
private Handler progressBarHandler = new Handler();
private long fileSize = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
image = (ImageView) findViewById(R.id.imageView1);
btnStartProgress = (Button) findViewById(R.id.button1);
btnStartProgress.setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
progressBar = new ProgressDialog(v.getContext());
progressBar.setCancelable(true);
progressBar.setMessage("Scanning ...");
progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.show();
progressBarStatus = 0;
fileSize = 0;
new Thread(new Runnable() {
public void run() {
while (progressBarStatus < 100) {
progressBarStatus = doSomeTasks();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Update the progress bar
progressBarHandler.post(new Runnable() {
public void run() {
TextView textView = (TextView) findViewById(R.id.textView1);
textView.setText("Completed!");
textView.setTextColor(0xFF00FF00);
image.setImageResource(R.drawable.yes_small);
progressBar.setProgress(progressBarStatus);
}
});
}
if (progressBarStatus >= 100) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
progressBar.dismiss();
}
}
}).start();
}
});
}
public int doSomeTasks() {
while (fileSize <= 1000000) {
fileSize++;
if (fileSize == 100000) {
return 10;
} else if (fileSize == 200000) {
return 20;
} else if (fileSize == 300000) {
return 30;
} else if (fileSize == 400000) {
return 40;
} else if (fileSize == 500000) {
return 50;
} else if (fileSize == 600000) {
return 60;
} else if (fileSize == 700000) {
return 70;
} else if (fileSize == 800000) {
return 80;
} else if (fileSize == 900000) {
return 90;
}
}
return 100;
}
}

The problem, I suspect, is in the logic of doSomeTasks(). You are looking for some really specific sizes here. What if the size fileSize is not an exact match to any of the values in your if-else ladder ?
Try changing the logic there and see if that makes a difference :)

Move the textView.set... code into the if (progressBarStatus >= 100) block. You are currently updating the text to "Completed!" every time the progress bar updates to any value.

Related

I need to perform add to cart functionality with the option of increment or decrement quantity of item

product quantity changes button but when I click the add to cart button and change the visibility to gone others + , - buttons visibility to visible show on the screen then I click any of this two-button and their listener didn't work and after restarting the app both + , - buttons start working correctly. One more thing, I need my notification badge value shouldn't be changed if one product is already in the cart then changing its quantity, but in my case value change if I increase the quantity or decrease the quantity of the product. Thanks in advance
HomeFragment
package colon.semi.com.dealMall.fragments;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.smarteist.autoimageslider.SliderAnimations;
import com.smarteist.autoimageslider.SliderView;
import java.util.ArrayList;
import java.util.List;
import colon.semi.com.dealMall.R;
import colon.semi.com.dealMall.Utils.AppClass;
import colon.semi.com.dealMall.Utils.Constants;
import colon.semi.com.dealMall.adapters.DealsSliderAdapter;
import colon.semi.com.dealMall.adapters.DevicesParentRecyclerAdapter;
import colon.semi.com.dealMall.adapters.SliderAdapterExample;
import colon.semi.com.dealMall.adapters.TopSellingAdapter;
import colon.semi.com.dealMall.dataModels.CartDataTable;
import colon.semi.com.dealMall.dataModels.CartResponse;
import colon.semi.com.dealMall.dataModels.HomeDataModel;
import colon.semi.com.dealMall.dataModels.ImagesListDM;
import colon.semi.com.dealMall.dataModels.Points;
import colon.semi.com.dealMall.dataModels.Product;
import colon.semi.com.dealMall.dataModels.SliderImagesResponse;
import colon.semi.com.dealMall.dataModels.TopSellingResponse;
import colon.semi.com.dealMall.viewModels.CartViewModel;
import colon.semi.com.dealMall.viewModels.HomeFragmentViewModel;
import colon.semi.com.dealMall.viewModels.PointsViewModel;
public class HomeFragment extends Fragment {
HomeFragmentViewModel homeFragmentViewModel;
View view;
SliderView sliderView;
SliderView imageSliderDeals;
RecyclerView recyclerView;
private ArrayList<String> listTitleRecycler = new ArrayList<>();
DevicesParentRecyclerAdapter homeParentRecyclerAdapter;
boolean topSellingComplete = false;
boolean latestComplete = false;
boolean allproductsComplete = false;
HomeDataModel homeDataModel = new HomeDataModel();
CartViewModel cartViewModel;
ProgressDialog progressDialog;
RecyclerView recylerTimeDeal;
TopSellingAdapter timeDealAdapter;
int[] imgResLounge = {R.drawable.ic_get_free, R.drawable.ic_seasonal_deals,
R.drawable.ic_grocery_deals,
R.drawable.ic_stationary_deals};
//CategoryNames
String[] imgNamesLounge = {"Men\nLounge", "Women\nLounge", "Cosmetic\nLounge", "Garments\nLounge"};
//Liststudent ERP OBJECT
ArrayList<ImagesListDM> loungeList = new ArrayList<>();
SharedPreferences sharedPreferences;
private TextView totalPointsTextView;
private TextView totalPrice;
private PointsViewModel pointsViewModel;
private View viewParent;
private boolean isLogin;
private int userId;
private List<CartDataTable> cartDataTableList = new ArrayList<>();
private int pos;
private int pQun;
private int pId;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_home, container, false);
initViews();
initListeners();
getSliderImages();
// getCartItem();
isLogin = sharedPreferences.getBoolean(Constants.USER_IS_LOGIN, false);
if (Constants.topSellingProduct != null) {
if (!Constants.topSellingProduct.isEmpty()) {
recylerTimeDeal.setLayoutManager(new LinearLayoutManager(getActivity(),
LinearLayoutManager.HORIZONTAL, false));
timeDealAdapter = new TopSellingAdapter(Constants.topSellingProduct, cartDataTableList,
getActivity());
recylerTimeDeal.setAdapter(timeDealAdapter);
clickListner(view);
}
} else {
getTopSellingProducts(1);
}
if (Constants.latestProduct != null) {
if (!Constants.latestProduct.isEmpty()) {
homeDataModel.setLatestList(Constants.latestProduct);
homeParentRecyclerAdapter = new DevicesParentRecyclerAdapter(getActivity(),
homeDataModel);
recyclerView.setAdapter(homeParentRecyclerAdapter);
}
} else {
getLatestProducts(1);
}
if (Constants.allProducts != null) {
if (!Constants.allProducts.isEmpty()) {
homeDataModel.setAllProductList(Constants.allProducts);
homeParentRecyclerAdapter = new DevicesParentRecyclerAdapter(getActivity(),
homeDataModel);
recyclerView.setAdapter(homeParentRecyclerAdapter);
}
} else {
getAllProducts(1);
}
return view;
}
#Override
public void onResume() {
isLogin = sharedPreferences.getBoolean(Constants.USER_IS_LOGIN, false);
// if (isLogin) {
userId = sharedPreferences.getInt(Constants.USER_ID, 0);
cartViewModel.getCartDataTableLiveData(userId).observe(getActivity(), new
Observer<List<CartDataTable>>() {
#Override
public void onChanged(List<CartDataTable> cartDataTables) {
if (cartDataTables != null) {
cartDataTableList = cartDataTables;
}
});
//}
super.onResume();
}
private void clickListner(View view1) {
timeDealAdapter.setOnItemClickListener(new TopSellingAdapter.OnItemClickListener() {
#Override
public void onAddtoCartClick(View view, Product product) {
Log.e("onAddtoCartClick", "called");
userId = sharedPreferences.getInt(Constants.USER_ID, 0);
progressDialog = new ProgressDialog(getActivity());
progressDialog.setIndeterminate(true);
progressDialog.setMessage("Please Wait...");
progressDialog.setCancelable(false);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
viewParent = (View) view.getParent();
progressDialog.dismiss();
int quantity = 1;
cartViewModel.addProductToCart(userId, product.getProduct_id(),
quantity).observe(getActivity(), new Observer<CartResponse>() {
#Override
public void onChanged(CartResponse cartResponse) {
if (cartResponse != null) {
progressDialog.dismiss();
if (cartResponse.getMessage().equals("Product added successfully")) {
progressDialog.dismiss();
CartDataTable cartDataTable = new CartDataTable();
cartDataTable.setProductId(product.getProduct_id());
cartDataTable.setProductQuantity(1);
cartDataTable.setUserId(userId);
cartViewModel.insertCart(cartDataTable);
progressDialog.invalidateOptionsMenu();
} else {
Toast.makeText(getActivity(), cartResponse.getMessage(),
Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getActivity(), "Something went wrong",
Toast.LENGTH_SHORT).show();
}
}
});
}
#Override
public void updateCartData(int productId, int quantity, int position, View view) {
pId = productId;
pQun = quantity;
pos = position;
viewParent = (View) view.getParent();
Log.d("updateCartData2", pId + " " + pQun);
CartDataTable cartDataTable = cartViewModel.getCart2(productId, userId);
userId = sharedPreferences.getInt(Constants.USER_ID, 0);
Log.d("userId2", String.valueOf(userId));
if (cartDataTable != null) {
if (pQun == 0) {
cartViewModel.deleteCart(cartDataTable);
} else {
CartDataTable cartDataTable1 = new CartDataTable();
cartDataTable1.setProductQuantity(pQun);
cartDataTable1.setProductId(pId);
cartDataTable1.setCartId(cartDataTable.getCartId());
cartDataTable1.setUserId(userId);
cartViewModel.updateCart(cartDataTable1);
Log.d("cart2", "updated");
}
}
}
});
}
private void getSliderImages() {
if (AppClass.isOnline(getActivity())) {
homeFragmentViewModel.getSliderImages().observe(getActivity(), new
Observer<SliderImagesResponse>() {
#Override
public void onChanged(SliderImagesResponse sliderImagesResponse) {
if (sliderImagesResponse.getStatus() == 1) {
SliderAdapterExample adapter = new SliderAdapterExample(getContext(),
sliderImagesResponse.getData());
sliderView.setSliderAdapter(adapter);
sliderView.setSliderTransformAnimation(SliderAnimations.SIMPLETRANSFORMATION);
sliderView.setAutoCycleDirection(SliderView.AUTO_CYCLE_DIRECTION_BACK_AND_FORTH);
sliderView.setIndicatorSelectedColor(Color.WHITE);
sliderView.setIndicatorUnselectedColor(Color.GRAY);
sliderView.setScrollTimeInSec(4); //set scroll delay in seconds :
sliderView.startAutoCycle();
} else {
progressDialog.dismiss();
Toast.makeText(getContext(), ""+sliderImagesResponse.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
});
} else {
AppClass.offline(getActivity());
}
}
private void getTopSellingProducts(int page) {
if (AppClass.isOnline(getActivity())) {
progressDialog.show();
homeFragmentViewModel.getTopSellingProducts(page).observe(getActivity(), new
Observer<TopSellingResponse>() {
#Override
public void onChanged(TopSellingResponse topSellingResponse) {
if (topSellingResponse.getStatus() == 1) {
progressDialog.dismiss();
topSellingComplete = true;
Constants.topSellingProduct = topSellingResponse.getData();
recylerTimeDeal.setLayoutManager(new LinearLayoutManager(getActivity(),
LinearLayoutManager.HORIZONTAL, false));
timeDealAdapter = new TopSellingAdapter(topSellingResponse.getData(),
cartDataTableList, getActivity());
recylerTimeDeal.setAdapter(timeDealAdapter);
clickListner(view);
if (latestComplete || allproductsComplete) {
homeParentRecyclerAdapter.notifyDataSetChanged();
} else {
homeParentRecyclerAdapter = new DevicesParentRecyclerAdapter(getActivity(),
homeDataModel);
recyclerView.setAdapter(homeParentRecyclerAdapter);
}
} else {
progressDialog.dismiss();
Toast.makeText(getContext(), ""+topSellingResponse.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
});
} else {
AppClass.offline(getActivity());
}
}
private void getLatestProducts(int page) {
if (AppClass.isOnline(getActivity())) {
progressDialog.show();
homeFragmentViewModel.getLatestProducts(page).observe(getActivity(), new
Observer<TopSellingResponse>() {
#Override
public void onChanged(TopSellingResponse topSellingResponse) {
if (topSellingResponse.getStatus() == 1) {
progressDialog.dismiss();
latestComplete = true;
Constants.latestProduct = topSellingResponse.getData();
homeDataModel.setLatestList(topSellingResponse.getData());
if(topSellingComplete || allproductsComplete) {
homeParentRecyclerAdapter.notifyDataSetChanged();
} else {
homeParentRecyclerAdapter = new DevicesParentRecyclerAdapter(getActivity(),
homeDataModel);
recyclerView.setAdapter(homeParentRecyclerAdapter);
}
} else {
progressDialog.dismiss();
Toast.makeText(getContext(), ""+topSellingResponse.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
});
} else {
AppClass.offline(getActivity());
}
}
private void getAllProducts(int page) {
if (AppClass.isOnline(getActivity())) {
progressDialog.show();
homeFragmentViewModel.getAllProducts(page).observe(getActivity(), new
Observer<TopSellingResponse>() {
#Override
public void onChanged(TopSellingResponse topSellingResponse) {
if (topSellingResponse.getStatus() == 1) {
progressDialog.dismiss();
allproductsComplete = true;
Constants.allProducts = topSellingResponse.getData();
homeDataModel.setAllProductList(topSellingResponse.getData());
if(topSellingComplete || latestComplete) {
homeParentRecyclerAdapter.notifyDataSetChanged();
} else {
homeParentRecyclerAdapter = new DevicesParentRecyclerAdapter(getActivity(),
homeDataModel);
recyclerView.setAdapter(homeParentRecyclerAdapter);
}
} else {
progressDialog.dismiss();
Toast.makeText(getContext(), ""+topSellingResponse.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
});
} else {
AppClass.offline(getActivity());
}
}
private void getPoints(int userId) {
pointsViewModel.getUserPointsById(userId).observe(getActivity(), new Observer<Points>() {
#Override
public void onChanged(Points points) {
progressDialog.dismiss();
if (points.getStatus() == 1) {
progressDialog.dismiss();
if (points.getTotalPoints() != null && points.getTotalDiscount() != null) {
totalPointsTextView.setText(points.getTotalPoints() + "");
totalPrice.setText(points.getTotalDiscount() + "");
} else {
Toast.makeText(getActivity(), "No Data Found", Toast.LENGTH_SHORT).show();
}
} else {
progressDialog.dismiss();
Toast.makeText(getActivity(), points.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
private void initViews() {
homeFragmentViewModel = new ViewModelProvider(this).get(HomeFragmentViewModel.class);
cartViewModel = new ViewModelProvider(this).get(CartViewModel.class);
sliderView = view.findViewById(R.id.imageSlider);
imageSliderDeals = view.findViewById(R.id.imageSliderDeals);
recyclerView = view.findViewById(R.id.recyclerView);
recylerTimeDeal = view.findViewById(R.id.recy_timeDeal);
totalPointsTextView = view.findViewById(R.id.pointsTextView);
totalPrice = view.findViewById(R.id.totalPrice);
pointsViewModel = new ViewModelProvider(this).get(PointsViewModel.class);
sharedPreferences = getActivity().getSharedPreferences(Constants.LOGIN_PREFERENCE,
Context.MODE_PRIVATE);
int userId = sharedPreferences.getInt(Constants.USER_ID, 0);
listTitleRecycler.add(0, "Top Selling");
listTitleRecycler.add(1, "Latest Products");
listTitleRecycler.add(2, "All Products");
homeDataModel.setTitleList(listTitleRecycler);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),
LinearLayoutManager.VERTICAL, false));
recyclerView.setHasFixedSize(true);
progressDialog = new ProgressDialog(getContext(), R.style.exitDialogTheme);
progressDialog.setIndeterminate(true);
progressDialog.setMessage("Please wait...");
progressDialog.setCancelable(false);
progressDialog.setCanceledOnTouchOutside(false);
getPoints(userId);
}
private void initListeners() {
addLoungeToList();
DealsSliderAdapter adapter = new DealsSliderAdapter(getContext(), loungeList);
imageSliderDeals.setSliderAdapter(adapter);
imageSliderDeals.setSliderTransformAnimation(SliderAnimations.SIMPLETRANSFORMATION);
imageSliderDeals.setAutoCycleDirection(SliderView.AUTO_CYCLE_DIRECTION_BACK_AND_FORTH);
imageSliderDeals.setScrollTimeInSec(4); //set scroll delay in seconds :
imageSliderDeals.startAutoCycle();
}
public void addLoungeToList() {
int i = 0;
for (String name : imgNamesLounge) {
ImagesListDM imagesListDM = new ImagesListDM();
imagesListDM.setImg(imgResLounge[i]);
imagesListDM.setNameImg(name);
loungeList.add(imagesListDM);
i++;
}
}
}
TopSellingAdapter
package colon.semi.com.dealMall.adapters;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import java.util.ArrayList;
import java.util.List;
import colon.semi.com.dealMall.R;
import colon.semi.com.dealMall.Utils.Constants;
import colon.semi.com.dealMall.dataModels.CartDataTable;
import colon.semi.com.dealMall.dataModels.Product;
import colon.semi.com.dealMall.uiActivities.ProductDetailActivity;
public class
TopSellingAdapter extends RecyclerView.Adapter<TopSellingAdapter.SingleItemRowHolder> {
private static OnItemClickListener mlistener; //just a variable
private ArrayList<Product> topSellingList;
private Context context;
private List<CartDataTable> cartDataTables;
int[] quantity;int[] pid;
int size;
public TopSellingAdapter(ArrayList<Product> topSellingList, List<CartDataTable> cartDataTableList, Context context) {
this.topSellingList = topSellingList;
this.cartDataTables = cartDataTableList;
this.context = context;
}
#Override
public void onBindViewHolder(#NonNull SingleItemRowHolder holder, final int position) {
SingleItemRowHolder viewHolder = (SingleItemRowHolder) holder;
Product product = topSellingList.get(position);
RequestOptions options = new RequestOptions()
.centerInside()
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.skipMemoryCache(true)
.priority(Priority.HIGH);
Glide
.with(context)
.load(topSellingList.get(position).getImage_name())
.apply(options)
.into(viewHolder.image);
viewHolder.name.setText(topSellingList.get(position).getProduct_title());
viewHolder.price.setText(topSellingList.get(position).getPrice());
viewHolder.linear_label.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, ProductDetailActivity.class);
intent.putExtra(Constants.PRODUCT_OBJECT, product);
context.startActivity(intent);
}
});
holder.addtocart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mlistener!=null && position != RecyclerView.NO_POSITION){
viewHolder.addtocart.setVisibility(View.GONE);
viewHolder.cartLayout.setVisibility(View.VISIBLE);
viewHolder.quanTv.setText("1");
mlistener.onAddtoCartClick(v, topSellingList.get(position));
}
}
});
if (cartDataTables != null && cartDataTables.size() > 0) {
for (int i = 0; i < cartDataTables.size(); i++) {
if (cartDataTables.get(i).getProductId() == product.getProduct_id()) {
viewHolder.addtocart.setVisibility(View.GONE);
viewHolder.cartLayout.setVisibility(View.VISIBLE);
viewHolder.quanTv.setText(String.valueOf(cartDataTables.get(i).getProductQuantity()));
}
}
size = cartDataTables.size();
quantity = new int[size];
pid = new int[size];
}
// if (cartDataTables != null && cartDataTables.size() > 0) {
// int size = cartDataTables.size();
// int[] quantity = new int[size];
// int[] pid = new int[size];
Log.d("CartDataSize", String.valueOf(size));
for (int i = 0; i < size; i++) {
quantity[i] = cartDataTables.get(i).getProductQuantity();
pid[i] = cartDataTables.get(i).getProductId();
Log.d("CartData1", quantity[i] + " " + pid[i]);
}
holder.addProdButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < size; i++) {
quantity[i]++;
viewHolder.quanTv.setText(String.valueOf(quantity[i]));
}
// int id = pid[size - 1];
//int q1 = quantity[size - 1];
// Log.d("q1", String.valueOf(q1));
for (int i = 0; i < size; i++) {
if (cartDataTables.get(i).getProductId() == product.getProduct_id()) {
// q1 = q1 + 1;
//Log.d("q2", String.valueOf(q1));
//Toast.makeText(context, String.valueOf(q1), Toast.LENGTH_SHORT).show();
mlistener.updateCartData(pid[i], quantity[i], holder.getAdapterPosition(), v);
}
else {
mlistener.updateCartData(pid[i], quantity[i], holder.getAdapterPosition(), v);
}
}
}
});
holder.minusProdButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < size; i++) {
if (quantity[i] < 1) {
holder.cartLayout.setVisibility(View.GONE);
holder.addtocart.setVisibility(View.VISIBLE);
} else {
quantity[i]--;
if (quantity[i] < 1) {
holder.cartLayout.setVisibility(View.GONE);
holder.addtocart.setVisibility(View.VISIBLE);
}
viewHolder.quanTv.setText(String.valueOf(quantity[i]));
}
}
Toast.makeText(context, "Already added", Toast.LENGTH_SHORT).show();
for (int i = 0; i < size; i++) {
if (cartDataTables.get(i).getProductId() == product.getProduct_id()) {
mlistener.updateCartData(pid[i], quantity[i], holder.getAdapterPosition(),
v);
}
else {
mlistener.updateCartData(pid[i], quantity[i], holder.getAdapterPosition(), v);
}
}
}
});
}
#NonNull
#Override
public SingleItemRowHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.temp_top_selling_products, viewGroup, false);
SingleItemRowHolder categoriesListVH = new SingleItemRowHolder(view);
return categoriesListVH;
}
#Override
public int getItemCount() {
return (null != topSellingList ? topSellingList.size() : 0);
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.mlistener = listener;
}
public interface OnItemClickListener {
void onAddtoCartClick(View view, Product product);
void updateCartData(int productId, int quantity, int position, View view);
}
public class SingleItemRowHolder extends RecyclerView.ViewHolder {
ImageView image;
TextView name;
TextView price;
LinearLayout linear_label;
Button addtocart;
Button addProdButton;
Button minusProdButton;
TextView quanTv;
RelativeLayout cartLayout;
public SingleItemRowHolder(View view) {
super(view);
image = view.findViewById(R.id.image);
name = view.findViewById(R.id.name);
price = view.findViewById(R.id.price);
linear_label = view.findViewById(R.id.linear_label);
addtocart = view.findViewById(R.id.addToCartButtonHome);
cartLayout = view.findViewById(R.id.rel_prodCart_home);
addProdButton = view.findViewById(R.id.prodAddButtonHome);
quanTv = view.findViewById(R.id.productQuanTextViewHome);
minusProdButton = view.findViewById(R.id.prodMinusButtonHome);
}
}
}
first time when adapter constructor called there is no data in the cartDataTables list and because of my condition if (cartDataTables != null && cartDataTables.size() > 0), when I click add to cart button they found no data in the cartDataTables list because I didn't call notifyDataSetChanged() method or call the adapter constructor again, and my addtocart click listener is inside this condition if (cartDataTables != null && cartDataTables.size() > 0) that's why add to cart button click listner didn't work.
solution: call the adapter constructor again.

How to solve stack memory problems?

iam trying to build an android app which makes users to do some calculation in a time limit. The code worked well until i divided the code into two parts and created another class for doing other task .
I have imported all the corressponding packages and class files to the new class .There were no errors in the code but when i run the app it crashes .I tried changing the code many times but no use. Code works well when i combine all the code into a single class.
the error i get is "java.lang.StackOverflowError: stack size 8MB on line number "
**MainActivity.java**
package e.nani.firstattempt;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Vibrator;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
public int a1;//random num 1
public int a2;//random num 2;
public TextView textview;
public Button b1;
public Button b2;
public Button b3;
public Button b4;
public int option1;
public int option2;
public int option3;
public int option4;
public int score=0;
TextView scoreid;
int numberofquestions=10;
TextView time;
public int answers[]=new int[4];
Logic c=new Logic();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview=(TextView)findViewById(R.id.sum);
b1=(Button)findViewById(R.id.option1);
b2=(Button)findViewById(R.id.option2);
b3=(Button)findViewById(R.id.option3);
b4=(Button)findViewById(R.id.option4);
time=(TextView)findViewById(R.id.timer);
scoreid=(TextView)findViewById(R.id.scoreid) ;
scoreid.setText((0+"/"+numberofquestions));
c.operatio();
timer.start();
}
public void operation(View V)
{
try{
switch(V.getId()) {
case R.id.option1:
if (b1.getText().equals(Integer.toString(option4))) {
score = score + 1;
c.operatio();
scoreid.setText((score +"/"+ numberofquestions));
} else {
Toast.makeText(this, "wrong answer", Toast.LENGTH_SHORT).show();
Vibrator vibrator=(Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(500);
c.operatio();
}
break;
case R.id.option2:
if (b2.getText().equals(Integer.toString(option4))) {
score = score + 1;
c.operatio();
scoreid.setText(score+"/"+ numberofquestions);
} else
{
Toast.makeText(this, "wrong answer", Toast.LENGTH_SHORT).show();
Vibrator vibrator=(Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(500);
c.operatio();
}
break;
case R.id.option3:
if (b3.getText().equals(Integer.toString(option4))) {
score = score + 1;
c.operatio();
scoreid.setText((score+"/"+ numberofquestions));
} else
{
Toast.makeText(this, "wrong answer", Toast.LENGTH_SHORT).show();
Vibrator vibrator=(Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(500);
c.operatio();
}
break;
case R.id.option4:
if (b4.getText().equals(Integer.toString(option4))) {
score = score + 1;
c.operatio();
scoreid.setText(score+"/"+ numberofquestions);
} else
{
Toast.makeText(this, "wrong answer", Toast.LENGTH_SHORT).show();
Vibrator vibrator=(Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(500);
c.operatio();
}
break;
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
CountDownTimer timer=new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
time.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
time.setText("done!");
}
};
}
Logic.java
package e.nani.firstattempt;
import java.util.Random;
class Logic {
MainActivity s=new MainActivity();
public void operatio() {
try {
Random n = new Random();
int n1 = n.nextInt(4);
int n2 = n.nextInt(4);
int n3 = n.nextInt(4);
int n4 = n.nextInt(4);
s.a1 = n.nextInt(51);
s.a2 = n.nextInt(35);
s.option1 = n.nextInt((s.a1 + s.a2) + 1);
s.option2 = n.nextInt((s.a1 + s.a2) + 1);
s.option3 = n.nextInt((s.a1 + s.a2) + 1);
s.option4 = s.a1 + s.a2;
s.answers[n1] = s.option1;
while (n2 == n1) {
n2 = n.nextInt(4);
}
while (s.option2 == s.option1 || s.option2 == s.option4) {
s.option2 = n.nextInt((s.a1 + s.a2) + 1);
}
s.answers[n2] = s.option2;
while (s.option3 == s.option2 || s.option3 == s.option1 || s.option3 == s.option4)
{
s.option3 = n.nextInt((s.a1 + s.a2) + 1);
}
while (n3 == n2 || n3 == n1)
{
n3 = n.nextInt(4);
}
s.answers[n3] = s.option3;
while (n4 == n2 || n4 == n1 || n4 == n3) {
n4 = n.nextInt(4);
}
s.answers[n4] = s.option4;
s.b1.setText(Integer.toString(s.answers[0]));
s.b2.setText(Integer.toString(s.answers[1]));
s.b3.setText(Integer.toString(s.answers[2]));
s.b4.setText(Integer.toString(s.answers[3]));
s.textview.setText(s.a1 + "+" + s.a2);
} catch (Exception e) {
e.printStackTrace();
}
}
}
The main question here is ,why is the app working fine when the code is only in main class but not working when some code is written in other class ?
ThankYou.
In your MainActivity you have a variable c with type Logic which is instantiated.
But in your Logic class has MainActivity type variable which try to instantiate MainActivity class. In a nutshell in a class A you instantiate class B which instatieates class A and so on...
By the way you cannot instantiate AppCompatActivity classes directly.
So remove MainActivity s=new MainActivity(); in your Logic.class.
Hope I could help you.

UI Thread stops running after random times

I've been having this issue for about a month now and I made a different question about it here: https://stackoverflow.com/questions/38005624/activity-stops-after-certain-time?noredirect=1#comment63454873_38005624
The issue appears to be a problem with the UI thread. After a seemingly random time of running, the UI thread appears to stop doing work. Any 'Runnable' objects passed to it with '.post()' or 'runOnUIThead()' do not get run. Even a simple log message. onClickListener objects also do not fire when the issue occurs. I've had runs where it starts happening within 2 seconds of starting, I've had runs where I make it through the entire game with no issue; all on the same code.
Here is my code if you need it:
The Activity:
package edu.rit.jacob.timeturner;
import android.app.ActivityManager;
import android.app.Application;
import android.app.Dialog;
import android.content.Intent;
import android.os.Looper;
import android.provider.ContactsContract;
import android.provider.Settings;
import android.support.annotation.StringRes;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Layout;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;
public class UniverseActivity extends AppCompatActivity {
public static final String TAG = UniverseActivity.class.getSimpleName();
private UniverseThread thread;
public UniverseThread getThread(){ return thread; }
public static int SECTORS_HEIGHT = 3;
public static int SECTORS_WIDTH = 3;
private Galaxy player;
public Galaxy getPlayer() {
return player;
}
//components:
private UniverseView surfaceView;
private ImageView playerSprite;
private ImageView otherSprite;
private TextView testText;
private SeekBar speedBar;
private ProgressBar timebar;
//Pop ups
private Dialog infoPopUp;
private Dialog startScreen;
//other Galaxies
Galaxy[] galaxies = new Galaxy[SECTORS_HEIGHT * SECTORS_WIDTH];
#Override
protected void onCreate(Bundle savedInstanceState) {
//Setup Activity
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_universe);
Runnable init = new Runnable() {
#Override
public void run() {
initInfoPopUp();
initActivityComponents();
initPlayerAndSprites();
initOtherGalaxies();
initStartScreen();
}
};
runOnUiThread(init);
thread = new UniverseThread(surfaceView.getHolder(), surfaceView);
thread.setRunning(true);
thread.start();
startScreen.setTitle("The Beginning...");
startScreen.show();
Log.d(TAG, "App launched with " + ((ActivityManager)getSystemService(ACTIVITY_SERVICE)).getLargeMemoryClass() + "MB max");
}
#Override
protected void onDestroy() {
super.onDestroy();
boolean retry = true;
while(retry){
try{
thread.join();
retry = false;
}
catch (InterruptedException e){
//try again
}
}
}
private void initStartScreen(){
startScreen = new Dialog(this);
startScreen.setContentView(R.layout.new_game_start);
((TextView)startScreen.findViewById(R.id.textView4)).setText(getString(R.string.start_game_info));
startScreen.findViewById(R.id.buttonStart).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player.setName("" + ((EditText)startScreen.findViewById(R.id.editText)).getText());
startScreen.dismiss();
}
});
}
private void initInfoPopUp(){
infoPopUp = new Dialog(this);
infoPopUp.setContentView(R.layout.info_popup);
Button infoOption1 = (Button) infoPopUp.findViewById(R.id.option1);
Button infoOption2 = (Button) infoPopUp.findViewById(R.id.option2);
infoOption1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "Option 1 clicked");
//player.incrementGas(galaxies[player.getSector() - 1].getGas());
player.merge(galaxies[player.getSector() - 1], true);
galaxies[player.getSector() - 1] = null;
infoPopUp.dismiss();
}
});
infoOption2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "Option 2 clicked");
//player.incrementGas(galaxies[player.getSector() - 1].getGas() / 2);
//galaxies[player.getSector() - 1].incrementGas(-galaxies[player.getSector() - 1].getGas() / 2);
player.merge(galaxies[player.getSector() - 1], false);
infoPopUp.dismiss();
}
});
}
private void initActivityComponents(){
surfaceView = (UniverseView) findViewById(R.id.surfaceView);
testText = (TextView) findViewById(R.id.testText);
speedBar = ((SeekBar)findViewById(R.id.seekBar));
timebar = ((ProgressBar)findViewById(R.id.progressBar));
}
private void initPlayerAndSprites(){
playerSprite = (ImageView) findViewById(R.id.playerGalaxy);
player = new Galaxy("Milky Way", true, new Velocity(), playerSprite, 25.0f, 1, this);
otherSprite = (ImageView) findViewById(R.id.otherGalaxy);
//Tapping on player galaxy
playerSprite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "Player sprite clicked, stopping time and bringing up window...");
speedBar.setProgress(0);
infoPopUp.setTitle(player.getName());
infoPopUp.findViewById(R.id.infoPicture).setBackgroundResource(R.drawable.random_temp_galaxy_01);
infoPopUp.findViewById(R.id.option1).setVisibility(View.INVISIBLE);
infoPopUp.findViewById(R.id.option2).setVisibility(View.INVISIBLE);
((TextView)infoPopUp.findViewById(R.id.infoText)).setText(player.toString());
infoPopUp.show();
}
});
//Tapping on other galaxy
otherSprite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(otherSprite.getVisibility() == View.VISIBLE){
Log.d(TAG, "Other sprite clicked, stopping time and bringing up window...");
speedBar.setProgress(0);
infoPopUp.setTitle(galaxies[player.getSector() - 1].getName());
infoPopUp.findViewById(R.id.infoPicture).setBackgroundResource(R.drawable.random_temp_galaxy_01);
infoPopUp.findViewById(R.id.option1).setVisibility(View.INVISIBLE);
infoPopUp.findViewById(R.id.option2).setVisibility(View.INVISIBLE);
((TextView)infoPopUp.findViewById(R.id.infoText)).setText(galaxies[player.getSector() - 1].toString());
infoPopUp.show();
}
}
});
}
private void initOtherGalaxies(){
for(Galaxy g: galaxies) g = null;
int i = 0, galaxiesNum = 0;
Log.d(TAG, "Populating the universe...");
while(i < galaxies.length){
double rand = Math.random();
if(rand <= 0.25){
galaxies[i] = new Galaxy("Random galaxy", new Velocity(0,0), otherSprite, 25.0f, i + 1, (Math.random()), this);
galaxiesNum += 1;
}
i += 1;
}
Log.d(TAG, "Universe populated with " + galaxiesNum + " galaxies.");
}
public int getSpeed(){
return speedBar.getProgress();
}
public void setTimeProgress(int progress){
try {
if (timebar.getProgress() != progress) {
switch (progress) {
case 10:
//((ProgressBar) findViewById(R.id.progressBar)).getProgress()
progress += 1;
//bring up new popup
break;
case 50:
//do stuff
break;
case 100:
//do end game stuff
boolean retry = true;
while (retry) {
try {
thread.join();
retry = false;
} catch (InterruptedException e) {
//try again
}
}
}
timebar.setProgress(progress);
}
}
catch (NullPointerException e){
Log.d(TAG, "Failed to grab progressBar");
e.printStackTrace();
}
}
public void changeBackground(){
Runnable changeBackground = new Runnable() {
#Override
public void run() {
//Set new background
Log.d(TAG, "Changing to background " + player.getSector());
//surfaceView.setBackgroundResource(R.drawable.background01 + (player.getSector() - 1));
testText.setText("Sector: " + player.getSector());
//Check for other galaxy
if(galaxies[player.getSector() - 1] != null){
otherSprite.setVisibility(View.VISIBLE);
}
else{
otherSprite.setVisibility(View.INVISIBLE);
}
synchronized (this) {
this.notify();
}
}
};
Log.d(TAG, "Passing background runnable to the UI thread");
synchronized (changeBackground){
runOnUiThread(changeBackground);
try {
changeBackground.wait();
}
catch (InterruptedException e){
e.printStackTrace();
}
}
}
public void checkForCollision(){
if(playerSprite.getX() + (playerSprite.getWidth() / 2) >= otherSprite.getX() &&
playerSprite.getX() + (playerSprite.getWidth() / 2) <= otherSprite.getX() + otherSprite.getWidth() &&
playerSprite.getY() + (playerSprite.getHeight() / 2) >= otherSprite.getY() &&
playerSprite.getY() + (playerSprite.getHeight() / 2) <= otherSprite.getY() + otherSprite.getHeight()){
if(otherSprite.getVisibility() == View.VISIBLE){
//switch (player.)
Runnable collide = new Runnable() {
#Override
public void run() {
speedBar.setProgress(0);
otherSprite.setVisibility(View.INVISIBLE);
((TextView)infoPopUp.findViewById(R.id.infoText)).setText("");
//infoPopUp.findViewById(R.id.infoPicture).setBackgroundResource(R.drawable.hubble_merger_01);
infoPopUp.findViewById(R.id.option1).setVisibility(View.VISIBLE);
infoPopUp.findViewById(R.id.option2).setVisibility(View.VISIBLE);
infoPopUp.setTitle("Incoming!");
infoPopUp.show();
}
};
runOnUiThread(collide);
}
}
//Set gauge
Runnable setGauge = new Runnable() {
#Override
public void run() {
((GaugeView)findViewById(R.id.gaugeView)).setTargetValue(player.getGas());
}
};
runOnUiThread(setGauge);
}
}
The Thread:
package edu.rit.jacob.timeturner;
import android.content.Context;
import android.graphics.Canvas;
import android.hardware.*;
import android.hardware.SensorManager;
import android.provider.Settings;
import android.util.Log;
import android.view.SurfaceHolder;
import android.widget.ImageView;
public class UniverseThread extends Thread {
public static final String TAG = UniverseThread.class.getSimpleName();
private double time;
public double getTime(){
return time;
}
private double speed;
public double getSpeed(){
return speed;
}
private boolean running;
public void setRunning(boolean running){
this.running = running;
}
public boolean isRunning(){
return running;
}
//Reference to the UniverseActivity
private SurfaceHolder surfaceHolder;
//Reference to the UniverseView that is the background
private UniverseView universeView;
public UniverseThread(SurfaceHolder surfaceHolder, UniverseView universeView){
super();
this.surfaceHolder = surfaceHolder;
this.universeView = universeView;
running = false;
time = 0;
}
public static float x = 0, y = 0, z = 0;
#Override
public void run(){
Log.d(TAG, "Starting Game Loop");
time = 0.0;
speed = 0.0;
//SensorManager gets SENSOR_SERVICE from android
//Accesses gravity sensor
SensorManager sm = (SensorManager) this.universeView.getContext().getSystemService(Context.SENSOR_SERVICE);
sm.registerListener(new SensorEventListener() {
#Override
public void onSensorChanged(SensorEvent event) {
x = event.values[0];
y = event.values[1];
z = event.values[2];
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}, sm.getDefaultSensor(Sensor.TYPE_GRAVITY), SensorManager.SENSOR_DELAY_GAME);
//save player, velocity, and sprite
Galaxy player = ((UniverseActivity)universeView.getContext()).getPlayer();
Velocity vel = player.getVel();
ImageView sprite = player.getSprite();
boolean changeBackground;
float timesRun = 0f;
while(running){
//every 16 milliseconds (~60 Hz)
timesRun += 1;
final String testStr = "Thread has run for " + (timesRun) + " frames.";
Runnable test = new Runnable() {
#Override
public void run() {
Log.d(TAG, testStr);
}
};
if(timesRun % 60 == 0) ((UniverseActivity) universeView.getContext()).runOnUiThread(test);
//increment the time
speed = ((UniverseActivity)universeView.getContext()).getSpeed();
time += (speed / 7200.0);
((UniverseActivity)universeView.getContext()).setTimeProgress((int) time);
//update player's velocity
((UniverseActivity)universeView.getContext()).getPlayer().getVel().incrementXV(-x / 9.81f);
((UniverseActivity)universeView.getContext()).getPlayer().getVel().incrementYV(y / 9.81f);
//update player's position
//move sprite
sprite.setX((float)(sprite.getX() + (vel.getxV() * speed / 400)));
sprite.setY((float)(sprite.getY() + (vel.getyV() * speed / 400)));
changeBackground = false;
//bottom -> top
if (sprite.getY() + (sprite.getHeight() / 2) > universeView.getHeight()) {
sprite.setY(0 - (sprite.getHeight() / 2));
sprite.setX(universeView.getWidth() / 2);
player.updateSector("BOTTOM");
changeBackground = true;
}
//right -> left
if (sprite.getX() + (sprite.getWidth() / 2) > universeView.getWidth()) {
sprite.setY(universeView.getHeight() / 2);
sprite.setX(0 - (sprite.getWidth() / 2));
player.updateSector("RIGHT");
changeBackground = true;
}
//top -> bottom
if (sprite.getY() + (sprite.getHeight() / 2) < 0) {
sprite.setY(universeView.getHeight() - (sprite.getHeight() / 2));
sprite.setX(universeView.getWidth() / 2);
player.updateSector("TOP");
changeBackground = true;
}
//left -> right
if (sprite.getX() + (sprite.getWidth() / 2) < 0) {
sprite.setY(universeView.getHeight() / 2);
sprite.setX(universeView.getWidth() - (sprite.getWidth() / 2));
player.updateSector("LEFT");
changeBackground = true;
}
if(changeBackground)
{((UniverseActivity)universeView.getContext()).changeBackground();}
((UniverseActivity)universeView.getContext()).checkForCollision();
try{
this.sleep(16L);
}
catch (InterruptedException e){
e.printStackTrace();
}
}
Log.d(TAG, "Game Loop terminated");
}
}
To be clear, no exceptions or errors are being thrown during runtime. The only messages in logcat are my own and the system messages for registering a click, for example. The application does not crash and the sprite is still perfectly controllable. The main things that do not work is the changing of backgrounds, checking for a collision, and the onClickListeners.
Has anyone run into something like this before? Any help would be appreciated.
So after having narrowed the problem down to the Runnable objects I was passing to the UI thread, I took steps to minimize the amount of time I called runOnUIThread() at first it didn't seem to have any effect but eventually I stopped having this problem and that's the only thing I can contribute it to. I might eventually do some more testing to be more concrete that that was the problem and respective solution.

Why does this code loop around with "Socket setup caught IOexception"

I am trying to troubleshoot an Android server/client set of software which appears to be logging vast amounts of logcat messages containing "Socket setup caught IOexception", so Im guessing its in a loop.
For troubleshooting purposes I installed the server and client on the same physical device and experience the same problem.
Reading the code I cant work out why this would be, I have added comments starting //STACKEXCHANGE where I see the three logcat messages getting logged. Could anyone give me some pointers, Im not a java programmer, just an enthusiastic techie trying to get this working..
The logcat logs the messages in the following order
ControlLooop starting
Socket setup caught IOexception
Socket Established
Socket Closing
controlLoopEnding
And here is the code for the client software.
package com.jrbowling.robotclient;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.util.Log;
import android.widget.ToggleButton;
import android.widget.SeekBar;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
import android.widget.EditText;
import android.widget.ImageView;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.view.Window;
import android.view.WindowManager;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.net.SocketTimeoutException;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.WifiLock;
import android.net.wifi.WifiInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.view.View.OnTouchListener;
import android.view.MotionEvent;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.net.SocketTimeoutException;
import android.app.AlertDialog;
import android.content.DialogInterface;
import java.net.SocketAddress;
import java.net.InetSocketAddress;
import android.os.Handler;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
public class MainActivity extends Activity {
Thread vidThread = null;
Thread clientThread = null;
private Socket s;
private static final String TAG = "RobotClient";
//private ToggleButton tb;
private ImageButton forward_button;
private ImageButton reverse_button;
private ImageButton right_button;
private ImageButton left_button;
private SeekBar throttle;
private ImageView imageWindow;
private ImageView connectedLED;
private ImageView signalStrengthIndicator;
private Boolean stayConnected = false;
String vidURL = "";
private String robotIP = "";
String direction = "stop";
Integer speed = 100;
Boolean robotEnabled = true;
Boolean robotConnected = false;
private Handler GUIUpdateHandler = new Handler();
private Integer signalStrength = 0;
private SharedPreferences pref;
private long timeLastPress = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set fullscreeen, black
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
setActivityBackgroundColor(0xff000000);
Log.i(TAG, "Robot client started");
initGUIComponents();
//get Robot IP from user, kick off threads and GUI updater
showIPAlert();
}
#Override
protected void onDestroy() {
Log.d(TAG,"onDestroy() called");
stayConnected = false;
socketCleanup();
super.onDestroy();
}
//#Override
//protected void onPause() {
// Log.d(TAG,"onPause() called");
// stayConnected = false;
// socketCleanup();
// super.onDestroy();
// }
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onBackPressed() {
//on back button, prompt user to press it again within 2 seconds to exit
Toast onBackPressedToast = Toast.makeText(this, "Press again within 2 seconds to confirm exit", Toast.LENGTH_SHORT);
long currentTime = System.currentTimeMillis();
if (currentTime - timeLastPress > 2000) {
onBackPressedToast.show();
timeLastPress = currentTime;
} else {
onBackPressedToast.cancel();
super.onBackPressed();
}
}
private void initGUIComponents()
{
imageWindow = (ImageView) findViewById(R.id.imageView1);
connectedLED = (ImageView) findViewById(R.id.imageViewConnectStatus);
signalStrengthIndicator = (ImageView) findViewById(R.id.ImageViewSignalStrength);
forward_button = (ImageButton) findViewById(R.id.forwardButton);
reverse_button = (ImageButton) findViewById(R.id.reverseButton);
right_button = (ImageButton) findViewById(R.id.rightButton);
left_button = (ImageButton) findViewById(R.id.leftButton);
throttle = (SeekBar)findViewById(R.id.throttleSeekbar);
throttle.setProgress(75);
forward_button.setOnTouchListener(forwardButtonListener);
reverse_button.setOnTouchListener(reverseButtonListener);
right_button.setOnTouchListener(rightButtonListener);
left_button.setOnTouchListener(leftButtonListener);
}
private void showIPAlert() {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Please enter robot IP address");
alert.setMessage("Example: 192.168.1.100");
final EditText input = new EditText(this);
alert.setView(input);
input.setText(loadIP().toString());
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
robotIP = input.getText().toString();
Log.i(TAG, "User entered IP " + robotIP);
saveIP(robotIP);
//Handler launches GUIUpdater every 1000 ms. Launch when user clicks ok.
updateGUI();
//start network thread
stayConnected = true;
//launch network thread
clientThread = new Thread(new ClientThread());
clientThread.start();
vidURL = "http://"+robotIP+":8082/shot.jpg";
vidLoop();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
appExit();
}
});
alert.show();
}
private void appExit()
{
Log.i(TAG, "Exit requested by user");
this.finish();
}
private OnTouchListener forwardButtonListener = new OnTouchListener(){
public boolean onTouch(View v, MotionEvent event) {
switch ( event.getAction() ) {
case MotionEvent.ACTION_DOWN:
//setActivityBackgroundColor(0xffff0000);
direction = "forward";
break;
case MotionEvent.ACTION_UP:
//setActivityBackgroundColor(0xff000000);
direction = "stop";
break;
}
return false;
}
};
private OnTouchListener reverseButtonListener = new OnTouchListener(){
public boolean onTouch(View v, MotionEvent event) {
switch ( event.getAction() ) {
case MotionEvent.ACTION_DOWN:
//setActivityBackgroundColor(0xffff0000);
direction = "reverse";
break;
case MotionEvent.ACTION_UP:
//setActivityBackgroundColor(0xff000000);
direction = "stop";
break;
}
return false;
}
};
private OnTouchListener rightButtonListener = new OnTouchListener(){
public boolean onTouch(View v, MotionEvent event) {
switch ( event.getAction() ) {
case MotionEvent.ACTION_DOWN:
//setActivityBackgroundColor(0xffff0000);
direction = "rotateRight";
break;
case MotionEvent.ACTION_UP:
//setActivityBackgroundColor(0xff000000);
direction = "stop";
break;
}
return false;
}
};
private OnTouchListener leftButtonListener = new OnTouchListener(){
public boolean onTouch(View v, MotionEvent event) {
switch ( event.getAction() ) {
case MotionEvent.ACTION_DOWN:
//setActivityBackgroundColor(0xffff0000);
direction = "rotateLeft";
break;
case MotionEvent.ACTION_UP:
//setActivityBackgroundColor(0xff000000);
direction = "stop";
break;
}
return false;
}
};
//save and retrieve IP address using the shared preferences framework
private void saveIP(String IP)
{
//set up shared preferences editor
pref = getApplicationContext().getSharedPreferences("RobotClient", 0);
Editor editor = pref.edit();
editor.putString("robotIP", IP);
editor.commit();
}
private String loadIP()
{
String result;
pref = getApplicationContext().getSharedPreferences("RobotClient", 0);
Editor editor = pref.edit();
result = pref.getString("robotIP", "10.20.30.43");
return result;
}
private void setActivityBackgroundColor(int color) {
//0xff00ff00 first two are transparency, then rgb
View view = this.getWindow().getDecorView();
view.setBackgroundColor(color);
}
//periodically run updater to set connection status and wifi signal strength from robot
private void updateGUI()
{
GUIUpdateHandler.postDelayed(GUIUpdater, 1000);
}
private Runnable GUIUpdater = new Runnable(){
public void run() {
//Periodically update GUI elements from sensor and other data
Log.d(TAG,"Connected is: " + robotConnected.toString());
//update connection status
if (robotConnected)
{
connectedLED.setImageResource(R.drawable.led_green);
//update the wifi signal strength indicator
if ((signalStrength == 5) || (signalStrength==4))
signalStrengthIndicator.setImageResource(R.drawable.wifi4);
if (signalStrength == 3)
signalStrengthIndicator.setImageResource(R.drawable.wifi3);
if (signalStrength == 2)
signalStrengthIndicator.setImageResource(R.drawable.wifi2);
if (signalStrength == 1)
signalStrengthIndicator.setImageResource(R.drawable.wifi1);
if (signalStrength == 0)
signalStrengthIndicator.setImageResource(R.drawable.wifi0);
}
else
{
connectedLED.setImageResource(R.drawable.led_red);
signalStrengthIndicator.setImageResource(R.drawable.wifi0);
}
if (stayConnected)
updateGUI();
}
};
public void setConnected(boolean connected) {
robotConnected = connected;
}
public void socketCleanup()
{
try {
Log.d("clientThread","Socket Closing");
if (s != null)
s.close();
setConnected(false);
} catch (IOException e) {
Log.d(TAG, "Client comm thread got IOException in socketCleanup().");
}
}
private void vidLoop() //started from GUI alert, then kept going with call me from asynctask
{
if (stayConnected)
{
ImageDownloader id = new ImageDownloader();
id.execute(vidURL);
}
}
//this very useful chunk of code is from http://www.peachpit.com/articles/article.aspx?p=1823692&seqNum=3
private class ImageDownloader extends AsyncTask<String, Integer, Bitmap>{
protected void onPreExecute(){
}
#Override
protected Bitmap doInBackground(String... params) {
//TODO Auto-generated method stub
try{
URL url = new URL(params[0]);
HttpURLConnection httpCon =
(HttpURLConnection)url.openConnection();
if(httpCon.getResponseCode() != 200)
throw new Exception("Failed to connect");
InputStream is = httpCon.getInputStream();
return BitmapFactory.decodeStream(is);
}catch(Exception e){
Log.e("Image","Failed to load image",e);
}
return null;
}
protected void onProgressUpdate(Integer... params){
//Update a progress bar here, or ignore it, it's up to you
}
protected void onPostExecute(Bitmap img){
ImageView iv = (ImageView)findViewById(R.id.imageView1);
if(iv!=null && img !=null){
iv.setImageBitmap(img);
//start next image grab
vidLoop();
}
}
protected void onCancelled(){
}
}
class ClientThread implements Runnable {
private static final int SERVERPORT = 8082;
public void run() {
Log.d("clientThread","clientThread started");
setConnected(false);
while ((!Thread.currentThread().isInterrupted()) && stayConnected)
{
controlLoop();
}
//user requested disconnect
Log.d("clientThread","clientThread ending");
}
void controlLoop()
{
BufferedReader s_input = null;
PrintWriter s_output = null;
String inputString = null;
String outputString = null;
Boolean continueLoop = true;
Log.d("clientThread","controlLoop starting");
//protocol:
//Java boolean: enabled or disabled
//Directions: stop, rotateRight, rotateLeft, forward, reverse
//Client sends: robotEnabled,direction,servoPanValue
//Server replies: sensor1,sensor2...
continueLoop = true;
try {
InetAddress serverAddr = InetAddress.getByName(robotIP);
Socket s = new Socket();
//int timeout = 2000; // DEFAULT milliseconds
int timeout = 6000; // milliseconds
SocketAddress sockaddr = new InetSocketAddress(serverAddr, SERVERPORT);
s.connect(sockaddr, timeout);
s_input = new BufferedReader(new InputStreamReader(s.getInputStream()));
s_output = new PrintWriter(s.getOutputStream(), true);
} catch (UnknownHostException e1) {
e1.printStackTrace();
Log.d("clientThread","Got invalid IP string in client thread");
continueLoop = false;
} catch (IOException e) {
Log.d("clientThread","Socket setup caught IOexception");
continueLoop = false;
//STACKEXCHANGE - THIS SEEMS TO BE WHERE THE CODE REPEATEDLY LOOPS AROUND TRYING TO CONNECT**********************************************************************************************
}
//STACKEXCHANGE - Loop starts below again and then logging "controlloop ending"
Log.d("clientThread","Socket Established");
setConnected(true);
try {
while ((stayConnected) && (continueLoop)) {
speed = throttle.getProgress();
if (stayConnected)
outputString = robotEnabled.toString() + "," + direction.toString() + "," + speed.toString();
else
outputString = "quit";
s_output.println(outputString);
if (!s_output.checkError())
{
inputString = s_input.readLine();
if (inputString == null)
{
continueLoop = false;
Log.d("clientThread","Unexpected disconnection.");
}
else
{
Log.d("clientThread","Client got: " + inputString.toString());
//parse returned string, which is just an integer containing the signal strength
try {
signalStrength = Integer.parseInt(inputString);
} catch(NumberFormatException nfe) {
Log.d(TAG,"Got invalid signal strength from client");
}
}
}
else
{
//printwriter.checkError returned true, something bad happened network-wise
continueLoop = false;
Log.d("clientThread","Printwriter.checkError() returned true, likely network problem");
}
}
socketCleanup();
Log.d("clientThread","controlLoop ending");
//STACKEXCHANGE - This is also logged in the catlog at the end of repetitive loop
} catch (IOException e) {
//this happens if the connection times out.
Log.d("clientThread", "Client comm thread got IOException in control loop.");
socketCleanup();
}
}//end control loop
} //end client thread
}
It's because you don't modify stayConnected.
You continually call
while ((!Thread.currentThread().isInterrupted()) && stayConnected)
{
controlLoop();
}
and get an exception each time with your call. Only modifying continueLoop to false. You should break your while on that condition or interrupt your thread as well for your intended behavior; or some other approach would work.

android development loop problems with timer

i need some help looping a timer in android. I need it to loop 5 times and then stop at 0 on the last run/countdown, but can seems to get it to work. I know it might be a simple.
Any help would be appreciated, thanks to anyone that does help.
package com.project.secondproject;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private int countdownValue;
private TextView textfield;
private Handler handler;
private boolean Running;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void start(View view){
int countdownValue = 30;
int counter = 0;
Running = true;
textfield = (TextView)findViewById(R.id.Timer);
handler = new Handler();
Runnable runnable = new Runnable(){
#Override
public void run(){
while(Running){
try{
//This controls the interval of the timer. every 1 second
Thread.sleep(1000);}
catch(InterruptedException e){
e.printStackTrace();
}
handler.post(new Runnable(){
#Override
public void run(){
countdownValue -= 1;
textfield.setText(String.valueOf(":" + countdownValue));
if(countdownValue == 1){
Running = false;
}
}
});
}
}
};
new Thread(runnable).start();
}
public void pause(View view){
Running = false;
}
}//End of class
Hope this is what you are looking for. Try this.!
public void run()
{
while(Running){
for (int counter = 5; counter > 0; counter++)
{
System.out.println(" loop..." + counter);
try{Thread.sleep(1000);} // 1 second pause
catch(Exception e){}
}
}

Categories

Resources