In This Tutorial we will lwarn how to vibrate device on buttons click.
Follow Below Steps
1. Create New Android Project Name it as vibrate.
2 . After Creating New Project , Open Your activity_main.xml. Follow Path .res --> Layout --> activity_main.xml.
3 . Add Below Code to your activity_main.xml.
activity_main.xml
4 . Add Below Code To your MainActivity.java
MainActivity.java
5. Now Compile And Run Your Project.
6. My Output Screen is as below
All The Best :)
Follow Below Steps
1. Create New Android Project Name it as vibrate.
2 . After Creating New Project , Open Your activity_main.xml. Follow Path .res --> Layout --> activity_main.xml.
3 . Add Below Code to your activity_main.xml.
activity_main.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=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginTop="72dp"
android:text="Vibrate" />
</RelativeLayout>
4 . Add Below Code To your MainActivity.java
MainActivity.java
package arshad.vibrate;
import android.os.Bundle;
import android.os.Vibrator;
import
android.app.Activity;
import
android.content.Context;
import android.view.Menu;
import android.view.View;
import
android.widget.Button;
public class MainActivity extends Activity {
Button vib;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vib=(Button)findViewById(R.id.button1);
vib.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view)
{
final Vibrator vibe =
(Vibrator) MainActivity.this.getSystemService(Context.VIBRATOR_SERVICE);
vibe.vibrate(80);
//80 represents the
milliseconds (the duration of the vibration)
}
});
}
@Override
public boolean
onCreateOptionsMenu(Menu menu) {
// Inflate the
menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
5. Now Compile And Run Your Project.
6. My Output Screen is as below
All The Best :)
No comments:
Post a Comment