How to update the text of the header layout every second in any activity and display on all screens?

header.xml

<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/txtTime" android:layout_width="0dip" android:layout_height="fill_parent" android:layout_marginRight="10dp" android:layout_weight="1" android:gravity="right" /> </LinearLayout> 

I added header.xml to all my screen layouts ...

  <include android:id="@+id/top_header" layout="@layout/header" /> 

Now I want to start the timer and set the text of all remaining seconds in the txtTime text header ... So, on which screen do I write the code for the countdown timer and set the textview , so the whole time the screen is displayed in textview ..

can someone suggest me the ability to set text on only one screen and display on all screens?

thanks

+4
source share
2 answers

Please try entering the code below. He will decide your flight.

Follow the instructions below.

 1) Make one BaseActivity Class. 2) Make One MainActivity Class and extend BaseActivity. 3) Make another SecondActivity class and extend BaseActivity. 

Here I post all the classes you need.

What am I doing in BaseActivity?

I am creating one countdown timer class called MyCount that will display the remaining time. I create one boolean flag that sets false to default. therefore, when the user calls the setHeading () function of the BaseActivity function, he will check the falg if it is false, then start the timer, and if it is true then set the timer again and run again.

Baseactivity

 import android.app.Activity; import android.os.Bundle; import android.os.CountDownTimer; import android.widget.TextView; public abstract class BaseActivity extends Activity { private TextView textView = null; public static MyCount counter = null; private static boolean flag = false; private static long millisUntilFinishedVariable = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } protected void setHeading(String message) { if(textView == null) { textView = (TextView)findViewById(R.id.textView); if(textView != null) textView.setText(message); if(flag == false) { counter = new MyCount(30000, 1000); counter.start(); } else { counter.cancel(); counter = new MyCount(millisUntilFinishedVariable, 1000); counter.start(); } } } // countdowntimer is an abstract class, so extend it and fill in methods public class MyCount extends CountDownTimer { public MyCount(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); } @Override public void onFinish() { textView.setText("done!"); //TextView object should be defined in onCreate flag = false; } @Override public void onTick(long millisUntilFinished) { millisUntilFinishedVariable = millisUntilFinished; flag = true; textView.setText("Left:" + millisUntilFinished/1000);// This will be called every Second. } } } 

Mainactivity

 import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends BaseActivity { private Button buttonClick = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setHeading("Text From Main Activity"); buttonClick = (Button)findViewById(R.id.buttonClick); buttonClick.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { startActivityForResult(new Intent(MainActivity.this,SecondActivity.class), 0); } }); } } 

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" > <include layout="@layout/header"/> <Button android:id="@+id/buttonClick" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="96dp" android:layout_marginTop="68dp" android:text="Second Activity" /> </RelativeLayout> 

Secondactivity

 import android.os.Bundle; public class SecondActivity extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second_activity); setHeading("From Second Activity"); } } 

second_activity.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" > <include layout="@layout/header"/> </RelativeLayout> 

header.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="18.0sp"/> </LinearLayout> 
+2
source

You can use SharedPreferences to save text for all screens. Then install textViews with the saved text. You can set textView to onCreate() or onResume() all actions.

+1
source

Source: https://habr.com/ru/post/1436751/


All Articles