Showing posts with label New to Android. Show all posts
Showing posts with label New to Android. Show all posts

Friday, 6 September 2013

Android Screen Dimension

How to get the Screen Dimension in Android Application


Hi, This is the way to easily get the screen dimensions of currently running android device.

1.activity_main.xml

<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=".MainActivity" >

    <TextView
        android:id="@+id/dimen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="screen dimensions" />

    <TextView
        android:id="@+id/res"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" 
        android:layout_below="@+id/dimen"/>

</RelativeLayout>

2.MainActivity.java

package com.sri.screendimension;

import android.os.Bundle;
import android.view.Display;
import android.widget.TextView;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Point;

public class MainActivity extends Activity {
TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Display disp = getWindowManager().getDefaultDisplay();
Point size = new Point();
disp.getSize(size);
int width = size.x;
int height = size.y;
tv = (TextView) findViewById(R.id.res);
tv.setText(width + "<----->" + height);
}

}

3.Run the Application.


Thursday, 5 September 2013

Multiple Button Click Listener

Multiple Button Listener

Now we'll learn how to manage Multiple Button click Event Listener.
As you have learn the single button click event in previous tutorial.
Now we'll Move to the Multiple Buttons Tutorial.

1.activity_main.xml

<LinearLayout 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=".MainActivity" 
    android:orientation="vertical">

    <Button

        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="test1" />
    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="test2" />
    <Button
        android:id="@+id/btn3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="test3" />
    <Button
        android:id="@+id/btn4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="test4" />


</LinearLayout>


2.MainActivity.java

package com.sri.buttontest;

import android.app.Activity;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{

Button btn1,btn2,btn3,btn4;

@Override

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
btn4 = (Button) findViewById(R.id.btn4);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
}

@Override

public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn1:
Toast.makeText(getApplicationContext(), "test1 is clicked", Toast.LENGTH_LONG).show();
break;
case R.id.btn2:
Toast.makeText(getApplicationContext(), "test2 is clicked", Toast.LENGTH_LONG).show();
break;
case R.id.btn3:
Toast.makeText(getApplicationContext(), "test3 is clicked", Toast.LENGTH_LONG).show();
break;
case R.id.btn4:
Toast.makeText(getApplicationContext(), "test4 is clicked", Toast.LENGTH_LONG).show();
break;
default:
break;
}
}

}
3. That's all.Run the Project Now.
Comments and doubts are encouraged.
Thank you.

Simple Button Listener

Button Event Listener


New for Android, then go step by step.
First we will learn the button click event.

First write the xml file in new project.

1.activity_main.xml

<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=".MainActivity" >

    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="test" />

</RelativeLayout>



MainActivity.java

package com.sri.buttontest;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn1);
btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Test is clicked",
Toast.LENGTH_LONG).show();
}
});
}

}


3.Now this is how the button listener works.
Thank you.