Monday 3 October 2016

How to change color of tab text And Background Image


Java Code

for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); //Unselected Tabs
tv.setTextColor(Color.parseColor("#ffffff"));
tv.setAllCaps(false);

}

Friday 30 January 2015

Facebook Menu

Java Code (Main Activity Class)

public class LayerStack extends Activity {

//Declare
private LinearLayout slidingPanel;
private boolean isExpanded;
private DisplayMetrics metrics;
private ListView listView;
private RelativeLayout headerPanel;
private RelativeLayout menuPanel;
private int panelWidth;
private ImageView menuViewButton;

FrameLayout.LayoutParams menuPanelParameters;
FrameLayout.LayoutParams slidingPanelParameters;
LinearLayout.LayoutParams headerPanelParameters ;
LinearLayout.LayoutParams listViewParameters;


@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layer_stack);

//Initialize
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
panelWidth = (int) ((metrics.widthPixels)*0.75);

headerPanel = (RelativeLayout) findViewById(R.id.header);
headerPanelParameters = (LinearLayout.LayoutParams) headerPanel.getLayoutParams();
headerPanelParameters.width = metrics.widthPixels;
headerPanel.setLayoutParams(headerPanelParameters);

menuPanel = (RelativeLayout) findViewById(R.id.menuPanel);
menuPanelParameters = (FrameLayout.LayoutParams) menuPanel.getLayoutParams();
menuPanelParameters.width = panelWidth;
menuPanel.setLayoutParams(menuPanelParameters);

slidingPanel = (LinearLayout) findViewById(R.id.slidingPanel);
slidingPanelParameters = (FrameLayout.LayoutParams) slidingPanel.getLayoutParams();
slidingPanelParameters.width = metrics.widthPixels;
slidingPanel.setLayoutParams(slidingPanelParameters);

listView = (ListView) findViewById(R.id.list);
listViewParameters = (LinearLayout.LayoutParams) listView.getLayoutParams();
listViewParameters.width = metrics.widthPixels;
listView.setLayoutParams(listViewParameters);


//Slide the Panel
menuViewButton = (ImageView) findViewById(R.id.menuViewButton);
menuViewButton.setOnClickListener(new OnClickListener() {
   public void onClick(View v) {
    if(!isExpanded){
    isExpanded = true;              
       
    //Expand
    new ExpandAnimation(slidingPanel, panelWidth,
       Animation.RELATIVE_TO_SELF, 0.0f,
       Animation.RELATIVE_TO_SELF, 0.75f, 0, 0.0f, 0, 0.0f);              
    }else{
    isExpanded = false;
   
    //Collapse
    new CollapseAnimation(slidingPanel,panelWidth,
               TranslateAnimation.RELATIVE_TO_SELF, 0.75f,
               TranslateAnimation.RELATIVE_TO_SELF, 0.0f, 0, 0.0f, 0, 0.0f);
 

    }          
   }
});

}
}


Java Code ( CollapseAnimation Class)

public class CollapseAnimation extends TranslateAnimation implements TranslateAnimation.AnimationListener{

private LinearLayout slidingLayout;
int panelWidth;

public CollapseAnimation(LinearLayout layout, int width, int fromXType, float fromXValue, int toXType,
float toXValue, int fromYType, float fromYValue, int toYType, float toYValue) {

super(fromXType, fromXValue, toXType, toXValue, fromYType, fromYValue, toYType, toYValue);

//Initialize
slidingLayout = layout;
     panelWidth = width;
setDuration(400);
   setFillAfter( false );
   setInterpolator(new AccelerateDecelerateInterpolator());
   setAnimationListener(this);
 
   //Clear left and right margins
   LayoutParams params = (LayoutParams) slidingLayout.getLayoutParams();
    params.rightMargin = 0;
    params.leftMargin = 0;
    slidingLayout.setLayoutParams(params);
    slidingLayout.requestLayout();      
    slidingLayout.startAnimation(this);
   
}
public void onAnimationEnd(Animation animation) {

}

public void onAnimationRepeat(Animation animation) {

}

public void onAnimationStart(Animation animation) {

}

}




