flipkart

Thursday, November 20, 2014

ViewFlipper in Android

Today I am going this in this post ViewFlipper in Android before going code we know what is ViewFlipper and what is use first thing is ViewFlipper is a some kind Viewer we can use Any business kind of applications we can use first of all if suppose 5 products is there we can show using viewflipper it will show same place all images with span of time today i am going present viewflipper with example code

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
   
    <RelativeLayout
        android:id="@+id/RelativeLayout02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ViewFlipper
            android:id="@+id/ViewFlipper01"
            android:layout_width="fill_parent"
   android:layout_height="200dp" >
   <RelativeLayout
                android:layout_width="fill_parent"
android:layout_height="fill_parent"
                >

                <ImageView
                    android:id="@+id/imageView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:src="@drawable/images" />
               
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="fill_parent"
android:layout_height="fill_parent"
                >

                <ImageView
                    android:id="@+id/imageView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:src="@drawable/image1" />
               
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
             
                android:orientation="vertical" >

                  <ImageView
                    android:id="@+id/imageView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:src="@drawable/image2" />
               
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
               
                android:orientation="vertical" >

                  <ImageView
                    android:id="@+id/imageView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:src="@drawable/image3" />
               
            </RelativeLayout>
           
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
               
                android:orientation="vertical" >

                   <ImageView
                    android:id="@+id/imageView4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:src="@drawable/image4" />
               
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
             
                android:orientation="vertical" >

                   <ImageView
                    android:id="@+id/imageView5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:src="@drawable/image5" />
               
            </RelativeLayout>
           
        </ViewFlipper>
    </RelativeLayout>
   
    <RelativeLayout
        android:id="@+id/RelativeLayout03"
        android:layout_below="@+id/RelativeLayout02"
     
        android:layout_width="fill_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/Previous"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Previous" >
        </Button>

        <Button
            android:id="@+id/Next"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="5dp"
            android:layout_marginRight="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Next" >
        </Button>
       
    </RelativeLayout>
   
</LinearLayout>

This layout file main activity and it consists of ViewFlipper,Buttons and ImageView inside RelativeLayout and LinearLayout






This are my images

MainActivity.java

package com.example.viewflipper;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ViewFlipper;

public class MainActivity extends Activity {

ViewFlipper viewFlipper;
Button Next, Previous;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        viewFlipper = (ViewFlipper) findViewById(R.id.ViewFlipper01);
       
        Next = (Button) findViewById(R.id.Next);
        Previous = (Button) findViewById(R.id.Previous);
        viewFlipper.setAutoStart(true);
        viewFlipper.setFlipInterval(1000);  
        viewFlipper.startFlipping();
     
        Next.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub

viewFlipper.showNext();
}
});
       
        Previous.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub

viewFlipper.showPrevious();
}
});
    }


}

This application two buttons one is for when ever user press it will show previous image and one for next image it will show if user still show different images based on viewflipper configuration following video is finally my application output


If you any doubts and comments are always welcome 

Tuesday, November 18, 2014

SMS application using Android mobile

Today every body using SMS sent to other means friends and business purpose etc.. This post i will decided to explain  SMS based application for that i will with example and my outputs. Following code my SMS project that user enter number and message after press button it will retrieve information using EditText and it will send destination number

activity_sms_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"
    android:background="@drawable/buttonshape"
    tools:context="com.example.smssent.SmsMainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="TO"
        android:textSize="24dp" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1"
        android:ems="10"
        android:hint="Enter 10 digits only"
        android:inputType="number" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText1"
        android:text="Message"
        android:textSize="24dp" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView2"
        android:ems="10"
        android:hint="Enter message"
        android:inputType="textMultiLine" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText2"
        android:layout_below="@+id/editText2"
        android:layout_marginTop="15dp"
        android:text="SENT" />


</RelativeLayout>


This my main layout file it under resource folder it consists of 2 TextView, 2 EditText and Button when ever user press button it will send message and background is "buttonshape" for source code press following link  


SmsMainActivity,java

package com.example.smssent;

import android.support.v7.app.ActionBarActivity;
import android.telephony.SmsManager;
import android.util.Log;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SmsMainActivity extends ActionBarActivity {

EditText ephoneno,emessage;
Button sent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms_main);
ephoneno=(EditText) findViewById(R.id.editText1);
emessage=(EditText) findViewById(R.id.editText2);
sent=(Button) findViewById(R.id.button1);
sent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String num=ephoneno.getText().toString();
String message=emessage.getText().toString();
Log.d("SmsMainActivity","phone number:"+num+"message:"+message);
try{
SmsManager smsM=SmsManager.getDefault();
smsM.sendTextMessage(num,null, message, null,null);
Toast.makeText(getApplicationContext(), "message sent", Toast.LENGTH_LONG).show();
}catch(Exception ex){
Toast.makeText(getApplicationContext(), ex.getMessage().toString(),Toast.LENGTH_LONG).show();
}
}
});
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

