Saturday, 6 April 2013

Most favorite Question : Logic Behind "System.out.println()"



System.out.println prints the argument passed, into the System.out which is generally stdout.

  • System – is a final class and cannot be instantiated. Therefore all its memebers (fields and methods) will be static and we understand that it is an utility class. As per javadoc, “…Among the facilities provided by the System class are standard input, standard output, and error output streams; access to externally defined properties and environment variables; a means of loading files and libraries; and a utility method for quickly copying a portion of an array…”



  • out – is a static member field of System class and is of typePrintStream. Its access specifiers are public final. This gets instantiated during startup and gets mapped with standard output console of the host. This stream is open by itself immediately after its instantiation and ready to accept data. When running a program from windows command line, it is the standard console.



  • println – println prints the argument passed to the standard console and a newline. There are multiple println methods with different arguments (overloading). Every println makes a call toprint method and adds a newline. print calls write()and the story goes on like that.

Thursday, 4 April 2013

Android Holo Colors Generator

Change Tabhost Setting Programattically


tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String arg0) {

// Log.i("***Selected Tab", "Im currently in tab with index::" +
// tabHost.getCurrentTab());
selTabPos = tabHost.getCurrentTab();
for (int i = 0; i < tabHost.getTabWidget().getTabCount(); i++) {
if (i == selTabPos) {
tabHost.getTabWidget()
.getChildAt(i)
.setBackgroundColor(Color.parseColor("#4a92ce"));
} else {
tabHost.getTabWidget()
.getChildAt(i)
.setBackgroundColor(Color.parseColor("#253040"));
}


}
}
});

for (int i = 0; i < tabHost.getTabWidget().getTabCount(); i++) {
if (i == selTabPos) {
tabHost.getTabWidget().getChildAt(i)
.setBackgroundColor(Color.parseColor("#4a92ce"));
} else {
tabHost.getTabWidget().getChildAt(i)
.setBackgroundColor(Color.parseColor("#253040"));
}

tabHost.getTabWidget().getChildAt(i).getLayoutParams().height = height / 15;
}

Wednesday, 3 April 2013

How to add tab layout without letting the activity to extend TabActivity? Part -2


How to add tab layout without letting the activity to extend TabActivity?

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TabHost tabHost = (TabHost) findViewById(R.id.mytabhost);
    tabHost.setup();

    TabSpec tab1 = tabHost.newTabSpec("TAB_1");
    tab1.setIndicator("Tab 1");
    tab1.setContent(R.id.tab1);
    tabHost.addTab(tab1);

    //tab 2 etc...
    TabSpec tab2 = tabHost.newTabSpec("TAB_2");
    tab2.setIndicator("Tab 2");
    tab2.setContent(R.id.tab2);
    tabHost.addTab(tab2);
}
-----------------------------------------------------------------------------------------------------------------------
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mytabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >


    <LinearLayout android:id="@+id/LinearLayout01"
        android:orientation="vertical" android:layout_height="fill_parent"
        android:layout_width="fill_parent">

        <TabWidget android:id="@android:id/tabs"
            android:layout_height="wrap_content" android:layout_width="fill_parent"></TabWidget>

        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_height="fill_parent" android:layout_width="fill_parent">
            <LinearLayout android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:id="@+id/tab1">
                <!-- tab 1 content goes here -->
            </LinearLayout>
            <LinearLayout android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:id="@+id/tab2">
                <!-- tab 2 content goes here -->
            </LinearLayout>

        </FrameLayout>

    </LinearLayout>

</TabHost>


Tuesday, 2 April 2013

Circular Menu Part-1

Main Activity


public class TestMenuActivity extends Activity {
CircleView cView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

int numberOfElements = 11;
View[] elems = new View[numberOfElements];

EditText tv = new EditText(this);
tv.setText("Some text");
tv.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
elems[0] = tv;

for (int i = 1; i < numberOfElements - 1; i++) {
Button newButton = new Button(this);
newButton.setText("Button   " + i);
newButton.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
elems[i] = newButton;
}

Spinner sp = new Spinner(this);
sp.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
elems[numberOfElements - 1] = sp;

cView = new CircleView(this, 115, elems);
setContentView(cView);
}
}


How To Create Circle View (Dynamically...)


public class CircleView extends RelativeLayout {
static final int centerId = 111;
private final int radius;

private RelativeLayout.LayoutParams createNewRelativeLayoutParams() {
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ABOVE, centerId);
lp.addRule(RIGHT_OF, centerId);
return lp;
}

private View prepareElementForCircle(View elem, int distX, int distY) {
RelativeLayout.LayoutParams lp = createNewRelativeLayoutParams();

elem.measure(0, 0);
int deltaX = elem.getMeasuredWidth() / 2;
int deltaY = elem.getMeasuredHeight() / 2;
lp.setMargins(distX - deltaX, 0, 0, radius - distY - deltaY);
elem.setLayoutParams(lp);
return elem;
}

public CircleView(Context context, int radius, View[] elements) {
super(context);
this.radius = radius;

RelativeLayout.LayoutParams lpView = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
this.setLayoutParams(lpView);

View center = new View(context);
center.setId(centerId);
RelativeLayout.LayoutParams lpcenter = new RelativeLayout.LayoutParams(
0, 0);
lpcenter.addRule(CENTER_HORIZONTAL);
lpcenter.addRule(CENTER_VERTICAL);
center.setLayoutParams(lpcenter);
this.addView(center);

this.addView(prepareElementForCircle(elements[0], 0, 0));
if (elements.length % 2 == 0) {
this.addView(prepareElementForCircle(elements[elements.length / 2],
0, 2 * radius));
}
if (elements.length > 2) {
for (int i = 1; i <= (elements.length - 1) / 2; i++) {
int y = i * 4 * radius / elements.length;
int x = (int) Math.sqrt(Math.pow(radius, 2)
- Math.pow((radius - y), 2));
this.addView(prepareElementForCircle(elements[i], x, y));
this.addView(prepareElementForCircle(elements[elements.length
- i], -x, y));
}
}
}

}


Monday, 1 April 2013

Google to Launch an Android-powered Smartwatch






























We are hearing a lot of news of Smart watch, that Apple and Samsung wants to launch Smart Watch now Google also wants to Jump in launch Android-powered Smartwatch. Google owns a patent of Dual-screen smart watch since 2011, and now Android team is working on that watch its possibiity that Googlesmart watch hit the market before Google Glass.



A smartwatch boom seems to be going on. Besides smaller companies debuting new wrist technology, the tech giants are also grabbing for a piece of the pie. Google is the newest company to supposedly be stepping into the watch world. According to The Financial Times, Google’s smartwatch is allegedly being developed by the company’s Android unit rather than its X Lab.
This is telling because it means that, unlike Google Glass, the company may be looking to get a consumer product out to users on a speedier timeline. According to The Financial Times, Google filed a patent application for a “smart watch” in 2011. Apparently, the patent has a dual-screened display and a interactive user interface. Over the past couple of weeks Apple has been rumored to be coming out with a smartwatch and Samsung has confirmed it’s working on one. Of course, none of these mammoth companies are breaking new ground. A lot of smartwatches, fitness bands, or some hybrid of the two have already been unveiled, including the long-awaited Pebble wristwatch and the 007-inspired Martian.