Wednesday, 29 June 2016

Create custom actionBar

 android:textColorHint="@color/hint_color"
 android:background="@android:color/transparent"

To do so, follow those 2 simple steps:

1. Java Code

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.actionbar);
Where R.layout.actionbar is the following XML.

2. 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="wrap_content"
    android:layout_gravity="center"
    android:orientation="vertical">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="YOUR ACTIVITY TITLE"
    android:textColor="#ffffff"
    android:textSize="24sp" />
</LinearLayout>

Saturday, 25 June 2016

Most asked interview programs

Program 1-    Upcasting and Downcasting

package com.sanjay.java.basic;

class Parent {

/*Parent() {
System.out.println("Parent constructor.");
}*/

public void show() {
System.out.println("I am Parent class method.");
}

}

public class Child extends Parent {
/*
Child() {
System.out.println("Child constructor.");
}*/

public void show() {
System.out.println("I am Child class method.");
}

static void check(Parent p) {

Child t = (Child) p;
t.show();
System.out.println("Down casting done");
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("*********Down casting*************");
// CastingTest test=(CastingTest) new Parent(); //Exception in thread
// "main" java.lang.ClassCastException: at run time
Parent b = new Child();
Child.check(b);

System.out.println("*********up casting*************");
Parent obj = new Child();
obj.show();

}

}

Output-
*********Down casting*************
I am Child class method.
Down casting done
*********up casting*************
I am Child class method.


Program 2-  Reverse Sentence

package com.sanjay.java.basic;

public class ReverseSentence {

public String doReverse(String str) {

String revStr = "";
String sp[] = str.split(" ");

for (int i = sp.length - 1; i >= 0; i--) {
revStr = revStr + sp[i]+" ";
}

return revStr;

}

public static void main(String arg[]) {
String str = "I live in delhi";
ReverseSentence obj = new ReverseSentence();
String reverse=obj.doReverse(str);
System.out.println("Reverse sentence = " + reverse);
}
}

Output-  
Reverse sentence = delhi in live I 

Program 3-      Reverse String

package com.sanjay.java.basic;

public class ReverseString {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

String str="My Name is sanjay verma";
String rev="";
for(int i=str.length()-1;i>=0;i--){
rev=rev+str.charAt(i);
}
System.out.println(rev);
}

}

Output-
amrev yajnas si emaN yM


Program- 4  Comosition Uses [ Using composition achieve inheritance ]

package com.sanjay.java.basic;

class Test {
public void name() {

System.out.println("I am android profesional.");

}
}

final class MyComposition {

public void show() {
System.out.println("I am Composition method.");
}
}


public class CompositionTest extends Test {

MyComposition composition;

public void display() {

composition = new MyComposition();
composition.show();
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

CompositionTest obj = new CompositionTest();

obj.display();
obj.name();

}

}

Output-
I am Composition method.
I am android profesional.

Program 5-  Prime Number check

package com.sanjay.java.basic;

public class PrimeNumber {

public void checkPrime(int num) {
int j = 2;
// for (int i = 0; i <= num; i++) {
// while(num<=500){
while (j < num) {
if (num % j == 0) {
break;
} else {
j++;
}

}
// }
if (num == j) {
System.out.println("Prime number -" + num);
} else {
System.out.println("Number is not prime");
}
// }
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

PrimeNumber obj = new PrimeNumber();
obj.checkPrime(23);
}
}

Output-
Prime number -23

Program- 6  Palindrome Check

package com.sanjay.java.basic;

public class PalindromeChek {
// static String mString = "madam";

private boolean isPalindrome(String str) {

int i = str.length() - 1;
int j = 0;

while (i > j) {

if (str.charAt(i) != str.charAt(j)) {
return false;
}
i--;
j++;

}
return true;

}

private void integerValueCheck(int n) {
int r, temp, sum = 0;

// int n = 454;

temp = n;

while (n > 0) {

r = n % 10;
sum = (sum * 10) + r;
n = n / 10;

}
if (sum == temp) {
System.out.println("Number is palindrome");
} else {
System.out.println("Number not palindrome");
}
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// ********************* STRING CHECK********************************
PalindromeChek obj = new PalindromeChek();
boolean b = obj.isPalindrome("aman");

//System.out.println("string check " + b);
if (b) {
System.out.println("String is palindrome");
} else {
System.out.println("String not palindrome");
}
// **********************INTEGER CHECK*******************************

obj.integerValueCheck(425124);

}

}

Output-
String is palindrome
Number is palindrome

Program -7  (Armstrong number check)

package com.sanjay.java;

public class ArmStrongNumber {

void getArmStrong(int num) {
int n, sum = 0;
int rem, cube;
n = num;
while (n > 0) {
rem = n % 10;
cube = rem * rem * rem;
sum = sum + cube;
n = n / 10;
}
if (sum == num) {
System.out.println("Armstrong number =" + sum);
}else{
System.out.println("Number not an ArmStrong");
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub
ArmStrongNumber obj = new ArmStrongNumber();
obj.getArmStrong(153);
                obj.getArmStrong(155);
}


}
Output-
Armstrong number =153
Number not an ArmStrong



Program -8  (One interface can extends more than one interface)

package com.sanjay.java;
interface printable{
void print();
}
interface scannable  {
void scan();
}
interface functional extends scannable , printable{
void work();
}

class A6 implements functional {
public void print(){System.out.println("print");}
public void scan(){System.out.println("scan");}
public void work(){System.out.println("work");}
public static void main(String args[]){
A6 obj = new A6();
obj.print();
obj.scan();
obj.work();
 }
}

Output-
    print
   scan
   work

Program -9  (Count number occurrence in array)


public class NumberOccurrance {
public static void main(String[] args) {
// TODO Auto-generated method stub
       int[] arr={2,3,4,2,5,6,2,1,3,2,8,9};
       int[] solArr=new int[12];
/**
*  Here i am creating solArr with 12 length, it initialized all index with 0 value.
* ___________________________________________________
* |_0_|_1_|_2_|_3_|_4_|_5_|_6_|_7_|_8_|_9_|_10_|_11_|
arr[i]=  2   3   4   2   5   6   2   1   3   2   8     9
   
   solArr[arr[i]]= 
                  solArr[arr[0]]=> solArr[2]=1
                  solArr[arr[1]]=> solArr[3]=1
                  solArr[arr[2]]=> solArr[4]=1
                  solArr[arr[3]]=> solArr[2]=1+1 = 2
                  solArr[arr[4]]=> solArr[5]=1
                  solArr[arr[5]]=> solArr[6]=1
                  solArr[arr[6]]=> solArr[2]=2+1 = 3
                  solArr[arr[7]]=> solArr[1]=1
                  solArr[arr[8]]=> solArr[3]=1+1 = 2
                  solArr[arr[9]]=> solArr[2]=3+1 = 4
                  .
                  .
                  So if same number is already in solArr index then increment by 1 in below code.
*/
       for(int i=0;i<arr.length;i++){
       solArr[arr[i]]+=1;
       }
System.out.println("Array elements and it's count");

           for (int j = 0; j < solArr.length; j++) {
               System.out.println("element "+j +" count "+solArr[j]);
     }
       if(solArr[2]>=solArr[3]){
System.out.println(2);
}else{
System.out.println(1);
}
}
}
Output-


index 0  count 0
index 1  count 1
index 2  count 4
index 3  count 2
index 4  count 1
index 5  count 1
index 6  count 1
index 7  count 0
index 8  count 1
index 9  count 1
index 10  count 0
index 11  count 0
2