Friday, October 31, 2014

Button click method 2

This is button click method 2 using onclicklistener
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/abc_action_bar_home_description"
/>
view raw activity_my.xml hosted with ❤ by GitHub
public class MyActivity extends ActionBarActivity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Log.d("SOMU","On Create called");
setContentView(R.layout.activity_my);
one = (Button) findViewById(R.id.button1);
one.setOnClickListener(this);
}
}
view raw MyActivity.java hosted with ❤ by GitHub

Button Click on Android

This is the first method of button Click
public void doSomething(View v){
if(v.getId()==R.id.button1){
Log.d("SOMU","Button was clicked");
}
}
view raw MyActivity.java hosted with ❤ by GitHub
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/abc_action_bar_home_description"
android:onClick="doSomething"/>
view raw myActivity.xml hosted with ❤ by GitHub

Know when the orientation changes in Android

This code snippet will make you know when the user changes the Orientation of the application screen.
<activity
android:name=".MyActivity"
android:label="@string/app_name"
android:configChanges="orientation|screenSize">
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
Log.d("SOMU","Orientation Changed to Landscape mode");
}
else if(newConfig.orientation==Configuration.ORIENTATION_PORTRAIT){
Log.d("SOMU","Orientation Changed to Potrait mode");
}
}
view raw MyActivity.java hosted with ❤ by GitHub

This code will make the orientation fix the activity to Landscape
<activity
android:name=".MyActivity"
android:label="@string/app_name"
android:screenOrientation="landscape">

Handy Operators in Java

This is some handy inc or dec operators.
import java.util.Scanner;
public class IncrementOperator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int pen = 5;
int pencil = 1;
//Post increment
System.out.println(pen++);
System.out.println(pen);
//Pre increment
System.out.println(++pencil);
// add n to it
pencil +=10;
System.out.println(pencil);
// mul n to it
pencil *=10;
System.out.println(pencil);
}
}
6
2
12
120
view raw output.txt hosted with ❤ by GitHub

Basic Mat Operations using Java

This is some basic maths operations that is calculated using a java program.
import java.util.Scanner;
public class MathOperators {
public static void main(String []args){
Scanner sc = new Scanner(System.in);
int girls,boys,people;
girls = sc.nextInt();
boys= sc.nextInt();
// Addition
people = boys + girls;
System.out.println(people);
// Sub
people = boys - girls;
System.out.println(people);
// Multiply
people = boys * girls;
System.out.println(people);
// Divide
people = boys / girls;
System.out.println(people);
// Divide
people = boys % girls;
System.out.println(people);
}
}
3
6
9
3
18
2
0
view raw output.txt hosted with ❤ by GitHub

A Basic Calc using Java

This is the code snippet for a basic add program by getting the user input.
import java.util.Scanner;
public class BacisCalc {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double fnum,snum,answer;
System.out.println("Enter the First number: ");
fnum = sc.nextDouble();
System.out.println("Enter the Second number: ");
snum = sc.nextDouble();
answer = fnum + snum;
System.out.println(answer);
}
}
view raw BasicCalc.java hosted with ❤ by GitHub
Enter the First number:
53.5
Enter the Second number:
65.9
119.4
view raw output.txt hosted with ❤ by GitHub

Read data from user using Java

This is reading input from the user using Scanner in Java.
hey now
hey now
view raw output hosted with ❤ by GitHub
import java.util.Scanner;
public class ReadInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(sc.nextLine());
}
}
view raw ReadInput.java hosted with ❤ by GitHub

Variables in Java

This is how variables are to be printed in the Java program.
The number is 5.
It can also be written as this 5
view raw output.java hosted with ❤ by GitHub
public class Variables {
public static void main(String[] args) {
int i=5;
System.out.print("The number is ");
System.out.print(i);
System.out.println(".");
System.out.print("It can also be written as this "+i);
}
}
view raw Variables.java hosted with ❤ by GitHub

My First Java Program

This was my first java program and still it is being the first code to write on IDE to check weather the JAVA things are setup correctly.
public class HelloWorld {
public static void main(String[] args) {
System.out.print("Hello World");
}
}
view raw HelloWorld.java hosted with ❤ by GitHub
Hello World
view raw Output.txt hosted with ❤ by GitHub