Skip to main content

Posts

Open Activity on Button Click

In this tutorial we will learn how to open a new Activity from a Button click. For this we will create a new Intent and pass it to the startActivity method. In activity_main.xml file add these lines of code:- <?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"     tools:context="com.example.application.myapplication.MainActivity">     <TextView         android:id="@+id/textView"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_centerHorizontal="true"         android:layout_centerVertical="true"         android:text="Activity 1" ...

Recyclerview and Cardview in android

Recyclerview ========== The RecyclerView class extends the ViewGroup class and implements ScrollingView interface. It is introduced in Marshmallow. It is an advanced version of the ListView with improved performance and other benefits. RecyclerView is mostly used to design the user interface with the fine-grain control over the lists and grids of android application. we will create a list of items with ImageView (for the icon) and TextView (for description) using RecyclerView Cardview ======== Android CardView UI component shows information inside cards. This component is generally used to show contact information. This component is available in another support library so we have to add its dependency too. Android CardView widget allows us to control the background color, shadow, corner radius, elevation etc. For using the custom attributes in XML, we need to add the following namespace declaration to the parent layout. In this programming tutorial we will ...

DodgeInsetEdges

The layout_dodgeInsetEdges together with the layout_insetEdge attribute, to move views within a CoordinatorLayout out of the way of other views. This behavior is the default behavior for FloatingActionButtons and Snackbars , but we will also apply it to views like normal buttons and bottom sheets. In Mainactivity.java add the code below stated:- import android.support.design.widget.BottomSheetBehavior; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity {     private BottomSheetBehavior bottomSheetBehavior;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activ...

Alternative Layout Resources

Density-independent pixel (dp) A virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way. The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities. dp = px / (dpi / 160) 320dp = 480px / (240dpi / 160) When supporting different screen sizes (densities) in  Android  often the focus is on creating different layouts for every possible ...

Include + Merge Layouts

Merge Layout <merge>  is used for optimizing layouts.It is used for reducing unnecessary nesting. when a layout containing  <merge>  tag is added into another layout,the  <merge>  node is removed and its child view is added directly to the new parent.     <Button         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="@string/add"/>     <Button         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="@string/delete"/> </merge> Include Layout Inside the layout to which you want to add the re-usable component, add the  <include/>  tag. For example, here's a layout that includes the title bar from above:   <inclu...

Constraint Layout

Android ConstraintLayout To use android ConstraintLayout, make sure you’re using the latest Android Studio version. Ideally, Android Studio 2.2 and above.  <?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"     android:layout_width="match_parent"     android:layout_height="match_parent">     <TextView         android:text="TextView"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:id="@+id/textView"         android:layout_marginStart="16dp"         android:layout_marginTop="16dp"         app:layout_constraintLeft_toLeftOf="parent" ...

How to add Base Activity in a Project in Android

To add Base Activity in a project we have to follow this code which is depected below:- import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.MotionEvent; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; import com.areteit.fettle.Admin.Utils.ConnectionDetector; import com.areteit.fettle.Admin.Utils.Constants; import com.areteit.fettle.Admin.Utils.LoadingBox; import com.areteit.fettle.Admin.Utils.RequestPermissions; import com.areteit.fettle.R; import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper; abstract public class BaseActivity extends AppCompatActivity {   private RequestPermissions requestPermissions; //  private GPSTracker gpsTracker;   private ConnectionDetector connectionDetector;  ...