Friday, 8 December 2017

AutoScrollable TextView






XML
--------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >    <slider.app.com.smoothviewpager.AutoScrollableTextView
        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="This is very very long text to be scrolled, This is very very long text to be scrolled, This is very very long text to be scrolled"        /></LinearLayout>

-----------------------------------------------------------

JAVA

------------------------------------------------------------------

import android.content.Context;import android.graphics.Rect;import android.text.TextUtils;import android.util.AttributeSet;
public class AutoScrollableTextView extends android.support.v7.widget.AppCompatTextView {

    public AutoScrollableTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);        setEllipsize(TextUtils.TruncateAt.MARQUEE);        setMarqueeRepeatLimit(-1);        setSingleLine();        setHorizontallyScrolling(true);    }

    public AutoScrollableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);        setEllipsize(TextUtils.TruncateAt.MARQUEE);        setMarqueeRepeatLimit(-1);        setSingleLine();        setHorizontallyScrolling(true);    }

    public AutoScrollableTextView(Context context) {
        super(context);        setEllipsize(TextUtils.TruncateAt.MARQUEE);        setMarqueeRepeatLimit(-1);        setSingleLine();        setHorizontallyScrolling(true);    }

    @Override    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        if(focused)
            super.onFocusChanged(focused, direction, previouslyFocusedRect);    }

    @Override    public void onWindowFocusChanged(boolean focused) {
        if(focused)
            super.onWindowFocusChanged(focused);    }

    @Override    public boolean isFocused() {
        return true;    }
}

-------------------------------------------------------------------------



22

Monday, 20 February 2017

Service : IBinder Service

Hello guys, Most of you are aware of the Binding service. You can bind the service with your Activity, Service and Content provider. You can not bind a service with Broadcast receiver.

There are total 3 ways to bind a service with application components


  1. Using IBinder class
  2. Using Messanger class
  3. Using AIDL
This post is for explain about IBinder class 
To implement IBinder class there are following steps
Steps for service
  1. Create a new Project name "BindServiceUsingBinderClass
  2. Create one Service in your application by extending the Service class 
  3. Create a class "LocalBinder" inside your service and extends "Binder" class in this class
  4. Implement the onBind() method of the service and return the instance of the "LocalBinder" class
Steps for activity
  1. Create one activity "Client" and create a instance of the "ServiceConnection" Interface
  2. Implement two methods of this interface onServiceConnected and onServiceDisconnected
  3. In  onServiceConnected method you will get instance of the iBinder so cast it to LocalBinder class which we have created in the service.
  4. Implement onStart() method and bind the service using bindService() method
  5. Implement onStop() method and unbind the service using unbindService() method
Source code of the IBinder class
Server.java

package com.example.bindservice.binder;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

public class Server extends Service{

 IBinder mBinder = new LocalBinder();

 @Override
 public IBinder onBind(Intent intent) {
  return mBinder;
 }

 public class LocalBinder extends Binder {
  public Server getServerInstance() {
   return Server.this;
  }
 }

 public String getTime() {
  SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  return mDateFormat.format(new Date());
 }
}


Client.java



package com.example.bindservice.binder;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.bindservice.binder.Server.LocalBinder;

public class Client extends Activity {

 boolean mBounded;
 Server mServer;
 TextView text;
 Button button;
 
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        text = (TextView)findViewById(R.id.text);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener() {
   
   public void onClick(View v) {
    text.setText(mServer.getTime());
   }
  });
    }

 @Override
 protected void onStart() {
  super.onStart();
  Intent mIntent = new Intent(this, Server.class);
        bindService(mIntent, mConnection, BIND_AUTO_CREATE);
 };
 
 ServiceConnection mConnection = new ServiceConnection() {
  
  public void onServiceDisconnected(ComponentName name) {
   Toast.makeText(Client.this, "Service is disconnected", 1000).show();
   mBounded = false;
   mServer = null;
  }
  
  public void onServiceConnected(ComponentName name, IBinder service) {
   Toast.makeText(Client.this, "Service is connected", 1000).show();
   mBounded = true;
   LocalBinder mLocalBinder = (LocalBinder)service;
   mServer = mLocalBinder.getServerInstance();
  }
 };
 
 @Override
 protected void onStop() {
  super.onStop();
  if(mBounded) {
   unbindService(mConnection);
   mBounded = false;
  }
 };
}