Skip to main content

Featured post

Simple RecyclerView example with filter option in Android

Hi Guys, Maybe you all are expert in terms of using RecyclerView in android. This blog is simple example for using filter option with RecyclerView adapter. As for now you will instantiate RecyclerView and set the adapter to RecyclerView as following way. RecyclerView list = (RecyclerView) findViewById(R.id.list); list.setLayoutManager(new LinearLayoutManager(this)); list.setHasFixedSize(true); ArrayList&ltNumber&gt numbers = new ArrayList&lt&gt(); String ONEs[] = {"ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN"}; String TENs[] = {"ZERO", "TEN", "TWENTY", "THIRTY", "FOURTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINETY", "HUNDRED"}; String HUNDREDS[] = {"ZERO", "HUNDRED", "TWO HUND...

Simple Timer Example in android

Here is the simple example of using timer in Android
public void startTimer() {
    timer = new Timer();
    TimerTask timerTask = new TimerTask() {

        @Override
        public void run() {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    textView.setText(String.format(Locale.getDefault(), "%d", time));
                    if (time > 0)
                        time -= 1;
                    else {
                        textView.setText(R.string.hello_world);
                        btnStart.setChecked(false);
                    }
                }
            });
        }
    };
    timer.scheduleAtFixedRate(timerTask, 0, 1000);
}
Full class
MainActivity.java
package com.guna.testapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;

import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    private int time = 20;
    private Timer timer;
    private TextView textView;
    private ToggleButton btnStart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.textView);
        btnStart = findViewById(R.id.toggleButton);

        btnStart.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    time = 20;
                    startTimer();
                } else {
                    timer.cancel();
                    timer.purge();
                    textView.setText(R.string.hello_world);
                }
            }
        });
    }

    public void startTimer() {
        timer = new Timer();
        TimerTask timerTask = new TimerTask() {

            @Override
            public void run() {
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        textView.setText(String.format(Locale.getDefault(), "%d", time));
                        if (time > 0)
                            time -= 1;
                        else {
                            textView.setText(R.string.hello_world);
                            btnStart.setChecked(false);
                        }
                    }
                });
            }
        };
        timer.scheduleAtFixedRate(timerTask, 0, 1000);
    }
}
And here is the activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="com.guna.testapplication.MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="2dp"
        android:text="@string/hello_world"
        android:textColor="@color/colorAccent"
        android:textSize="22sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/toggleButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed" />

    <ToggleButton
        android:id="@+id/toggleButton"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="2dp"
        android:checked="false"
        android:textOff="@string/start"
        android:textOn="@string/stop"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />
</android.support.constraint.ConstraintLayout>
Screenshot:

Interesting right?


If you are really interested in this code, then please share this post with your friends and share your feedback as comments.



Thank You


