Presentation is loading. Please wait.

Presentation is loading. Please wait.

Android Developer Fundamentals V2

Similar presentations


Presentation on theme: "Android Developer Fundamentals V2"— Presentation transcript:

1 Android Developer Fundamentals V2
User Interaction Lesson 4

2 4.1 Buttons and clickable images

3 Button & ImageButton View that responds to tapping (clicking) or pressing Usually text or visuals indicate what will happen when tapped State: normal, focused, disabled, pressed, on/off ImageButton only shows image

4 Button image assets Right-click app/res/drawable
Choose New > Image Asset Choose Action Bar and Tab Items from drop down menu Click the Clipart: image (the Android logo) Experiment: Choose New > Vector Asset

5 Responding to button taps
In your code: Use OnClickListener event listener. In XML: use android:onClick attribute in the XML layout: <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="sendMessage" /> android:onClick A click event occurs when the user taps or clicks a button, or an image used as a button. The object notifies an event listener called OnClickListener, which is an interface in the View class. You can use the listener to respond to a click event. Android Studio provides a shortcut for setting up an OnClickListener for the clickable object in your Activity code, and for assigning a callback method: use the android:onClick attribute with the clickable object’s element in the XML layout. For example: <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="sendMessage" /> In this case, when a user clicks the button, the Android system calls the Activity’s sendMessage() method.

6 Setting listener with onClick callback
Button button = findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Do something in response to button click } });

7 Clickable images

8 ImageView ImageView with android:onClick attribute
Image for ImageView in app>src>main>res>drawable folder in project

9 Responding to ImageView taps
In your code: Use OnClickListener event listener. In XML: use android:onClick attribute in the XML layout: <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="orderDonut"/> android:onClick A click event occurs when the user taps or clicks a button, or an image used as a button. The object notifies an event listener called OnClickListener, which is an interface in the View class. You can use the listener to respond to a click event. Android Studio provides a shortcut for setting up an OnClickListener for the clickable object in your Activity code, and for assigning a callback method: use the android:onClick attribute with the clickable object’s element in the XML layout. For example: <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="sendMessage" /> In this case, when a user clicks the button, the Android system calls the Activity’s sendMessage() method.

10 Dialogs

11 Dialog A dialog is a small window that prompts the user to make a decision or enter additional information. A dialog does not fill the screen and is normally used for modal events that require users to take an action before they can proceed.

12 AlertDialog The AlertDialog class allows you to build a variety of dialog designs and is often the only dialog class you'll need. AlertDialog dialog; AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Enter Job"); EditText input = (EditText) new EditText(this); builder.setView(input);

13 AlertDialog builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which){ dialog.dismiss(); String jobName = input.getText().toString(); todolistAdapter.add(jobName); input.setText(""); } }); builder.setNegativeButton(android.R.string.cancel,new DialogInterface.OnClickListener(){

14 AlertDialog dialog=builder.create(); dialog.show();

15 ListView

16 ListView ListView is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query.

17 ListView private ArrayList<String> todolist; private ArrayAdapter<String> todolistAdapter; todolist=new ArrayList(); todolist.add("AAA"); todolist.add("BBBB"); todolistAdapter =new ArrayAdapter<String> (this, R.layout.simplerow, todolist); list= findViewById(R.id._list1); list.setAdapter(todolistAdapter); todolistAdapter.add( "Eris" );

18 ListView We need to Create a layout with name simplerow.xml
Simplerow is a layout that determines how items should be appear in the LstView It can be a Simple Text View <TextView xmlns:android=" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="itemclicked" android:padding="10dp" android:textSize="16sp"></TextView>

19 Floating action button

20 Floating Action Buttons (FAB)
Raised, circular, floats above layout Primary or "promoted" action for a screen One per screen For example: Add Contact button in Contacts app

21 Using FABs Start with Basic Activity template Layout:
<android.support.design.widget.FloatingActionButton android:layout_gravity="bottom|end" .../>

22 FAB size 56 x 56 dp by default Set mini size (30 x 40 dp) with app:fabSize attribute: app:fabSize="mini" Set to 56 x 56 dp (default): app:fabSize="normal"

23 Common Gestures

24 Touch Gestures Touch gestures include: long touch double-tap fling
drag scroll pinch Don’t depend on touch gestures for app's basic behavior!

25 Detect gestures Classes and methods are available to help you handle gestures. GestureDetectorCompat class for common gestures MotionEvent class for motion events

26 Touch Gestures class MyGestureListner extends GestureDetector.SimpleOnGestureListener { public boolean onFling(MotionEvent event1, MotionEvent event2 ,float velocityX, float velocityY) { Log.d("Gestur", "onFling: " + event1.toString()+event2.toString()); return true; } } private GestureDetectorCompat mDetector; mDetector=new GestureDetectorCompat(this,new MyGestureListner()); public boolean onTouchEvent(MotionEvent event){ this.mDetector.onTouchEvent(event)){ return super.onTouchEvent(event); }

27 Scale Gestures class MyPinchGesture extends ScaleGestureDetector.SimpleOnScaleGestureListener { public boolean onScale(ScaleGestureDetector scaleGestureDetector){ … return true; } } private ScaleGestureDetector gs; g2=new ScaleGestureDetector(this,new MyPinchGesture ()); LinearLayout l2=findViewById(R.id.lay); l2.setOnTouchListener(new View.OnTouchListener(){ public boolean onTouch(View v, MotionEvent event) { g2.onTouchEvent(event); return true; } });

28 END


Download ppt "Android Developer Fundamentals V2"

Similar presentations


Ads by Google