Can not trigger on Click - java

Can not trigger on Click listener
I tried
setClickable(true);
but still not working
minimum sdk is 15
the code doesn't have xml file, i need to fix it programmatically
the complete code:
public class MainActivity extends AppCompatActivity {
private static char CHAR = 'a';
private Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
int rowNo = ROWS;
int colNo = (screenWidth().widthPixels * ROWS /screenWidth().heightPixels) ;
for (int i = 0; i < 5; i++) {
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f));
for (int j = 0; j < 5; j++) {
String txt = String.valueOf(CHAR);
Cell cell = new Cell(this, txt, 50, 50);
cell.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
System.out.println("value is " + txt);
}
});
row.addView(cell);
CHAR += 1;
}
layout.addView(row);
}
setContentView(layout);
}
private class Cell extends LinearLayout{
public Cell(Context context, final String value, int width, int height) {
super(context);
LinearLayout.LayoutParams cellParams = new LinearLayout.LayoutParams(width, height);
setLayoutParams(cellParams);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
setBackground(getGradientDrawable());
}else{
setBackgroundDrawable(getGradientDrawable());
}
}
private GradientDrawable getGradientDrawable() {
GradientDrawable shape = new GradientDrawable();
shape.setShape(GradientDrawable.RECTANGLE);
shape.setColor(Color.TRANSPARENT);
shape.setStroke(2, Color.BLACK);
return shape;
}
}
}
so, why and how to fix it.
at clicking the associated text will be printed

Try onClickListener inside your loop
Cell cell = new Cell(this, txt, 50, 50);
cell.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
System.out.println("value is " + value);
}
});

Related

How to make expandable cardview childlist in recycle view by reusing the same layout

