Thursday, 31 December 2015

Utilizing Resources and Media in Andoird

Utilizing Resources and Media:
===============================================
ImageView  example
===============================================


(1)write   code  for   MainActivity.java :
package imageview.test.om.com.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

   
@Override
   
protected void onCreate(Bundle savedInstanceState)
    {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        ImageView  image=(ImageView)findViewById(R.id.
imageView);
        image.setImageResource(R.drawable.
om);
    }

}

(2)write code for  activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout

    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:fitsSystemWindows="true"

    tools:context=".MainActivity">



    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"

        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">



        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"

            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"

            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />



    </android.support.design.widget.AppBarLayout>



    <include layout="@layout/content_main" />



    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"

        android:layout_width="wrap_content" android:layout_height="wrap_content"

        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"

        android:src="@android:drawable/ic_dialog_email" />



</android.support.design.widget.CoordinatorLayout>


(3)write  content_main.xml   file (note  keep  om.jpg image file inside drawable and then your path  “res/drawable/om.jpg” directory :
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"

    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    tools:showIn="@layout/activity_main" tools:context=".MainActivity">



    <ImageView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/imageView"

        android:src="@drawable/om"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="197dp" />



</RelativeLayout>

 output :

==========================================
Audio example :
==========================================


(1)write   code  for    MainActivity.java    file:
package menu.test.om.com.mypractice;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);

        Button b=(Button)findViewById(R.id.
button);
        b.setOnClickListener(
new View.OnClickListener() {
           
@Override
           
public void onClick(View view) {
                MediaPlayer  mp=MediaPlayer.create(MainActivity.
this,R.raw.song);
                mp.start();
            }
        });
    }


}

(2)write code for  activity_main.xml file:
<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout

    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:fitsSystemWindows="true"

    tools:context=".MainActivity">



    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"

        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">



        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"

            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"

            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />



    </android.support.design.widget.AppBarLayout>



    <include layout="@layout/content_main" />



    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"

        android:layout_width="wrap_content" android:layout_height="wrap_content"

        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"

        android:src="@android:drawable/ic_dialog_email" />



</android.support.design.widget.CoordinatorLayout>
 
 
(3) write    code content_main.xml    file  :
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"

    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    tools:showIn="@layout/activity_main" tools:context=".MainActivity">







    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="PLAY"

        android:id="@+id/button"

        android:layout_marginTop="147dp"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true"></Button>



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceLarge"

        android:text="PLAYING AUDIO"

        android:id="@+id/textView"

        android:layout_alignBottom="@+id/button"

        android:layout_alignParentTop="true"

        android:layout_alignLeft="@+id/button"

        android:layout_alignStart="@+id/button" />



</RelativeLayout>

 output :

================================================================================================================================================

TOGGLE BUTTON  example:
================================================================================================================================================

(1)   write   code  for MainActivity.java:
package newyear.test.om.com.happynewyear;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {
    TextView
response;
    MediaPlayer
mp;
   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);

        
response=(TextView)findViewById(R.id.response);
       
response.setText("Click on play to listen surprise word by OM");
       
mp=MediaPlayer.create(MainActivity.this,R.raw.song);
       
final ToggleButton playStopButton=(ToggleButton)findViewById(R.id.playstop_btn);
        playStopButton.setOnClickListener(
new View.OnClickListener() {
           
@Override
           
public void onClick(View view) {

               
if(playStopButton.isChecked())
                {
               
response.setText("HAPPY NEW YEAR FROM OM");
                   
mp.start();

                }
               
else
               
{
                   
response.setText("Click on play to listen surprise word by OM");
                    
mp.pause();
                }
            }
        });

    }


}

(2)write   code for activity_main.xml   file :
<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout

    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:fitsSystemWindows="true"

    tools:context=".MainActivity">



    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"

        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">



        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"

            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"

            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />



    </android.support.design.widget.AppBarLayout>



    <include layout="@layout/content_main" />



    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"

        android:layout_width="wrap_content" android:layout_height="wrap_content"

        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"

        android:src="@android:drawable/ic_dialog_email" />



</android.support.design.widget.CoordinatorLayout>

(3)write  code for  content_main.xml   file :
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"

    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    tools:showIn="@layout/activity_main" tools:context=".MainActivity">



    <ToggleButton

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/playstop_btn"

        android:textOn="Stop"

        android:textOff="Play"

        android:layout_marginTop="197dp"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true"

        android:checked="false" />



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceLarge"

      android:gravity="center"

        android:id="@+id/response"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="60dp" />



</RelativeLayout>


 output :



Wednesday, 30 December 2015

Array in Android

   
Arrays:

Arrays refer to Collection of values or elements.Arrays are known for their capability of direct reference;

(1)String Arrays :
As the name Suggests ,the String array provides an array of strings. such a resource is popularly  used with selection widgets such as
Listview and spinner that need to display  a collection of selectable items to the user.

to define string array syntax is:

<string-array  name="array_name">
<item>text1</item>
<item>text 2</item>
.....
.....
</string>
(2)Integer Arrays:

<integer-array name="OddNumbers">
    <item>1</item>
    <item>3</item>
    <item>5</item>
    <item>7</item>
    <item>9</item>
</integer-array>


  ==========================================================================================================
      String Array example:
 =====================================================


  (1)  write  code for   MainActivity.java :
package checkbox.test.om.com.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        TextView  fruitsView=(TextView)findViewById(R.id.
fruits_view);
        String []  fruitsArray=getResources().getStringArray(R.array.
fruits);
        String str=
"";
       
for(int i=0; i<fruitsArray.length;i++)
        {
            str +=fruitsArray[i]+
"\n";
        }
        fruitsView.setText(str);
    }


}

(2)write code  activity_main.xml  file:

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout

    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:fitsSystemWindows="true"

    tools:context=".MainActivity">



    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"

        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">



        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"

            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"

            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />



    </android.support.design.widget.AppBarLayout>



    <include layout="@layout/content_main" />



    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"

        android:layout_width="wrap_content" android:layout_height="wrap_content"

        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"

        android:src="@android:drawable/ic_dialog_email" />



</android.support.design.widget.CoordinatorLayout>

(3)write code for content_main.xml file:
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"

    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    tools:showIn="@layout/activity_main" tools:context=".MainActivity">

    <TextView

        android:id="@+id/fruits_view"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" />

</LinearLayout>

(4)write code for strings.xml file :

<resources>

    <string name="app_name">StringArrayApp</string>

    <string name="action_settings">Settings</string>

    <string name="title_activity_string_array_app">StringArrayAppActivity</string>

    <string-array name="fruits">

        <item>Apple</item>

        <item>Mango</item>

        <item>Orange</item>

        <item>Grapes</item>

        <item>Banana</item>

    </string-array>

</resources>



output :

========================================================================
========================================================================

Integer array example:
========================================================================

(1) first   write code for  MainActivity.java :

package checkbox.test.om.com.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        TextView  oddNumsView=(TextView)findViewById(R.id.
oddnums_view);
       
int []  oddNumsArray=getResources().getIntArray(R.array.OddNumbers);
        String str=
"";
       
for(int i=0; i<oddNumsArray.length;i++)
        {
            str +=oddNumsArray[i]+
"\n";
        }
        oddNumsView.setText(str);
    }


}

(2)write code for activity_main.xml  file:
<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout

    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:fitsSystemWindows="true"

    tools:context=".MainActivity">



    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"

        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">



        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"

            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"

            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />



    </android.support.design.widget.AppBarLayout>



    <include layout="@layout/content_main" />



    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"

        android:layout_width="wrap_content" android:layout_height="wrap_content"

        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"

        android:src="@android:drawable/ic_dialog_email" />



</android.support.design.widget.CoordinatorLayout>

(3)write  code for content_main.xml file :

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"

    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    tools:showIn="@layout/activity_main" tools:context=".MainActivity">

    <TextView

        android:id="@+id/oddnums_view"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" />

</LinearLayout>

(4) write   code   for   strings.xml   file  :
<resources>

    <string name="app_name">InegerArrayApp</string>

    <string name="action_settings">Settings</string>

    <integer-array name="OddNumbers">

        <item>1</item>

        <item>3</item>

        <item>5</item>

        <item>7</item>

        <item>9</item>

    </integer-array>

</resources>



 output :




Recent Posts

Powered by Blogger.

Pages

About Me

My Photo
B.E.(COMPUTER ENGINEER) PHP Web developer & Provide Training for Web Development , Digital Marketing ,Android Mobile Apps Development

 

© 2013 ANDROID TRAINING IN MUMBAI BY OM SIR. All rights resevered. Designed by Templateism

Back To Top