Tuesday, 24 May 2016

How to add Toolbar In android Programming

Hello Friend's

now in android programming all is want advance technology like martial design  so i want to share how to add tool in activity ..

Main . xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.example.kogta.androidadvance.MainActivity">


    <include        
android:id="@+id/app_bar"  
      layout="@layout/app_bar"/>
    <TextView       
 android:layout_below="@+id/app_bar"  
      android:layout_width="wrap_content"      
  android:layout_height="wrap_content"      
  android:text="Hello World!" />
</RelativeLayout>

create app_bar.xml file

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar 
xmlns:android="http://schemas.android.com/apk/res/android"  
  android:layout_width="match_parent"   
 android:layout_height="wrap_content"   
 android:background="#CCC">

</android.support.v7.widget.Toolbar>




java file

package com.example.kogta.androidadvance;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;


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

        toolbar=(Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);
    }

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

Convert Centimeter to inches In android programming .

Hello Friend's . would i like to share today how to we can convert centimeter to inches in android programming .
first thing design part so first create new android studio project then go to main. xml class

and just copy that code

<?xml version="1.0" encoding="utf-8"?>
<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.kogta.imagesave.MainActivity">
<TextView
android:layout_width="wrap_content"   
     android:layout_height="wrap_content"  
      android:textAppearance="?android:attr/textAppearanceMedium"  
      android:text="Centimeter"       
 android:id="@+id/textView"       
 android:layout_alignParentTop="true"       
 android:layout_alignParentLeft="true"       
 android:layout_alignParentStart="true"       
android:layout_marginTop="48dp" />
<EditText  
 android:layout_width="wrap_content"     
   android:layout_height="wrap_content"     
   android:inputType="number"      
  android:ems="10"        
android:id="@+id/editText"   
     android:layout_alignBottom="@+id/textView"  
      android:layout_toRightOf="@+id/textView"  
      android:layout_toEndOf="@+id/textView"      
  android:layout_marginLeft="40dp"   
android:layout_marginStart="40dp" />
<TextView  
 android:layout_width="wrap_content"      
  android:layout_height="wrap_content"     
   android:textAppearance="?android:attr/textAppearanceMedium"    
    android:text=" Inches"        android:id="@+id/textView2"   
     android:layout_below="@+id/textView"       
 android:layout_alignParentLeft="true"   
     android:layout_alignParentStart="true"  
android:layout_marginTop="40dp" />
<EditText  
android:layout_width="wrap_content"   
     android:layout_height="wrap_content"  
      android:inputType="number"       
 android:ems="10"       
 android:id="@+id/editText2"   
     android:layout_alignBottom="@+id/textView2"
        android:layout_alignLeft="@+id/editText"    
android:layout_alignStart="@+id/editText" />
<Button
android:layout_width="wrap_content" 
       android:layout_height="wrap_content"      
  android:text="Convert "       
 android:id="@+id/button"        
android:layout_centerVertical="true"      
android:layout_centerHorizontal="true" />
</RelativeLayout>







then java code .------------->>>>>>>>>



package com.example.kogta.imagesave;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
//ImageView image; @Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//image=(ImageView)findViewById(R.id.image);
final EditText editTextcenti=(EditText)findViewById(R.id.editText);
final EditText editTextinch=(EditText)findViewById(R.id.editText2);
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
double centimeter=Double.valueOf( editTextcenti.getText().toString());
double inches= centimeter * 0.393700787;
editTextinch.setText(String.valueOf(inches));
}
});
}
}


the App is Done .Its so simple so just try it your self 

Wednesday, 4 May 2016

Android Bluetooth Printing with Actual Printer Device Example

 import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Button;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;
public class MainActivity extends Activity {
}

/ will show the statuses like bluetooth open, close or data sent
TextView myLabel;
// will enable user to enter any text to be printed
EditText myTextbox;
// android built in classes for bluetooth operations
BluetoothAdapter mBluetoothAdapter;
BluetoothSocket mmSocket;
BluetoothDevice mmDevice;
// needed for communication to bluetooth device / network
OutputStream mmOutputStream;
InputStream mmInputStream;
Thread workerThread;
byte[] readBuffer;
int readBufferPosition;
volatile boolean stopWorker;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try {
        // more codes will be here
    }catch(Exception e) {
        e.printStackTrace();
    }
}



/ we are going to have three buttons for specific functions
Button openButton = (Button) findViewById(R.id.open);
Button sendButton = (Button) findViewById(R.id.send);
Button closeButton = (Button) findViewById(R.id.close);
// text label and input box
myLabel = (TextView) findViewById(R.id.label);
myTextbox = (EditText) findViewById(R.id.entry);


// open bluetooth connection
openButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        try {
            findBT();
            openBT();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
});


this will find a bluetooth printer device
void findBT() {
    try {
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if(mBluetoothAdapter == null) {
            myLabel.setText("No bluetooth adapter available");
        }
        if(!mBluetoothAdapter.isEnabled()) {
            Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBluetooth, 0);
        }
        Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
        if(pairedDevices.size() > 0) {
            for (BluetoothDevice device : pairedDevices) {
                 
                // RPP300 is the name of the bluetooth printer device
                // we got this name from the list of paired devices
                if (device.getName().equals("RPP300")) {
                    mmDevice = device;
                    break;
                }
            }
        }
        myLabel.setText("Bluetooth device found.");
    }catch(Exception e){
        e.printStackTrace();
    }
}