I want to make view by adding child list on android using cardview in recycleview.
i have made class to store these item and make arraylist on each parent of object if the object have child item, so i can differentiate if the list has child or not.
But i have difficulties to render the same layout to make the child list below the parent list. I am pragmatically make the layout one by one but it will be difficult to make that. So i want to render the same layout to my adapter when i want to make a child list whenever the object/class has arraylist of child list in that object.
this is my adapter right now
public class NotificationListAdapter extends BaseListAdapter<NotificationData, NotificationListAdapter.CustomViewHolder> {
private List<NotificationData> notificationData;
private ArrayList<Integer> indexForHide;
public NotificationListAdapter(Context context) {
super(context);
}
#Override
public void addAll(List<NotificationData> items) {
super.addAll(items);
this.notificationData = items;
}
void setChildNotif(){
ArrayList<NotificationData> childDataItems;
List<NotificationData> notificationDataChild = new ArrayList<NotificationData>();
notificationDataChild.addAll(notificationData);
indexForHide = new ArrayList<Integer>();
for (int i = 0; i < notificationData.size(); i++) {
childDataItems = new ArrayList<>();
if (notificationDataChild.size() == 1) {
notificationData.get(i).setChildNotification(childDataItems);
break;
}
for (int j = i; j < notificationDataChild.size(); j++){
if(i==j){
continue;
}else if (notificationDataChild.size() == 1) {
notificationData.get(i).setChildNotification(childDataItems);
break;
}else{
if(notificationDataChild.get(j).getRfqId()!=0){
if(notificationDataChild.get(j).getRfqId()==notificationData.get(i).getRfqId()){
childDataItems.add(notificationDataChild.get(j));
indexForHide.add(j);
}
}
if(!notificationDataChild.get(j).getInvoiceNumber().equalsIgnoreCase("")) {
if (notificationDataChild.get(j).getInvoiceNumber().equalsIgnoreCase(notificationData.get(i).getInvoiceNumber())) {
childDataItems.add(notificationDataChild.get(j));
indexForHide.add(j);
}
}
}
}
notificationData.get(i).setChildNotification(childDataItems);
Log.d("sizeNotif", i+" :"+notificationData.get(i).getChildNotification().size());
}
Log.d("sizeNotification", " :"+notificationData.size());
Log.d("sizeNotificationDummy", " :"+notificationData.size());
Log.d("isiHideAdapter", Arrays.toString(indexForHide.toArray()));
Log.d("isiHideAdapterSize", indexForHide.size()+"");
}
#Override
protected int getItemResourceLayout(int viewType) {
return R.layout.item_notification_list;
}
int position;
#Override
public int getItemViewType(int position) {
this.position = position;
return position;
}
#Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
setChildNotif();
return new CustomViewHolder(getView(parent, viewType), onItemClickListener);
}
public class CustomViewHolder extends BaseViewHolder<NotificationData> {
private TextView tvNotifTitle, tvNotifBody, tvNotifStatus, tvNotifTime, tvSignUnread, tvHeaderTitle;
private CardView formContent;
private LinearLayout linearLayout_childItems;
private ImageView expandList;
public CustomViewHolder(View itemView, OnItemClickListener onItemClickListener) {
super(itemView, onItemClickListener);
tvNotifTitle = itemView.findViewById(R.id.tvNotifTitle);
tvNotifBody = itemView.findViewById(R.id.tvNotifBody);
// tvNotifStatus = itemView.findViewById(R.id.tvNotifStatus);
tvNotifTime = itemView.findViewById(R.id.tvNotificationTime);
formContent = itemView.findViewById(R.id.formContent);
tvSignUnread = itemView.findViewById(R.id.tvSignUnread);
tvHeaderTitle = itemView.findViewById(R.id.tvHeaderNotifList);
linearLayout_childItems = itemView.findViewById(R.id.ll_child_items);
expandList = itemView.findViewById(R.id.arrow_expand_notif_list);
//SET CHILD
int intMaxNoOfChild = 0;
for (int index = 0; index < notificationData.size(); index++) {
int intMaxSizeTemp = notificationData.get(index).getChildNotification().size();
if (intMaxSizeTemp > intMaxNoOfChild) intMaxNoOfChild = intMaxSizeTemp;
}
linearLayout_childItems.removeAllViews();
for (int indexView = 0; indexView < intMaxNoOfChild; indexView++) {
TextView textView = new TextView(context);
textView.setId(indexView);
textView.setPadding(20, 20, 0, 20);
textView.setGravity(Gravity.LEFT);
textView.setTextSize(14);
textView.setTextColor(ContextCompat.getColor(context, R.color.colorText));
textView.setBackground(ContextCompat.getDrawable(context, R.drawable.bg_sub_notif_text));
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.leftMargin = 16;
textView.setOnClickListener(this);
linearLayout_childItems.addView(textView, layoutParams);
}
}
#Override
public void bind(NotificationData item) {
String title = splitString(item.getTitle());
tvNotifTitle.setText(title);
tvNotifBody.setText(item.getContent());
String txtTime = item.getCreatedAtDisplay().replaceAll("WIB", "");
tvNotifTime.setText(txtTime);
if (position == 0) {
((ViewGroup.MarginLayoutParams) formContent.getLayoutParams()).topMargin = dpToPx(12);
}
((ViewGroup.MarginLayoutParams) formContent.getLayoutParams()).bottomMargin = dpToPx(12);
if (position == getItemCount() - 1) {
((ViewGroup.MarginLayoutParams) formContent.getLayoutParams()).bottomMargin = dpToPx(80);
}
if(item.getIsRead()==1){
formContent.setCardBackgroundColor(context.getResources().getColor(R.color.gray1));
formContent.setCardElevation(5);
formContent.setRadius(15);
tvSignUnread.setVisibility(View.GONE);
}else if(item.getIsRead()==0){
}
//setHeader
Calendar calendar;
SimpleDateFormat dateFormat;
calendar = Calendar.getInstance();
dateFormat = new SimpleDateFormat("yyyy-MM-dd") ;
String date, yesterdayDate, dateDisplayData, dateData;
date = dateFormat.format(calendar.getTime());
calendar.add(Calendar.DATE, -1);
yesterdayDate = dateFormat.format(calendar.getTime());
String[] arrDateDisplayData = item.getCreatedAtDisplay().split(",", 5);
dateDisplayData = arrDateDisplayData[0];
String[] arrDateData = item.getCreatedAt().split(" ", 5);
dateData = arrDateData[0];
Log.d("datenow", date);
Log.d("dateyesterday", yesterdayDate);
Log.d("dateDisplayData", dateDisplayData);
Log.d("dateData", dateData);
if(dateData.equalsIgnoreCase(date)){
tvHeaderTitle.setText("Hari ini");
}else if(dateData.equalsIgnoreCase(yesterdayDate)){
tvHeaderTitle.setText("Kemarin");
}else{
tvHeaderTitle.setText(dateDisplayData);
// tvHeaderTitle.setVisibility(View.GONE);
}
if (getAdapterPosition() > 0) {
String[] timePrev = notificationData.get(getAdapterPosition() - 1).getCreatedAt().split(" ");
if (dateData.equalsIgnoreCase(timePrev[0])) {
tvHeaderTitle.setVisibility(View.GONE);
}
}
if(indexForHide.contains(getAdapterPosition())){
formContent.setVisibility(View.GONE);
}
//SET CHILD
NotificationData dummyParentDataItem = notificationData.get(position);
int noOfChildTextViews = linearLayout_childItems.getChildCount();
for (int index = 0; index < noOfChildTextViews; index++) {
TextView currentTextView = (TextView) linearLayout_childItems.getChildAt(index);
currentTextView.setVisibility(View.VISIBLE);
}
int noOfChild = 0;
if(dummyParentDataItem.getChildNotification()==null){
noOfChild = 0;
}else{
noOfChild = dummyParentDataItem.getChildNotification().size();
}
if (noOfChild < noOfChildTextViews) {
for (int index = noOfChild; index < noOfChildTextViews; index++) {
TextView currentTextView = (TextView) linearLayout_childItems.getChildAt(index);
// currentTextView.setVisibility(View.GONE);
}
}
for (int textViewIndex = 0; textViewIndex < noOfChild; textViewIndex++) {
TextView currentTextView = (TextView) linearLayout_childItems.getChildAt(textViewIndex);
currentTextView.setText(dummyParentDataItem.getChildNotification().get(textViewIndex).getTitle());
/*currentTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(mContext, "" + ((TextView) view).getText().toString(), Toast.LENGTH_SHORT).show();
}
});*/
}
if (noOfChild > 0) {
expandList.setVisibility(View.VISIBLE);
expandList.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (linearLayout_childItems.getVisibility() == View.VISIBLE) {
linearLayout_childItems.setVisibility(View.GONE);
// Toast.makeText(context, "expand", Toast.LENGTH_SHORT).show();
expandList.setImageResource(R.drawable.ic_arrow_up_blue);
}
else {
linearLayout_childItems.setVisibility(View.VISIBLE);
expandList.setImageResource(R.drawable.ic_arrow_down_blue);
}
}
});
}else {
linearLayout_childItems.setVisibility(View.GONE);
}
}
public int dpToPx(int dp) {
Resources r = context.getResources();
int px = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
dp, r.getDisplayMetrics()
);
return px;
}
public String splitString(String tempString){
StringBuilder finalString = new StringBuilder(tempString);
int i = 0;
while ((i = finalString.indexOf(" ", i + 20)) != -1) {
finalString.replace(i, i + 1, "\n");
}
return finalString.toString();
}
}
}
please help me to render my layout whenever i want to make a child list under my parent list so i can simplify my code above and not making the child layout programatically