Java Code (ExpandAnimation)

public class ExpandAnimation extends TranslateAnimation implements Animation.AnimationListener{

private LinearLayout slidingLayout;
int panelWidth;

public ExpandAnimation(LinearLayout layout, int width, int fromXType, float fromXValue, int toXType,
float toXValue, int fromYType, float fromYValue, int toYType, float toYValue) {

super(fromXType, fromXValue, toXType, toXValue, fromYType, fromYValue, toYType, toYValue);

//Initialize
slidingLayout = layout;
panelWidth = width;
setDuration(400);
     setFillAfter( false );
     setInterpolator(new AccelerateDecelerateInterpolator());
     setAnimationListener(this);
     slidingLayout.startAnimation(this);
}


public void onAnimationEnd(Animation arg0) {

//Create margin and align left
LayoutParams params = (LayoutParams) slidingLayout.getLayoutParams();
    params.leftMargin = panelWidth;
    params.gravity = Gravity.LEFT;  
    slidingLayout.clearAnimation();
    slidingLayout.setLayoutParams(params);
    slidingLayout.requestLayout();
   
}

public void onAnimationRepeat(Animation arg0) {

}

public void onAnimationStart(Animation arg0) {

}

}


XML (Main Activity XML)

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent" 
    android:layout_weight="1"
    android:background="@drawable/blue_bg">

    <!-- Menu Panel -->
    <RelativeLayout
           android:id="@+id/menuPanel"
           android:layout_height="match_parent"
           android:layout_width="wrap_content"
           android:gravity="right"
           android:background="@drawable/gray_bg"
           android:orientation="vertical" >
       
            <TextView
                android:id="@+id/menu_title_1"
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:layout_alignParentTop="true"
                android:layout_marginLeft="0dp" 
                android:paddingLeft="15dp"             
                android:gravity="center_vertical"
                android:background="#353535"
                android:textColor="@android:color/white"
                android:text="@string/menu_title">     
            </TextView>
               
            <View
       android:id="@+id/menu_item_divider_1"
       android:layout_width="fill_parent"
       android:layout_height="0.5dp"
       android:layout_marginLeft="0dp"
       android:layout_marginRight="0dp"
       android:layout_below="@+id/menu_title_1"
       android:background="#b5b5b5"/>
            
            <TextView
                android:id="@+id/menu_item_1"
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:layout_marginLeft="15dp"             
                android:gravity="center_vertical"
                android:layout_below="@+id/menu_item_divider_1"
                android:text="@string/item_1">     
            </TextView>     
             <View
       android:id="@+id/menu_item_divider_2"
       android:layout_width="fill_parent"
       android:layout_height="0.5dp"
       android:layout_marginLeft="5dp"
       android:layout_marginRight="5dp"
       android:layout_below="@+id/menu_item_1"
       android:background="#b5b5b5"/> 
            <TextView
                android:id="@+id/menu_item_2"
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:layout_below="@+id/menu_item_divider_2"
                android:layout_marginLeft="15dp"
                android:gravity="center_vertical"
                android:text="@string/item_2">
            </TextView>
            <View
       android:id="@+id/menu_item_divider_3"
       android:layout_width="fill_parent"
       android:layout_height="0.5dp"
       android:layout_marginLeft="5dp"
       android:layout_marginRight="5dp"
       android:layout_below="@+id/menu_item_2"
       android:background="#b5b5b5" />           
            </RelativeLayout>
            
       <!-- Sliding Panel -->     
