Inflating a xml layout in a custom View class

You can’t add views to the View class instead you should use ViewGroup or one of its subclasses(like Linearlayout, RelativeLayout etc). Then your code will be like this: public class View1 extends LinearLayout { View view; String[] countries = new String[] {“India”, “USA”, “Canada”}; public View1( Context context) { super(context); inflate(context, R.layout.test2, this); } }

Plugins architecture for an Android app? [closed]

I have done a framework that works like Robo-guice with some of its primary IoC functions. (Cut down on boilerplate codes that load views/service etc…) But the core of which, I believe is the solution to your problem, is the ability to load separate APK “plugin” files, that includes “res” as well as <layouts>.xml files. … Read more

Problem inflating custom view for AlertDialog in DialogFragment

The first error line gives me the hint that this is related to how you are creating your dialog – not the dialog itself. Are you creating the dialog automatically (which could mean this gets called before the views are all set up) or in response to a button click? I initially had problems with … Read more

Inflate layout programmatically within another layout

You could use something like LayoutInflater inflater = LayoutInflater.from(context); //to get the MainLayout View view = inflater.inflate(container_destacado, null); … //Avoid pass null in the root it ignores spaces in the child layout View inflatedLayout= inflater.inflate(R.layout.yourLayout, (ViewGroup) view, false); containerDestacado.addView(inflatedLayout);

NPE while inflating layout (Attempt to invoke virtual method ‘boolean java.lang.String.equals(java.lang.Object)’ on a null object reference)

Change <view to <View, because view is not about empty view. It’s for custom view defined through class attr, like below: <view android:layout_width=”wrap_content” android:layout_height=”wrap_content” class=”com.your.package.YourCustomView” /> And you got Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ‘boolean java.lang.String.equals(java.lang.Object)’ on a null object reference because of LayoutInflater tries to parse class attr: LayoutInflater source code … Read more

‘setHasOptionsMenu(Boolean): Unit’ is deprecated. Deprecated in Java

From the Developer documentation, this can be achieved by the following: /** * Using the addMenuProvider() API directly in your Activity **/ class ExampleActivity : ComponentActivity(R.layout.activity_example) { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Add menu items without overriding methods in the Activity addMenuProvider(object : MenuProvider { override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) { // … Read more

Custom Layout for DialogFragment OnCreateView vs. OnCreateDialog

I had the same exception with the following code: public class SelectWeekDayFragment extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { return new AlertDialog.Builder(getActivity()) .setMessage(“Are you sure?”).setPositiveButton(“Ok”, null) .setNegativeButton(“No way”, null).create(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.week_day_dialog, container, false); return view; } } You must choose … Read more

android.view.InflateException: Binary XML file line #12: Error inflating class

The inflate exception is not actually the problem but really comes from another deeper issue in your layout that is then wrapped in an InflateException. A common issue is an out of memory exception when trying to inflate an ImageView loading a drawable resource. If one of these resources has a high pixel resolution it … Read more