Wednesday, 15 April 2015

Android Studio Temperature Conversion

Hi , Now I am moving towards new Program that is Android Studio Calculator , a very basic calculator which will look as below :


Temperature Converter Android Studio


Step 1 : Create a new project named as Temperature Convertor Android Studio

Temperature Converter Android Studio

  Step 2 : activity_temperature_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"
    android:background="@color/myColor">

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:ems="10"
        android:inputType="numberSigned|numberDecimal" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/celsius" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/fahrenheit" />
    </RadioGroup>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/radioGroup1"
        android:layout_below="@+id/radioGroup1"
        android:layout_marginTop="22dp"
        android:text="@string/calc"
        android:onClick="onClick" />

</RelativeLayout>


 Step 3 : TemperatureMainActivity.java




package com.example.sony.temperatureconvertorandroidstudio;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;


public class TemperatureMainActivity extends ActionBarActivity {
    private EditText text;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_temperature_main);
        text = (EditText) findViewById(R.id.editText1);

    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button1:
                RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0);
                RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1);
                if (text.getText().length() == 0) {
                    Toast.makeText(this, "Please enter a valid number",
                            Toast.LENGTH_LONG).show();
                    return;
                }

                float inputValue = Float.parseFloat(text.getText().toString());

                if (celsiusButton.isChecked()) {
                    text.setText(String
                            .valueOf(ConverterUtil.convertFahrenheitToCelsius(inputValue)));
                    celsiusButton.setChecked(false);
                    fahrenheitButton.setChecked(true);
                } else {
                    text.setText(String
                            .valueOf(ConverterUtil.convertCelsiusToFahrenheit(inputValue)));
                    fahrenheitButton.setChecked(false);
                    celsiusButton.setChecked(true);
                }
                break;
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_temperature_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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

 Step 4 : ConvertorUtil.java


package com.example.sony.temperatureconvertorandroidstudio;

/**
 * Created by sony on 4/15/2015.
 */
public class ConverterUtil {

    public static float convertFahrenheitToCelsius(float fahrenheit) {
        return ((fahrenheit - 32) * 5 / 9);
    }

    // converts to fahrenheit
    public static float convertCelsiusToFahrenheit(float celsius) {
        return ((celsius * 9) / 5) + 32;
    }
}


 Output:


Temperature Converter Android Studio

Read More »

Friday, 13 February 2015

WAP to find first Position of a String in another String


Hi , My today's post is on a program to find  Position of a String in another String.
For Example ,Given two strings, A and B, how to find the first position of B in A? For instance, A = " ab123cdefgcde"; B= "cde" Then the first position of B in A is 6.



import java.util.Scanner;




public class SB

{




 public static void main(String[] args) {
 
  {
   String test=null;
   String index=null;
   int j=0;
   System.out.println("Enter the string");
   test= new Scanner(System.in).nextLine();
   System.out.println("Enter the substring you want to count");
   index= new Scanner(System.in).nextLine();
   for (int i = 0; i <= test.length()-index.length(); i++) {
    String testing=test.substring(i, index.length()+i);
   
    if(testing.equals(index))
    {
    j=i+1;
    System.out.println("It occurs first  at "+ j +" position" );
    break;
    }
   }

  }
 }
}
   
Read More »

Saturday, 7 February 2015

Android Studio Calculator Example

Hi , Now I am moving towards new Program that is Android Studio Calculator , a very basic calculator which will look as below :


Calculator Example Android Studio

Step 1 : Create a new project named as Android Studio Calculator Example 

Calculator Example Android Studio


Step 2 : activity_main.xml


Now , add one textview to show output .
In two textview with text as First no. and other Second no. 
Two editview to enter no.s
Four buttons as ADD,SUB,MUL and DIV

Below is the code for 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:weightSum="1"
        android:background="#fff9fdff">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:text=""
            android:id="@+id/textView1"
            android:visibility="visible"
            android:autoText="true"
            android:background="#fffffad9" />

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="@android:dimen/app_icon_size"
            android:layout_weight="0.19">


            <TextView
                android:layout_width="10dp"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="First No."
                android:id="@+id/textView2"
                android:layout_weight="0.09" />

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/editText1"
                android:layout_weight="0.09" />



            <TextView
                android:layout_width="10pt"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Second No."
                android:id="@+id/textView3"
                android:layout_weight="0.09" />

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/editText2"
                android:layout_weight="0.09" />
        </LinearLayout>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="ADD"
            android:id="@+id/button1"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="SUB"
            android:id="@+id/button2" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="MUL"
            android:id="@+id/button3" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="DIV"
            android:id="@+id/button4" />
    </LinearLayout>

</RelativeLayout>

Step 3: MainActivity.java



Read More »

Thursday, 5 February 2015

Android Button Example

Hi Today's my post on How to use Button in Android .Button is a UI widget . If you are very beginner first go through my first example of Android Hello World which helps you how to run a very basic program in android.


Step 1 : Create a new Project



Android Button Example


Step 2: Create a new activity.I named it as Button Activity



Android Button Example


Step 3:

 Now you can see screen as activity_button.xml. Just drag two edittext widget and one button .
Go to properties and change name as ADD or anything you want .

Android Button Example

Code of activity_button.xml is as shown below :