<LinearLayout
                android:id="@+id/slidingPanel"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:gravity="left"
                android:orientation="vertical"
                android:background="@android:color/white" >
        
        <RelativeLayout
           android:id="@+id/header"
           android:layout_width="match_parent"
           android:layout_height="50dp"
           android:background="@drawable/blue_bg" >

          <View
               android:id="@+id/header_vertical_divider_1"
               android:layout_width="2dp"
               android:layout_height="fill_parent"
               android:layout_alignParentTop="true"
               android:layout_marginLeft="15dp"
               android:layout_toRightOf="@+id/menuViewButton"
               android:background="@drawable/engraved_bg" />
  
           <ImageView
               android:id="@+id/menuViewButton"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_alignParentLeft="true"
               android:layout_centerVertical="true"
               android:layout_marginLeft="15dp"
               android:clickable="true"
               android:contentDescription="@string/description"
               android:src="@drawable/icon_menu"
               android:visibility="visible" />
  </RelativeLayout>

        <View
    android:id="@+id/dividerHeaderBottom"
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:background="#414141" />
       <ListView
           android:id="@+id/list"
           android:layout_width="match_parent"
           android:layout_height="fill_parent"
           android:divider="#b5b5b5"
           android:dividerHeight="0.5dp"
           android:background="@android:color/white"
            >
       </ListView>

</LinearLayout>
</FrameLayout>




Swipe ListView on item click

Java Code (Main Activity Class)


public class MainActivity extends Activity {

private SwipeListView lv_listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv_listView = (SwipeListView)findViewById(R.id.NoteList_Listview);
lv_listView.setAdapter(new AdapterClass(MainActivity.this));
}
}




Java Code (Adapter Class)


public class AdapterClass extends BaseAdapter{


Context context;


public AdapterClass(Context context) {
// TODO Auto-generated constructor stub
this.context=context;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return 5;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub


LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.item_row, null);
View layout = (View)view.findViewById(R.id.NoteList_Row_Layout);
View  deleteLayout = (View)view.findViewById(R.id.NoteList_Row_delete);
ImageView image = (ImageView)view.findViewById(R.id.NoteList_Row_close);
TextView titel = (TextView)view.findViewById(R.id.NoteList_Row_titel);



LinearLayout.LayoutParams lp1 = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
layout.setLayoutParams(lp1);
LinearLayout.LayoutParams lp2 = new LayoutParams(SwipeListView.mRightViewWidth , LayoutParams.MATCH_PARENT);

deleteLayout.setLayoutParams(lp2);

titel.setText("Item: "+position);

image.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub

SwipeListView.mFirstX=200;
SwipeListView.mFirstY=14;
SwipeListView.motionPosition=position;
return true;
}
});

deleteLayout.setOnClickListener(new OnClickListener() {

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

Toast.makeText(context, "Click on Delete item "+position, Toast.LENGTH_SHORT).show();
}
});


return view;
}
}




Java Code (Swipe Class)


public class SwipeListView extends ListView {
private Boolean mIsHorizontal;
private View mPreItemView;
private View mCurrentItemView;
public static float mFirstX;
public static float mFirstY;
public static int motionPosition=-1;
public static int mRightViewWidth = 100;
private final int mDuration = 100;
private final int mDurationStep = 10;
private boolean mIsShown;


private boolean mIsFooterCanSwipe = false;

private boolean mIsHeaderCanSwipe = false;

public SwipeListView(Context context) {
super(context);
}

public SwipeListView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public SwipeListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// float lastX = 24;

float lastX = ev.getX();
float lastY = ev.getY();
//
Log.e("Hiiiiiii ", "onInterceptTouchEvent MotionPosition: "+motionPosition+" X: "+lastX+" y: " );
if(motionPosition>=0)
{
swipeOnClick(motionPosition,ev.getX(),ev.getY());

}


switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mIsHorizontal = null;

break;

case MotionEvent.ACTION_MOVE:

break;

case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:


if (mIsShown) {
hiddenRight(mPreItemView);
}

if (mIsHorizontal != null && mIsHorizontal) {
if (mFirstX - lastX > mRightViewWidth / 2) {
showRight(mCurrentItemView);
} else {
hiddenRight(mCurrentItemView);
}

return false;
}


break;
}




return super.onInterceptTouchEvent(ev);
}

private boolean isHitCurItemLeft(float x) {
return x < getWidth() - mRightViewWidth;
}

