Featured post

Displaying Multiple Fragments in a single Activity

Sunday, 24 July 2016

Displaying Multiple Fragments in a single Activity


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"
    android:layout_height="match_parent"  
    android:orientation="vertical">  
    <FrameLayout
          android:id="@+id/credentialscontainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#d3d3d3" >      
    </FrameLayout>
      <FrameLayout
          android:id="@+id/submissioncontainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ff0000ff" >        
      </FrameLayout>
   
    </LinearLayout>
---
credentialscontainer.xml
--
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
   >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter Name" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter Password" />

</LinearLayout>
--
submissioncontainer.xml
--
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

</LinearLayout>
--

MainActivity.java

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;

public class MainActivity extends FragmentActivity {
        CredentialsContainer cc;
SubmissionContainer sc;
FragmentTransaction ft;
@Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
setContentView(R.layout.activity_main);
cc=new CredentialsContainer();
sc=new SubmissionContainer ();
ft=getSupportFragmentManager().beginTransaction();
ft.add(R.id.credentialscontainer, cc);
ft.add(R.id.submissioncontainer, sc);
ft.commit();
}    
}
--
CredentialsContainer.java
--
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class CredentialsContainer extends Fragment{
    
   

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.credentialscontainer, container, false);
        return rootView;

}
}
--
SubmissionContainer.java
--
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class SubmissionContainer extends Fragment{
 

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
           Bundle savedInstanceState) {
       View rootView = inflater.inflate(R.layout.submissioncontainer, container, false);
       return rootView;

}

}
-
Output:

Saturday, 23 July 2016

Storage Mechanisms in Android Application-1

#Tutorial 1 (Creating Database and table)
Android provides several options to store persistent data. Some of them are Internal Storage, External Storage, SQLite Database and Shared Preferences. In this tutorial, i will discuss about SQLite Database to store persistent data.

Important Points:
  • SQLite is an open source database
  • SQLite supports standard relational database features
  • Each android device comes up with built in SQLite database implementation
  • Data stored in database remains private to other applications
  • To create SQLite database, you need to create a subclass of SQLiteOpenHelper class.
Let's us go for a simple demo inheriting SQLiteOpenHelper class to create a database and a table inside it. To start with, create a new android application project and assign the names as per IDE defaults. I assume that, the name of the Activity is MainActivity and layout_name is activity_main.xml.

activity_main.xml-----
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">

<EditText
android:id="@+id/editText1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="Enter Book Name"/>

<EditText
android:id="@+id/editText2"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="Enter Book Author"/>

<Button
android:id="@+id/submit"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Submit"/>

</LinearLayout>


MainActivity.java---

public class MainActivity extends Activity
{
DatabaseHelper myDb;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb=new DatabaseHelper(this);
}
}

DatabaseHelper.java---

public class DatabaseHelper extends SQLiteOpenHelper
{
public static final String DATABASE_NAME="Library.db";
public static final String TABLE_NAME="Books";
// constructor with one argument
public DatabaseHelper(Context context)
{
super(context,DATABASE_NAME,null,1);
SQLiteDatabase db=this.getWritableDatabase();
}
// implementing default methods
@Override
public void onCreate(SQLiteDatabase db)
{
db.exexSQL("create table"+TABLE_NAME+ "(id number primary key autoincrement , book_name text, book_author text)");
}
@Override
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS"+TABLE_NAME);
onCreate(db);
}
}

So running this application, will only create the database and the table, To learn how to insert data inside the table follow the next series of this tutorial. 

Friday, 22 July 2016

Android Interview Questions

Posting the list of Interview questions that i have faced in the recent time. The questions in interview will be basic and fundamental. It can be asked to freshers and experienced ones also.

Android Questions
----------------------------------------------------------------------------------------------------------
List of Interview questions:

1. What is Activity?
Ans: Activity is an important component of android application. It is the visible screen which enables the user to interact with the application as example making calls, sending messages and many more.
2. What is Broadcast Receiver?
Ans: It is one among the building blocks of android application which lets the user notify/alert regarding notifications.
3. What is Intent.
And: Intent is a class used to activate various other components in android applications like Services, Activities and broadcast receivers. It also used while switching from one activity to another activity. Intent are of two types; Implicit intent and Explicit intent.
4. What is Service and the different types of Service?
5. What the difference between BaseAdapter and ListAdapter?
6. Define the architecture of Android?
7. What is a Fragment? Call back methods of Fragment.
8. Can we call attach() method before onStart() method.
9. What's the state of the child thread when parent thread is in paused state.
10. Why we use JSON in android application.
11. Name some of the class used for Location and Sensors.
12. Difference between Location and LocationManager class
13. Can we use layout_gravity in relative layout. If not why?
14. What is the difference between layout_gravity and gravity attribute?
15. How to make a button or a view transparent?
16. How to apply border to a button?
17. What is Adapter and uses of adapter?
18. Why cannot you run standard java byte codes in android?
Ans: Android used Dalvik Virtual Machine which requires the files to be in .dex format. Therefore it's not possible to run java byte codes.
19. Languages used for developing android applications?
Ans: Java, C and C++