Linearlayout is not able to remove it's child

One Linearlayout I have created and dynamically I am adding RadioGroup and textView in it. When next time I am calling the method, Textview is getting removed but old radio buttons are not getting removed.
Here is the code:
public void setShippingMethods() {
shippingChargesLayout.removeAllViews();
final RadioGroup radioGroup = new RadioGroup(getActivity());
radioGroup.setOrientation(LinearLayout.VERTICAL);
TextView textView = new TextView(getActivity());
textView.setText("Select Shipping Options:");
shippingChargesLayout.addView(textView);
for (int i = 0; i < shippingDetailsList.size(); i++) {
RadioButton radioButton = new RadioButton(getActivity());
radioButton.setText("" + shippingDetailsList.get(i).getMethodTitle() + "-" + (new SessionManager(getActivity()).getCurrency()) + df.format(shippingDetailsList.get(i).getAmount()));
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
radioGroup.addView(radioButton, params);
radioButton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
}
shippingChargesLayout.addView(radioGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
shipmentCode = shippingDetailsList.get(i - 1).getCarrierCode();
Log.v("carrierCode", "" + shipmentCode);
}
});
checkout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (radioGroup.getCheckedRadioButtonId() == -1) {
Toast.makeText(getActivity(),"Select shipping options.",Toast.LENGTH_LONG).show();
} else {
completeOrder();
}
}
});
}
Here is the image:
I have made some changes in your code.
public void setShippingMethods() {
shippingChargesLayout.removeAllViews();
final RadioGroup radioGroup = new RadioGroup(getActivity());
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
radioGroup.setLayoutParams(params);
radioGroup.setOrientation(LinearLayout.VERTICAL);
TextView textView = new TextView(getActivity());
textView.setText("Select Shipping Options:");
shippingChargesLayout.addView(textView);
for (int i = 0; i < shippingDetailsList.size(); i++) {
RadioButton radioButton = new RadioButton(getActivity());
radioButton.setText("" + shippingDetailsList.get(i).getMethodTitle() + "-" + (new SessionManager(getActivity()).getCurrency()) + df.format(shippingDetailsList.get(i).getAmount()));
radioGroup.addView(radioButton);
}
shippingChargesLayout.addView(radioGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
shipmentCode = shippingDetailsList.get(i - 1).getCarrierCode();
Log.v("carrierCode", "" + shipmentCode);
}
});
checkout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (radioGroup.getCheckedRadioButtonId() == -1) {
Toast.makeText(getActivity(),"Select shipping options.",Toast.LENGTH_LONG).show();
} else {
completeOrder();
}
}
});
}

