Translate

Tuesday, July 20, 2021

Could not identify launch activity: Default activity not found : Error while Launching activity


Problem :

I got this Error , When I tried to create an application without any Activity . Basically like to develop an Android Headless application.

And this App has only one Service and BroadcastReceiver which are actually in use but it also has sample dummy Activity which was not using and will delete later while doing code cleanup.

In this scenario tried to run the application from Studio , but getting this error inside Studios Run console.

The reason to make an App with no activity or service could be making a Homescreen Widget app that doesn't need to be started.

Once you start a project don't create any activities. After you created the project just hit run. Android studio will say No default activity found.

Solution :
In AndroidStudio goto -> Edit Configuration (From the Run menu) and in the Launch option part set the Launch value to Nothing. Then click ok and run the App.

(Since there is no launcher activity, No app will be show in the Apps menu.).


This fixed my problem. Thanks

Monday, August 17, 2020

Android code to send data from XML layout file to Custom View

 Following code sample demonstrates to set maxLength limit for text in Custom view EditText.

This restrict the user to enter max 30 chars in CustomView component .

main_activity.xml :

<com.view.ViewCustomComponent

    android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapWords"
app:title="Address"
app:required="false"
app:hint="Address"
app:maxlength="30"/>


attributes.xml :

<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="title" format="string" />
<attr name="required" format="boolean" />
<attr name="hint" format="string" />
<attr name="text" format="string" />
<attr name="lines" format="integer" />
<attr name="maxlength" format="integer" />

<
declare-styleable name="FormEditTextComponent">
<
attr name="title" />
<
attr name="required" />
<
attr name="hint" />
<
attr name="text" />
<
attr name="lines" />
<attr name="maxlength" />
<
attr name="android:inputType" />
</
declare-styleable>
</
resources>


ViewCustomComponent.kt :

class ViewCustomComponent : LinearLayout, IBaseFormComponent {

private fun initialize(context: Context, attributeSet: AttributeSet? = null) {


attributeSet?.let {

val title = typedArray.getString(R.styleable.FormEditTextComponent_title)
val text = typedArray.getString(R.styleable.FormEditTextComponent_text)
val placeholder = typedArray.getString(R.styleable.FormEditTextComponent_hint)
val lines = typedArray.getInt(R.styleable.FormEditTextComponent_lines, 0)
val maxlength = typedArray.getInt(R.styleable.FormEditTextComponent_maxlength , 0)

component_title.
text = title
component_text.setText(text, TextView.BufferType.
NORMAL)
component_text.
hint = placeholder
component_text.
inputType = InputType.TYPE_TEXT_FLAG_CAP_SENTENCES

/**
* This CustomView used by many views and here the condition to set maxlength=50 only for
* address1 , address2 , city of EmergencyProfile and no impact for other views
*/
if(maxlength == 50) {
component_text.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(maxlength))
} else {
component_text.filters = arrayOf(InputEmojiFilter())
}



}
}

}




Tuesday, July 14, 2020

Code to display ViewMore for RecyclerView list and click on ViewMore will launch full list Android


RestaurantTabFragment.java:
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.cbrecodingtest.R;
import com.example.cbrecodingtest.adapter.RestaurantsAdapter;
import com.example.cbrecodingtest.model.RestaurantsModel;
import com.example.cbrecodingtest.utils.Utils;

import java.util.ArrayList;

public class RestaurantTabFragment extends Fragment {

private RecyclerView mRecyclerViewOpen , mRecyclerViewClosed;
private RecyclerView.Adapter mRecyclerAdapter;
private RelativeLayout mOpenLayout , mClosedLayout;
private ImageView mOpenArrow , mClosedArrow;
private TextView mViewMoreOpen , mViewMoreClosed;
private ArrayList<RestaurantsModel> openList ,closedList;
private View mLineView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.restaurant_tab_fragment, container, false);
initViewIds(rootView);

openList = (ArrayList<RestaurantsModel>) getArguments().getSerializable(Utils.RESTAURANT_LIST_KEY);
//openList = new ArrayList<>();
if(openList.size() == 0) {//open list is empty
//Hide the open section
mOpenLayout.setVisibility(View.GONE);
mLineView.setVisibility(View.GONE);
} else if(openList.size() > 4 ) {
// if list of items more than 4 rows, then display only 4 rows with ViewMore text ..
mOpenLayout.setVisibility(View.VISIBLE);
mViewMoreOpen.setVisibility(View.VISIBLE);

mRecyclerAdapter = new RestaurantsAdapter(openList.subList(0, 3), getActivity());
mRecyclerViewOpen.setAdapter(mRecyclerAdapter);

} else {
//Show open section and set adapter
mOpenLayout.setVisibility(View.VISIBLE);
mViewMoreOpen.setVisibility(View.GONE);
mRecyclerAdapter = new RestaurantsAdapter(openList, getActivity());
mRecyclerViewOpen.setAdapter(mRecyclerAdapter);
}


closedList = (ArrayList<RestaurantsModel>) getArguments().getSerializable(Utils.RESTAURANT_LIST_KEY);
//closedList = new ArrayList<>(); //For empty list..
if(closedList.size() == 0) {//closed list empty
//Hide the closed section
mClosedLayout.setVisibility(View.GONE);
} else if(closedList.size() > 4) {
// if list of items more than 4 rows, then display only 4 rows with ViewMore text ..
mClosedLayout.setVisibility(View.VISIBLE);
mViewMoreClosed.setVisibility(View.VISIBLE);
mRecyclerAdapter = new RestaurantsAdapter(closedList.subList(0, 3), getActivity());
mRecyclerViewClosed.setAdapter(mRecyclerAdapter);

} else {
//Show closed section and set Adapter
mClosedLayout.setVisibility(View.VISIBLE);
mViewMoreClosed.setVisibility(View.GONE);
mRecyclerAdapter = new RestaurantsAdapter(closedList, getActivity());
mRecyclerViewClosed.setAdapter(mRecyclerAdapter);
}


return rootView;
}

private void initViewIds(View rootView) {

mOpenLayout = rootView.findViewById(R.id.openLayout);
mLineView = rootView.findViewById(R.id.lineView);
mClosedLayout = rootView.findViewById(R.id.closedLayout);

mRecyclerViewOpen = rootView.findViewById(R.id.recycler_view_open);
mRecyclerViewClosed = rootView.findViewById(R.id.recycler_view_closed);

mOpenArrow = rootView.findViewById(R.id.open_arrow);
mOpenArrow.setOnClickListener(mOnClickListener);
mClosedArrow = rootView.findViewById(R.id.closed_arrow);
mClosedArrow.setOnClickListener(mOnClickListener);

mViewMoreOpen = rootView.findViewById(R.id.viewMoreOpen);
mViewMoreOpen.setOnClickListener(mOnClickListener);
mViewMoreClosed = rootView.findViewById(R.id.viewMoreClosed);
mViewMoreClosed.setOnClickListener(mOnClickListener);

mRecyclerViewOpen.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerViewClosed.setLayoutManager(new LinearLayoutManager(getActivity()));
}

private View.OnClickListener mOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.open_arrow:
if (mRecyclerViewOpen.getVisibility() == View.VISIBLE) {
// Recycler view visible
mRecyclerViewOpen.setVisibility(View.GONE);
mViewMoreOpen.setVisibility(View.GONE);
mOpenArrow.setBackgroundResource(R.drawable.arrow_down);
} else {
// Recycler view gone or invisible
mRecyclerViewOpen.setVisibility(View.VISIBLE);
mViewMoreOpen.setVisibility(View.VISIBLE);
mOpenArrow.setBackgroundResource(R.drawable.arrow_up);
}
break;

case R.id.closed_arrow:
if (mRecyclerViewClosed.getVisibility() == View.VISIBLE) {
// Recycler view visible
mRecyclerViewClosed.setVisibility(View.GONE);
mViewMoreClosed.setVisibility(View.GONE);
mClosedArrow.setBackgroundResource(R.drawable.arrow_down);
} else {
// Recycler view gone or invisible
mRecyclerViewClosed.setVisibility(View.VISIBLE);
mViewMoreClosed.setVisibility(View.VISIBLE);
mClosedArrow.setBackgroundResource(R.drawable.arrow_up);
}
break;

case R.id.viewMoreOpen:
//Hide closed list and display open list in full screen ..
/*mClosedLayout.setVisibility(View.GONE);
mViewMoreOpen.setVisibility(View.GONE);
mLineView.setVisibility(View.GONE);
mRecyclerAdapter = new RestaurantsAdapter(openList, getActivity());
mRecyclerViewOpen.setAdapter(mRecyclerAdapter);*/
launchFullFragment(openList , "open");
break;

case R.id.viewMoreClosed:
//Hide open list and display closed list in full screen ..
/* mOpenLayout.setVisibility(View.GONE);
mViewMoreClosed.setVisibility(View.GONE);
mLineView.setVisibility(View.GONE);
mRecyclerAdapter = new RestaurantsAdapter(closedList, getActivity());
mRecyclerViewClosed.setAdapter(mRecyclerAdapter);*/

launchFullFragment(closedList , "close");
break;
}
}
};


private void launchFullFragment(ArrayList<RestaurantsModel> list , String title) {
//code to launch fragment in fullscreen

Bundle bundle = new Bundle();
bundle.putSerializable("list", list);
bundle.putString("title", title);

ViewMoreFragment nextFrag= new ViewMoreFragment();
nextFrag.setArguments(bundle);
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.mainActivityView, nextFrag, "FragmentTag")
.addToBackStack(null)
.commit();
}
}


restaurant_tab_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/restTabView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<RelativeLayout
android:id="@+id/openLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">

<TextView
android:id="@+id/openViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Open"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
android:textSize="18sp"
android:textStyle="bold" />

<ImageView
android:id="@+id/open_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:background="@drawable/arrow_up" />

<!-- A RecyclerView with some commonly used attributes -->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_open"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/openViewTitle"
android:layout_marginTop="10dp"
android:scrollbars="vertical" />

<TextView
android:id="@+id/viewMoreOpen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="View More"
android:textStyle="bold"
android:visibility="gone"
android:textColor="@color/red"
android:gravity="center_horizontal"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_below="@+id/recycler_view_open"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"/>
</RelativeLayout>


<View
android:id="@+id/lineView"
android:layout_width="match_parent"
android:layout_height="8dp"
android:layout_marginTop="10dp"
android:background="#cecdcb" />


<RelativeLayout
android:id="@+id/closedLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">

<TextView
android:id="@+id/closedViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Closed"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
android:textSize="18sp"
android:textStyle="bold" />

<ImageView
android:id="@+id/closed_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:background="@drawable/arrow_up" />

<!-- A RecyclerView with some commonly used attributes -->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_closed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/closedViewTitle"
android:layout_marginTop="10dp"
android:scrollbars="vertical" />

<TextView
android:id="@+id/viewMoreClosed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="View More"
android:textStyle="bold"
android:visibility="gone"
android:textColor="@color/red"
android:gravity="center_horizontal"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_below="@+id/recycler_view_closed"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"/>
</RelativeLayout>


</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>


ViewMoreFragment.java:
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.cbrecodingtest.MainActivity;
import com.example.cbrecodingtest.R;
import com.example.cbrecodingtest.adapter.RestaurantsAdapter;
import com.example.cbrecodingtest.model.RestaurantsModel;

import java.util.ArrayList;

public class ViewMoreFragment extends Fragment{

private RecyclerView mRecyclerView;
ArrayList<RestaurantsModel> arrayList;
RecyclerView.LayoutManager mLayoutManager;
private RecyclerView.Adapter mRecyclerAdapter;
private Toolbar toolbar;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.view_more_fragment, container, false);

toolbar = rootView.findViewById(R.id.toolbar);

((MainActivity) getActivity()).setSupportActionBar(toolbar);

((MainActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
((MainActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// remove the back button
((MainActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false);
((MainActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(false);
getActivity().onBackPressed();
}
});

if(getArguments() != null) {
arrayList = (ArrayList<RestaurantsModel>)getArguments().getSerializable("list");
toolbar.setTitle(getArguments().getString("title"));
}

initViewIds(rootView);

return rootView;
}

private void initViewIds(View rootView) {
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView = rootView.findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerAdapter = new RestaurantsAdapter(arrayList, getActivity());
mRecyclerView.setAdapter(mRecyclerAdapter);
}
}

view_more_fragment.xml :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/white">

<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/app_bar"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintLeft_toLeftOf="parent"/>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/closedViewTitle"
android:layout_marginTop="10dp"
android:scrollbars="vertical"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar"/>

</androidx.constraintlayout.widget.ConstraintLayout>


app_bar.xml :
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:theme="@style/MyToolbarTheme"
app:title="@string/title" />







Wednesday, June 24, 2020

Android Code to stop progressBar spinning


While implementing circler progressBar , I have faced issue of ProgressBar spin never stops.
After loading the UI progressBar circle continuously rotating.

I have tried various solutions and out of all those following solution works for me to stop the circle spinning.

circle.xml :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:innerRadiusRatio="2.3"
android:thicknessRatio="15.0"
android:useLevel="true">
<solid android:color="#0099ff"/>

</shape>


main_activity.xml :
RelativeLayout
android:layout_width="45dp"
android:layout_height="45dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginRight="24dp">
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_centerInParent="true"
android:indeterminate="false"
android:clickable="false"
android:progress="80"
android:max="10"
style="?android:progressBarStyleHorizontal"
android:progressDrawable="@drawable/circular" />
<TextView
android:id="@+id/txtProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="134"
android:layout_centerInParent="true"
android:textSize="8sp"
android:textColor="#FFF" />
</RelativeLayout>


Monday, June 22, 2020

Best coding practices to implement Android applications with clean architecture

Could not identify launch activity: Default activity not found : Error while Launching activity

Problem : I got this Error , When I tried to create an application without any Activity . Basically like to develop an Android Headless appl...