private boolean judgeScrollDirection(float dx, float dy) {
boolean canJudge = true;

if (Math.abs(dx) > 30 && Math.abs(dx) > 2 * Math.abs(dy)) {
mIsHorizontal = true;
} else if (Math.abs(dy) > 30 && Math.abs(dy) > 2 * Math.abs(dx)) {
mIsHorizontal = false;
} else {
canJudge = false;
}

return canJudge;
}

private boolean judgeFooterView(float posX, float posY) {
// if footer can swipe
if (mIsFooterCanSwipe) {
return true;
}
// footer cannot swipe
int selectPos = pointToPosition((int) posX, (int) posY);
if (selectPos >= (getCount() - getFooterViewsCount())) {
// is footer ,can not swipe
return false;
}
// not footer can swipe
return true;
}

private boolean judgeHeaderView(float posX, float posY) {
// if header can swipe
if (mIsHeaderCanSwipe) {
return true;
}
// header cannot swipe
int selectPos = pointToPosition((int) posX, (int) posY);
if (selectPos >= 0 && selectPos < getHeaderViewsCount()) {
// is header ,can not swipe
return false;
}
// not header can swipe
return true;
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
float lastX = 24;
//float lastX = ev.getX();
float lastY = ev.getY();


Log.e("Hiiiiiii ", "onTouchEvent: "+ "X: "+lastX+" y: "+lastY );

// test footer and header
if (!judgeFooterView(mFirstX, mFirstY)
|| !judgeHeaderView(mFirstX, mFirstY)) {
return super.onTouchEvent(ev);
}

switch (ev.getAction()) {




case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:

Log.e("Hiiiiiii ", "onTouchEvent ACTION_CANCEL: " );


//clearPressedState();
if (mIsShown) {
hiddenRight(mPreItemView);
}

if (mIsHorizontal != null && mIsHorizontal) {
if (mFirstX - lastX > mRightViewWidth / 2) {
showRight(mCurrentItemView);
} else {
hiddenRight(mCurrentItemView);
}

return false;
}

break;
}

return super.onTouchEvent(ev);
}

private void clearPressedState() {
// TODO current item is still has background, issue
mCurrentItemView.setPressed(false);
setPressed(false);
refreshDrawableState();
// invalidate();
}

private void showRight(View view) {
Message msg = new MoveHandler().obtainMessage();
msg.obj = view;
msg.arg1 = view.getScrollX();
msg.arg2 = mRightViewWidth;
msg.sendToTarget();

mIsShown = true;
}

private void hiddenRight(View view) {
if (mCurrentItemView == null) {
return;
}
Message msg = new MoveHandler().obtainMessage();//
msg.obj = view;
msg.arg1 = view.getScrollX();
msg.arg2 = 0;

msg.sendToTarget();

mIsShown = false;
}

@SuppressLint("HandlerLeak")
class MoveHandler extends Handler {
int stepX = 0;
int fromX;
int toX;
View view;
private boolean mIsInAnimation = false;

private void animatioOver() {
mIsInAnimation = false;
stepX = 0;
}

@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (stepX == 0) {
if (mIsInAnimation) {
return;
}
mIsInAnimation = true;
view = (View) msg.obj;
fromX = msg.arg1;
toX = msg.arg2;
stepX = (int) ((toX - fromX) * mDurationStep * 1.0 / mDuration);
if (stepX < 0 && stepX > -1) {
stepX = -1;
} else if (stepX > 0 && stepX < 1) {
stepX = 1;
}
if (Math.abs(toX - fromX) < 10) {
view.scrollTo(toX, 0);
animatioOver();
return;
}
}

fromX += stepX;
boolean isLastStep = (stepX > 0 && fromX > toX)
|| (stepX < 0 && fromX < toX);
if (isLastStep) {
fromX = toX;
}

view.scrollTo(fromX, 0);
invalidate();

if (!isLastStep) {
this.sendEmptyMessageDelayed(0, mDurationStep);
} else {
animatioOver();
}
}
}

public int getRightViewWidth() {
return mRightViewWidth;
}