Application Crashed when trying to input on an edittext that has an TextChangedListener

I was trying to create an application that automatically calculate the percentage. the Percentage value will be shown in a TextView. The value that will be shown in the textview will be based on the inputted value of the user in an edittext.
I've finished creating my codes for this program but, it keeps crashing every time i tried to input some numbers in the edittext.
I hope you understand what am i saying.
Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample__table);
btnAdd = (Button)findViewById(R.id.btnAdd);
//btn Function
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
AddRow();
}
});
//end of btn function
//text changed
tl = (TableLayout) findViewById(R.id.tl);
row = new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
fruit = new EditText(this);
freq = new EditText(this);
perc = new TextView(this);
freq.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
ReCalculate();
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
//text changed end
}
private void AddRow(){
row.addView(fruit);
row.addView(freq);
row.addView(perc);
tl.addView(row);
}
private void ReCalculate(){
float sum = 0f;
int rows = tl.getChildCount();
// Get the total
for (int i = 0; i < rows; i++) {
TableRow elem = (TableRow) tl.getChildAt(i);
sum+= Integer.parseInt(((EditText)elem.getChildAt(1)).getText().toString());
}
// Recalculate every row percent
for (int i = 0; i < rows; i++) {
TableRow elem = (TableRow) tl.getChildAt(i);
int amount​ = Integer.parseInt(((TextView)elem.getChildAt(1)).getText().toString());
((TextView)elem.getChildAt(1)).setText(String.valueOf(amount​/sum*100));
}
}
}
If you want to detect your text's change, you can use onKeyListener:
EditText edtText = (EditText)findViewById(R.id.editText);
edtText.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
Toast.makeText(MainActivity.this, "Text changed!", Toast.LENGTH_SHORT).show();
return false;
}
});
it worked for me, hope it helps.

Android Studio App Unfortunately has stopped-Java code

My app in Android Studio is OK but when I run it, it tells me your app has closed. The application is a card game.
My code:
## Heading ##
public class MainActivity extends Activity {
*Button btn_retry;*
*int lastImageView = 0;
int beforLastImageView = 0;
int lastCard=0;
int beforLastCard=0;
int imageNumber=0;
int beforImageNumber=0;
int numberOfCards = 6;
int numberOfImages = 30;*
final ImageView[] Imageview = new ImageView[numberOfCards];
int[] Imageview_Id = {R.id.img1, R.id.img2, R.id.img3,
R.id.img4, R.id.img5, R.id.img6};
final int[] images = {R.drawable.i1, R.drawable.i2, R.drawable.i3, R.drawable.i4, R.drawable.i5,
R.drawable.i6, R.drawable.i7, R.drawable.i8, R.drawable.i9, R.drawable.i10,
R.drawable.i11, R.drawable.i12, R.drawable.i13, R.drawable.i14, R.drawable.i15,
R.drawable.i16, R.drawable.i17, R.drawable.i18, R.drawable.i19, R.drawable.i20,
R.drawable.i21, R.drawable.i22, R.drawable.i23, R.drawable.i24, R.drawable.i25,
R.drawable.i26, R.drawable.i27, R.drawable.i28, R.drawable.i29, R.drawable.i30,
R.drawable.i31, R.drawable.i32, R.drawable.i33, R.drawable.i34, R.drawable.i35,
R.drawable.i36, R.drawable.i37, R.drawable.i38, R.drawable.i39, R.drawable.i40};
final List<Integer> cardlist = new ArrayList<Integer>();
#Override
**protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
clickOnButton1();**
final List<Integer> imagelist = new ArrayList<Integer>();
for (int i = 0; i < numberOfCards; i++) {
imagelist.add(i);
}
Collections.shuffle(imagelist);
for (int i = 0; i < numberOfCards; i++) {
Imageview[i] = (ImageView) findViewById(Imageview_Id[i]);
}
final List<Integer> cardlist = new ArrayList<Integer>();
for (int i = 0; i< numberOfCards / 2; i++) {
cardlist.add(imagelist.get(i));
}
for (int i=numberOfCards/2;i<numberOfCards;i++){
cardlist.add(imagelist.get(i-numberOfCards/2));
}
Collections.shuffle(cardlist);
for (int i=0 ; i<numberOfCards;i++){
Imageview[i].setOnClickListener(onClickImage);
}
};
protected View.OnClickListener onClickImage= new
View.OnClickListener() {
**#Override
public void onClick(final View v) {**
switch (v.getId()){
case(R.id.img1):
imageNumber = 1;
case(R.id.img2):
imageNumber = 2;
case(R.id.img3):
imageNumber = 3;
case(R.id.img4):
imageNumber = 4;
case(R.id.img5):
imageNumber = 5;
case(R.id.img6):
imageNumber = 6;
}
android.os.Handler handler =new android.os.Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
String imageName="i"+cardlist.get(imageNumber);
int res = getResources().getIdentifier(imageName, "drawable", "com.example.arash.card");
Imageview[imageNumber].setImageResource(res);
lastImageView=(v.getId());
lastCard=cardlist.get(imageNumber);
}
},1000);
if (lastImageView != beforLastImageView) {
if (lastCard == beforLastCard) {
Imageview[imageNumber].setVisibility(View.INVISIBLE);
Imageview[imageNumber].setEnabled(false);
Imageview[beforImageNumber].setVisibility(View.INVISIBLE);
Imageview[beforImageNumber].setEnabled(false);
beforLastCard = 0;
beforLastImageView = 0;
lastCard = 0;
lastImageView = 0;
} else {
beforLastImageView = lastImageView;
beforLastCard = lastCard;
}
}
beforImageNumber=imageNumber;
}
};
**public void clickOnButton1() {**
final Context context = this;
btn_retry = (Button) findViewById(R.id.button);
btn_retry.setOnClickListener(new View.OnClickListener() {
***#Override***
public void onClick(View arg0) {
Intent intent = new Intent(context, MainActivity.class);
finish();
*startActivity(intent);*
}
});
}

