Menus

Monday, 25 May 2020

Android edit text to show the dd-mm-yyyy date format






Add this code your onTextChanged event.


        EditText date;

        date = (EditText)findViewById(R.id.edtDateofbirth);
        date.addTextChangedListener(new TextWatcher() {
            private String current = "";
            private String ddmmyyyy = "DDMMYYYY";
            private Calendar cal = Calendar.getInstance();


            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!s.toString().equals(current)) {
                    String clean = s.toString().replaceAll("[^\\d.]", "");
                    String cleanC = current.replaceAll("[^\\d.]", "");

                    int cl = clean.length();
                    int sel = cl;
                    for (int i = 2; i <= cl && i < 6; i += 2) {
                        sel++;
                    }
                    //Fix for pressing delete next to a forward slash
                    if (clean.equals(cleanC)) sel--;

                    if (clean.length() < 8){
                        clean = clean + ddmmyyyy.substring(clean.length());
                    }else{
                        //This part makes sure that when we finish entering numbers
                        //the date is correct, fixing it otherwise
                        int day  = Integer.parseInt(clean.substring(0,2));
                        int mon  = Integer.parseInt(clean.substring(2,4));
                        int year = Integer.parseInt(clean.substring(4,8));

                        if(mon > 12) mon = 12;
                        cal.set(Calendar.MONTH, mon-1);

                        year = (year<1900)?1900:(year>2100)?2100:year;
                        cal.set(Calendar.YEAR, year);
                        // ^ first set year for the line below to work correctly
                        //with leap years - otherwise, date e.g. 29/02/2012
                        //would be automatically corrected to 28/02/2012

                        day = (day > cal.getActualMaximum(Calendar.DATE))? cal.getActualMaximum(Calendar.DATE):day;
                        clean = String.format("%02d%02d%02d",day, mon, year);
                    }

                    clean = String.format("%s/%s/%s", clean.substring(0, 2),
                            clean.substring(2, 4),
                            clean.substring(4, 8));

                    sel = sel < 0 ? 0 : sel;
                    current = clean;
                    date.setText(current);
                    date.setSelection(sel < current.length() ? sel : current.length());



                }
            }


            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void afterTextChanged(Editable s) {}
        });







Monday, 4 May 2020

How to add custom notification in android app


Create and Display Notification on Android with Example






import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.app.NotificationCompat; import android.view.View; import android.widget.Button; public class NotficationtestActivity extends AppCompatActivity { NotificationCompat.Builder notification; private static final int appuniqueID = 10012; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notficationtest); notification = new NotificationCompat.Builder(this); notification.setAutoCancel(true); Button btnNotification = (Button) findViewById(R.id.btnNotificationttest); btnNotification.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //Build the notification notification.setSmallIcon(R.drawable.photo); notification.setTicker("Ticker"); notification.setWhen(System.currentTimeMillis()); notification.setContentTitle("TechProgrammingIdeas"); notification.setContentText("Added new video. Subscribe my channel"); Intent intent = new Intent(NotficationtestActivity.this,NotficationtestActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(NotficationtestActivity.this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT); notification.setContentIntent(pendingIntent); //Builds notification and issue it NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); nm.notify(appuniqueID,notification.build()); } }); } }








Thursday, 23 April 2020

Program to add two matrices in C


#include<stdio.h>

void main()
{

int a[10][10],b[10][10],c[10][10];
int i,j,m,n,p,q;

printf("Enter the rows and column of first matrix : ");
scanf("%d%d",&m,&n);

printf("Enter the rows and column of second matrix : ");
scanf("%d%d",&p,&q);

if((m==p) && (n==q))
{

printf("Enter first matrix \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
 scanf("%d",&a[i][j]);
}
}


printf("Enter second matrix \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
 scanf("%d",&b[i][j]);
}
}


for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
 c[i][j] = a[i][j] + b[i][j];
}
}


printf("Sum of matrix : \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
 printf(" %d",c[i][j]);
}
printf("\n");
}


}
else
{
  printf("\nInvalid rows and columns\n");
}

}








Wednesday, 22 April 2020

C program to print two dimensional array





#include<stdio.h>

void main()
{

int a[10][10],i,j,m,n;

printf("Enter size of rows and columns of array : ");
scanf("%d%d",&m,&n);

printf("Enter elements in the array :");

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
  printf("a[%d][%d] = ",i,j);
  scanf("%d",&a[i][j]);
}
}


printf("Array elements \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
  printf(" %d",a[i][j]);
}
printf("\n");
}


}




Tuesday, 21 April 2020

C program for add two one dimensional array


#include<stdio.h>

void main()
{

int a[10],b[10],c[10];
int n,i;

printf("Enter the size : ");
scanf("%d",&n);

printf("Enter first array \n");

for(i=0;i<n;i++)
 scanf("%d",&a[i]);

printf("Enter second array \n");

for(i=0;i<n;i++)
 scanf("%d",&b[i]);


for(i=0;i<n;i++)
 c[i]=a[i]+b[i];

printf("Sum of arrays : ");
for(i=0;i<n;i++)
 printf("\n%d",c[i]);


}




Thursday, 2 April 2020

C Program to find largest element in an array

#include<stdio.h>

void main()
{

int arr[100],size,i,max,location;

printf("Enter the number of elements in an array : ");
scanf("%d",&size);

for(i=0;i<size;i++)
{
printf("Enter array value ");
scanf("%d",&arr[i]);
}


max= arr[0];

for(i=1;i<size;i++)
{
if(arr[i]>max)
{
max=arr[i];
location=i+1;
}
}

printf("Maximum element in location %d and its value is %d \n",location,max);


}




Wednesday, 1 April 2020

C Program to reverse the number


#include<stdio.h>

int reversenum(int n)
{

int rev=0,a;

while(n>0)
{
a=n%10;
n=n/10;
rev=rev*10+a;
}

return rev;


}


void main()
{

int n,rev;
printf("Enter the number : ");
scanf("%d",&n);

rev = reversenum(n);

printf("The reverse of the number is %d \n",rev);


}