public void setRightViewWidth(int mRightViewWidth) {
this.mRightViewWidth = mRightViewWidth;
}

public void setFooterViewCanSwipe(boolean canSwipe) {
mIsFooterCanSwipe = canSwipe;
}

public void setHeaderViewCanSwipe(boolean canSwipe) {
mIsHeaderCanSwipe = canSwipe;
}

public void setFooterAndHeaderCanSwipe(boolean footer, boolean header) {
mIsHeaderCanSwipe = header;
mIsFooterCanSwipe = footer;
}


public void swipeOnClick(int position,float dx, float dy)
{

View currentItemView = getChildAt(position- getFirstVisiblePosition());
mPreItemView = mCurrentItemView;
mCurrentItemView = currentItemView;
mIsHorizontal=true;
//mIsShown =true;




if (mIsHorizontal == null) {
if (!judgeScrollDirection(dx, dy)) {
}
}

if (mIsHorizontal) {
if (mIsShown && mPreItemView != mCurrentItemView) {
hiddenRight(mPreItemView);
}

if (mIsShown && mPreItemView == mCurrentItemView) {
dx = dx - mRightViewWidth;
}

// can't move beyond boundary
if (dx < 0 && dx > -mRightViewWidth) {
mCurrentItemView.scrollTo((int) (-dx), 0);
}

} else {
if (mIsShown) {
hiddenRight(mPreItemView);
}
}




}

}


XML (Main Activity 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.swipelistviewonperticularitrmclick.MainActivity" >

     <com.example.swipelistviewonperticularitrmclick.SwipeListView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/NoteList_Listview"
        
        />

</RelativeLayout>


XML (Adapter item 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="horizontal" >

    <LinearLayout
        android:id="@+id/NoteList_Row_Layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:padding="10dp"
        android:orientation="horizontal" >

        
            <ImageView
                android:id="@+id/NoteList_Row_close"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_launcher"
                 />

            <TextView
                android:layout_weight="1"
                android:id="@+id/NoteList_Row_titel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:textStyle="bold" />
             
        </LinearLayout>

       

        <LinearLayout
            android:id="@+id/NoteList_Row_delete"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="#FF0000"
            android:gravity="center"
            android:orientation="horizontal"
             >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:text="Delete"
                android:textColor="@android:color/white"
                android:textStyle="bold" />
       
    </LinearLayout>

</LinearLayout>





Monday 22 December 2014

Check Network Connection Using BroadCast

Java Class

public class NetworkStatus extends BroadcastReceiver{

public static  boolean isConnected;

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub

isConnected = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);

  if(isConnected)
   {
       Toast.makeText(context, "Internet Connection Lost", Toast.LENGTH_LONG).show();
     
   }
   else
   {
       Toast.makeText(context, "Internet Connected", Toast.LENGTH_LONG).show();
     
          // new RequestService(context);

       
   }
}

}


ManiFest XML

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.aditmicrosys.easylife"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

 
   <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/appicon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity android:name="com.aditmicrosys.easylife.Splash_Screen" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
 <receiver android:name="com.aditmicrosys.comman.NetworkStatus" >
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
        </receiver>

</application>

JSON Pars With MVC

Link Of Demo Project

Download source code


Sunday 14 December 2014

Android Activity Animation From Left To right

Animation xml files:

right_slide_in.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
       android:interpolator="@android:anim/accelerate_decelerate_interpolator">
  
     <translate
        android:fromXDelta="100%p"
        android:toXDelta="0"
        android:duration="300"/>

</set>

left_slide_out.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    
    <translate
         android:fromXDelta="0" 
         android:toXDelta="-100%p" 
         android:duration="300"/>
</set>

FirstActivity.java


    Button b_next = (Button)findViewById(R.id.button1);
    b_next.setOnClickListener(new OnClickListener() {
  @Override
   public void onClick(View arg0) {
  // TODO Auto-generated method stub
      
                    Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
                    startActivity(intent);
                    overridePendingTransition(R.anim.right_slide_in, R.anim.left_side_out);
                    FirstActivity.this.finish(); 

}
     });