Friday 13 February 2015

Parallax View Pager

ParallaxViewPager

An easy-to-use ViewPager subclass with parallax background.
Setup requires little extra effort, using the ParallaxViewPager is just like using a standard ViewPager, with the same adapter. Of course, there's no silver bullet - the developer has to supply a background tailored to the current needs (eg. the number of items in the adapter and the size of the ViewPager). Check out this gif and this blog post to see what it's all about.
  1. Include it in your project as a Gradle dependency:
  2.     dependencies {
            compile 'com.andraskindler.parallaxviewpager:parallaxviewpager:0.3.1'
        }
    
    1. Create a ParallaxViewPager programmatically or in a layout xml.
    2. Set the background via xml or one of the following methods:
      • setBackgroundResource(int resid)
      • setBackground(Drawable background) or setBackgroundDrawable(Drawable background)
      • setBackground(Bitmap bitmap)
    3. (Optional) Specify how the view should scale the background with the setScaleType(final int scaleType) method. Choose from the following parameters:
      • FIT_HEIGHT means the height of the image is resized to matched the height of the View, also stretching the width to keep the aspect ratio. The non-visible part of the bitmap is divided into equal parts, each of them sliding in at the proper position. This is the default value.
      • FIT_WIDTH means the width of the background image is divided into equal chunks, each taking up the whole width of the screen. This mode is not the usual parallax-effect, as the speed of the background scrolling equals the speed of the views.
    4. (Optional) Set the amount of overlapping with the setOverlapPercentage(final float percentage) method. This is a number between 0 and 1, the smaller it is, the slower is the background scrolling. The default value is 50 percent. This only works with FIT_HEIGHT.

An example, inside the 
onCreate() of an activity:

  1.     //...
        final ParallaxViewPager parallaxViewPager = new ParallaxViewPager(this);
        parallaxViewPager.setAdapter(new MyPagerAdapter());
        parallaxViewPager.setBackgroundResource(R.drawable.nagy);
        setContentView(parallaxViewPager);
        //...

Circle Avatar Image View

DSAvataImageView

Subclass of ImageView to create a circle avatar image view with user name initial image view.

XML (Use Circle avatar image view)

    <com.xperi.avataimageview.DSAvatarImageView
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:src="@drawable/image"
        app:border_color="#EEEEEE"
        app:border_width="5dp"
        />

XML (Use user name initial view) output 'SJ'


    <com.xperi.avataimageview.DSAvatarImageView
        android:layout_width="250dp"
        android:layout_height="250dp"
        app:border_color="#EEEEEE"
        app:border_width="5dp"
        app:name="steve jobs"
        app:background_color="@android:color/white"
        app:text_color="@android:color/black"
        />
You may use the following properties in your XML to customize your DSAvataImageView.

Properties:
  • app:border (boolean) -> default true
  • app:border_color (color) -> default GRAY
  • app:border_width (dimension) -> default 2dp
  • app:name (String) -> default
  • app:background_color (color) -> default WHITE
  • app:text_color (color) -> default GRAY ###JAVA (Use Circle avatar image view)
    DSAvatarImageView avataImageView=getView(convertView, R.id.circularIv);
    avataImageView.setBorderColor(getResources().getColor(R.color.GrayLight));
    avataImageView.setBorderWidth(10);
    avataImageView.setImageResource(R.id.sample);

JAVA (Use user name initial view) output 'SJ'


    DSAvatarImageView avataImageView=getView(convertView, R.id.circularIv);
    avataImageView.setBorderColor(getResources().getColor(R.color.GrayLight));
    avataImageView.setName("steve jobs")

Android Twitter

AndroidTwitter

AndroidTwitter is a small android library to let you connect to Twitter and access all it's public API (tweet status, retweet, etc).

How to Use

public class TestConnect extends Activity {
    private TwitterApp mTwitter;
    private CheckBox mTwitterBtn;

    private static final String twitter_consumer_key = "xxx";
    private static final String twitter_secret_key = "xxxx";

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

        setContentView(R.layout.main);

        mTwitterBtn = (CheckBox) findViewById(R.id.twitterCheck);

        mTwitterBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onTwitterClick();
            }
        });

        mTwitter    = new TwitterApp(this, twitter_consumer_key,twitter_secret_key);

        mTwitter.setListener(mTwLoginDialogListener);

        if (mTwitter.hasAccessToken()) {
            mTwitterBtn.setChecked(true);

            String username = mTwitter.getUsername();
            username        = (username.equals("")) ? "Unknown" : username;

            mTwitterBtn.setText("  Twitter (" + username + ")");
            mTwitterBtn.setTextColor(Color.WHITE);
        }

        Button goBtn = (Button) findViewById(R.id.button1);

        goBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(TestConnect.this, TestPost.class));
            }
        });
    }

    private void onTwitterClick() {
        if (mTwitter.hasAccessToken()) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(this);

            builder.setMessage("Delete current Twitter connection?")
                   .setCancelable(false)
                   .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                           mTwitter.resetAccessToken();

                           mTwitterBtn.setChecked(false);
                           mTwitterBtn.setText("  Twitter (Not connected)");
                           mTwitterBtn.setTextColor(Color.GRAY);
                       }
                   })
                   .setNegativeButton("No", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();

                            mTwitterBtn.setChecked(true);
                       }
                   });
            final AlertDialog alert = builder.create();

            alert.show();
        } else {
            mTwitterBtn.setChecked(false);

            mTwitter.authorize();
        }
    }

    private final TwDialogListener mTwLoginDialogListener = new TwDialogListener() {
        @Override
        public void onComplete(String value) {
            String username = mTwitter.getUsername();
            username        = (username.equals("")) ? "No Name" : username;

            mTwitterBtn.setText("  Twitter  (" + username + ")");
            mTwitterBtn.setChecked(true);
            mTwitterBtn.setTextColor(Color.WHITE);

            Toast.makeText(TestConnect.this, "Connected to Twitter as " + username, Toast.LENGTH_LONG).show();
        }

        @Override
        public void onError(String value) {
            mTwitterBtn.setChecked(false);

            Toast.makeText(TestConnect.this, "Twitter connection failed", Toast.LENGTH_LONG).show();
        }
    };
}