‏إظهار الرسائل ذات التسميات FloatingActionButton. إظهار كافة الرسائل
‏إظهار الرسائل ذات التسميات FloatingActionButton. إظهار كافة الرسائل

الخميس، 1 أكتوبر 2015

Hello World to open photo using Intent.ACTION_OPEN_DOCUMENT, with FloatingActionButton and Snackbar

This example work on last post "Updated Android Studio now provide template of Blank Activity with FloatingActionButton and Snackbar", modify the default Hello World to open image with ACTION_OPEN_DOCUMENT,  display on ImageView.


edit layout/activity_main.xml, to modify the icon of the FloatingActionButton, android:src inside <android.support.design.widget.FloatingActionButton>.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context=".MainActivity">

<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton android:id="@+id/fab"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_menu_gallery" />

</android.support.design.widget.CoordinatorLayout>


layout/content_main.xml, it's the main layout of our app.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold"/>

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:id="@+id/texturi"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"/>

</LinearLayout>
</ScrollView>
</LinearLayout>


com.blogspot.android_er.androidhello.MainActivity.java
package com.blogspot.android_er.androidhello;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.FileNotFoundException;

public class MainActivity extends AppCompatActivity {

private static final int RQS_OPEN_IMAGE = 1;

ImageView imageView;
TextView textUri;

Bitmap bmOriginal = null;
Uri targetUri = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Open photo", Snackbar.LENGTH_LONG)
.setAction("OK", snackbarOnClickListener)
.show();
}
});

textUri = (TextView) findViewById(R.id.texturi);
imageView = (ImageView) findViewById(R.id.image);
}

OnClickListener snackbarOnClickListener = new OnClickListener(){
@Override
public void onClick(View v) {

bmOriginal = null;
imageView.setImageBitmap(null);

Intent intent = new Intent();

if (Build.VERSION.SDK_INT >=
Build.VERSION_CODES.KITKAT) {
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
} else {
intent.setAction(Intent.ACTION_GET_CONTENT);
}

intent.addCategory(Intent.CATEGORY_OPENABLE);

// set MIME type for image
intent.setType("image/*");

startActivityForResult(intent, RQS_OPEN_IMAGE);

}
};

@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {

if (resultCode == Activity.RESULT_OK) {

Uri dataUri = data.getData();

if (requestCode == RQS_OPEN_IMAGE) {
targetUri = dataUri;
textUri.setText(dataUri.toString());
updatImage(dataUri);
}
}

}

private void updatImage(Uri uri){

if (uri != null){
Bitmap bm;
try {
bm = BitmapFactory.decodeStream(
getContentResolver()
.openInputStream(uri));
imageView.setImageBitmap(bm);
bmOriginal = bm;

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}



download filesDownload the files (Android Studio Format) .

Related:
- Using Intent.ACTION_OPEN_DOCUMENT, for KitKat API 19 or higher

Next:
- Apply photo effects using Media Effects APIs

Updated Android Studio now provide template of Blank Activity with FloatingActionButton and Snackbar




Related:
- Modify this Hello World to open image using Intent.ACTION_OPEN_DOCUMENT, with FloatingActionButton and Snackbar.
- Disable FloatingActionButton


الاثنين، 10 أغسطس 2015

CoordinatorLayout + FloatingActionButton + Snackbar of Android Design Support Library

This example show how CoordinatorLayout + FloatingActionButton + Snackbar work together - a FloatingActionButton added as a child of CoordinatorLayout, and then pass that CoordinatorLayout to Snackbar.make() call - instead of the snackbar displaying over the floating action button, the FloatingActionButton takes advantage of additional callbacks provided by CoordinatorLayout to automatically move upward as the snackbar animates in and returns to its position when the snackbar animates out on Android 3.0 and higher devices - no extra code required.


layout/activity_main.xml
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:padding="10dp"
android:id="@+id/coordinatorLayout"
android:background="#000050"
tools:context=".MainActivity">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:padding="10dp"
android:background="#005000">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web"
android:textSize="24dp"
android:layout_alignParentTop="true"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="30dp"
android:layout_alignParentBottom="true"
android:text="Android FloatingActionButton example" />
</RelativeLayout>

<android.support.design.widget.FloatingActionButton
android:id="@+id/floatingActionButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginRight="20dp"
android:src="@mipmap/ic_launcher"
app:fabSize="normal"
app:layout_anchor="@id/coordinatorLayout"
app:layout_anchorGravity="bottom|left|end"/>

<android.support.design.widget.FloatingActionButton
android:id="@+id/floatingActionButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginRight="20dp"
android:src="@mipmap/ic_launcher"
app:fabSize="mini"
app:layout_anchor="@id/coordinatorLayout"
app:layout_anchorGravity="bottom|right|end"/>

</android.support.design.widget.CoordinatorLayout>


com.example.eric.androidfloatingactionbutton.MainActivity.java
package com.example.eric.androidfloatingactionbutton;

import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

FloatingActionButton floatingActionButton1, floatingActionButton2;
CoordinatorLayout coordinatorLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
coordinatorLayout = (CoordinatorLayout)findViewById(R.id.coordinatorLayout);
floatingActionButton1 =
(FloatingActionButton)findViewById(R.id.floatingActionButton1);
floatingActionButton2 =
(FloatingActionButton)findViewById(R.id.floatingActionButton2);

floatingActionButton1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {

Snackbar snackbar = Snackbar.make(
coordinatorLayout,
"Snackbar: floatingActionButton1 (normal) clicked",
Snackbar.LENGTH_LONG);

snackbar.setAction("OK", new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(
MainActivity.this,
"snackbar OK clicked",
Toast.LENGTH_LONG).show();
}
});

snackbar.show();
}
});

