Menus

Saturday 14 December 2019

c program to find area of circle


Code

#include<stdio.h>

void main()
{

float area,radius,pi=3.14;

printf("Enter the radius of a circle : ");
scanf("%f",&radius);

area = pi * radius * radius;

printf("Area of Circle is %f\n",area);

}


Video




Friday 13 December 2019

Write a c program to find whether a number odd or even


Code


#include<stdio.h>

void main()
{
int a;
printf("Enter a number ");
scanf("%d",&a);

if(a%2==0)

  printf("The %d is even number\n",a);

else

  printf("The %d is odd number\n",a);

}

Video





Monday 9 December 2019

Write a C program for to find largest among three numbers


#include<stdio.h>

void main()
{

int x,y,z;

printf("Enter three numbers : ");
scanf("%d,%d,%d",&x,&y,&z);

if((x>y) && (x>z))
{
printf("The greater value is %d\n",x);
}
else if(y>z)
{
printf("The greater value is %d\n",y);
}
else
{
printf("The greater value is %d\n",z);
}

}







Monday 2 December 2019

Write a C program for Arithmetic Operations



Arithmetic Operators: These are used to perform arithmetic/mathematical operations on operands. 


  • Addition: The ‘+’ operator adds two operands. For example, x+y.
  • Subtraction: The ‘-‘ operator subtracts two operands. For example, x-y.
  • Multiplication: The ‘*’ operator multiplies two operands. For example, x*y.
  • Division: The ‘/’ operator divides the first operand by the second. For example, x/y.
  • Modulus: The ‘%’ operator returns the remainder when first operand is divided by the second. For example, x%y.


Program

#include<stdio.h>

void main()
{

int a,b,c,d,f,g,h;


printf("Enter first number ");
scanf("%d",&a);
printf("Enter Second number ");
scanf("%d",&b);

c=a+b;
printf("Sum %d ",c);

d=a-b;
printf("\nDifference %d ",d);

f=a*b;
printf("\nProduct %d ",f);

g=a/b;
printf("\nDiv %d ",g);

h=a%b;
printf("\nMod %d \n",h);



}







Sunday 1 December 2019

Write C program to print "Hello World"



ALGORITHM

1. BEGIN
2. PRINT "Hello World"
3. END

PROGRAM 


#include<stdio.h>
void main()
{
 printf("Hello World\n");

}


Print Hello World - Malayalam




Introduction to GCC and example



Monday 25 November 2019

How to open website by clicking a button in android


You can use this on your button click activity


Intent myLink = new Intent(Intent.ACTION_VIEW);
myLink.setData(Uri.parse("http://yii2ideas.blogspot.com/p/blog-page.html"));
startActivity(myLink);



and import this on your code


import android.net.Uri;














Error Problem



Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?



Solutions to add - FLAG_ACTIVITY_NEW_TASK flag to your intent:


Intent myLink = new Intent(Intent.ACTION_VIEW);
myWebLink.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
myLink.setData(Uri.parse("http://yii2ideas.blogspot.com/p/blog-page.html")); startActivity(myLink);




Tuesday 29 October 2019

Download File From Server in Android


How to Download an image in ANDROID programatically?

How to Download an pdf in ANDROID programatically?



Open new project and add these code.
This an simple example for downloading files from server in android programatically.

The java code to download the file.
MainActivity.java

package com.example.root.download;

import android.os.AsyncTask;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    private static Button btnDownload;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnDownload = (Button) findViewById(R.id.btnDownload);
        btnDownload.setOnClickListener(new View.OnClickListener() {

        @Override
         public void onClick(View view) {

                //ok calll downloading
                new Downloading().execute();

                Toast.makeText(MainActivity.this,"Downloading             completed",Toast.LENGTH_LONG).show();
            }
        });

    }


    //add downloading
    private class Downloading extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... voids) {

            try {
             
               //This is the file we are going to download.
               //Please add your server file location .
                URL url = new URL("http://www.mission2win.in/pdf/pdf22.pdf");
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

                urlConnection.setRequestMethod("GET");
                urlConnection.setDoOutput(true);

                //Connect
                urlConnection.connect();

                //set path where to save the file
                File SDCardRoot = Environment.getExternalStorageDirectory();
                //create a new file to save
                File file = new File(SDCardRoot,"pdf22.pdf");

                FileOutputStream fileOutput = new FileOutputStream(file);

                //stream used for reading the data from the internet
                InputStream inputStream = urlConnection.getInputStream();


                //create a buffer
                byte[] buffer = new byte[124];
                int bufferLength = 0;


                while( (bufferLength = inputStream.read(buffer)) > 0){
                    fileOutput.write(buffer,0,bufferLength);
                }

                //close the output stream when complete
                fileOutput.close();

            }
            catch (final MalformedURLException e){
                showError("Error : MalformURLException " + e);
                e.printStackTrace();
            }
            catch (final IOException e){
                showError("Error : IOException " + e);
                e.printStackTrace();
            }
            catch (final Exception e){
                showError("Error : Please check your internet connection " + e);
            }

            return null;
        }

        void showError(final String err){
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(MainActivity.this,err,Toast.LENGTH_LONG).show();
                }
            });
        }

    }
}