Comments

  1. Thanks for sharing this message.It's useful for us
    artificial intelligence training in coimbatore

    Blue prism training in coimbatore

    RPA Course in coimbatore

    Robotic process automation training in coimbatore

    C and C++ training in coimbatore

    big data training in coimbatore

    amazon web services training in coimbatore

    ReplyDelete
  2. You there, this is really good post here. Thanks for taking the time to post such valuable information. Quality content is what always gets the visitors coming. blackmart apk old version

    ReplyDelete
  3. hi was just seeing if you minded a comment. i like your website and the thme you picked is super. I will be back. smart glass

    ReplyDelete
  4. The ambition to become a software tester is common and is worth praising because of the fast-growing significance and cost-effectiveness of the job. With software testing name, it refers to a procedure that is conducted to ensure the quality and standard of a product or service. HyFy TV For PC

    ReplyDelete
  5. The world needs no introduction regarding the rivalry between iOS and Android smart phones. While the applications and facilities provided by both Android and iOS operating systems is a lot different from each other, but the world smart phone customer base gets hugely affected with each new launch from these giants. While iOS devices are known for their high- end modern smart phones, the Android operating system made it possible for the people to have access to all advanced technologies at a range of affordable prices. Source

    ReplyDelete
  6. The development of Wireless technology owes it all to Michael Faraday - for discovering the principle of electromagnetic induction, to James Maxwell - for the Maxwell's equations and to Guglielmo Marconi - for transmitting a wireless signal over one and a half miles. The sole purpose of Wireless technology is wireless communication, through which information can be transferred between two or more points that are not connected by electrical conductors. Wireless technologies were in use since the advent of radios, which use electromagnetic transmissions. Eventually, consumer electronics manufacturers started thinking about the possibilities of automating domestic micro-controller based devices. Timely and reliable relay of sensor data and controller commands were soon achieved, which led to the discovery of Wireless communications that we see everywhere now. mobile tracker free

    ReplyDelete
  7. The development of Wireless technology owes it all to Michael Faraday - for discovering the principle of electromagnetic induction, to James Maxwell - for the Maxwell's equations and to Guglielmo Marconi - for transmitting a wireless signal over one and a half miles. The sole purpose of Wireless technology is wireless communication, through which information can be transferred between two or more points that are not connected by electrical conductors. Wireless technologies were in use since the advent of radios, which use electromagnetic transmissions. Eventually, consumer electronics manufacturers started thinking about the possibilities of automating domestic micro-controller based devices. Timely and reliable relay of sensor data and controller commands were soon achieved, which led to the discovery of Wireless communications that we see everywhere now. best app to spy the camera

    ReplyDelete
  8. I have read all the comments and suggestions posted by the visitors for this article are very fine,We will wait for your next article so only.Thanks! spacedesk

    ReplyDelete
  9. I admit, I have not been on this web page in a long time... however it was another joy to see It is such an important topic and ignored by so many,
    even professionals. I thank you to help making people more aware of possible issues.spacedesk

    ReplyDelete
  10. When you purchase spy software iPhone and iPad for iPhones, you will get a limited free version and a paid version. The limited version will not have all the advanced features of the paid version. With the limited version, you will only be able to track and log online activities. Get detailed info about spy phone on this site.

    ReplyDelete
  11. I can’t believe focusing long enough to research; much less write this kind of article. You’ve outdone yourself with this material without a doubt. It is one of the greatest contents. Text Auto Reply

    ReplyDelete
  12. You make so many great points here that I read your article a couple of times. Your views are in accordance with my own for the most part. This is great content for your readers. out of office text message

    ReplyDelete
  13. The article looks magnificent, but it would be beneficial if you can share more about the suchlike subjects in the future. Keep posting. Auto Reply while Driving

    ReplyDelete
  14. Wow, this is fascinating reading. I am glad I found this and got to read it. Great job on this content. I liked it a lot. Thanks for the great and unique info. Auto Reply while Driving

    ReplyDelete
  15. If you loved the easy and user-friendly interface of either Android 2.3 Gingerbread or Android 3.0 Honeycomb, you will also love the new improvements in Android 4.0 Ice Cream Sandwich, which supposedly brings together the best elements of Gingerbread and Honeycomb. Here are the three top design improvements that Google implemented in Android 4.0 Ice Cream Sandwich. Veracious Tips for fitness apps

    ReplyDelete
  16. Ingredients How to use it? Pros Cons Customer Experience We've had a lot of success stories in 2020, and there are hundreds of happy customers who have used Aloe rid shampoo. If you ask us, we have never seen a customer pass a hair drug test without first using the aloe rid shampoo. To summarize, this shampoo is still effective, and it will continue to be effective and powerful for those seeking the best option and methods for passing a hair drug test. Many online forums and blog comments confirm that this shampoo is the genuine article that aids in passing common hair drug tests. The Macujo Method Macujo is the most well-known hair detoxing procedure. It has the following detection time frame: Weed lasts the longest in your hair – blood vessels transport weed to your hair follicles

    ReplyDelete
  17. I need to to thank you for this fantastic read!! I definitely loved every little bit of it. I’ve got you saved as a favorite to look at new stuff you post… 토토

    ReplyDelete
  18. You completed a number of fine points there. I did a search on the subject and found mainly people will agree with your blog. 경마사이트

    ReplyDelete

Post a Comment

Popular posts from this blog

Simple example of OCRReader in Android.

Hi Friends, Maybe you all heard/used text scanning using camera feature or extracting text from Image. But this sample made it very easy for you. You can made it in very simple line of code. You can download the source code from OCRSample and import the library as a module into your project. Example usage : MainActivity.java public class MainActivity extends AppCompatActivity { private TextView textView; private final int CAMERA_SCAN_TEXT = 0; private final int LOAD_IMAGE_RESULTS = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.textView); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_main, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSele...

Simple example of using Spinner in Kotlin | Android

Though Kotlin has lot massive features to speedup the development time, here is the simple way of using Spinner in Android. In Kotlin we don't need to declare and initialize Spinner. We can simply access the id of Spinner from xml. Ex : import android.os.Bundle import android.support.v7.app.AppCompatActivity import android.view.View import android.widget.AdapterView import android.widget.ArrayAdapter import android.widget.Toast import android.widget.Toast.LENGTH_LONG import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) //String array. val myStrings = arrayOf("One", "Two", "Three", "Four", "Five") //Adapter for spinner mySpinner.adapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_i...

Set limit for fraction in decimal numbers in EditText

            Already we know that we can set which type of input the edittext should accept from user using android:inputType="numberDecimal" But there is no predefined function to set the limit for the edittext to How many digit it should accept after the decimal point from user . We can achieve this by using TextWatcher . Full code example. Following program creates a Decimal Filter. DecimalFilter.java import android.text.Editable; import android.text.InputFilter; import android.text.TextWatcher; import android.view.KeyEvent; import android.view.View; import android.view.View.OnKeyListener; import android.widget.EditText; public class DecimalFilter implements TextWatcher { int count= -1 ; EditText et; Activity activity; public DecimalFilter(EditText edittext, Activity activity) { et = edittext; this.activity = activity; } public void afterTextChanged(Editable s) { if (s.length() > 0) { String str = et.get...