Android : how to enable/disable scrolling in collapsing toolbar layout programmatically

    CollapsingToolbarLayout toolbar_layout;
        toolbar_layout=findViewById(R.id.toolbar_layout);

 AppBarLayout.LayoutParams p = (AppBarLayout.LayoutParams) toolbar_layout.getLayoutParams();
        p.setScrollFlags(0);
        toolbar_layout.setLayoutParams(p);

.bashrc syntax error: warning here document at line delimited by end of file wanted end ')

bash: warning: here-document at line 44 delimited by end-of-file (wanted `EOF')
bash: /etc/bash.bashrc: line 69: syntax error: unexpected end of file


I opened the file in .bashrc file in gedit:
Here in first iamge you can see the tab after the EOF. 



After removing the space after EOF the EOF gets highlighted in color as valid syntax



Sceneform SDK Android Studio Error - java.lang.IllegalStateException: Couldn't create Engine

Android Error:
Internal error. Please report to https://code.google.com/p/android/issues java.lang.IllegalStateException: Couldn't create Engine at com.google.android.filament.Engine.create(Engine.java:44) at com.google.ar.sceneform.plugin.viewer.Filamentor$3.run(Filamentor.java:102)

Android Rounded Corners Button with Background Color

Content:
1) How to create a rounded corners Button in Android
2) How to set Button pressed state background color in Android when clicked

1.Create a xml file in your drawable folder like rounded_corner_background.xml and paste the following markup:

rounded_corner_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<!--button pressed state background color-->
    <item android:state_pressed="true" >
        <shape android:shape="rectangle"  >
            <corners android:radius="10dip" />
            <stroke android:width="2dip" android:color="#5e7974" />
            <gradient android:angle="-90" android:startColor="#CC9933" android:endColor="#CC9933"  />
        </shape>
    </item>

<!--button focused state background color-->
    <item android:state_focused="true">
        <shape android:shape="rectangle"  >
            <corners android:radius="10dip" />
            <stroke android:width="2dip" android:color="#5e7974" />
            <gradient android:angle="-90" android:startColor="#CCCC33" android:endColor="#CCCC33"  />
        </shape>
    </item>

 <!--button default background color-->
    <item >
        <shape android:shape="rectangle"  >
            <corners android:radius="10dip" />
            <stroke android:width="2dip" android:color="#5e7974" />
            <gradient android:angle="-90" android:startColor="#CCCCCC" android:endColor="#CCCCCC" />
        </shape>
    </item>
</selector>

Button Default Background Color

button focused state background color

button pressed state background color


2. Now use this drawable for the background of your Button.
activity_main.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:padding="30dp"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/rounded_corner_background"
        android:text="Rounded Corners Button !"
        android:padding="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</RelativeLayout>

Create Toggleable Tabs in HTML

Android

London is the capital city of England.

Kotlin

Paris is the capital of France.
https://www.w3schools.com/howto/howto_js_tabs.asp

push rejected push to origin/master was rejected android studio bitbucket

push rejected push to origin/master was rejected android studio bitbucket

Push rejected: Push to origin/master was rejected

https://stackoverflow.com/questions/39010123/existing-android-studio-project-import-into-github-push-rejected/39010180

check status

First I refered this:

Bitbucket Tutorial: How to Sync Android studio project with Bitbucket account.  :  https://www.youtube.com/watch?v=AyJVK1Qay2E
Bitbucket integration with android studio tutorial || Bitbucket Tutorial | Bitbucket for Beginners: https://www.youtube.com/watch?v=vS39tcSKBHc

Did folllowing steps:
user@user-All-Series:~/Android-Studio-Projects/ocdlive_3.0.7_live$ git remote add origin https://et248@bitbucket.org/et248/ocd-android-in-nov18.git
user@user-All-Series:~/Android-Studio-Projects/ocdlive_3.0.7_live$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .gradle/4.6/
        .gradle/buildOutputCleanup/
        .gradle/vcsWorkingDirs/
        .idea/assetWizardSettings.xml
        .idea/libraries/Gradle__com_android_support_multidex_instrumentation_1_0_2.xml
        .idea/workspace.xml


Then I am come accross error:
Push rejected: Push to origin/master was rejected


Then comes a POst from github which helped:

Push failed - Using Android Studio VCS: https://github.com/desktop/desktop/issues/2476#issuecomment-405497443

user@user-All-Series:~/Android-Studio-Projects/applive_3.0.7_live$ git remote -v
origin  https://et248@bitbucket.org/et248/app-android-in-nov18.git (fetch)
origin  https://et248@bitbucket.org/et248/app-android-in-nov18.git (push)

user@user-All-Series:~/Android-Studio-Projects/applive_3.0.7_live$ git push origin master --force
Password for 'https://et248@bitbucket.org':
Counting objects: 14305, done.

Delta compression using up to 8 threads.
Compressing objects: 100% (13453/13453), done.
Writing objects: 100% (14305/14305), 195.80 MiB | 1.09 MiB/s, done.
Total 14305 (delta 3256), reused 109 (delta 25)
remote: Resolving deltas: 100% (3256/3256), done.
To https://et248@bitbucket.org/et248/app-android-in-jan18.git
 + c03fb24...97b55ee master -> master (forced update)
user@user-All-Series:~/Android-Studio-Projects/applive_3.0.7_live$

how to check internet connection in android programmatically


If the device is in airplane mode (or presumably in other situations where there's no available network), cm.getActiveNetworkInfo() will be null, so you need to add a null check.
Modified (Eddie's solution) below:
public boolean isOnline() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = cm.getActiveNetworkInfo(); return netInfo != null && netInfo.isConnectedOrConnecting(); }
Also add the following permission to the AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
One other small point, if you absolutely need a network connection at the given point in time, then it might be better to use netInfo.isConnected() rather than netInfo.isConnectedOrConnecting. I guess this is up to the individual use-case however.
Found at and modified (!) from this link :
In your manifest file add at least:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
You probably already have the INTERNET permission if you are accessing it. Then a boolean function that allows to test for connectivity is:
private boolean checkInternetConnection() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); // test for connection if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isAvailable() && cm.getActiveNetworkInfo().isConnected()) { return true; } else { Log.v(TAG, "Internet Connection Not Present"); return false; } }

How to create RecyclerView with multiple view type in Android Kotlin?


It's possible to create recyclerview with multiple data types. Just implement getItemViewType(), and take care of the viewType parameter in onCreateViewHolder().
So you do something like:
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { class ViewHolder0 extends RecyclerView.ViewHolder { ... public ViewHolder0(View itemView){ ... } } class ViewHolder2 extends RecyclerView.ViewHolder { ... public ViewHolder2(View itemView){ ... } @Override public int getItemViewType(int position) { // Just as an example, return 0 or 2 depending on position // Note that unlike in ListView adapters, types don't have to be contiguous return position % 2 * 2; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { switch (viewType) { case 0: return new ViewHolder0(...); case 2: return new ViewHolder2(...); ... } } @Override public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) { switch (holder.getItemViewType()) { case 0: ViewHolder0 viewHolder0 = (ViewHolder0)holder; ... break; case 2: ViewHolder2 viewHolder2 = (ViewHolder2)holder; ... break; } } }

close/hide the soft keyboard android kotlin


created a static utility method which can do the job VERY solidly, provided you call it from an Activity.
public static void hideKeyboard(Activity activity) { InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE); //Find the currently focused view, so we can grab the correct window token from it. View view = activity.getCurrentFocus(); //If no view currently has focus, create a new one, just so we can grab a window token from it if (view == null) { view = new View(activity); } imm.hideSoftInputFromWindow(view.getWindowToken(), 0); }
You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow, passing in the token of the window containing your focused view.
// Check if no view has focus: View view = this.getCurrentFocus(); if (view != null) { InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(), 0); }
This will force the keyboard to be hidden in all situations. In some cases you will want to pass in InputMethodManager.HIDE_IMPLICIT_ONLY as the second parameter to ensure you only hide the keyboard when the user didn't explicitly force it to appear (by holding down menu).
Note: If you want to do this in Kotlin, use:context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
Kotlin Syntax
// Check if no view has focus: val view = this.currentFocus view?.let { v -> val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager imm?.let { it.hideSoftInputFromWindow(v.windowToken, 0) } }

difference between "px", "dip", "dp" and "sp" in Android


px
Pixels - corresponds to actual pixels on the screen

dp or dip  Density-independent pixel (dp)
Density-independent Pixels - an abstract unit type that is based on the physical density of the mobile phone screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel changes with change in the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dp" and "dip", though "dp" is more consistent with "sp".

spScale-independent Pixels
- this is like the dp unit, but it is also scaled by the user's font size preference. It is recommended you use this unit when specifying font sizes, so they will be adjusted for both the screen density and user's preference.
Always use dp and sp only. sp for font sizes and dp for everything else. It will make UI compatible for Android devices with different densities.
So, when should you use sp and when you should you use dp?
Use sp for text size…because but it is scaled by the user’s font size preference.
Use dp for everything else.
Example:
<TextView
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”Hello, world!”
android:textSize=”30sp”
android:padding=”15dp”/>
<Button
android:layout_width=”40dp”
android:layout_height=”wrap_content”
android:layout_marginLeft=“20dp”/>



Android StartActivityForResult Example in Kotlin

Android StartActivityForResult Example in Kotlin

MainActivity.kt
package com.example.espl.passingdatabetweenactivities
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import android.content.Intent
import android.app.Activity

class MainActivity : AppCompatActivity() {

    private val SECOND_ACTIVITY_REQUEST_CODE = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // "Go to Second Activity" button click
        btn_send_data_to_another_activity.setOnClickListener {
            val intent = Intent(this, SecondActivity::class.java)
            // start the SecondActivity
            startActivityForResult(intent, SECOND_ACTIVITY_REQUEST_CODE)  // Activity is started with requestCode 0
        }
    }

    // This method is called when the second activity finishes
    // Call Back method  to get the Message form other Activity
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        // check if the request code is same as what is passed  here it is 0
        if (requestCode === SECOND_ACTIVITY_REQUEST_CODE) {
            // check that it is the SecondActivity with an OK result
            if (resultCode === Activity.RESULT_OK) {
                // get String data from Intent
                val returnString = data?.getStringExtra("MESSAGE")
                // set text view with string
                tv_RecivedData_fromSecondActivity.text ="Recived String:: "+ returnString
            }
        }
    }
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <TextView
            android:layout_margin="15dp"
            android:id="@+id/tv_RecivedData_fromSecondActivity"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Default String data MainActivity"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

    <Button
            android:layout_margin="10dp"
            android:layout_below="@id/tv_RecivedData_fromSecondActivity"
            android:text="GO TO SecondActivity"
            android:id="@+id/btn_send_data_to_another_activity"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
</RelativeLayout>

SecondActivity.kt
package com.example.espl.passingdatabetweenactivities
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_second.*
import android.content.Intent

class SecondActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)

        // "Send text back" button click
        btn_ToPassingDataBackonActivityResult.setOnClickListener {

           // get the text from the EditText
           val stringToPassBack : String =et_ToPassingDataBack.getText().toString();

           // put the String to pass back into an Intent and close this activity
           val intent : Intent=Intent();
                intent.putExtra("MESSAGE", stringToPassBack);
                setResult(RESULT_OK, intent);
                finish();
           }
        }
}

activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".SecondActivity">

    <EditText
            android:id="@+id/et_ToPassingDataBack"
            android:layout_margin="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Default String data SecondActivity"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

    <Button
            android:layout_below="@id/et_ToPassingDataBack"
            android:layout_margin="10dp"
            android:text="SEND TEXT BACK"
            android:id="@+id/btn_ToPassingDataBackonActivityResult"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
</RelativeLayout>

Passing Data Between Activities Android Kotlin Tutorial

Passing Data Between Activities Android Kotlin Tutorial

MainActivity.kt
package com.example.espl.passingdatabetweenactivities
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import android.content.Intent
import android.app.Activity

class MainActivity : AppCompatActivity() {

    private val SECOND_ACTIVITY_REQUEST_CODE = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // "Go to Second Activity" button click
        btn_send_data_to_another_activity.setOnClickListener {
            val intent = Intent(this, SecondActivity::class.java)
            // get the text to pass
            intent.putExtra(Intent.EXTRA_TEXT, edittextTosend.getText().toString().trim())
            // start the SecondActivity
            startActivityForResult(intent, SECOND_ACTIVITY_REQUEST_CODE)
        }
    }

    // This method is called when the second activity finishes
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        // check that it is the SecondActivity with an OK result
        if (requestCode === SECOND_ACTIVITY_REQUEST_CODE) {
            if (resultCode === Activity.RESULT_OK) {

                // get String data from Intent
                val returnString = data?.getStringExtra(Intent.EXTRA_TEXT)
                // set text view with string
                tv_RecivedData_fromSecondActivity.text ="Recived String:: "+ returnString
            }
        }
    }
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        android:orientation="vertical"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <EditText
            android:id="@+id/edittextTosend"
            android:layout_width="match_parent"
            android:layout_margin="10dp"
            android:layout_height="wrap_content"
            android:text="Default String data Main Activity"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

    <Button
            android:layout_margin="10dp"
            android:text="GO TO SecondActivity"
            android:id="@+id/btn_send_data_to_another_activity"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    <TextView
            android:layout_margin="15dp"
            android:id="@+id/tv_RecivedData_fromSecondActivity"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Default String data MainActivity"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
</LinearLayout>

SecondActivity.kt
package com.example.espl.passingdatabetweenactivities
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_second.*
import android.content.Intent

class SecondActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)
       
        // get the text from MainActivity
        val intent = intent
        val text = intent.getStringExtra(Intent.EXTRA_TEXT)

        // use the text in a TextView
        tv_RecivedData_fromMainActivity.text = "Recived String:: "+text

        // "Send text back" button click
        btn_ToPassingDataBackonActivityResult.setOnClickListener {

           // get the text from the EditText
           val stringToPassBack : String =et_ToPassingDataBack.getText().toString();

           // put the String to pass back into an Intent and close this activity
           val intent : Intent=Intent();
                intent.putExtra(Intent.EXTRA_TEXT, stringToPassBack);
                setResult(RESULT_OK, intent);
                finish();
           }
        }
}

activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        android:orientation="vertical"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".SecondActivity">

    <EditText
            android:id="@+id/et_ToPassingDataBack"
            android:layout_margin="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Default String data SecondActivity"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>


    <Button
            android:layout_margin="10dp"
            android:text="SEND TEXT BACK"
            android:id="@+id/btn_ToPassingDataBackonActivityResult"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    <TextView
            android:id="@+id/tv_RecivedData_fromMainActivity"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="Default String data SecondActivity"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
</LinearLayout>

API 'variant.getMergeAssets()' is obsolete and has been replaced with 'variant.getMergeAssetsProvider()' Android Studio 3.3 WARNING

API 'variant.getMergeAssets()' is obsolete and has been replaced with 'variant.getMergeAssetsProvider()'.
It will be removed at the end of 2019.
For more information, see https://d.android.com/r/tools/task-configuration-avoidance.
To determine what is calling variant.getMergeAssets(), use -Pandroid.debug.obsoleteApi=true on the command line to display a stack trace.


Popular Posts