floatingActionButton2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Snackbar snackbar = Snackbar.make(
coordinatorLayout,
"Snackbar: floatingActionButton2 (mini) clicked",
Snackbar.LENGTH_LONG);

snackbar.setAction("OK", new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(
MainActivity.this,
"snackbar OK clicked",
Toast.LENGTH_LONG).show();
}
});

snackbar.show();
}
});
}

}


Android Design Support Library is needed, read "How to Add Android Design Support Library to Android Studio Project".


Next:
- Set text and background color of Snackbar

السبت، 8 أغسطس 2015

Android FloatingActionButton example

FloatingActionButton are used for a special type of promoted action. They are distinguished by a circled icon floating above the UI and have special motion behaviors related to morphing, launching, and the transferring anchor point.

Floating action buttons come in two sizes: the default and the mini. The size can be controlled with the fabSize attribute.


Example:


<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:padding="10dp"
android:id="@+id/coordinatorLayout"
android:background="#000050"
tools:context=".MainActivity">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:padding="10dp"
android:background="#005000">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web"
android:textSize="24dp"
android:layout_alignParentTop="true"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="30dp"
android:layout_alignParentBottom="true"
android:text="Android FloatingActionButton example" />
</RelativeLayout>

<android.support.design.widget.FloatingActionButton
android:id="@+id/floatingActionButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginRight="20dp"
android:src="@mipmap/ic_launcher"
app:fabSize="normal"
app:layout_anchor="@id/coordinatorLayout"
app:layout_anchorGravity="bottom|left|end"/>

<android.support.design.widget.FloatingActionButton
android:id="@+id/floatingActionButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginRight="20dp"
android:src="@mipmap/ic_launcher"
app:fabSize="mini"
app:layout_anchor="@id/coordinatorLayout"
app:layout_anchorGravity="bottom|right|end"/>

</android.support.design.widget.CoordinatorLayout>


package com.example.eric.androidfloatingactionbutton;

import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

FloatingActionButton floatingActionButton1, floatingActionButton2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
floatingActionButton1 =
(FloatingActionButton)findViewById(R.id.floatingActionButton1);
floatingActionButton2 =
(FloatingActionButton)findViewById(R.id.floatingActionButton2);

floatingActionButton1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,
"floatingActionButton1 (normal) clicked",
Toast.LENGTH_LONG).show();
}
});

floatingActionButton2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,
"floatingActionButton2 (mini) clicked",
Toast.LENGTH_LONG).show();
}
});
}

}


Design Support Library is needed, refer to "Add Android Design Support Library to Android Studio Project".

Next:
- CoordinatorLayout + FloatingActionButton + Snackbar of Android Design Support Library