This is the contents of xml file.
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.root.download.MainActivity">

    <Button
        android:id="@+id/btnDownload"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Download File"
        />

</RelativeLayout>


Add Internet and Write to External Directory permissions to AndroidManifest.xml
AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.root.download">

    <!-- Permission required for Downloading Files -->
    <uses-permission android:name="android.permission.INTERNET" />
    <!-- Permission required for Checking Internet Connection -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <!-- Permission required for Reading Writing SD Card/Memory -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 Now run the app on your Android device.






open failed: EACCES (Permission denied)

Verify permission and add this code to give a permission

 public static void verifyStoragePermissions(Activity activity) {
        // Check if we have write permission
        int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

        if (permission != PackageManager.PERMISSION_GRANTED) {
            // We don't have permission so prompt the user
            ActivityCompat.requestPermissions(
                    activity,
                    PERMISSIONS_STORAGE,
                    REQUEST_EXTERNAL_STORAGE
            );
        }
    }


Other wise add several permission to Manifest file.

<!-- Permission required for Downloading Files -->
<uses-permission android:name="android.permission.INTERNET" />

<!-- Permission required for Checking Internet Connection -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<!-- Permission required for Reading Writing SD Card/Memory -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />






Android download a file from remote server With progressbar 


Create a new xml file inside res/layout folder and name it progressdialog.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    >

    <TextView
        android:id="@+id/heading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#0F0"
        android:text="Heading"
        android:textStyle="bold|italic"
        android:layout_marginTop="5dp"

        />

    <ProgressBar
        android:id="@+id/progressbar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:progress="0"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="10dp"
        style="?android:attr/progressBarStyleHorizontal"
        android:maxHeight="10dip"
        android:minHeight="10dip"
        />

</LinearLayout>


Colored code add to Main_Activity.java

package com.example.root.download;

import android.app.Dialog;
import android.os.AsyncTask;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    private static Button btnDownload;

    ProgressBar progressBar;
    Dialog dialog;
    int downloadedSize =0;
    int totalSize=0;
    TextView heading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        btnDownload = (Button) findViewById(R.id.btnDownload);

        btnDownload.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {


                showProgress();
                //ok calll downloading
                new Downloading().execute();

                Toast.makeText(MainActivity.this,"Downloading completed",Toast.LENGTH_LONG).show();

            }
        });


    }


    //add downloading
    private class Downloading extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... voids) {

            try {

                URL url = new URL("http://www.mission2win.in/pdf/pdf22.pdf");
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

                urlConnection.setRequestMethod("GET");
                urlConnection.setDoOutput(true);

                //Connect
                urlConnection.connect();

                //set path where to save the file
                File SDCardRoot = Environment.getExternalStorageDirectory();
                //create a new file to save
                File file = new File(SDCardRoot,"pdf22.pdf");

                FileOutputStream fileOutput = new FileOutputStream(file);

                //stream used for reading the data from the internet
                InputStream inputStream = urlConnection.getInputStream();

                //This is total size of the downloading file
                totalSize = urlConnection.getContentLength();

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        progressBar.setMax(totalSize);
                    }
                });


                //create a buffer
                byte[] buffer = new byte[124];
                int bufferLength = 0;


                while( (bufferLength = inputStream.read(buffer)) > 0){
                    fileOutput.write(buffer,0,bufferLength);
                    downloadedSize += bufferLength;
                    //Update progress bar
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            progressBar.setProgress(downloadedSize);
                        }
                    });
                }

                //close the output stream when complete
                fileOutput.close();

                //close dialog
                dialog.dismiss();

            }
            catch (final MalformedURLException e){
                showError("Error : MalformURLException " + e);
                e.printStackTrace();
            }
            catch (final IOException e){
                showError("Error : IOException " + e);
                e.printStackTrace();
            }
            catch (final Exception e){
                showError("Error : Please check your internet connection " + e);
            }



            return null;
        }

        void showError(final String err){
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(MainActivity.this,err,Toast.LENGTH_LONG).show();
                }
            });
        }


    }



    void showProgress(){
        dialog = new Dialog(MainActivity.this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.progressdialog); //this is my created xml file
        dialog.setTitle("Download Progress");

        heading = (TextView) dialog.findViewById(R.id.heading);
        heading.setText("Download starting ...");
        dialog.show();

        progressBar = (ProgressBar)dialog.findViewById(R.id.progressbar);
        progressBar.setProgress(0);

    }


}