<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".ButtonActivity">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="41dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText2"
        android:layout_below="@+id/editText1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="45dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add"
        android:id="@+id/button"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>



Step 4: 


In activity class ButtonActivity.java we need to make a event listener for ADD button which will take values from both edittext widgets ,add them and show result on toast.


Code for ButtonActivity.java



package com.example.sony.androidbuttonexample;

import android.support.v7.app.ActionBarActivity;
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 ButtonActivity extends ActionBarActivity {
    private Button btnSum;
    private EditText edt1,edt2;

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

        addbuttonlistener();
    }

    public void addbuttonlistener()
    {


       btnSum =(Button)findViewById(R.id.button);
        edt1 = (EditText)findViewById(R.id.editText1);
        edt2 = (EditText)findViewById(R.id.editText2);

        btnSum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

             int s1=Integer.parseInt(edt1.getText().toString());
             int s2=Integer.parseInt(edt2.getText().toString());

                int s3= s1 + s2;

                Toast.makeText(getApplicationContext(),String.valueOf(s3),Toast.LENGTH_LONG).show();

            }
        });
    }

}


Output :



Android Button Example




Read More »

Sunday, 1 February 2015

Hello World Android Studio

Hi, After installing Android Studio Let us see how to create our very first program Hello World

Step1 : Welcome to Android Studio 


Hello World Android Studio
Hello World Android

 You will see the above screen , Click on first option Start a New Android Studio Project.


Step 2: New Project 


Hello World Android Studio
Hello World Android


 Give name as your with in application name .I have given it as Hello World on my convenience.
You can also change project location and click next.

Step 3: Form factors


Hello World Android Studio
Hello World Android

 In this Step you have to choose the device on which you want to run your app .I have chosen only for tablets and phone .So we have tremendous options in tablet and phone emulators in Android Studio.

Step 4: Choose activity 


Hello World Android Studio
Hello World Android

 You can choose any activity from the options as shown above .If you are beginner I would suggest you to choose  Blank Acitvity  for your first app.


Step 5: Activity Name 



Hello World Android Studio
Hello World Android



Fill the activity name and other things as shown above . As my program is HelloWorld so i have chosen name as Hello Activity .

Step 6: Project opens  



Hello World Android Studio

Hello World Android

Step 7: Code  


A screen of main project will open as shown below with text view as Hello World .


Hello World Android Studio



As this is our first program need not to go  in depth just run it with Run green button in the top 

Whole structure and code we will learn in next program.



Step 8: Run 


Run this program and this will show you the output on emulator .I am using Nexus 7 as AVD .How to add a AVD in your studio you can see here.


Hello World Android Studio
Hello World Android
If you are facing some errors in running the above program you can refer below as I was also facing many errors so I have drafted them for your help .



Read More »

Android Studio Installation

Android Studio is the official IDE for Android application development, based on IntelliJ IDEA. On top of the capabilities you expect from IntelliJ, Android Studio offers:
  • Flexible Gradle-based build system
  • Build variants and multiple apk file generation
  • Code templates to help you build common app features
  • Rich layout editor with support for drag and drop theme editing
  • Lint tools to catch performance, usability, version compatibility, and other problems
  • ProGuard and app-signing capabilities
  • Built-in support for Google Cloud Platform, making it easy to integrate Google Cloud Messaging and App Engine

You can download Android Studio from the link below:

http://developer.android.com/sdk/index.html


 After downloading Android Studio keep on clicking next and install android studio .


You may like :
Android Studio Hello World Program

Android Button Example


Read More »

Tuesday, 27 January 2015

Program for Shifting characters in a String eg. String "abcde" should be printed as "eabcd"



import java.util.Scanner;

public class SB
{
public static void main(String []args) {

Scanner in = new Scanner(System.in);

         System.out.println("Enter a string to shift characters");
      String  test = in.nextLine();


 For further visit    Javainhouse
Read More »

Saturday, 24 January 2015

Find length of String without using any length or size method

import java.util.Scanner;


public class SB

{

public static void main(String[] args) {

{
String test;
      Scanner in = new Scanner(System.in);

     For further program please visit my new blog http://javainhouse.blogspot.com










Read More »

Print "welcome bangalore" as welcome erolagnab

import java.util.Scanner;


public class SB

{

public static void main(String[] args) {

{
String test="welcome bangalore";

String reverse="";


 int i=0;

 String a[]=test.split(" ");


 for (int j = a[1].length()-1; j >= 0; j--)
 {

reverse = reverse + a[1].charAt(j);
 }


 a[1]=reverse;
 test=a[0] +" "+ a[1];
 System.out.println(test);



}
}
}











Read More »

Thursday, 22 January 2015

Program in java to check whether a String is palindrome or not

import java.util.*;
public class Palindrome
{
   public static void main(String args[])
   {
      String original, reverse = "";
      Scanner in = new Scanner(System.in);
 
Program in java to check whether a String is palindrome or not       System.out.println("Enter a string to check palindrome");
      original = in.nextLine();
 
      int length = original.length();
 
      
     
      for ( int i = length - 1 ; i >= 0 ; i-- )
         reverse = reverse + original.charAt(i);
 
      if(reverse.equals(original))
      System.out.println("Yes String is palindrome");
      else
       System.out.println("NO String is not palindrome"); 
   }
}
Read More »