Thursday, 21 July 2016

ListView in Android Applications

In this tutorial, i am going to show you how to group lot of items and put it in a list in your application. To achieve this, i am going to use the class ListView in android which extends
(AbsListView). 

Let's us start with a simple example of ListView.
1. Create a new android application.
2. You will end with two files, one is the activity_main and the other one is the MainActivity( which is by default naming convention).
3. Write the below code


activity_main.xml coding
---------
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="">

<TextView
android:id="@+id/textView1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Cars" />

<ListView
android:id="@+id/listView"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>

</LinearLayout>
-----------------
MainActivity.java Code:


public class MainActivity extends Activity
{
ListView list;
String [] cars={"Cabrio","Capri","Brava","Brat","Boxter","Camry"};
ArrayAdapter<String> mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list=(ListView)findViewById(R.id.listView);
mAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,cars);
list.setAdapter(mAdapter);
}
}

output:


Saturday, 31 October 2015

Developing Complex UI Part 1 Outputs

Div Button is pressed
Add Button is pressed


Sub Button is pressed
Mul Button is pressed









Developing Complex UI Part 1(Contd...)

Functional Logic for Calculator App..

package com.findandroidhub.democalculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity
{
 private EditText t1,t2;
 private Button b1,b2,b3,b4;
 private TextView tv;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  t1=(EditText)findViewById(R.id.editText1);
  t2=(EditText)findViewById(R.id.editText2);
  b1=(Button)findViewById(R.id.button1);
  b2=(Button)findViewById(R.id.button2);
  b3=(Button)findViewById(R.id.button3);
  b4=(Button)findViewById(R.id.button4);
  tv=(TextView)findViewById(R.id.disp);

  b1.setOnClickListener(new View.OnClickListener() {
 
   @Override
   public void onClick(View v) {
    float a,b;
    a=Float.parseFloat(t1.getText().toString());
    b=Float.parseFloat(t2.getText().toString());
    float add=a+b;
    tv.setText("Your total is:"+add);
  
  
   }
  });
  b2.setOnClickListener(new View.OnClickListener() {
 
   @Override
   public void onClick(View v) {
    float a,b;
    a=Float.parseFloat(t1.getText().toString());
    b=Float.parseFloat(t2.getText().toString());
    float sub=a-b;
    tv.setText("Your total is:"+sub);
  
  
   }
  });
  b3.setOnClickListener(new View.OnClickListener() {
 
   @Override
   public void onClick(View v) {
    float a,b;
    a=Float.parseFloat(t1.getText().toString());
    b=Float.parseFloat(t2.getText().toString());
    float multi=a*b;
    tv.setText("Your total is:"+multi);
  
  
   }
  });
  b4.setOnClickListener(new View.OnClickListener() {
 
   @Override
   public void onClick(View v) {
    float a,b;
    a=Float.parseFloat(t1.getText().toString());
    b=Float.parseFloat(t2.getText().toString());
    float div=a/b;
    tv.setText("Your total is:"+div);
  
  
   }
  });


 }


}

Developing Complex UI Part 1

Layouts can be nested inside another layouts...

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
  android:paddingLeft="10dp"
   android:paddingRight="10dp" >
   
    <TextView android:text="Welcome to the Calculator App"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:gravity="left"/>
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Enter the First Number"
        android:id="@+id/editText1"
        android:paddingTop="20dp"/>
   
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Enter the Second Number"
        android:id="@+id/editText2"
        android:paddingTop="20dp"/>
   
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="60"
        android:id="@+id/l1">
       
    <Button
        android:id="@+id/button1"
        android:text="Add"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="30"
        />   
       
    <Button
        android:id="@+id/button2"
        android:text="Sub"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         android:layout_weight="30"/>   
    </LinearLayout>
   
      <LinearLayout    
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="60"
        android:id="@+id/l2"> 
       
        <Button
        android:id="@+id/button3"
        android:text="Mul"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="30"
        />   
       
    <Button
        android:id="@+id/button4"
        android:text="Div"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         android:layout_weight="30"/>        
       
        </LinearLayout>
          
     
    <TextView
        android:text="Your Result is"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:paddingLeft="80dp"/>
   
   
</LinearLayout>

Following is the UI :