This one having Button when ever user press button it will sent message to destination number following is my screen shots 

This is main screen of project and user have to enter phone number and message after press button it will sent to destination number
 After sent will it will show Toast message for indicating successfully sent
This is SmsMainActivity.java

Accelerometer using Android mobile phone

Hi friends ,
  Today I am going to explain Accelerometer using existing hardware inside  Android mobile first you have to know why we have to learn Accelerometer answer is i think all most android apps they using indirectly some sensors preexisting mobile hardware and especially Accelerometer is used swipe application and touch based application so Accelerometer is very use-full sensor one of existing.
Today this i will discuss with Accelerometer  sensor following is my source code of project.

activity_accelerometer.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.accelerometer.AccelerometerActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="64dp"
        android:layout_marginTop="52dp"
        android:textSize="24dp"
        />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
         android:textSize="24dp"
         />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
         android:textSize="24dp"
         />

</RelativeLayout>


This layout of project it have 3 Text-view one for X value ,2nd for Y value and 3rd for Z value

AccelerometerActivity.java

package com.example.accelerometer;

import android.support.v7.app.ActionBarActivity;
import android.annotation.SuppressLint;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class AccelerometerActivity extends ActionBarActivity implements SensorEventListener {
TextView tvx,tvy,tvz;
SensorManager sensormanager;

@SuppressLint("ServiceCast")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_accelerometer);
tvx=(TextView) findViewById(R.id.textView1);
tvy=(TextView) findViewById(R.id.textView2);
tvz=(TextView) findViewById(R.id.textView3);
sensormanager=(SensorManager) getSystemService(SENSOR_SERVICE);
sensormanager.registerListener(this, sensormanager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);

}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){

// assign directions
float x=event.values[0];
float y=event.values[1];
float z=event.values[2];

tvx.setText("X: "+x);
tvy.setText("Y: "+y);
tvz.setText("Z: "+z);
}

}


@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub

}
}

This java file inside first sensor initialization with type of Accelerometer sensor and implements SensorEventListener when values are changed it will onSensorChanged subroutine (function) you can see following video is my out of above code 


This is video of my Accelerometer project any it comes to end you any doubts and comments will be accepted 

Saturday, November 15, 2014

Find out battery level of android mobile

Today i am going explain how to calculate battery level of android mobile first of all are know with out battery we can't use mobile phone so battery is very important. Today i will display battery percentage on screen using android API any now i will explain battery level with example following code is my battery level finder.

I am using broadcast-receiver it is used when battery level changed it will update

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



    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@+id/textView1" />


</RelativeLayout>




MainActivity.java



package com.example.battery;

import android.support.v7.app.ActionBarActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

TextView battery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
battery =(TextView) findViewById(R.id.textView2);
batterylevelfinder();
}

private void batterylevelfinder() {
BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            context.unregisterReceiver(this);
            int currentLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
            int level = -1;
            if (currentLevel >= 0 && scale > 0) {
                level = (currentLevel * 100) / scale;
            }
            battery.setText("Battery Level Remaining: " + level + "%");
         
        }


    };
    IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    registerReceiver(batteryLevelReceiver, batteryLevelFilter);

}

@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) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}


This main java file and i register broadcast-receiver for finding battery level when ever it changed it will display on screen using text-view following is my screenshot of project

This output of battery level


Friday, November 14, 2014

Create custom button in Android using xml file

Today i am going to explain customized buttons in android using xml file. In our daily life we are developing number of new applications we are using number of buttons also  but may some body knows how to customize button in android, this will new in android programming , I will explain taking example code with proof screenshots of concept.


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

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button2"
        android:layout_centerHorizontal="true"
        android:background="@drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
        android:layout_marginTop="36dp"
        android:text="Button" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
        android:layout_below="@+id/button3"
        android:layout_marginTop="22dp"
        android:layout_toLeftOf="@+id/button3"
        android:text="Button" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
        android:layout_alignBaseline="@+id/button4"
        android:layout_alignBottom="@+id/button4"
        android:layout_toRightOf="@+id/button3"
        android:text="Button" />

    <Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
        android:layout_alignBaseline="@+id/button2"
        android:layout_alignBottom="@+id/button2"
        android:layout_alignLeft="@+id/button5"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/button1"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="28dp"
        android:text="Button" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="16dp"
        android:layout_toRightOf="@+id/button2"
        android:background="@drawable/buttonshape"
        android:shadowColor="#A8A8A8"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="5"
        android:text="Button" />

    <Button
        android:id="@+id/button7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button3"
        android:layout_below="@+id/button5"
        android:layout_marginTop="34dp"
        android:layout_toLeftOf="@+id/button5"
        android:background="@drawable/buttonshape"
        android:shadowColor="#A8A8A8"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="5"
        android:text="Button" />


