Monday, 31 August 2015

Android Programming Tutorial 43 : ActionBar In Android

ActionBar:

ActionBar are added on the top of the Title bar in an Activity screen.

Action Bar in Android is used to provide navigation and to perform some actions.





In Android ActionBar Break up into 4 parts like Application ICON, View Control,
Action Buttons, OverFlow.

In the above figure its clearly seen that on Number 1 is an App Icon,
                                                                          2 View Control
                                                                          3 Action Buttons
                                                                          4 Overflow


OUT PUT Of The Tutorial:




   
Create Android Application:

  • File >> New >> Android Application
  • Enter Project name: ActionBarApp
  • Package: com.ambilpursunil.newapp
  • Keep other default selections, click Next until you reach Finish

                                   

     1.Simply Copy and Past the Code which is display below in the  MainActivity.java 

    package com.ambilpursunil.newapp;

    import android.app.ActionBar;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.Toast;

    public class MainActivity extends Activity {

                @Override
                protected void onCreate(Bundle savedInstanceState) {
                            super.onCreate(savedInstanceState);
                            setContentView(R.layout.activity_main);
                           
                            ActionBar actionBar = getActionBar();
                            //actionBar.setTitle("Hello");
                }

                @Override
                public boolean onCreateOptionsMenu(Menu menu) {
             // Inflate the menu; this adds items to the action bar if it is present.
                            getMenuInflater().inflate(R.menu.main, menu);
                            return true;
                }

                @Override
                public boolean onOptionsItemSelected(MenuItem item) {
                           
                            switch(item.getItemId()){
                           
                            case R.id.phone:
    Toast.makeText(getApplicationContext(), "You Selected Phone",                                               Toast.LENGTH_SHORT).show();
                                        break;
                           
                            }
                           
                            return super.onOptionsItemSelected(item);
                }
    }

     2.Simple Copy and Past the below code:  actvitiy_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="com.example.addingactionbuttons.MainActivity" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />

    </RelativeLayout>

    3.Change the main.xml file which is located res>>main>>main.xml Copy and Past the below code: 

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context="com.example.addingactionbuttons.MainActivity" >

        <item
            android:id="@+id/phone"
            android:title="@string/phone"
            android:showAsAction="ifRoom|withText"
            android:icon="@drawable/phone"/>
       
        <item
            android:id="@+id/computer"
            android:title="@string/computer"
            android:showAsAction="always"
            android:icon="@drawable/computer"/>
       
        <item
            android:id="@+id/gamepad"
            android:title="@string/gamepad"
            android:showAsAction="always"
            android:icon="@drawable/gamepad"/>
       
        <item
            android:id="@+id/video"
            android:title="@string/video"
            android:showAsAction="ifRoom"
            android:icon="@drawable/video"/>
       
        <item
            android:id="@+id/email"
            android:title="@string/email"
            android:showAsAction="ifRoom"
            android:icon="@drawable/email"/>
       
        <item
            android:id="@+id/camera"
            android:title="@string/camera"
            android:showAsAction="always"
            android:icon="@drawable/camera"/>

    </menu>

     4.Copy and past the code for manifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.ambilpursunil.newapp"
        android:versionCode="1"
        android:versionName="1.0" >

        <uses-sdk
            android:minSdkVersion="18"
            android:targetSdkVersion="18" />


        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name" >
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />

               <category android:name="android.intent.category.LAUNCHER"/>
                </intent-filter>
            </activity>
                </application>

    </manifest>

    5.Right click on the project and Run As-->Android Application




    OUT PUT Of The Tutorial:




       
    OutPut:

    When our application launch Just check.....it 

    Please Send Your Comments To ambilpura.sunil@gmail.com






    Stay Tune For Next Tutorial... Custom Toast In Android:

      

    Android Programming Tutorial 42 : WebServices Using GET Method In Android

    Create Android Application:

    • File >> New >> Android Application
    • Enter Project name: WebServicesUsingGETApp
    • Package: com.ambilpursunil.newapp
    • Keep other default selections, click Next until you reach Finish

                                     

       1.Simply Copy and Past the Code which is display below in the  MainActivity.java 

      package com.ambilpursunil.newapp;

      import java.io.IOException;
      import java.io.InputStream;

      import org.apache.http.HttpEntity;
      import org.apache.http.HttpResponse;
      import org.apache.http.client.HttpClient;
      import org.apache.http.client.methods.HttpGet;
      import org.apache.http.impl.client.DefaultHttpClient;
      import org.apache.http.protocol.BasicHttpContext;
      import org.apache.http.protocol.HttpContext;

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

      public class MainActivity extends Activity implements OnClickListener {

                  Button my_button;

                  @Override
                  protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.activity_main);
                  findViewById(R.id.btn_REST).setOnClickListener(this);
                   }
                  @Override
                  public void onClick(View v) {
                  my_button = (Button) findViewById(R.id.btn_REST);
                  my_button.setClickable(false);
                  new GetDataRESTfull().execute();
                  }
      public class GetDataRESTfull extends AsyncTask<Void, Void, String> {
                  protected String getASCIIContentFromEntity(HttpEntity entity)
                  throws IllegalStateException, IOException {
                  InputStream inputStream = entity.getContent();
                  StringBuffer stringBuffer = new StringBuffer();
                  int n = 1;
                  while (n > 0) {
                  byte[] b = new byte[4096];
                  n = inputStream.read(b);
                  if (n > 0)
                  stringBuffer.append(new String(b, 0, n));
                  }// while
                  return stringBuffer.toString();
                  }// getASCII
                  @Override
                  protected String doInBackground(Void... params) {
                  HttpClient httpClient = new DefaultHttpClient();
                  HttpContext httpContext = new BasicHttpContext();
                  //Here we are Using GET Method 
                   
                  HttpGet httpGet = new HttpGet(
       "http://www.cheesejedi.com/rest_services/get_big_cheese.php?puzzle=1");
                  String text = null;
                  try {
      HttpResponse httpResponse = httpClient.execute(httpGet,
                  httpContext);
                  HttpEntity httpEntity = httpResponse.getEntity();
                  text = getASCIIContentFromEntity(httpEntity);
                 } catch (Exception e) {
                 e.getLocalizedMessage();
                 }
                 return text;
                }// doInBack
                @Override
                protected void onPostExecute(String result) {
                if (result != null) {
        EditText editTextREST = (EditText) findViewById(R.id.editTextREST);
                editTextREST.setText(result);
                }// if
                my_button.setClickable(true);
                              }// doPost
                  }
      }



       2.Simple Copy and Past the below code:  actvitiy_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:orientation="vertical" >

          <TextView
              android:id="@+id/textView1"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="RESTful WebServices"
              android:textAppearance="?android:attr/textAppearanceLarge"
              android:textColor="#176CEC"
              android:gravity="center" />

          <Button
              android:id="@+id/btn_REST"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="GET Data"
              android:layout_marginLeft="100dp"
              android:padding="14dp" />

          <EditText
              android:id="@+id/editTextREST"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:minLines="13"
              android:editable="false"
              android:textSize="12sp"
              android:maxLines="13"
              android:ems="10" />

      </LinearLayout>

      3.Copy and past the code for manifest.xml

      <?xml version="1.0" encoding="utf-8"?>
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.ambilpursunil.newapp"
          android:versionCode="1"
          android:versionName="1.0" >

          <uses-sdk
              android:minSdkVersion="18"
              android:targetSdkVersion="18" />

          <uses-permission android:name="android.permission.INTERNET" />

          <application
              android:allowBackup="true"
              android:icon="@drawable/ic_launcher"
              android:label="@string/app_name"
              android:theme="@style/AppTheme" >
              <activity
                  android:name=".MainActivity"
                  android:label="@string/app_name" >
                  <intent-filter>
                      <action android:name="android.intent.action.MAIN" />

                      <category android:name="android.intent.category.LAUNCHER" />
                  </intent-filter>
              </activity>
                  </application>

      </manifest>

      4.Right click on the project and Run As-->Android Application


      OutPut:

      When our application launch you will find a GET Button and a EditText simply click on the GET Button and see the output on the screen.

      Please Send Your Comments To ambilpura.sunil@gmail.com




      Stay Tune For Next Tutorial...  ActionBar In Android: