Android error inflating class <unknown> while loading highchart - android

I am trying to load highcharts via Dialog. Below is my code
Gradle
implementation 'com.highsoft.highcharts:highcharts:9.0.1'
XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="vertical"
android:padding="10dp">
<com.highsoft.highcharts.core.HIChartView
android:id="#+id/goly_gauge"
android:layout_width="match_parent"
android:layout_height = "350dp"
/>
<com.highsoft.highcharts.core.HIChartView
android:id="#+id/golm_gauge"
android:layout_width="match_parent"
android:layout_height="350dp"
/>
<Button
android:id="#+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="OK" />
</LinearLayout>
</ScrollView>
Fragment
suggestion_list.setOnItemClickListener((parent, view, position, id) -> {
String selectedFromList = (String) (suggestion_list.getItemAtPosition(position));
showPreFilledData(selectedFromList);
Log.e(TAG, arraylist.get(position));
});
private void showPreFilledData(String string) {
final Dialog dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.product_sales_layout);// error comes at this point
dialog.setTitle("Product Sales");
Button ok;
ok = (Button) dialog.findViewById(R.id.ok);
switch (string) {
case "A2A 100 MG TABS":
GolYgauge(55.1);
GolMgauge(32.9);
break;
case "AQUA-VIT SACHET":
GolYgauge(45.8);
GolMgauge(22.7);
break;
case "BRONCOPHYLINE SYRUP":
GolYgauge(65.7);
GolMgauge(42.4);
break;
}
ok.setOnClickListener(v -> dialog.dismiss());
Window window = dialog.getWindow();
window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT);
dialog.show();
}
When I click on an item in my list view I want to open a layout and want to display data inside a gauge. But I am getting error
android.view.InflateException: Binary XML file line #15 in com.thumbsol.accuratemobileassetsmanagament:layout/product_sales_layout: Binary XML file line #15 in com.thumbsol.accuratemobileassetsmanagament:layout/product_sales_layout: Error inflating class
The line number 15 is <com.highsoft.highcharts.core.HIChartView. I am stuck to it. How can get rid from this issue?
Any help would be highly appreciated.

Seems like a combination of Dialog class wrapping the Activity context and the library's HIChartView not able to process this wrapped context. I force-unwrapped the context and then your code started working. How to:
Extend the HIChartView with a custom implementation handling the wrapped context:
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.util.AttributeSet;
import com.highsoft.highcharts.core.HIChartView;
public class CustomHIChartView extends HIChartView {
public CustomHIChartView(Context context) {
super(unwrap(context));
}
public CustomHIChartView(Context context, AttributeSet attributeSet) {
super(unwrap(context), attributeSet);
}
// unwrap the context until I get the original passed-in Activity context
private static Activity unwrap(Context context) {
while (!(context instanceof Activity) && context instanceof ContextWrapper) {
context = ((ContextWrapper) context).getBaseContext();
}
return (Activity) context;
}
}
The unwrapping function is copied from https://stackoverflow.com/a/51640906/12321475, there you can also read on why the base context are sometimes being wrapped.
Modify your layout to use the custom implementation:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<your.package.name.CustomHIChartView
android:id="#+id/goly_gauge"
android:layout_width="match_parent"
android:layout_height = "350dp"
/>
<your.package.name.CustomHIChartView
android:id="#+id/golm_gauge"
android:layout_width="match_parent"
android:layout_height="350dp"
/>
<Button
android:id="#+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="OK" />
</LinearLayout>
</ScrollView>

Related

Error inflating class androidx.cardview.widget.CardView on CardStackView library

I am trying to implement this library. The sample code which is written here is in Kotlin but my project was in Java so I tried converting the code from kotlin to java. But I am getting the following error :
android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class androidx.cardview.widget.CardView
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class androidx.cardview.widget.CardView
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
in inflating the cardview
class CardStackAdapter extends RecyclerView.Adapter<CardStackAdapter.ViewHolder> {
private Context context;
private ArrayList<Spot> listItem;
public CardStackAdapter(Context applicationContext, ArrayList<Spot> listItem) {
this.context = applicationContext;
this.listItem = listItem;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.test, parent, false); // ----> I am getting error here..
return new ViewHolder(context, view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
final String name = listItem.get(position).name;
String url = listItem.get(position).URL;
String city = listItem.get(position).city;
holder.name.setText(name);
holder.city.setText(city);
Picasso.get().load(url).into(holder.image);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context, name, Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return listItem != null ? listItem.size() : 0;
}
public void setItem(ArrayList<Spot> listItem){
this.listItem = listItem;
}
public ArrayList<Spot> getItem(){
return listItem;
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView name;
TextView city;
ImageView image;
ViewHolder(Context context, View view) {
super(view);
this.name = view.findViewById(R.id.item_name);
this.city = view.findViewById(R.id.item_city);
this.image = view.findViewById(R.id.item_image);
}
}
}
My XML code for this cardview is :
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
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="?attr/selectableItemBackground"
android:foreground="?attr/selectableItemBackground"
app:cardUseCompatPadding="true"
app:cardPreventCornerOverlap="false"
app:cardCornerRadius="8dp"
app:cardBackgroundColor="#android:color/white">
<com.makeramen.roundedimageview.RoundedImageView
android:id="#+id/item_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
app:riv_corner_radius="8dp"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:padding="16dp"
android:background="#drawable/gradation_black">
<TextView
android:id="#+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#android:color/white"
android:textSize="26sp"/>
<TextView
android:id="#+id/item_city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#android:color/white"
android:textSize="20sp"/>
</LinearLayout>
<FrameLayout
android:id="#+id/left_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/overlay_black">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/skip_white_120dp"
android:layout_gravity="center"/>
</FrameLayout>
<FrameLayout
android:id="#+id/right_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/overlay_black">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/like_white_120dp"
android:layout_gravity="center"/>
</FrameLayout>
<FrameLayout
android:id="#+id/top_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<FrameLayout
android:id="#+id/bottom_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</androidx.cardview.widget.CardView>
I have uploaded this project on Github Repository. Any suggestion is most welcome. Thanks!
delete the two lines marked here in the attached image** because these 2 lines included to the old appcompact support lib not included in androidx.appcompact and the view holder can't inflate because these 2 lines, I had the same problem and I tried to solve it by several ways change cardview to linear and makes it worked and after that tried to delete some lines from the card view and it works now perfectly
Use this:
<androidx.cardview.widget.CardView
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"
app:cardUseCompatPadding="true"
app:cardPreventCornerOverlap="false"
app:cardCornerRadius="8dp"
app:cardBackgroundColor="#android:color/white">
instead of
<androidx.cardview.widget.CardView
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="?attr/selectableItemBackground"
android:foreground="?attr/selectableItemBackground"
app:cardUseCompatPadding="true"
app:cardPreventCornerOverlap="false"
app:cardCornerRadius="8dp"
app:cardBackgroundColor="#android:color/white">
To keep the riddle effect on AndroidX use
android:background="?android:attr/selectableItemBackground"
android:foreground="?android:attr/selectableItemBackground"
use of android:attr applies the attribute defined for the current theme of the app
In my case i am trying to set an image on low end devices for card view using
android:background="drawable/temp_image"
If you are trying to set an image using background or foreground then remove it.
I've also had the same problem and have found a solution. Check if CardView library is available, if not then add it.
How to add it

DialogPreference in Full Screen Width

I created a custom dialog preference in my Android application, but I can not figure out how to get the dialog which is displayed to span the complete width of the display.
image of dialog with too much space on left and right side
I found many proposed solutions to get a normal Dialog in full screen mode
Android get full width for custom Dialog
https://gist.github.com/koocbor/88db64192638bff09aa4
http://blog.jimbaca.com/force-dialog-to-take-up-full-screen-width/
But setting the attributes via getWindow does not work:
#Override
public Dialog getDialog() {
Dialog dialog = super.getDialog();
dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
// or
// dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
return dialog;
}
And applying a full screen theme to my dialogs root element didn't do the job neither:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
[...]
android:theme="#style/FullscreenTheme">
Moreover I'm not able to access the onCreate Method (at least I don't know how) of the Dialog, to set the style there.
Did anyone had the same problem and figured out a solution for this very specific issue?
My layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical"
android:padding="0dp"
android:paddingTop="#dimen/preferences_dialog_def_padding"
android:paddingBottom="#dimen/preferences_dialog_def_padding">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="-2dp"
android:background="#color/expandable_preference_divider"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/preferences_expandable_margin_top_bottom"
android:layout_marginTop="#dimen/preferences_expandable_margin_top_bottom">
<RelativeLayout
android:id="#+id/icon_wrapper_choose"
android:layout_width="#dimen/preferences_expandable_icon_wrapper_size"
android:layout_height="#dimen/preferences_expandable_icon_wrapper_size"
android:layout_marginBottom="0dp"
android:layout_marginEnd="#dimen/preference_expandable_icon_margin"
android:layout_marginStart="#dimen/preference_expandable_icon_margin"
android:layout_marginTop="0dp"
android:gravity="center">
<ImageView
android:layout_width="#dimen/preferences_expandable_icon_size"
android:layout_height="#dimen/preferences_expandable_icon_size"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:cropToPadding="true"
android:scaleType="centerCrop"
android:src="#drawable/ic_settings_white_36dp"/>
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="#+id/icon_wrapper_choose"
android:paddingBottom="#dimen/preferences_expandable_text_padding_top_bottom"
android:paddingTop="#dimen/preferences_expandable_text_padding_top_bottom"
android:text="#string/pref_wheel_circumference_choose"
android:textColor="#color/colorAccent"
android:textSize="#dimen/text_size_medium"
android:textStyle="bold"/>
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_marginEnd="#dimen/preference_expandable_icon_margin"
android:layout_marginStart="#dimen/preference_expandable_icon_margin"
android:layout_height="wrap_content"
android:text="#string/etrto_hint"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:orientation="horizontal"
android:paddingBottom="20dp"
android:paddingEnd="?android:attr/scrollbarSize"
android:layout_marginStart="#dimen/preference_expandable_icon_margin"
android:weightSum="3"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/etrto"/>
<Spinner
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingBottom="20dp"
android:paddingEnd="?android:attr/scrollbarSize"
android:layout_marginStart="#dimen/preference_expandable_icon_margin"
android:weightSum="3"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/manufacturer"/>
<Spinner
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"/>
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="-2dp"
android:background="#color/expandable_preference_divider"/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/preference_category_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical"
android:padding="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/preferences_expandable_margin_top_bottom"
android:layout_marginTop="#dimen/preferences_expandable_margin_top_bottom">
<RelativeLayout
android:id="#+id/icon_wrapper_manual"
android:layout_width="#dimen/preferences_expandable_icon_wrapper_size"
android:layout_height="#dimen/preferences_expandable_icon_wrapper_size"
android:layout_marginBottom="0dp"
android:layout_marginEnd="#dimen/preference_expandable_icon_margin"
android:layout_marginStart="#dimen/preference_expandable_icon_margin"
android:layout_marginTop="0dp"
android:gravity="center">
<ImageView
android:id="#+android:id/icon"
android:layout_width="#dimen/preferences_expandable_icon_size"
android:layout_height="#dimen/preferences_expandable_icon_size"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:cropToPadding="true"
android:scaleType="centerCrop"
android:src="#drawable/ic_edit_white_36dp"/>
</RelativeLayout>
<TextView
android:id="#+android:id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/icon_wrapper_manual"
android:paddingBottom="#dimen/preferences_expandable_text_padding_top_bottom"
android:paddingTop="#dimen/preferences_expandable_text_padding_top_bottom"
android:text="#string/pref_wheel_circumference_manually"
android:textColor="#color/colorAccent"
android:textSize="#dimen/text_size_medium"
android:textStyle="bold"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:orientation="horizontal"
android:paddingBottom="20dp"
android:paddingEnd="?android:attr/scrollbarSize"
android:layout_marginStart="#dimen/preference_expandable_icon_margin"
android:weightSum="2.5"
>
<EditText
android:id="#+id/pref_dialog_wheelcircumference_et"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:textAlignment="textEnd"
android:textColor="#color/colorFont"
android:textSize="#dimen/text_size_small"
android:inputType="number"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:textAlignment="center"
android:text="#string/wheel_circumference_unit"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
My custom preference class
public class WheelCircumferencePreference extends android.preference.DialogPreference {
private static String TAG = "CustomSwitchPreference";
private int mWheelCircumference;
public static int WHEEL_CIRCUMFERENCE_DEFAULT = 2125;
private int mDialogLayoutResId = R.layout.pref_dialog_wheelcircumference;
public WheelCircumferencePreference(Context context) {
this(context, null);
}
public WheelCircumferencePreference(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.dialogPreferenceStyle);
}
public WheelCircumferencePreference(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
setLayoutResource(R.layout.custom_preference);
setDialogLayoutResource(mDialogLayoutResId);
setPositiveButtonText(getContext().getString(R.string.dialog_save));
setNegativeButtonText(getContext().getString(R.string.dialog_cancel));
}
#Override
protected Object onGetDefaultValue(TypedArray a, int index) {
// Default value from attribute. Fallback value is set to WHEEL_CIRCUMFERENCE_DEFAULT.
return a.getInteger(index, WHEEL_CIRCUMFERENCE_DEFAULT);
}
#Override
protected void onSetInitialValue(boolean restorePersistedValue,
Object defaultValue) {
would load value from shared preferences
if (restorePersistedValue) {
mWheelCircumference = getPersistedInt(WHEEL_CIRCUMFERENCE_DEFAULT);
} else {
mWheelCircumference = (Integer) defaultValue;
persistInt(mWheelCircumference);
}
}
private EditText mWheelCircumferenceEt;
#Override
protected void onBindDialogView(View view) {
mWheelCircumferenceEt = view.findViewById(R.id.pref_dialog_wheelcircumference_et);
if (mWheelCircumferenceEt == null) {
throw new IllegalStateException("preference dialog view must contain" +
" a EditText with id 'pref_dialog_wheelcircumference_et'");
}
mWheelCircumferenceEt.setText(Integer.toString(mWheelCircumference));
super.onBindDialogView(view);
}
#Override
public Dialog getDialog() {
//Dialog dialog = super.getDialog();
// WindowManager.LayoutParams p = getDialog().getWindow().getAttributes();
//p.height = LinearLayout.LayoutParams.WRAP_CONTENT;
//dialog.getWindow().setAttributes(p);
return dialog;
}
#Override
protected void onDialogClosed(boolean positiveResult) {
if (positiveResult) {
String circumferenceText = mWheelCircumferenceEt.getText().toString();
try {
mWheelCircumference = Integer.parseInt(circumferenceText);
} catch (Exception e) {
NLog.e(TAG, "onDialogClosed - ", e);
mWheelCircumference = WheelCircumferencePreference.WHEEL_CIRCUMFERENCE_DEFAULT;
}
persistInt(mWheelCircumference);
}
}
Edit:
Actually I only want the dialog to span over the full width of the screen, not the height. If I would use a additional PreferenceFragment (as the DialogPreference is already embedded in a PreferenceFragment ) the "Dialog" (aka Fragment) would take the complete width and height (i guess).
I already implemented a solution without a DialogPrefrence, that works but is not exactly elegant
using just a normal EditTextPreference
adding an onPreferenceClickListener to this preference in my SettingsFragment Code
the ClickListener displays a simple Dialog
Example:
Preference preference = findPreference(EXAMPLE_PREFRENCE);
if (preference != null) {
preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
// showDialog();
}
});
But as I have a lot of preferences which will display dialogs the code for the dialog creation and display bloads the SettingsFragment and makes it nearly unreadable. Therefore I thought it would be a nice solution to put the responsibility of displaying the dialog and handling the preference values to the Preference and the XML layout.
Unfortunately I got stuck with the "full width issue" mentioned above.
Note: fixed the code of getDialog as I tested different versions (also in combination with the xml theme set)
Finally I did find a solution for this problem:
Fetch the AlertDialog of the Preference in showDialog method
#Override
protected void showDialog(Bundle state) {
super.showDialog(state);
CustomDialogPreference.makeDialogFullScreen((AlertDialog) getDialog());
}
make it span the complete width:
public static void makeDialogFullScreen(AlertDialog d) {
NLog.d(TAG, "makeDialogFullScreen enter ");
if (d != null) {
ViewGroup.LayoutParams params = d.getWindow().getAttributes();
if (params != null) {
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
d.getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
}
}
}
Try this in the onResume of your dialog.
// Store access variables for window and blank point
Window window = getDialog().getWindow();
Point size = new Point();
// Store dimensions of the screen in `size`
Display display = window.getWindowManager().getDefaultDisplay();
display.getSize(size);
// Set the width of the dialog proportional to 75% of the screen width and height
window.setLayout((int) (size.x * 0.75), (int) (size.y * 0.75));
window.setGravity(Gravity.CENTER);
// Call super onResume after sizing
Adjust accordingly for 100%. It works great for a dialogFragment. Haven't tried it for your case though.
Wait, you're not looking for the bog-standard 'Pref settings user options appear in a dialog' thing are you? That's almost definitely already done in AndroidStudio's add activity...> Settings Activity in boiler plate, check it out, or look for sample settings apps
Anyway, I do actually have a fullscreen dialog in my app, although it purposely doesn't fill the full screen, and I actually use an activity with some fragments now instead.
Personally I think this is what your problem is, I remember having this exact issue when I first needed a dialog like this. You should just use activities and have up navigation (if you want a full screen "popup" type thing you could use the Navigation pattern that makes the home/up button an 'X' instead of a '<');
Or anything else, you don't need to have a dialog explicitly, and if you do then extend activity or dialog and get what you want.
Here's my activity stuff in case it's any use
my theme:
<style name="AppTheme.FullScreenDialog"
parent="#style/Theme.AppCompat.Light.Dialog">
<item name="windowActionBar">true</item>
<item name="windowNoTitle">true</item>
</style>
my onCreate gist:
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
...
requestWindowFeature(Window.FEATURE_NO_TITLE);
...
super.onCreate(savedInstanceState);
setContentView(getConcreteContentView());
ButterKnife.bind(this);
setUpUIComponents();
...
}
my general layout gist:
<CoordinatorLayout>
<AppBarLayout>
<android.support.v7.widget.Toolbar/>
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:id="#+id/container_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:paddingTop="6dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<RelativeLayout
android:id="#+id/container_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_security_word"
android:paddingEnd="18dp"
android:paddingStart="18dp" />
<RelativeLayout
android:id="#+id/container_security"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/container_recycler"
android:minHeight="150dp"
android:paddingEnd="18dp"
android:paddingStart="18dp"
android:visibility="visible" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/security_container"
android:layout_centerHorizontal="true"
android:contentDescription="#string/app_name"
android:minHeight="50dp"
android:scaleType="centerInside" />
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
Bon Chance!

Adding fragment to layout results in blank UI except for new fragment

Rookie android dev, inherited an app which works fine. They want to add simple bottom nav. Just to see what it might look like, I added some radio buttons into a fragment, included that fragment into the main activity page's layout, and had the main activity class implement the required interface to support the new fragment. The main activity class extends a custom class which extends FragmentActivity.
Upon running the app with my addition, nothing shows up on the main activity screen except those radio buttons below a blank, white screen. Worse, if I touch in the blank area, the app responds to the touch, so apparently it thinks it's still displaying the stuff it should be displaying (a map and a list of businesses, and a touch navigates to the touched business's detail page).
Worst of all, there are no compile nor runtime errors. Not even sure what to Google at this point...any help welcome. I've tried shrinking the size of the bottom nav all the way down to barely visible, and I've tried shrinking other components on the main page.
Here's the layout of the main page; almost the absolute bottom is where I added the new include :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/white" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="#dimen/content_view_top_margin"
android:background="#color/white">
<RelativeLayout
android:id="#+id/mapContents"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:visibility="invisible" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraTargetLat="41.7150"
map:cameraTargetLng="-77.1625"
map:cameraZoom="10"/>
<LinearLayout
android:id="#+id/mapButtons"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:alpha="0"
android:orientation="horizontal"
android:padding="#dimen/content_view_padding" >
<ImageButton
android:id="#+id/mapCenterLocation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="20"
android:layout_marginRight="10dp"
style="#style/GreenButtonLargeText"
android:src="#drawable/map_home_arrow"/>
<Button
android:id="#+id/mapRedoSearch"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="60"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
style="#style/GreenButtonLargeText"
android:text="Redo Search Here" />
<ImageButton
android:id="#+id/mapCashBackToggle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="20"
android:layout_marginLeft="10dp"
style="#style/GreenButtonLargeText"
android:src="#drawable/icon_percentage"/>
</LinearLayout>
</RelativeLayout>
<ListView
android:id="#+id/lvBusinesses"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:cacheColorHint="#android:color/transparent"
android:fadingEdgeLength="0dp"
android:overScrollMode="never"
android:visibility="invisible" />
<include
layout="#layout/no_results_found_layout"/>
<LinearLayout
android:id="#+id/llLoading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/rounded_corners"
android:padding="20dp"
android:orientation="vertical" >
<ImageView
android:id="#+id/imgSteam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:background="#drawable/loading_animation" />
<ImageView
android:id="#+id/imgLoader"
android:layout_width="100dp"
android:layout_height="49dp"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
<TextView
android:id="#+id/tvLoading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Loading..." />
</LinearLayout>
</RelativeLayout>
<include android:id="#+id/include_header"
layout="#layout/header" />
<!-- this is the only change Ive made -->
<include android:id="#+id/include_bottom_nav"
layout="#layout/bottom_nav" />
<!-- end change -->
<FrameLayout
android:id="#+id/searchFragmentHolder"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true" />
</RelativeLayout>
And here's the bottom nav piece I included (include_bottom_nav.xml):
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/b_nav_fragment"
android:name="com.example.activity.BottomNavFragment"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
Which references this fragment (BottomNavFragment.java):
package com.example.activity;
import com.example.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RadioButton;
public class BottomNavFragment extends Fragment
{
private View fragmentView;
private OnFragmentInteractionListener mListener;
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #return A new instance of fragment BottomNavFragment.
*/
public static BottomNavFragment newInstance ()
{
BottomNavFragment fragment = new BottomNavFragment ();
Bundle args = new Bundle ();
fragment.setArguments ( args );
return fragment;
}
public BottomNavFragment ()
{
// Required empty public constructor
}
#Override
public void onCreate ( Bundle savedInstanceState )
{
super.onCreate ( savedInstanceState );
}
RadioButton radioButton1;
RadioButton radioButton2;
RadioButton radioButton3;
RadioButton radioButton4;
RadioButton radioButton5;
#Override
public View onCreateView ( LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState )
{
// Inflate the layout for this fragment
fragmentView = inflater.inflate ( R.layout.fragment_bottom_nav, container, false );
radioButton1 = (RadioButton) fragmentView.findViewById ( R.id.btnAll );
radioButton1.setOnCheckedChangeListener(btnNavBarOnCheckedChangeListener);
radioButton2 = (RadioButton) fragmentView.findViewById ( R.id.btnPicture );
radioButton2.setOnCheckedChangeListener(btnNavBarOnCheckedChangeListener);
radioButton3 = (RadioButton) fragmentView.findViewById ( R.id.btnVideo );
radioButton3.setOnCheckedChangeListener(btnNavBarOnCheckedChangeListener);
radioButton4 = (RadioButton) fragmentView.findViewById ( R.id.btnFile );
radioButton4.setOnCheckedChangeListener(btnNavBarOnCheckedChangeListener);
radioButton5 = (RadioButton) fragmentView.findViewById(R.id.btnMore);
radioButton5.setOnCheckedChangeListener(btnNavBarOnCheckedChangeListener);
return fragmentView;
}
public void toggleButton( CompoundButton buttonView )
{
if ( null == buttonView )
{
System.out.println ( "null button view -- wtf" );
return;
}
switch ( buttonView.getId () )
{
case R.id.btnPicture:
radioButton2.setButtonDrawable ( R.drawable.navbar_pictureselected );
break;
case R.id.btnMore:
radioButton5.setButtonDrawable ( R.drawable.navbar_moreselected );
break;
case R.id.btnAll:
radioButton1.setButtonDrawable ( R.drawable.navbar_allselected );
break;
case R.id.btnFile:
radioButton4.setButtonDrawable ( R.drawable.navbar_fileselected );
break;
case R.id.btnVideo:
radioButton3.setButtonDrawable ( R.drawable.navbar_videoselected );
break;
}
}
private CompoundButton.OnCheckedChangeListener btnNavBarOnCheckedChangeListener = new CompoundButton.OnCheckedChangeListener()
{
public void onCheckedChanged( CompoundButton buttonView, boolean isChecked)
{
Intent i = null;
if (isChecked)
{
if ( mListener != null )
{
switch ( buttonView.getId () )
{
// do nav here later
}
mListener.onFragmentInteraction ( buttonView, i );
}
}
}
};
#Override
public void onAttach ( Activity activity )
{
super.onAttach ( activity );
try
{
mListener = (OnFragmentInteractionListener) activity;
} catch ( ClassCastException e )
{
throw new ClassCastException ( activity.toString ()
+ " must implement OnFragmentInteractionListener" );
}
}
#Override
public void onDetach ()
{
super.onDetach ();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
*/
public interface OnFragmentInteractionListener
{
public void onFragmentInteraction ( CompoundButton buttonView, Intent intent );
}
}
Definitely in over my head. Thanks for any guidance.
EDIT: request for the fragment_bottom_nav.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white"
>
<RadioGroup
android:id="#+id/radiogroup"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:background="#drawable/navbar_background"
>
<RadioButton
android:id="#+id/btnAll"
style="#style/navbar_button"
android:drawableTop="#drawable/navbar_allselector"
android:text="All"
/>
<RadioButton
android:id="#+id/btnPicture"
style="#style/navbar_button"
android:drawableTop="#drawable/navbar_pictureselector"
android:text="Pictures"
android:layout_marginLeft="5dp"
/>
<RadioButton
android:id="#+id/btnVideo"
style="#style/navbar_button"
android:drawableTop="#drawable/navbar_videoselector"
android:text="Videos"
android:layout_marginLeft="5dp"
/>
<RadioButton
android:id="#+id/btnFile"
style="#style/navbar_button"
android:drawableTop="#drawable/navbar_fileselector"
android:text="Files"
android:layout_marginLeft="5dp"
/>
<RadioButton
android:id="#+id/btnMore"
style="#style/navbar_button"
android:drawableTop="#drawable/navbar_moreselector"
android:text="More"
android:layout_marginLeft="5dp"
/>
</RadioGroup>
</RelativeLayout>
In the fragment_bottom_nav.xml- move the android:layout_alignParentBottom="true" line from the RadioGroup to the parent RelativeLayout (also, you probably want the height of the RelativeLayout to be wrap_content and not match_parent). When/if you are checking it out in Android Studio designer, make sure to take a look at the main layout, not at the fragment_bottom_nav.xml (here it will look all aligned to the top)

Class not found in xml created in Android Studio

I am trying CardsUI, I have following code: Including NowActivity Code
NowActivity.java where I define the behaviour
package com.example.rushdesktop.testingweather;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
public class NowActivity extends LinearLayout implements OnGlobalLayoutListener {
public NowActivity(Context context, AttributeSet attrs)
{ super(context, attrs); initLayoutObserver(); }
public NowActivity(Context context)
{ super(context); initLayoutObserver(); }
private void initLayoutObserver()
{ // force vertical orientation and add observer
setOrientation(LinearLayout.VERTICAL);
getViewTreeObserver().addOnGlobalLayoutListener(this); }
#Override public void onGlobalLayout()
{
getViewTreeObserver().removeGlobalOnLayoutListener(this);
final int heightPx =getContext().getResources().getDisplayMetrics().heightPixels;
boolean inversed = false;
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++)
{
View child = getChildAt(i);
int[] location = new int[2];
child.getLocationOnScreen(location);
if (location[1] > heightPx) { break; }
if (!inversed) {
child.startAnimation( AnimationUtils.loadAnimation(getContext(), R.anim.slide_up_left));
}
else {
child.startAnimation( AnimationUtils.loadAnimation(getContext(), R.anim.slide_up_right));
}
inversed = !inversed;
}
}
I use it in my main activity xml:
<ScrollView 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" android:fillViewport="true">
<com.example.rushdesktop.testingweather.NowActivity android:layout_width="match_parent" android:layout_height="match_parent" android:background="#e3e3e3" android:orientation="vertical" >
<TextView android:id="#+id/txtView" style="#style/nowCardStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="#string/hello_world" tools:context=".MyActivity" />
<TextView android:id="#+id/txtView1" style="#style/nowCardStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="#string/hello_world" tools:context=".MyActivity" />
<TextView android:id="#+id/txtView2" style="#style/nowCardStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="#string/hello_world" tools:context=".MyActivity" />
<TextView android:id="#+id/txtView3" style="#style/nowCardStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="#string/hello_world" tools:context=".MyActivity" />
<TextView android:id="#+id/txtView4" style="#style/nowCardStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="#string/hello_world" tools:context=".MyActivity" />
</com.example.rushdesktop.testingweather.NowActivity>
</ScrollView>
As in the code I use the Java class in my main activity xml. But when I build my code I get error showing the class was not found in the XML. Please help me with the issue.
Here is the link to my full android studio code:
https://drive.google.com/file/d/0B6P-8t-14MK-dEZ0bHJqRzBvUmVaSW5ySTR3My1mN0U1OThV/edit?usp=sharing
Thanks in advance.
Be sure that the only child of the scroll layout (com.example.rushdesktop.testingweather.NowActivity) inherits some sort of layout (i.e. RelativeLayout, FrameLayout...) does it?
in your xml layout you need to use the View, not the activity.. the activity will set the layout it uses via setContentView(R.layout.[your layout])
in your xml layout, if you switch to graphical editor view you should be able to see the view you want listen in the custom & library views tab (in eclipse), providing you have included the library in the project build

Could not click on the buttons in SlidingDrawer

I have a problem with SlidingDrawer. All buttons in it could not be click.
Here is my xml file for SlidingDrawer:
<?xml version="1.0" encoding="utf-8"?>
<SlidingDrawer xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="110dp"
android:content="#+id/content"
android:handle="#+id/handle">
<Button
android:id="#+id/handle"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="#string/menu" />
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_gravity="center"
>
<Button
android:id="#+id/btn_video"
style="#style/button_menu_bottom"
android:drawableTop="#drawable/mnu_video"
android:text="#string/video" />
...
...
</LinearLayout>
</SlidingDrawer>
Here is my java file extended from SlidingDrawer
package com.example;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SlidingDrawer;
import com.example.R;
public class BottomMenuBar extends SlidingDrawer {
private SlidingDrawer mSlidingDrawer;
private Button mButtonSlidingDrawer;
private LinearLayout mLayoutContent;
private Button mButtonVideo;
private Button mButtonPhoto;
private Button mButtonChat;
private Button mButtonSetting;
public BottomMenuBar2(Context context, AttributeSet attrs) {
super(context, attrs);
String infService = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li;
li = (LayoutInflater) getContext().getSystemService(infService);
li.inflate(R.layout.bar_bottom_menu, this, true);
mSlidingDrawer=(SlidingDrawer)findViewById(R.id.sld_bottom_menu);
mButtonSlidingDrawer=(Button)findViewById(R.id.handle);
mLayoutContent=(LinearLayout)findViewById(R.id.content);
mButtonVideo=(Button)findViewById(R.id.btn_video);
mButtonSetting=(Button)findViewById(R.id.btn_setting);
}
public Button getmButtonSlidingDrawer() {
return mButtonSlidingDrawer;
}
public void setmButtonSlidingDrawer(Button mButtonSlidingDrawer) {
this.mButtonSlidingDrawer = mButtonSlidingDrawer;
}
...
Setter & Getter methods
...
}
Here is my main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg_main"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.1" >
...
Any content
...
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<com.example.BottomMenuBar
android:id="#+id/sld_bottom_menu"
android:layout_width="wrap_content"
android:layout_height="110dp"
android:handle="#+id/handle"
android:content="#+id/content"
/>
</LinearLayout>
</LinearLayout>
For my buttons in sliding drawers, I tell each button what method to use in the xml with android:onClick.
<Button
android:id="#+id/sortbutton"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:onClick="SortClickHandler"
android:text="#string/sd_sortmethod_button"
android:textSize="12dp" />
And then define the method in whatever activity is appropriate:
public void SortClickHandler(View v) {
// Do stuff here
}
EDIT
I think I found your issue. From the developer docs here it says, and I quote:
SlidingDrawer should be used as an overlay inside layouts. This means SlidingDrawer
should only be used inside of a FrameLayout or a RelativeLayout for instance.
So, I think if you change your layout to a RelativeLayout you will find that your buttons will work. I just double-checked all of my uses of a sliding drawer and in all cases the root element of the view containing it is a RelativeLayout.
I don't understand why you're extending SlidingDrawer.
You should look up the documentation here.
<SlidingDrawer
android:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:handle="#+id/handle"
android:content="#+id/content">
<ImageView
android:id="#id/handle"
android:layout_width="88dip"
android:layout_height="44dip" />
<FrameView
android:id="#id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">
[INSERT VIEWS HERE]
<FrameView>
</SlidingDrawer>
In your Activity you don't even technically need to gain access to the SlidingDrawer object (The sliding functionality is already in place as standard, it simply contains the the view's in content, you don't need to interfere).
From your Activity gain reference to the view's you add in [INSERT VIEWS HERE]. You can then do setOnClickListener() on each of these buttons.

Categories

Resources