</RelativeLayout>

This is my layout of main activity and this one having buttons with relativelayout

buttonshape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:radius="14dp"
/>
<gradient
android:angle="45"
android:centerX="35%"
android:centerColor="#8240A8"
android:startColor="#58E862"
android:endColor="#D6FFF1"
android:type="linear"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<size
android:width="100dp"
android:height="30dp"
/>
<stroke
android:width="3dp"
android:color="#75AD3E"
/>

</shape>

This main file for customizing button above code will generate following screenshot



Inside layout file i am used for above screen shot is buttonshape.xml if customize colors according you simple change red co-lour digits it will customize according you. following are some of screenshots of my project




This are screenshots of after changing colors please give feedback about my blog it is very useful for me.


Thursday, November 13, 2014

How to run Linux commands in Android application?

Today i am going explain how to run Linux commands in android application how it's use-full for developer i will clarify your doubts. Hope fully every body android is based Linux platform that means it will kernel and like as Linux if you know basic commands on linux it will run same as in android also suppose take one example i want directory of files that Files concept in java replacing we can get using simple "ls"  command it will give same out put . This post i will explain with example following is my source code.


MainActivity.java

package com.example.linuxcommand;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

TextView tv;
EditText et;
Button bt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt=(Button) findViewById(R.id.button1);
tv=(TextView) findViewById(R.id.textView2);
et=(EditText) findViewById(R.id.editText1);
bt.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Log.d("TAG", "This is onclick method");

String command= et.getText().toString();
tv.setText(command);
String output=LinuxRun(command);
tv.setText(output);
}


});
}

public String LinuxRun(String command) {
     StringBuffer retu = new StringBuffer();
     Log.d("TAG", "Linux coomand.."+command);
     Process p;
     try {
       p = Runtime.getRuntime().exec(command);
       p.waitFor();
       BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
             String name = "";
       while ((name = reader.readLine())!= null) {
         retu.append(name + "\n");
       }
     } catch (Exception e) {
       e.printStackTrace();
     }
         String output = retu.toString();
     return output;
   }


@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) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

}

Above file is java file it consists of initialization if elements and out will show using TextView on screen


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

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="26dp"
        android:layout_toRightOf="@+id/textView1"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText1"
        android:layout_alignBottom="@+id/editText1"
        android:layout_alignParentLeft="true"
        android:text="Enter Command" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="24dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="output" />

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/scrollView1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="38dp" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </ScrollView>
         />


</RelativeLayout>



This is layout of main activity it having Text-view,Edit-text and Button after entering command in Edit-text when ever user press button it show out put on screen following are my screenshot of above code.
 The above screenshot is showing of "ls" command out put and i applied ScrollView to TextView it will increase based on output

The above screenshot is showing of "cat /proc/cpuinfo" it will show CPU information of Android mobile it will show CPU information 


Thank for studying please put comments and doubts of post i will improve next posts it will be very  use-full for me. 

Saturday, November 8, 2014

Spinner in Android

Spinners provide a quick way to select on value from a set . In the default state , a spinner shows its currently selected value or first one of list. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one.

     Today i am going explain example of simple spinner 

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

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />


</RelativeLayout>

Above code layout of main activity and i declared spinner 

MainActivity.java

package com.vamsi.spinner;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends ActionBarActivity {

Spinner sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp=(Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
       R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

sp.setAdapter(adapter);

}

@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;
}



}

This file main activity file this one having declaration of adapter for showing list of names when ever you press spinner 

strings.xml

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

    <string name="app_name">Spinner</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
  
    <string-array name="planets_array">
        <item>Hyderabad</item>
        <item>Bangalore</item>
        <item>Nellore</item>
        <item>Chennai</item>
        <item>Jaipur</item>
        <item>Delhi</item>
        <item>Mumbai</item>
        <item>Vijayavada</item>
    </string-array>



</resources>

Any way above file is values of project and spinner is default it show's first name of list in above list is "Hyderabad" .finally out put is 


This is first screen of my project and following screen is when ever user press it will show and one more with spinner is it will default take scroll-view of list .



Thanks for studying please leave comments and doubts .