Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //app fo setting an alarm
- activity.main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <TimePicker
- android:id="@+id/timePicker"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center" />
- <ToggleButton
- android:id="@+id/toggleButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_margin="20dp"
- android:checked="false"
- android:onClick="OnToggleClicked" />
- </LinearLayout>
- Mainactivity.java
- package com.example.akshay.alarm;
- import android.app.AlarmManager;
- import android.app.PendingIntent;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.TimePicker;
- import android.widget.Toast;
- import android.widget.ToggleButton;
- import java.util.Calendar;
- public class MainActivity extends AppCompatActivity
- {
- TimePicker alarmTimePicker;
- PendingIntent pendingIntent;
- AlarmManager alarmManager;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- alarmTimePicker = (TimePicker) findViewById(R.id.timePicker);
- alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
- }
- public void OnToggleClicked(View view)
- {
- long time;
- if (((ToggleButton) view).isChecked())
- {
- Toast.makeText(MainActivity.this, "ALARM ON", Toast.LENGTH_SHORT).show();
- Calendar calendar = Calendar.getInstance();
- calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
- calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
- Intent intent = new Intent(this, AlarmReceiver.class);
- pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
- time=(calendar.getTimeInMillis()-(calendar.getTimeInMillis()%60000));
- if(System.currentTimeMillis()>time)
- {
- if (calendar.AM_PM == 0)
- time = time + (1000*60*60*12);
- else
- time = time + (1000*60*60*24);
- }
- alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, 10000, pendingIntent);
- }
- else
- {
- alarmManager.cancel(pendingIntent);
- Toast.makeText(MainActivity.this, "ALARM OFF", Toast.LENGTH_SHORT).show();
- }
- }
- }
- AlarmReciver.java
- package com.example.akshay.alarm;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.media.Ringtone;
- import android.media.RingtoneManager;
- import android.net.Uri;
- import android.widget.Toast;
- public class AlarmReceiver extends BroadcastReceiver
- {
- @Override
- public void onReceive(Context context, Intent intent)
- {
- Toast.makeText(context, "Alarm! Wake up! Wake up!", Toast.LENGTH_LONG).show();
- Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
- if (alarmUri == null)
- {
- alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
- }
- Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
- ringtone.play();
- }
- }
- ////alert on recive a message
- activity.main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_margin="10dp"
- android:orientation="vertical">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Message"
- android:textSize="30sp" />
- <EditText
- android:id="@+id/editText"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:singleLine="true"
- android:textSize="30sp" />
- <Button
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_margin="30dp"
- android:layout_gravity="center"
- android:text="Notify"
- android:textSize="30sp"/>
- </LinearLayout>
- Mainactivity.java
- package com.example.akshay.alarm;
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- public class MainActivity extends AppCompatActivity
- {
- Button notify;
- EditText e;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- notify= (Button) findViewById(R.id.button);
- e= (EditText) findViewById(R.id.editText);
- notify.setOnClickListener(new View.OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- Intent intent = new Intent(MainActivity.this, SecondActivity.class);
- PendingIntent pending = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
- Notification noti = new Notification.Builder(MainActivity.this).setContentTitle("New Message").setContentText(e.getText().toString()).setSmallIcon(R.mipmap.ic_launcher).setContentIntent(pending).build();
- NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
- noti.flags |= Notification.FLAG_AUTO_CANCEL;
- manager.notify(0, noti);
- }
- });
- }
- }
- Then Go to file
- new>activity>emptyactivity
- name it as second activity
- //read and write data to sdcard
- Add the below line to android_mainfest.xml above the application tag
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
- activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_margin="20dp"
- android:orientation="vertical">
- <EditText
- android:id="@+id/editText"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:singleLine="true"
- android:textSize="30dp" />
- <Button
- android:id="@+id/button"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="10dp"
- android:text="Write Data"
- android:textSize="30dp" />
- <Button
- android:id="@+id/button2"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="10dp"
- android:text="Read data"
- android:textSize="30dp" />
- <Button
- android:id="@+id/button3"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="10dp"
- android:text="Clear"
- android:textSize="30dp" />
- </LinearLayout>
- Mainactivity.java
- package com.example.akshay.alarm;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.InputStreamReader;
- public class MainActivity extends AppCompatActivity
- {
- EditText e1;
- Button write,read,clear;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- e1= (EditText) findViewById(R.id.editText);
- write= (Button) findViewById(R.id.button);
- read= (Button) findViewById(R.id.button2);
- clear= (Button) findViewById(R.id.button3);
- write.setOnClickListener(new View.OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- String message=e1.getText().toString();
- try
- {
- File f=new File("/sdcard/myfile.txt");
- f.createNewFile();
- FileOutputStream fout=new FileOutputStream(f);
- fout.write(message.getBytes());
- fout.close();
- Toast.makeText(getBaseContext(),"Data Written in SDCARD",Toast.LENGTH_LONG).show();
- }
- catch (Exception e)
- {
- Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_LONG).show();
- }
- }
- });
- read.setOnClickListener(new View.OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- String message;
- String buf = "";
- try
- {
- File f = new File("/sdcard/myfile.txt");
- FileInputStream fin = new FileInputStream(f);
- BufferedReader br = new BufferedReader(new InputStreamReader(fin));
- while ((message = br.readLine()) != null)
- {
- buf += message;
- }
- e1.setText(buf);
- br.close();
- fin.close();
- Toast.makeText(getBaseContext(),"Data Recived from SDCARD",Toast.LENGTH_LONG).show();
- }
- catch (Exception e)
- {
- Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
- }
- }
- });
- clear.setOnClickListener(new View.OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- e1.setText("");
- }
- });
- }
- }
- ////tic-toc-toe game
- activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context="com.codinginflow.tictactoe.MainActivity">
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <TextView
- android:id="@+id/text_view_p1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:freezesText="true"
- android:text="Player 1: 0"
- android:textSize="30sp" />
- <TextView
- android:id="@+id/text_view_p2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@+id/text_view_p1"
- android:freezesText="true"
- android:text="Player 2: 0"
- android:textSize="30sp" />
- <Button
- android:id="@+id/button_reset"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentEnd="true"
- android:layout_centerVertical="true"
- android:layout_marginEnd="33dp"
- android:text="reset" />
- </RelativeLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:layout_weight="1">
- <Button
- android:id="@+id/button_00"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:freezesText="true"
- android:textSize="60sp" />
- <Button
- android:id="@+id/button_01"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:freezesText="true"
- android:textSize="60sp" />
- <Button
- android:id="@+id/button_02"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:freezesText="true"
- android:textSize="60sp" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:layout_weight="1">
- <Button
- android:id="@+id/button_10"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:freezesText="true"
- android:textSize="60sp" />
- <Button
- android:id="@+id/button_11"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:freezesText="true"
- android:textSize="60sp" />
- <Button
- android:id="@+id/button_12"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:freezesText="true"
- android:textSize="60sp" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:layout_weight="1">
- <Button
- android:id="@+id/button_20"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:freezesText="true"
- android:textSize="60sp" />
- <Button
- android:id="@+id/button_21"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:freezesText="true"
- android:textSize="60sp" />
- <Button
- android:id="@+id/button_22"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:freezesText="true"
- android:textSize="60sp" />
- </LinearLayout>
- </LinearLayout>
- mainActivity.java
- package com.example.akshay.alarm;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
- private Button[][] buttons = new Button[3][3];
- private boolean player1Turn = true;
- private int roundCount;
- private int player1Points;
- private int player2Points;
- private TextView textViewPlayer1;
- private TextView textViewPlayer2;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- textViewPlayer1 = findViewById(R.id.text_view_p1);
- textViewPlayer2 = findViewById(R.id.text_view_p2);
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- String buttonID = "button_" + i + j;
- int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
- buttons[i][j] = findViewById(resID);
- buttons[i][j].setOnClickListener(this);
- }
- }
- Button buttonReset = findViewById(R.id.button_reset);
- buttonReset.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- resetGame();
- }
- });
- }
- @Override
- public void onClick(View v) {
- if (!((Button) v).getText().toString().equals("")) {
- return;
- }
- if (player1Turn) {
- ((Button) v).setText("X");
- } else {
- ((Button) v).setText("O");
- }
- roundCount++;
- if (checkForWin()) {
- if (player1Turn) {
- player1Wins();
- } else {
- player2Wins();
- }
- } else if (roundCount == 9) {
- draw();
- } else {
- player1Turn = !player1Turn;
- }
- }
- private boolean checkForWin() {
- String[][] field = new String[3][3];
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- field[i][j] = buttons[i][j].getText().toString();
- }
- }
- for (int i = 0; i < 3; i++) {
- if (field[i][0].equals(field[i][1])
- && field[i][0].equals(field[i][2])
- && !field[i][0].equals("")) {
- return true;
- }
- }
- for (int i = 0; i < 3; i++) {
- if (field[0][i].equals(field[1][i])
- && field[0][i].equals(field[2][i])
- && !field[0][i].equals("")) {
- return true;
- }
- }
- if (field[0][0].equals(field[1][1])
- && field[0][0].equals(field[2][2])
- && !field[0][0].equals("")) {
- return true;
- }
- if (field[0][2].equals(field[1][1])
- && field[0][2].equals(field[2][0])
- && !field[0][2].equals("")) {
- return true;
- }
- return false;
- }
- private void player1Wins() {
- player1Points++;
- Toast.makeText(this, "Player 1 wins!", Toast.LENGTH_SHORT).show();
- updatePointsText();
- resetBoard();
- }
- private void player2Wins() {
- player2Points++;
- Toast.makeText(this, "Player 2 wins!", Toast.LENGTH_SHORT).show();
- updatePointsText();
- resetBoard();
- }
- private void draw() {
- Toast.makeText(this, "Draw!", Toast.LENGTH_SHORT).show();
- resetBoard();
- }
- private void updatePointsText() {
- textViewPlayer1.setText("Player 1: " + player1Points);
- textViewPlayer2.setText("Player 2: " + player2Points);
- }
- private void resetBoard() {
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- buttons[i][j].setText("");
- }
- }
- roundCount = 0;
- player1Turn = true;
- }
- private void resetGame() {
- player1Points = 0;
- player2Points = 0;
- updatePointsText();
- resetBoard();
- }
- @Override
- protected void onSaveInstanceState(Bundle outState) {
- super.onSaveInstanceState(outState);
- outState.putInt("roundCount", roundCount);
- outState.putInt("player1Points", player1Points);
- outState.putInt("player2Points", player2Points);
- outState.putBoolean("player1Turn", player1Turn);
- }
- @Override
- protected void onRestoreInstanceState(Bundle savedInstanceState) {
- super.onRestoreInstanceState(savedInstanceState);
- roundCount = savedInstanceState.getInt("roundCount");
- player1Points = savedInstanceState.getInt("player1Points");
- player2Points = savedInstanceState.getInt("player2Points");
- player1Turn = savedInstanceState.getBoolean("player1Turn");
- }
- }
- Cdma
- import java.util.*;
- public class Main{
- private int[][] walshTable;
- private int[][] copy;
- private int[] channelSeq;
- public void setUp(int data[],int numStations){
- walshTable=new int[numStations][numStations];
- copy=new int[numStations][numStations];
- buildWalshTable(numStations, 0, numStations-1, 0, numStations-1, false);
- showWalshTable(numStations);
- for(int i=0;i<numStations;i++)
- for(int j=0;j<numStations;j++){
- copy[i][j]=walshTable[i][j];
- walshTable[i][j]*=data[i];
- }
- channelSeq=new int [numStations];
- for (int i=0;i<numStations;i++)
- for(int j=0;j<numStations;j++){
- channelSeq[i]+=walshTable[j][i];
- }
- }
- public void buildWalshTable(int len,int i1,int i2,int j1,int j2,boolean val){
- if(len==2){
- if(!val){
- walshTable[i1][j1]=1;
- walshTable[i1][j2]=1;
- walshTable[i2][j1]=1;
- walshTable[i2][j2]=-1;
- }
- else{
- walshTable[i1][j1]=-1;
- walshTable[i1][j2]=-1;
- walshTable[i2][j1]=-1;
- walshTable[i2][j2]=1;
- }
- }
- else{
- int midi=(i1+i2)/2;
- int midj=(j1+j2)/2;
- buildWalshTable(len/2,i1,midi,j1,midj,val);
- buildWalshTable(len/2,midi+1,i2,j1,midj,val);
- buildWalshTable(len/2,i1,midi,midj+1,j2,val);
- buildWalshTable(len/2,midi+1,i2,midj+1,j2,!val);
- }
- }
- public void showWalshTable(int numStations){
- System.out.println("Walsh Table");
- for(int i=0;i<numStations;i++)
- {
- for(int j=0;j<numStations;j++){
- System.out.print(walshTable[i][j]+"\t");
- }
- System.out.println();
- }
- }
- public void listenTo(int numStations,int sourceStation){
- int innerProduct=0;
- for (int i=0;i< numStations;i++)
- innerProduct+=copy[sourceStation][i]*channelSeq[i];
- System.out.println("Data received is : "+innerProduct/numStations);
- }
- public static void main(String args[]){
- Main channel= new Main();
- int numStations=4;
- int[] data=new int[numStations];
- data[0]=-1;
- data[1]=-1;
- data[2]=0;
- data[3]=1;
- channel.setUp(data,numStations);
- int sourceStation=3;
- channel.listenTo(numStations,sourceStation);
- }
- }
- cdma python
- def mult(c,d):
- return list(map(lambda x : x * d, c))
- c1=[1,1,1,1]
- c2=[1,-1,1,-1]
- c3=[1,1,-1,-1]
- c4=[1,-1,-1,1]
- C=[c1,c2,c3,c4]
- d=[int(x) for x in input("Enter data bits for 4 channels:").split()]
- result=[]
- for i in range(4):
- result.append(mult(C[i],d[i]))
- print(result)
- channel=[]
- for i in range(4):
- res=0
- for j in range(4):
- res+=result[j][i]
- channel.append(res)
- station=int(input("Enter station you want to listen:"))
- res2=0
- for i in range(4):
- res2+=channel[i]*C[station-1][i]
- print("Data bit transmitted:",res2//4)
- Phonebook
- ANDROID : PHONEBOOK ActivityMain.xml:
- <? xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context=".MainActivity" >
- <EditText
- android:id="@+id/etName"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:ems="10"
- android:inputType="textPersonName"
- android:hint="Name" />
- <EditText
- android:id="@+id/etPhone"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:ems="10"
- android:inputType="phone"
- android:hint="Phone Number"/>
- <EditText
- android:id="@+id/etEmail"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:ems="10"
- android:inputType="textEmailAddress"
- android:hint="Email"/>
- <EditText
- android:id="@+id/etAddress"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:ems="10"
- android:inputType="textPostalAddress"
- android:hint="Address"/>
- <Button
- android:id="@+id/btnAdd"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Add" />
- <Button
- android:id="@+id/btnView"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="View Contacts" />
- </LinearLayout>
- ActivityView.xml:
- <? xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".ViewActivity"
- android:orientation="vertical">
- <TextView
- android:id="@+id/tvData"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text=""
- android:scrollbars="vertical"/>
- </LinearLayout>
- MainActivity.java:
- package com.example.phonebookmcc;
- import android.content.Intent;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import static android.widget.Toast.makeText ;
- public class MainActivity extends AppCompatActivity {
- static DatabaseHandler dbH ;
- EditText etName,etPhone,etAddress,etEmail;
- Button btnAdd,btnView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main );
- etAddress=(EditText)findViewById(R.id.etAddress );
- etEmail=(EditText)findViewById(R.id.etEmail );
- etName=(EditText)findViewById(R.id.etName );
- etPhone=(EditText)findViewById(R.id.etPhone );
- btnAdd=(Button)findViewById(R.id.btnAdd );
- btnView=(Button)findViewById(R.id.btnView );
- dbH =new DatabaseHandler(this);
- btnAdd.setOnClickListener(new View.OnClickListener() {
- @Override public void onClick(View v) {
- String name=etName.getText().toString();
- String phone=etPhone.getText().toString();
- String email=etEmail.getText().toString();
- String address=etAddress.getText().toString();
- int check=dbH .addContact(name,phone,email,address);
- if (check==1){
- Toast.makeText (MainActivity.this,"CONTACT ADDED",Toast.LENGTH_SHORT ).show(); etAddress.setText("");
- etPhone.setText("");
- etEmail.setText("");
- etName.setText("");
- etName.requestFocus();
- }
- else {
- Toast.makeText (MainActivity.this,"Issues",Toast.LENGTH_SHORT ).show();
- }
- }
- });
- btnView.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent i=new Intent(MainActivity.this,ViewActivity.class);
- startActivity(i);
- }
- });
- } }
- ViewActivity.java:
- package com.example.phonebookmcc;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.text.method.ScrollingMovementMethod;
- import android.widget.TextView;
- public class ViewActivity extends AppCompatActivity {
- TextView tvData;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_view );
- tvData=(TextView)findViewById(R.id.tvData );
- String data=MainActivity.dbH .viewContact();
- tvData.setMovementMethod(new ScrollingMovementMethod());
- if(data.length()==0)
- tvData.setText("No records to show");
- else
- tvData.setText(data);
- } }
- DatabaseHandler.java:
- package com.example.phonebookmcc;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- public class DatabaseHandler extends SQLiteOpenHelper {
- SQLiteDatabase db;
- Context context;
- public DatabaseHandler(Context context) {
- super(context,"contacts",null,1);
- this.context=context;
- db=this.getWritableDatabase();
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL("create table phone(name TEXT,phoneNo text,email text,address text)"); }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- }
- public int addContact(String name,String phone,String email,String address){
- ContentValues values=new ContentValues();
- values.put("name",name);
- values.put("phoneNo",phone);
- values.put("email",email);
- values.put("address",address);
- long rid=db.insert("phone",null,values);
- if(rid<0)
- return 0;
- else
- return 1;
- }
- public String viewContact(){
- Cursor cursor=db.query("phone",new String[]{"name","phoneNo","email","address"},null,null,null,null,null);
- StringBuffer sb=new StringBuffer();
- cursor.moveToFirst();
- if(cursor.getCount()>0)
- do
- {
- sb.append("Name: "+cursor.getString(0)+"\nPhone No: "+cursor.getString(1)+"\nEmail: "+cursor.getString(2)+"\nAddress: "+cursor.getString(3)+
- "\n--------------------------------------------\n");
- }while (cursor.moveToNext());
- return sb.toString();
- } }
- //Timer
- ANDROID: TIMER activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:padding="10dp">
- <TextView
- android:id="@+id/tvCounter"
- android:layout_marginTop="150dp"
- android:layout_marginBottom="150dp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:layout_gravity="center_horizontal"
- android:textSize="60sp"
- android:text="00:00:00"
- android:textStyle="bold"
- android:textColor="#4CAF50"/>
- <EditText
- android:id="@+id/etMinutes"
- android:layout_width="wrap_content"
- android:layout_gravity="center"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10dp"
- android:layout_marginRight="10dp"
- android:hint="Enter Minutes!"
- android:padding="10dp"
- android:maxLength="4"
- android:inputType="number"
- android:textSize="26sp"
- android:gravity="center"
- android:textColor="#000000"
- android:textColorHint="#9E9E9E" />
- <LinearLayout
- android:layout_marginTop="30dp"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:gravity="center"
- android:padding="10dp"
- android:layout_gravity="center"
- android:layout_height="wrap_content">
- <Button
- android:id="@+id/btnStartStopTimer"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginRight="10dp"
- android:padding="10dp"
- android:background="#009688"
- android:textColor="#ffffff"
- android:textSize="16sp"
- android:text="Start Timer" />
- <Button
- android:id="@+id/btnResetTimer"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#009688"
- android:textColor="#ffffff"
- android:textSize="16sp"
- android:padding="10dp"
- android:layout_marginLeft="10dp"
- android:text="Reset Timer" />
- </LinearLayout>
- </LinearLayout>
- MainActivity.java:
- package com.karanmadhu.mytimer;
- import androidx.appcompat.app.AppCompatActivity;
- import android.os.Bundle;
- import android.os.CountDownTimer;
- import android.view.View;
- import android.view.WindowManager;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.util.concurrent.TimeUnit;
- public class MainActivity extends AppCompatActivity {
- TextView tvCounter;
- EditText etMinutes;
- Button btnStartStopTimer, btnResetTimer;
- private static CountDownTimer countDownTimer;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main); // Hide status bar
- getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) ; // Hide action bar getSupportActionBar().hide();
- tvCounter = findViewById(R.id.tvCounter);
- etMinutes = findViewById(R.id.etMinutes);
- btnStartStopTimer = findViewById(R.id.btnStartStopTimer);
- btnResetTimer = findViewById(R.id.btnResetTimer);
- btnStartStopTimer.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- if (countDownTimer == null) {
- String getMinutes = etMinutes.getText().toString(); //Get minutes from editText //Check validation over editText
- if (!getMinutes.equals("") && getMinutes.length() > 0) {
- int noOfMinutes = Integer.parseInt(getMinutes);
- int milliseconds = noOfMinutes * 60 * 1000; //Convert minutes into milliseconds
- countDownTimer = new CountDownTimer(milliseconds, 1000) {
- public void onTick(long millisUntilFinished) {
- long millis = millisUntilFinished;
- //Convert milliseconds into hour, minute and seconds
- long hours = TimeUnit.MILLISECONDS.toHours(millis);
- long minutes = TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(hours);
- long seconds = TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(minutes);
- String hms = String.format("%02d:%02d:%02d", hours, minutes, seconds);
- tvCounter.setText(hms);
- }
- public void onFinish() {
- tvCounter.setText("TIME'S UP!!");
- countDownTimer = null;
- btnStartStopTimer.setText("Start Timer");
- }
- }.start();
- btnStartStopTimer.setText("Stop Timer");//Change Text
- } else
- Toast.makeText(getApplicationContext(),
- "Please enter no. of Minutes.",
- Toast.LENGTH_SHORT).show();
- }
- else {
- countDownTimer.cancel();
- countDownTimer = null;
- btnStartStopTimer.setText("Start Timer");
- }
- }
- });
- btnResetTimer.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- if (countDownTimer != null) {
- countDownTimer.cancel();
- countDownTimer = null;
- }
- btnStartStopTimer.setText("Start Timer");
- tvCounter.setText("00:00:00");
- }
- });
- }
- }
- ///frequency reuse
- mport java.util.Scanner;
- class Cofrequency{
- public static void main(String[] args) {
- int[][] arr = new int[11][11];
- int i,j,k;
- Scanner sc=new Scanner(System.in);
- System.out.print("Enter i: ");
- i=sc.nextInt();
- System.out.print("Enter j: ");
- j=sc.nextInt();
- int x,y;
- System.out.println("\nBefore");
- for(x=0;x<11;x++){
- for(y=0;y<11;y++){
- System.out.print(arr[x][y] + " ");
- }
- System.out.println();
- }
- arr[5][5]=2;
- arr[5+j][5+j+i]=1;
- arr[5-j][5+j+i]=1;
- arr[5+j][5-j-i]=1;
- arr[5-j][5-j-i]=1;
- arr[5-i-j-1][5]=1;
- arr[5+i+j+1][5]=1;
- System.out.println("\nAfter");
- for(x=0;x<11;x++){
- for(y=0;y<11;y++){
- System.out.print(arr[x][y] + " ");
- }
- System.out.println();
- }
- for(i=2;i<=8;i++)
- for(j=3;j<8;j++)
- if (arr[i][j]==0)
- arr[i][j]=1;
- System.out.println("\nCluster Formation");
- for(x=0;x<11;x++){
- for(y=0;y<11;y++){
- System.out.print(arr[x][y] + " ");
- }
- System.out.println();
- }
- }
- }
- //fontandroid
- XML FILE:
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <TextView
- android:id="@+id/textView"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="30dp"
- android:layout_marginBottom="8dp"
- android:layout_marginTop="160dp"
- android:gravity="center"
- android:text="Hello World!"
- android:textSize="25sp"
- android:textStyle="bold"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.214" />
- <Button
- android:id="@+id/button1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="20dp"
- android:layout_marginStart="16dp"
- android:gravity="center"
- android:text="Change font size"
- android:textSize="25sp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- <Button
- android:id="@+id/button2"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="20dp"
- android:layout_marginBottom="8dp"
- android:layout_marginTop="8dp"
- android:gravity="center"
- android:text="Change color"
- android:textSize="25sp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/button1" />
- </android.support.constraint.ConstraintLayout>
- JAVA FILE:
- package com.example.sharad.font;
- import android.graphics.Color;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- public class MainActivity extends AppCompatActivity {
- int ch=1;
- float font=30;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- final TextView t= (TextView) findViewById(R.id.textView);
- Button b1= (Button) findViewById(R.id.button1);
- b1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- t.setTextSize(font);
- font = font + 5;
- if (font == 50)
- font = 30;
- }
- });
- Button b2= (Button) findViewById(R.id.button2);
- b2.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- switch (ch) {
- case 1:
- t.setTextColor(Color.RED);
- break;
- case 2:
- t.setTextColor(Color.GREEN);
- break;
- case 3:
- t.setTextColor(Color.BLUE);
- break;
- case 4:
- t.setTextColor(Color.CYAN);
- break;
- case 5:
- t.setTextColor(Color.YELLOW);
- break;
- case 6:
- t.setTextColor(Color.MAGENTA);
- break;
- }
- ch++;
- if (ch == 7)
- ch = 1;
- }
- });
- }
- }
- //graphical premitives
- Activity_main.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <ImageView
- android:id="@+id/image"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- />
- </RelativeLayout>
- MainActivity.java:
- package com.example.basicprimitivesmcc;
- import android.graphics.Bitmap;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.drawable.BitmapDrawable;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.widget.ImageView;
- public class MainActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Bitmap bm = Bitmap.createBitmap(720,1280,Bitmap.Config.ARGB_8888);
- ImageView i = (ImageView)findViewById(R.id.image);
- i.setBackgroundDrawable(new BitmapDrawable(bm));
- Canvas canvas=new Canvas(bm);
- Paint paint=new Paint();
- paint.setColor(Color.RED);
- paint.setTextSize(50);
- canvas.drawRect(400,200,600,700,paint);//left top right bottom
- canvas.drawCircle(200,200,100,paint);//cx,cy,radius
- canvas.drawRect(50,800,300,1100,paint);
- canvas.drawLine(500,800,500,1100,paint);//startx,starty,stopx,stopy
- }
- }
- ///gps
- ANDROID : GPS Give permissions after installing apk in your phone
- MainActivity.xml:
- <? xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/tvInfo"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="" />
- <Button
- android:id="@+id/btnShare"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Share" />
- </LinearLayout>
- MainActivity.java:
- package com.example.locationmcc;
- import android.Manifest;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.location.Address;
- import android.location.Geocoder;
- import android.location.Location;
- import android.support.annotation.NonNull;
- import android.support.annotation.Nullable;
- import android.support.v4.app.ActivityCompat;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.google.android.gms.common.ConnectionResult;
- import com.google.android.gms.common.api.GoogleApiClient;
- import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
- import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
- import com.google.android.gms.location.LocationServices;
- import java.io.IOException;
- import java.util.List;
- import java.util.Locale;
- public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
- TextView tvInfo;
- Button btnShare;
- GoogleApiClient gac;
- Location loc;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main );
- tvInfo = findViewById(R.id.tvInfo );
- btnShare = findViewById(R.id.btnShare );
- GoogleApiClient.Builder builder = new GoogleApiClient.Builder(this);
- builder.addApi(LocationServices.API );
- builder.addConnectionCallbacks(this);
- builder.addOnConnectionFailedListener(this);
- gac = builder.build();
- btnShare.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Intent i = new Intent(Intent.ACTION_SEND );
- i.setType("text/plain");
- i.putExtra(Intent.EXTRA_TEXT , "My address is: " + tvInfo.getText().toString());
- startActivity(i);
- }
- });
- }
- @Override
- protected void onStart() {
- super.onStart();
- if (gac != null)
- gac.connect();
- }
- @Override
- protected void onStop() {
- super.onStop();
- if (gac != null) {
- gac.disconnect();
- //btnShare.setEnabled(false);
- }
- }
- @Override
- public void onPointerCaptureChanged(boolean hasCapture) {
- }
- @Override
- public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
- Toast.makeText (this, "Connection Failed", Toast.LENGTH_SHORT ).show();
- }
- @Override
- public void onConnected(@Nullable Bundle bundle) {
- if (ActivityCompat.checkSelfPermission
- (this, android.Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission
- (this, android.Manifest.permission.ACCESS_COARSE_LOCATION ) != PackageManager.PERMISSION_GRANTED
- )
- {
- return;
- }
- loc = LocationServices.FusedLocationApi.getLastLocation(gac);
- if(loc != null){
- double lat = loc.getLatitude();
- double lon = loc.getLongitude();
- tvInfo.setText("Latitude: "+ lat+ " Longitude: "+ lon);
- Geocoder g = new Geocoder(this, Locale.ENGLISH );
- try {
- List<android.location.Address> la = g.getFromLocation(lat, lon, 1); //number of results = 1
- android.location.Address add = la.get(0);
- String msg = add.getCountryName() + " " + add.getAdminArea() + " " + add.getSubAdminArea() + " " + add.getLocality() + " " + add.getSubLocality() + " " + add.getThoroughfare() + " " +
- add.getSubThoroughfare() + " " + add.getPostalCode(); tvInfo.setText(msg);
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }
- else{
- tvInfo.setText("Please enable GPS");
- }
- }
- @Override
- public void onConnectionSuspended(int i) {
- Toast.makeText (this, "Connection Suspended "+CAUSE_NETWORK_LOST , Toast.LENGTH_SHORT ).show();
- }
- }
- AndroidManifest.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.locationmcc"> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
- <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
- <uses-permission android:name="android.permission.INTERNET"/>
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- 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>
- <meta-data
- android:name="com.google.android.geo.API_KEY"
- android:value="YOUR KEY HERE "/>
- </application> </manifest>
- Gradle: (Add this line) implementation 'com.google.android.gms:play-services:12.0.1'
- /login form react native
- import React, { Component } from 'react';
- import { Alert, Button, TextInput, View, StyleSheet } from 'react-native';
- export default class App extends Component {
- constructor(props) {
- super(props);
- this.state = {
- username: '',
- password: '',
- };
- }
- onLogin() {
- const { username, password } = this.state;
- Alert.alert('Credentials', `${username} + ${password}`);
- }
- render() {
- return (
- <View style={styles.container}>
- <TextInput
- value={this.state.username}
- onChangeText={(username) => this.setState({ username })}
- placeholder={'Username'}
- style={styles.input}
- />
- <TextInput
- value={this.state.password}
- onChangeText={(password) => this.setState({ password })}
- placeholder={'Password'}
- secureTextEntry={true}
- style={styles.input}
- />
- <Button
- title={'Login'}
- style={styles.input}
- onPress={this.onLogin.bind(this)}
- />
- </View>
- );
- }
- }
- const styles = StyleSheet.create({
- container: {
- flex: 1,
- alignItems: 'center',
- justifyContent: 'center',
- backgroundColor: '#ecf0f1',
- },
- input: {
- width: 200,
- height: 44,
- padding: 10,
- borderWidth: 1,
- borderColor: 'black',
- marginBottom: 10,
- },
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement