Tuesday, 31 March 2015

Working with tabs in Android using Fragments

Hey Guys,

Today i worked on tabs using fragments. And Always try to keep things simple as a like that        way.
 So here we go...
 
Since we have seen alots of examples which maintain fragment stacks manually using hashmaps  and all . But i've read this from stackoverflow and other reference web sites about           
 fragmenttabhost so going to use this.
And thats how it works.
Base Classes :

 1. AppMainTabActivity.java
    
public class AppTabBSecondFragment extends BaseFragment {

Button mBackbtn;
View mView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.app_tab_b_second_screen, container,
false);
initializeWidgets();
return mView;
}

private void initializeWidgets() {
mBackbtn = (Button) mView.findViewById(R.id.backbtn);

mBackbtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

mActivity.handleBackPress();

}
});
}

}

2. BaseFragment.java

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;

public class BaseFragment extends Fragment {
public AppMainTabActivity mActivity;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mActivity = (AppMainTabActivity) this.getActivity();
}

public boolean onBackPressed() {
return false;
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {

}
}


Fragments which we are going to add in tabs and replace now 

1. AppTabAFirstFragment.java


public class AppTabAFirstFragment extends BaseFragment {
    private Button mGotoButton;

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

        mGotoButton =   (Button) view.findViewById(R.id.id_next_tab_a_button);
        mGotoButton.setOnClickListener(listener);

        return view;
    }

    private OnClickListener listener        =   new View.OnClickListener(){
        @Override
        public void onClick(View v){
            /* Go to next fragment in navigation stack*/
            mActivity.mAddFragments(AppConstants.TAB_A, new AppTabASecondFragment(),true,true);
        }
    };
}


2. AppTabASecondFragment.java

public class AppTabASecondFragment extends BaseFragment {

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

}

3. AppTabBFirstFragment.java

public class AppTabBFirstFragment extends BaseFragment {

    private Button mGotoButton;

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

        mGotoButton =   (Button) view.findViewById(R.id.id_next_tab_b_button);
        mGotoButton.setOnClickListener(listener);

        return view;
    }

    private OnClickListener listener        =   new View.OnClickListener(){
        @Override
        public void onClick(View v){
            /* Go to next fragment in navigation stack*/
            mActivity.mAddFragments(AppConstants.TAB_B, new AppTabBSecondFragment(),true,true);
        }
    };
}


4. AppTabBSecondFragment.java

public class AppTabBSecondFragment extends BaseFragment {

Button mBackbtn;
View mView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.app_tab_b_second_screen, container,
false);
initializeWidgets();
return mView;
}

private void initializeWidgets() {
mBackbtn = (Button) mView.findViewById(R.id.backbtn);

mBackbtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

mActivity.handleBackPress();

}
});
}

}

5. AppTabCFirstFragment.java

public class AppTabCFirstFragment extends BaseFragment {

    private Button mGotoButton;

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

        mGotoButton =   (Button) view.findViewById(R.id.id_next_tab_C_button);
        mGotoButton.setOnClickListener(listener);

        return view;
    }

    private OnClickListener listener   =  new View.OnClickListener(){
        @Override
        public void onClick(View v){
            /* Go to next fragment in navigation stack*/
            mActivity.mAddFragments(AppConstants.TAB_C, new AppTabCSecondFragment(),true,true);
        }
    };
}


6. AppTabCSecondFragment.java

public class AppTabCSecondFragment extends BaseFragment {
Button mBackbtn;
View mView; 
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.app_tab_c_second_screen,
container, false);
initializeWidgets();
return mView;
}

private void initializeWidgets() {
mBackbtn=(Button)mView.findViewById(R.id.backbtn);
mBackbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {

mActivity.handleBackPress();
}
});

}
}


Now interface that i used to handle backpress normally :

1. BackButtonCallback.java

public interface BackButtonCallback {

void handleBackPress();
}


Now the constants class for this ....


1. AppConstants.java

public class AppConstants {
public static final String TAB_A = "tab_a_identifier";
public static final String TAB_B = "tab_b_identifier";
public static final String TAB_C = "tab_c_identifier";

}


And finally the XML layouts :  


1. app_main_tab_fragment_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:orientation="horizontal" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" />

        <FrameLayout
            android:id="@+id/realtabcontent"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </LinearLayout>

</TabHost>

2. tabs_icon.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabsLayout" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@drawable/tab_bg_selector"
android:gravity="center" android:orientation="vertical">
    <ImageView android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:id="@+id/tab_icon" />
</LinearLayout>

3. app_tab_a_first_screen.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:background="#aa5500"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="TAB A FIRST SCREEN"
        android:textColor="@android:color/white"
        android:textSize="30sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/id_next_tab_a_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:text="Next" />

</LinearLayout>

4. app_tab_a_second_screen.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"  android:background="#0055aa"
    android:gravity="center">
    
    <TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:gravity="center" android:text="TAB A SECOND SCREEN"
        android:textSize="30sp" android:textStyle="bold"
        android:textColor="@android:color/white"/>
    
</LinearLayout>

5. app_tab_b_first_screen.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"  android:background="#000000"
    android:gravity="center">
    
    <TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:gravity="center" android:text="TAB B FIRST SCREEN"
        android:textSize="30sp" android:textStyle="bold"
        android:textColor="@android:color/white"/>
    
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="Next" android:id="@+id/id_next_tab_b_button"
        android:padding="10dip"/>

</LinearLayout>

6. app_tab_b_second_screen.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:background="#00ffff"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="TAB B SECOND SCREEN"
        android:textColor="@android:color/white"
        android:textSize="30sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/backbtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/back_button" />

</LinearLayout>

7.  app_tab_c_first_screen.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"  android:background="@android:color/darker_gray"
    android:gravity="center">
    
    <TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:gravity="center" android:text="@string/tab_c_first_screen"
        android:textSize="30sp" android:textStyle="bold"
        android:textColor="@android:color/white"/>
    
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="Next" android:id="@+id/id_next_tab_C_button"
        android:padding="10dip"/>

</LinearLayout>

8. app_tab_c_second_screen.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"  android:background="@android:color/background_dark"
    android:gravity="center">
    
    <TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:gravity="center" android:text="@string/tab_c_second_screen"
        android:textSize="30sp" android:textStyle="bold"
        android:textColor="@android:color/white"/>
    
    
    <Button 
        android:id="@+id/backbtn"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/back_button"
        />
</LinearLayout>


Output :

 

 

 




Friday, 27 March 2015

Implementing Different Notifications in Android

Hey Guys,

I'm just providing a demo code for the newest notification going now a days in our android phones.

Try these methods and Enjoy new phase of notifications in development.


1. Set Normal Notification 


private Notification setNormalNotification() {
Bitmap mNotificationPicture = null;

try {
mNotificationPicture = BitmapFactory
.decodeStream((InputStream) new URL(sample_url)
.getContent());
} catch (IOException e) {
e.printStackTrace();
}

// Setup an explicit intent for an ResultActivity to receive.
Intent resultIntent = new Intent(this, ResultActivity.class);

// TaskStackBuilder ensures that the back button follows the recommended
// convention for the back key.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

// Adds the back stack for the Intent (but not the Intent itself).
stackBuilder.addParentStack(ResultActivity.class);

// Adds the Intent that starts the Activity to the top of the stack.
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);

return new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setLargeIcon(mNotificationPicture)
.setContentIntent(resultPendingIntent)
.addAction(R.drawable.ic_launcher, "One", resultPendingIntent)
.addAction(R.drawable.ic_launcher, "Two", resultPendingIntent)
.addAction(R.drawable.ic_launcher, "Three", resultPendingIntent)
.setContentTitle("Normal Notification")
.setContentText("This is an example of a Normal Style.")
.build();
}

Result :



2. Big Text Style Notification


private Notification setBigTextStyleNotification() {
Bitmap mNotificationPicture = null;

// Create the style object with BigTextStyle subclass.
NotificationCompat.BigTextStyle notiStyle = new NotificationCompat.BigTextStyle();
notiStyle.setBigContentTitle("Big Text Expanded");
notiStyle.setSummaryText("Nice big text.");

try {
mNotificationPicture = BitmapFactory
.decodeStream((InputStream) new URL(sample_url)
.getContent());
} catch (IOException e) {
e.printStackTrace();
}

CharSequence bigText = "This is a demo Big Text Style notification.This is a demo Big Text Style notification.This is a demo Big Text Style notification.This is a demo Big Text Style notification.This is a demo Big Text Style notification.This is a demo Big Text Style notification.This is a demo Big Text Style notification.";
notiStyle.bigText(bigText);

// Creates an explicit intent for an ResultActivity to receive.
Intent resultIntent = new Intent(this, ResultActivity.class);

// This ensures that the back button follows the recommended convention
// for the back key.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

// Adds the back stack for the Intent (but not the Intent itself).
stackBuilder.addParentStack(ResultActivity.class);

// Adds the Intent that starts the Activity to the top of the stack.
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);

return new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setLargeIcon(mNotificationPicture)
.setContentIntent(resultPendingIntent)
.addAction(R.drawable.ic_launcher, "One", resultPendingIntent)
.addAction(R.drawable.ic_launcher, "Two", resultPendingIntent)
.addAction(R.drawable.ic_launcher, "Three", resultPendingIntent)
.setContentTitle("Big Text Normal")
.setContentText("This is an example of a Big Text Style.")
.setStyle(notiStyle).build();
}

Result :



3. Big Picture Style Notification

private Notification setBigPictureStyleNotification() {
Bitmap mNotificationPicture = null;

// Create the style object with BigPictureStyle subclass.
NotificationCompat.BigPictureStyle notiStyle = new NotificationCompat.BigPictureStyle();
notiStyle.setBigContentTitle("Big Picture Expanded");
notiStyle.setSummaryText("Nice big picture.");

try {
mNotificationPicture = BitmapFactory
.decodeStream((InputStream) new URL(sample_url)
.getContent());
} catch (IOException e) {
e.printStackTrace();
}

// Add the big picture to the style.
notiStyle.bigPicture(mNotificationPicture);

// Creates an explicit intent for an ResultActivity to receive.
Intent resultIntent = new Intent(this, ResultActivity.class);

// This ensures that the back button follows the recommended convention
// for the back key.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

// Adds the back stack for the Intent (but not the Intent itself).
stackBuilder.addParentStack(ResultActivity.class);

// Adds the Intent that starts the Activity to the top of the stack.
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);

return new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setLargeIcon(mNotificationPicture)
.setContentIntent(resultPendingIntent)
.addAction(R.drawable.ic_launcher, "One", resultPendingIntent)
.addAction(R.drawable.ic_launcher, "Two", resultPendingIntent)
.addAction(R.drawable.ic_launcher, "Three", resultPendingIntent)
.setContentTitle("Big Picture Normal")
.setContentText("This is an example of a Big Picture Style.")
.setStyle(notiStyle).build();
}

Result :



4. Inbox Style Notification

private Notification setInboxStyleNotification() {
Bitmap mNotificationPicture = null;

// Create the style object with InboxStyle subclass.
NotificationCompat.InboxStyle notiStyle = new NotificationCompat.InboxStyle();
notiStyle.setBigContentTitle("Inbox Style Expanded");

// Add the multiple lines to the style.
// This is strictly for providing an example of multiple lines.
for (int i = 1; i <= 5; i++) {
notiStyle.addLine("(" + i + " of 6) Line one here.");
}
notiStyle.setSummaryText("+6 more Line Samples");

try {
mNotificationPicture = BitmapFactory
.decodeStream((InputStream) new URL(sample_url)
.getContent());
} catch (IOException e) {
e.printStackTrace();
}

// Creates an explicit intent for an ResultActivity to receive.
Intent resultIntent = new Intent(this, ResultActivity.class);

// This ensures that the back button follows the recommended convention
// for the back key.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

// Adds the back stack for the Intent (but not the Intent itself).
stackBuilder.addParentStack(ResultActivity.class);

// Adds the Intent that starts the Activity to the top of the stack.
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);

return new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setLargeIcon(mNotificationPicture)
.setContentIntent(resultPendingIntent)
.addAction(R.drawable.ic_launcher, "One", resultPendingIntent)
.addAction(R.drawable.ic_launcher, "Two", resultPendingIntent)
.addAction(R.drawable.ic_launcher, "Three", resultPendingIntent)
.setContentTitle("Inbox Style Normal")
.setContentText("This is an example of a Inbox Style.")
.setStyle(notiStyle).build();
}


Result :



5. Custom View Notification

private Notification setCustomViewNotification() {

// Creates an explicit intent for an ResultActivity to receive.
Intent resultIntent = new Intent(this, ResultActivity.class);

// This ensures that the back button follows the recommended convention
// for the back key.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.class);

// Adds the Intent that starts the Activity to the top of the stack.
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);

// Create remote view and set bigContentView.
RemoteViews expandedView = new RemoteViews(this.getPackageName(),
R.layout.notification_custom_remote);
expandedView.setTextViewText(R.id.text_view, "Neat logo!");

Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher).setAutoCancel(true)
.setContentIntent(resultPendingIntent)
.setContentTitle("Custom View").build();

notification.bigContentView = expandedView;

return notification;
}

Result :






Happy Coding !! Cheers !!





Wednesday, 26 June 2013

Program To Add Two Numbers In Java.

import java.util.Scanner; 

class AddNumbers
{
   public static void main(String args[])
   {
      int no1, no2, result;
      System.out.println("Enter two integers to calculate their sum ");
      Scanner in = new Scanner(System.in);
      no1 = in.nextInt();
      no2 = in.nextInt();
      result = no1 + no2;
      System.out.println("Sum of entered integers = "+result);
   }
}




Tuesday, 10 July 2012

How To use multiview in C# in asp.net

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
     
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = DropDownList1.SelectedIndex;
    }
}


How To Use Multi View In C# in asp.net


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
     
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = DropDownList1.SelectedIndex;
    }
}

File Upload Code in asp.net


using System;
using System.Web.UI.WebControls;

public partial class Management_FileUpload : System.Web.UI.Page
{

  protected void Button1_Click(object sender, EventArgs e)
  {


  FileUpload FileUpload1 = file_Image;

  string virtualFolder = "~/images/tutorialimages/";

  string physicalFolder = Server.MapPath(virtualFolder);


  FileUpload1.SaveAs(physicalFolder + FileUpload1.FileName);


  lbl_Result.Text = "Your file " + FileUpload1.FileName + " has been uploaded.";

  Image1.Visible = true;
  Image1.ImageUrl = virtualFolder + FileUpload1.FileName;


  }
}

Code To Travel In a Html Page. . . .


<!DOCTYPE html>
<html>
<body>


<p>
<a href="#C4">See also Chapter 4.</a>
</p>


<h2>Chapter 1</h2>
<p>This chapter explains ba bla bla</p>


<h2>Chapter 2</h2>
<p>This chapter explains ba bla bla</p>


<h2>Chapter 3</h2>
<p>This chapter explains ba bla bla</p>


<h2><a name="C4">Chapter 4</a></h2>
<p>This chapter explains ba bla bla</p>


<h2>Chapter 5</h2>
<p>This chapter explains ba bla bla</p>


<h2>Chapter 6</h2>
<p>This chapter explains ba bla bla</p>


<h2>Chapter 7</h2>
<p>This chapter explains ba bla bla</p>


<h2>Chapter 8</h2>
<p>This chapter explains ba bla bla</p>


<h2>Chapter 9</h2>
<p>This chapter explains ba bla bla</p>


<h2>Chapter 10</h2>
<p>This chapter explains ba bla bla</p>


<h2>Chapter 11</h2>
<p>This chapter explains ba bla bla</p>


<h2>Chapter 12</h2>
<p>This chapter explains ba bla bla</p>


<h2>Chapter 13</h2>
<p>This chapter explains ba bla bla</p>


<h2>Chapter 14</h2>
<p>This chapter explains ba bla bla</p>


<h2>Chapter 15</h2>
<p>This chapter explains ba bla bla</p>


<h2>Chapter 16</h2>
<p>This chapter explains ba bla bla</p>


<h2>Chapter 17</h2>
<p>This chapter explains ba bla bla</p>


</body>
</html>