Found 34494 Articles for Programming

How to get the dimensions of a view in Android?

Arjun Thakur
Updated on 25-Jun-2020 09:55:03

5K+ Views

There are so many cases, we should create dynamic view rather than creating view in XML. In that scenario, we should need to get the dimensions of a view. So here is a simple solution to get the dimensions of a view in android.To get the height of any view use the following codeint width = view.getMeasuredHeight();To get the width of any view use the following codeint height = view.getMeasuredWidth();Before get the width and height, we should assign default measure for a view as shown belowview.measure(0, 0);In the above code view is like either textview ,editText, button ..etc. Here is ... Read More

Java program to extract ‘k’ bits from a given position

karthikeya Boyini
Updated on 25-Jun-2020 10:04:23

399 Views

Extraction of k bits from the given position in a number involves converting the number into its binary representation. An example of this is given as follows −Number = 20 Binary representation = 10100 k = 3 Position = 2 The bits extracted are 010 which represent 2.A program that demonstrates this is given as follows.Example Live Demopublic class Example {    public static void main (String[] args) {       int number = 20, k = 3, pos = 2;       int exNum = ((1 > (pos - 1));       System.out.println("Extract " + k + ... Read More

How to display HTML in TextView in Android?

George John
Updated on 26-Jun-2020 05:51:31

6K+ Views

In Some situations, we should need to show HTML as text in android. Here is the simple solution to show HTML in TextView in android.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.     Step 3 − Add the following code to src/MainActivity.javapackage com.example.andy.myapplication; import android.os.Bundle; import android.support.v4.text.HtmlCompat; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; public class MainActivity extends AppCompatActivity {    String htmlText = "What is Android?" + "Android is an open source and Linux-based ... Read More

Java program to check if two given matrices are identical

Samual Sam
Updated on 26-Jun-2020 05:52:55

1K+ Views

Two matrices are identical if their number of rows and columns are equal and the corresponding elements are also equal. An example of this is given as follows.Matrix A = 1 2 3 4 5 6 7 8 9 Matrix B = 1 2 3 4 5 6 7 8 9 The matrices A and B are identicalA program that checks if two matrices are identical is given as follows.Example Live Demopublic class Example {    public static void main (String[] args) {       int A[][] = { {7, 9, 2}, {3, 8, 6}, {1, 4, 2} };   ... Read More

How to close or hide the virtual keyboard on Android?

Chandu yadav
Updated on 25-Jun-2020 10:17:36

4K+ Views

In Android there are some situations, we should close android default keyboard forcefully. For that this example is help for you.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.             Step 3Add the following code to src/MainActivity.javaimport android.app.ProgressDialog; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.support.annotation.RequiresApi; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.EditText; import android.widget.ProgressBar; public class MainActivity extends AppCompatActivity implements View.OnClickListener {    Handler mHandler;   ... Read More

Java program to count words in a given string

karthikeya Boyini
Updated on 31-May-2024 12:58:51

13K+ Views

A string is a class in Java that stores a series of characters enclosed within double quotes. Those characters act as String-type objects. The aim of this article is to write Java programs that count words in a given string. Counting words of the given strings can be solved using the string manipulation techniques. In Java interviews, questions from string manipulation are very frequent, hence, it is important to understand this problem properly. Java Program to Count Words in a given String Before jumping to the example programs, let's understand the problem statement with the help of an example. Input ... Read More

How to check visibility of virtual keyboard on Android?

Ankith Reddy
Updated on 30-Jul-2019 22:30:23

835 Views

There are some situations, we should find keyboard is visible or not in particular activity. In this example we can check visibility of virtual keyboard on android.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.             Step 3 − Add the following code to src/MainActivity.javaimport android.graphics.Rect; import android.os.Build; import android.os.Bundle; import android.support.annotation.RequiresApi; import android.support.constraint.ConstraintLayout; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.view.ViewTreeObserver; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public ... Read More

Swap two variables in one line in using Java

Samual Sam
Updated on 30-Jul-2019 22:30:23

5K+ Views

Two variables can be swapped in one line in Java. This is done by using the given statement. x = x ^ y ^ (y = x); where x and y are the 2 variables. A program that demonstrates this is given as follows − Example Live Demo public class Example { public static void main (String[] args) { int x = 12, y = 25; System.out.println("Original values of x and y"); System.out.println("x = " ... Read More

Java program to count total bits in a number

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

670 Views

The total bits in a number can be counted by using its binary representation. An example of this is given as follows − Number = 9 Binary representation = 1001 Total bits = 4 A program that demonstrates this is given as follows. Example Live Demo public class Example { public static void main(String[] arg) { int num = 10; int n = num; int count = 0; while ... Read More

How to access unique Android device ID?

Arjun Thakur
Updated on 30-Jul-2019 22:30:23

2K+ Views

If you want to check unique device id like IMEI number through programmatically we can do this by telephonic manger as shown below example −Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.     Step 3 − Add the following code to src/MainActivity.javaimport android.Manifest; import android.annotation.SuppressLint; import android.app.ProgressDialog; import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.support.annotation.RequiresApi; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import android.telephony.TelephonyManager; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.EditText; import ... Read More

Advertisements