Android: service not running

I have a service:
import android.app.IntentService;
import android.content.Intent;
import android.os.CountDownTimer;
import android.widget.Button;
public class CustTimer extends IntentService {
int length;
long timeLeft;
Button button;
long endTime;
public CustTimer() {
super("CustTimer");
}
public CustTimer(int length, Button button) {
super("CustTimer");
this.length = length;
this.button = button;
this.endTime = System.currentTimeMillis() + length;
}
#Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
new CountDownTimer(length, 1000) {
public void onTick(long millisUntilFinished) {
String min = String.valueOf(millisUntilFinished/60000 );
long re = millisUntilFinished%60000;
String secs = String.valueOf(re/1000);
button.setText(min + ":" + secs);
timeLeft = millisUntilFinished;
}
public void onFinish() {
//button.setText("done");
}
}.start();
}
}
and an activity which calls it, the onCreate method is:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Intent intent = new Intent(this, CustTimer.class);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout l2 = new LinearLayout(this);
l2.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout l3 = new LinearLayout(this);
l3.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout l4 = new LinearLayout(this);
l4.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout l5 = new LinearLayout(this);
l5.setOrientation(LinearLayout.HORIZONTAL);
LayoutParams hor = new LayoutParams(0, LayoutParams.MATCH_PARENT, 1f);
LayoutParams ver = new LayoutParams(LayoutParams.MATCH_PARENT, 0, 1f);
for (int i = 0; i < 16; i++){
buts[i] = new Button(this);
buts[i].setText(String.valueOf(i+1));
timerRun[i] = false;
}
for (int i = 0; i < 4; i++) {
l2.addView(buts[i], hor);
}
for (int i = 4; i < 8; i++) {
l3.addView(buts[i], hor);
}
for (int i = 8; i < 12; i++) {
l4.addView(buts[i], hor);
}
for (int i = 12; i < 16; i++) {
l5.addView(buts[i], hor);
}
for (int i = 0; i < 16; i++) {
final int j = i;
buts[i].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CustTimer t1 = new CustTimer(times[j], buts[j]);
timers[j] = t1;
startService(intent);
}
});
}
ll.addView(l2, ver);
ll.addView(l3, ver);
ll.addView(l4, ver);
ll.addView(l5, ver);
this.setContentView(ll);
}
My problem is that the startService() call doesn't return any errors however it wont run. just looking for some basic input on if i've missed anything important to do with services
edit:
manifest section is:
<service android:name="CustTimer"> </service>
There's an easier way to implement a simple timer:
private final Handler mHandler = new Handler();
private final Runnable mRunnable = new Runnable() {
#Override
public void run() {
// TODO: update button text, manipulate views, etc.
mHandler.postDelayed(mRunnable, 1000);
}
};
private void start() {
mHandler.postDelayed(mRunnable, 1000);
}
private void stop() {
mHandler.removeCallbacks(mRunnable);
}

Categories