I want to show continuous notifications in my application.
Currently I am able to raise notification automatically after 4sec but what I want is it should continuously raise after specific time interval.
Following is my code.
my xml file is
<?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="wrap_content"
>
<Button android:id="#+id/notify"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Raise a notification"
android:onClick="notifyMe"
/>
<Button android:id="#+id/cancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Clear the notification"
android:onClick="clearNotification"
/>
</LinearLayout>
this is my activity file
package com.vimaltuts.android.notificationexample;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class NotificationExampleActivity extends Activity {
private static final int NOTIFY_ME_ID=1987;
private int count=0;
private NotificationManager mgr=null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mgr=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Timer timer = new Timer();
timer.schedule(new TimerTask()
{
public void run()
{
View v = new View(getApplicationContext());
notifyMe(v);
}},4000);
}
#SuppressWarnings("deprecation")
public void notifyMe(View v)
{
#SuppressWarnings("deprecation")
Notification note=new Notification(R.drawable.stat_notify_chat,"Status message!",System.currentTimeMillis());
PendingIntent i=PendingIntent.getActivity(this,0,new Intent(this, NotificationMessage.class),0);
note.setLatestEventInfo(this, "New Email","Unread Conversation", i);
note.number=++count;
note.vibrate=new long[] {500L, 200L, 200L, 500L};
note.flags|=Notification.FLAG_AUTO_CANCEL;
mgr.notify(NOTIFY_ME_ID, note);
}
public void clearNotification(View v) {
mgr.cancel(NOTIFY_ME_ID);
}
}
Please tell me where should I need to have additional code if any?
Also is it possible with service..how?
i tried following code foe test purpose but it did't work...can anyone tell me where i goes wrong
following is my activity code
Intent i= new Intent(NotificationExampleActivity.this,NotificationService.class);
PendingIntent pi=PendingIntent.getService(NotificationExampleActivity.this, 0, i, 0);
AlarmManager alarmMgr=(AlarmManager)getSystemService(ALARM_SERVICE);
alarmMgr.cancel(pi);
alarmMgr.setRepeating(alarmMgr.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis(),(10*1000), pi);
Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();
And following is my broadcast reciver
Toast.makeText(context, "Repeating Alarm worked.", Toast.LENGTH_LONG).show();
i want to display msg="Repeating Alarm worked" after some time intervel
Try using an AlarmManager along with the notification.
You can set a repeating alarm in your Activity. Then create a class that extends the BroadcastReceiver. Override the onReceive method where in you can write the code for your notification.
A tutorial on this is explained very well over here.
Related
I made a small app that takes input from the user in the form of a TimePicker, stores it in variables, that are later used to trigger an alarm which will keep the ringer at the desired mode until required.
But everytime I try running it on the AVD, it keeps crashing whenever I press any button, either silent or vibrate.
The MainActivity.java file:
package com.example.android.implementingringermechanism;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.Toast;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
//TimePicker to take time input from user when dialog activity pops
TimePicker picker;
//Buttons to change mode to silent or vibrate
Button silentButton, vibrateButton;
//Variables to store hours and minutes chosen by user
private int hour, minute;
//AudioManager instance used to change ringer mode
private AudioManager mAudioManager;
//AlarmManager instance used to set alarm to background task
AlarmManager mgr;
//Calendar instance to manipulate time for alarm with user input
Calendar time;
//PendingIntent to start when alarm is finished
private PendingIntent pendingIntent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
picker = (TimePicker) findViewById(R.id.timePicker);
//TimePicker will show time in 24 hour mode
picker.setIs24HourView(true);
silentButton = (Button) findViewById(R.id.silentButton);
vibrateButton = (Button) findViewById(R.id.vibrateButton);
//AlarmManager instance created to set an alarm for changing ringer mode back to normal
mgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, MyAlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
time = Calendar.getInstance();
time.setTimeInMillis(System.currentTimeMillis());
}
public void silent(View view){
if(Build.VERSION.SDK_INT >= 23){
hour = picker.getHour();
minute = picker.getMinute();
}
else {
hour = picker.getCurrentHour();
minute = picker.getCurrentMinute();
}
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
Toast.makeText(this, "Silent mode till " + hour + ":" + minute, Toast.LENGTH_LONG).show();
time.add(Calendar.HOUR, hour);
time.add(Calendar.MINUTE, minute);
mgr.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), pendingIntent);
}
public void vibrate(View view) {
if(Build.VERSION.SDK_INT >= 23){
hour = picker.getHour();
minute = picker.getMinute();
}
else {
hour = picker.getCurrentHour();
minute = picker.getCurrentMinute();
}
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
Toast.makeText(this, "Vibrate mode till " + hour + ":" + minute, Toast.LENGTH_LONG).show();
time.add(Calendar.HOUR, hour);
time.add(Calendar.MINUTE, minute);
mgr.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), pendingIntent);
}
}
The activity_main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
tools:context=".MainActivity"
android:layout_gravity="center"
android:orientation="vertical"
>
<TimePicker
android:id="#+id/timePicker"
android:layout_width="273dp"
android:layout_height="368dp"
android:layout_marginLeft="40dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/silentButton"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="40dp"
android:text="#string/silentText"
android:textStyle="bold"
android:onClick="silent"/>
<Button
android:id="#+id/vibrateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:text="#string/vibrateText"
android:textStyle="bold"
android:onClick="vibrate"
/>
</LinearLayout>
</LinearLayout>
The AndroidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.implementingringermechanism">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/setTime"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat.Dialog">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyAlarmReceiver" />
</application>
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
</manifest>
The MyAlarmReceiver.java file:
package com.example.android.implementingringermechanism;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.widget.Toast;
class MyAlarmReceiver extends BroadcastReceiver{
private AudioManager AM;
#Override
public void onReceive(Context context, Intent intent){
AM.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
Toast.makeText(context, "Ringer Mode changed to Normal", Toast.LENGTH_LONG).show();
}
}
For now, the problem I'm encountering is that on app startup, the dialog box appears, but pressing any of the two buttons makes it crash.
Your code isn't working because of two reasons:
Your AudioManager is null. Add this code to your onCreate():
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
If you're testing on a device that's running Android Marshmallow or higher, you'll need to request permission from the user before you're able to set the ringer mode. You can gain that request by adding this code:
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& !notificationManager.isNotificationPolicyAccessGranted()) {
startActivity(new Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS));
}
Sir if you are testing on higher android api you need to add ask administrator previlagers first
private void fornougat() {
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
&& !notificationManager.isNotificationPolicyAccessGranted()) {
Toast.makeText(this, R.string.permission, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(
android.provider.Settings
.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivity(intent);
}
}
i am trying to make a small project in android for the college where the user has the option to select time from time picker dialog and based on the time selected an notification to drink water will be issued to the user and repeated every 15 minutes.However the alarm manager used issues notification vaguely.Sometimes notification are issued on time and repeated after every 15 minutes although after midnight the notifications stop.Also sometimes there is a delay to issue the notification.Cant figure out why! Help appreciated..Here are the necessary xml and java files
activity_water.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="#+id/relative"
tools:context="com.example.priyam.databaselogin.Water">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/darker_gray"
android:layout_marginTop="70dp"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="15dp">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/reminderWaterCheckbox"
android:buttonTint="#color/red"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Remind me once at"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="170dp"
android:text="09:30 AM"
android:textColor="#color/red"
android:id="#+id/textViewTime"/>
</LinearLayout>
</RelativeLayout>
water.java
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import java.util.Calendar;
public class Water extends AppCompatActivity implements View.OnClickListener {
TextView time;
CheckBox waterReminder;
int h,m;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_water);
setTitle("Water Reminder");
getId();
Toast.makeText(getApplicationContext(),"this",Toast.LENGTH_LONG).show();
}
private void getId(){
time=(TextView)findViewById(R.id.textViewTime);
waterReminder=(CheckBox)findViewById(R.id.reminderWaterCheckbox);
time.setOnClickListener(this);
}
private void showDialog(){
TimePickerDialog timePickerDialog = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
h=hourOfDay;
m=minute;
Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,h);
calendar.set(Calendar.MINUTE,m);
Intent intent=new Intent(getApplicationContext(),Notification_receiver.class);
PendingIntent pendingIntent=PendingIntent.getBroadcast(getApplicationContext(),100,intent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager= (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_FIFTEEN_MINUTES,pendingIntent);
time.setText(h+":"+m);
}
}, h, m, false);
timePickerDialog.show();
}
#Override
public void onClick(View v) {
showDialog();
}
}
Notification_receiver.java
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
public class Notification_receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent1=new Intent(context,DashBoard.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent=PendingIntent.getActivity(context,100,intent1,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder=new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.logo)
.setContentTitle("Hitfit")
.setContentText("Its time you gulped a glass of water!Stay hydrated,stay fit!")
.setAutoCancel(true);
builder.setVibrate(new long[] { 1000, 1000});
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(alarmSound);
notificationManager.notify(100,builder.build());
}
}
Minnimum sdkVersion used is 18
Stuck on this since days.Thank you!
In the documentation of AlarmManager, you can see the following notice :
Note: as of API 19, all repeating alarms are inexact. If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time as described above. Legacy applications whose targetSdkVersion is earlier than API 19 will continue to have all of their alarms, including repeating alarms, treated as exact.
I think you are in this case actually, and your alarms aren't exact because that's how the system keeps battery as of API 19.
I have a simple app that generates notifications on button Click. I also have an editText so that the content Text is typed by the user. I have two buttons that generates two different notifications. My requirement is that on every button click there should be only one notification generated and the rest should be in the notification stack. I have tried using notification setGroup property but the method doesn't seem to work. I have attached my code below for further reference.
Main Activity(Java file):-
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.TaskStackBuilder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private static final String GROUP_NOTIFICATIONS="group_notifications";
NotificationCompat.Builder notification;
NotificationCompat.Builder notification1;
PendingIntent pIntent;
NotificationManager manager,manager1;
Intent resultIntent;
Button button,button1;
EditText editText;
int a=0,b=100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText= (EditText) findViewById(R.id.edittext);
button= (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startNotification();
}
});
button1= (Button) findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Notifications();
}
});
}
public void buttonClick(View view)
{
startNotification();
}
public void startNotification()
{
// Intent intent=new Intent(MainActivity.this,MyService.class);
// intent.putExtra("id",Integer.toString(a));
// startService(intent);
// PendingIntent pendingIntent=PendingIntent.getService(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
String str=editText.getText().toString();
notification=new NotificationCompat.Builder(this);
notification.setContentTitle("ABC");
notification.setContentText(str);
notification.setTicker("New Message Arrived");
notification.setSmallIcon(R.mipmap.ic_launcher);
notification.setGroup(GROUP_NOTIFICATIONS);
notification.setGroupSummary(true);
int num=0;
// notification.setNumber(++num);
Uri uri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notification.setSound(uri);
manager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(a,notification.build());
a++;
}
public void Notifications()
{
String str=editText.getText().toString();
notification1=new NotificationCompat.Builder(MainActivity.this);
notification1.setContentTitle("ABC");
notification1.setContentText(str);
notification1.setTicker("New Message Arrived");
notification1.setSmallIcon(R.mipmap.ic_launcher);
notification1.setGroup(GROUP_NOTIFICATIONS);
notification1.setGroupSummary(true);
int num=0;
// notification1.setNumber(++num);
Uri uri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notification1.setSound(uri);
manager1= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager1.notify(a,notification1.build());
a++;
}
}
activity_main.xml(XML file)
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.pratik.stacked.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/edittext"
android:textColor="#color/colorPrimary"
android:textSize="20sp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Stacked Notification"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Stacked notifiction 1"
android:id="#+id/button2"
android:layout_above="#+id/button"
android:layout_alignLeft="#+id/button"
android:layout_alignStart="#+id/button"
/>
</RelativeLayout>
AndroidManifest.xml(Manifest file)
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
This is all about setting properly flags.
FLAG_GROUP_SUMMARY
The problem is that you are incrementing (a++) the notification ID:
manager1.notify(a,notification1.build());
a++;
If you want to update an existing notification you should call notify with the notification ID that you want to update. You can refer to the documentation
If you are on Android >= 4.1 you can use expanded layouts. Take into account that in this case you may want to ensure compatibility following these guidelines.
This is my sample code that i used to generate notifications. So far i was able to get English Notifications. But now i want to get another language notifications on the notification bar ("Sinhala" )
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification ")
.setContentText("Hello World!");
mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(context, View.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(View.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(0, mBuilder.build());
(i)Add any sinhala TTF file to the assets directory in the project(copy and paste).
For a sample create main.xml file using two edit text filed and a button.
<?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"
android:weightSum="1">
<LinearLayout android:layout_width="match_parent" android:id="#+id/linearLayout1" android:orientation="vertical" android:layout_height="wrap_content">
<EditText android:layout_height="wrap_content" android:id="#+id/editText1" android:layout_width="match_parent">
<requestFocus></requestFocus>
</EditText>
</LinearLayout>
<Button android:text="Tnkak" android:layout_height="wrap_content" android:id="#+id/button1" android:layout_width="104dp"></Button>
<EditText android:layout_height="wrap_content" android:id="#+id/editText2" android:layout_width="match_parent"></EditText>
</LinearLayout>
android:text="Tnkak", this English letters are related to Sinhala ඔබන්න(Click) word.
In LanguageAppActivity class
package com.languageapp;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class LanguageAppActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button myButton=(Button)findViewById(R.id.button1);
Typeface type=Typeface.createFromAsset(getAssets(), "ARADANA.TTF");
myButton.setTypeface(type);
myButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Typeface tf = Typeface.createFromAsset(getAssets(),"ARADANA.TTF");
EditText et=(EditText)findViewById(R.id.editText2);
et.setTypeface(tf);
EditText editText=(EditText)findViewById(R.id.editText1);
et.setText(editText.getText());
}
}
You can do the same thing for your title and notification bar!
I want to create a clock showing the hour and Minute of the current time like this 02:15 PM. What i want is now to keep the minutes part updating obviously after every 60 seconds as it is changed in our systems. So i want to know what is the best way to keep the time updating
thanks
.xml file's code,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<TextView
android:id="#+id/txtTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
</RelativeLayout>
Activity file's code,
package com.example.demoproject;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.text.format.Time;
import android.widget.TextView;
public class MainActivity extends Activity
{
private static TextView textview;
private Timer timer;
private Time today;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview = (TextView ) findViewById( R.id.txtTime );
today = new Time(Time.getCurrentTimezone());
timer = new Timer();
timer.schedule(new RemindTask(), 1000, 1000 );
}
private class RemindTask extends TimerTask
{
public void run()
{
runOnUiThread(new Runnable()
{
public void run()
{
today.setToNow();
textview.setText(today.format("%k:%M:%S")); // Current time
}
});
}
}
}