#StackBounty: #android #device-admin Android DeviceAdminReceiver: onNetworkLogsAvailable intent never received

Bounty: 250

UPDATE

I’m noticing that I actually am receiving the NETWORK_LOGS_AVAILABLE intent! The problem is, it’s taking a very long time (over an hour?) to receive it.

Is there any known way to increase the frequency of receiving these events?

Original Question

I am trying to process DNS events that can now be read after receiving the onNetworkLogsAvailable intent in a DeviceAdminReceiver application. This functionality was made available as of Android 8.0.

For some reason, I am never receiving this intent, even though I am successfully calling the setNetworkLoggingEnabled method. Upon admin being enabled, I am receiving the ACTION_DEVICE_ADMIN_ENABLED event, but nothing else after that.

Here’s where I enable network logging:

public class NetworkAdminReceiver extends DeviceAdminReceiver {
    @Override
    public void onEnabled(Context context, Intent intent) {
        DevicePolicyManager manager =
            (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
        if ( manager == null )
        {
            throw new IllegalStateException("Unable to get DevicePolicyManager");
        }
        if (manager.isDeviceOwnerApp(context.getPackageName())) {
            manager.setNetworkLoggingEnabled(getWho(context), true);
        }
        else
        {
            Toast.makeText(context, "This application is not device owner. DNS logging only works" +
                " when this application is setup as the Device Owner", Toast.LENGTH_LONG).show();
        }
    }
    // *snip* rest of class
}

Although I am not sure whether it’s required (cannot find in documentation), I’ve also added the NETWORK_LOGS_AVAILABLE intent action to the receiver’s filter:

<receiver android:name=".admin.NetworkAdminReceiver"
        android:label="@string/device_admin"
        android:description="@string/device_admin_description"
        android:permission="android.permission.BIND_DEVICE_ADMIN">
    <meta-data android:name="android.app.device_admin"
            android:resource="@xml/device_admin" />
    <intent-filter>
        <action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
        <action android:name="android.app.action.NETWORK_LOGS_AVAILABLE"/>
    </intent-filter>
</receiver>

The application is marked as the device owner, network logging is enabled, and yet I never receive the intent. The only explanation I could think of is that network logs do not become available very frequently, but I could find no documentation supporting this theory.

I am also currently only testing this in the emulator. I am unsure if that would have an effect on this, though I cannot see how it would.

Is there anything that I am missing in order to properly receive the network logs via the DeviceAdminReceiver?


Get this bounty!!!

#StackBounty: #android #imageview #crop #heartview Crop/Adjust image in heart shape/frame

Bounty: 100

I am trying to crop or adjust image as per heart shape. User can adjust image as per heart shape and set image to that heart shape.

Currently, I am using Github Project library to crop the image as a square and to set to heart shape. But all images are getting cut from top side where heart is curved. So, I want to allow user to crop image as per heart border and then set the way user want. But there is no any library which I can customize or use.

Here is current code which I am using from above github project.

  1. To open Camera and Gallery :

    CropImage.activity().setGuidelines(CropImageView.Guidelines.ON) .start(this);

  2. Getting Crop Image and Setting to ImageHeart View.

    CropImage.ActivityResult result = CropImage.getActivityResult(data);
    picUri = result.getUri();
    bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), picUri);
    imvHeartRed.setImageBitmap(bitmap);

Here is image in which I am cropping glass using square cropping but while setting full glass image is not coming. But If user crop it using heart shape then full glass will be shown because User will know what part of image will get cropped.

enter image description here

Any help, reference will be really appreciated.

I don’t want to set image in heart directly but user can adjust image in heart by touching/moving


Get this bounty!!!

#StackBounty: #android #broadcastreceiver LocationManager.PROVIDERS_CHANGED_ACTION will not work on API 26 and higher

Bounty: 50

I am using following code to get location on/off event.

<receiver
    android:name=".receivers.GpsReceiver"
    android:enabled="true">
    <intent-filter>
        <action android:name="android.location.PROVIDERS_CHANGED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

I am developing geofence based app. Based on Re-register geofences only when required we have to re register the geofences after the app has received a GEOFENCE_NOT_AVAILABLE alert. This typically happens after NLP (Android’s Network Location Provider) is disabled.

By using this broadcast receiver I re registered the geofences when Android’s Network Location Provider is enabled.

But From API level 26 this broadcast receiver will never work. Ref: Background Execution Limits.

So how can I achieve the same task in API 26 and higher?

Any solution/suggestion will be highly appreciated.

Note : I need to re register the geofences even app is in background.


Get this bounty!!!

#StackBounty: #android #android-viewpager Viewpager – Try catch handled but getting pointer index out of range

Bounty: 50

I am getting the following exception

 Exception java.lang.IllegalArgumentException: pointerIndex out of range
    android.view.MotionEvent.nativeGetAxisValue (MotionEvent.java)
    android.view.MotionEvent.getX (MotionEvent.java:2141)
    android.support.v4.view.ViewPager.onInterceptTouchEvent 
    (ViewPager.java:2092)
    android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2254)
    android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2711)
    android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2400)
    android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2711)
    android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2400)
    android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2711)
    android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2400)
    com.android.internal.policy.DecorView.superDispatchTouchEvent 
    (DecorView.java:416)
    com.android.internal.policy.PhoneWindow.superDispatchTouchEvent 
    (PhoneWindow.java:1837)
    android.app.Activity.dispatchTouchEvent (Activity.java:3154)
    com.android.internal.policy.DecorView.dispatchTouchEvent 
    (DecorView.java:378)
    android.view.View.dispatchPointerEvent (View.java:10177)
    android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent 
    (ViewRootImpl.java:4634)
    android.view.ViewRootImpl$ViewPostImeInputStage.onProcess 
    (ViewRootImpl.java:4502)
    android.view.ViewRootImpl$InputStage.deliver (ViewRootImpl.java:3953)
    android.view.ViewRootImpl$InputStage.onDeliverToNext 
    (ViewRootImpl.java:4006)
    android.view.ViewRootImpl$InputStage.forward (ViewRootImpl.java:3972)
    android.view.ViewRootImpl$InputStage.apply (ViewRootImpl.java:3980)
    android.view.ViewRootImpl$InputStage.deliver (ViewRootImpl.java:3953)
    android.view.ViewRootImpl$InputStage.onDeliverToNext 
    (ViewRootImpl.java:4006)
    android.view.ViewRootImpl$InputStage.forward (ViewRootImpl.java:3972)
    android.view.ViewRootImpl$AsyncInputStage.forward (ViewRootImpl.java:4101)
    android.view.ViewRootImpl$InputStage.apply (ViewRootImpl.java:3980)
    android.view.ViewRootImpl$AsyncInputStage.apply (ViewRootImpl.java:4158)
    android.view.ViewRootImpl$InputStage.deliver (ViewRootImpl.java:3953)
    android.view.ViewRootImpl$InputStage.onDeliverToNext 
    (ViewRootImpl.java:4006)
    android.view.ViewRootImpl$InputStage.forward (ViewRootImpl.java:3972)
    android.view.ViewRootImpl$InputStage.apply (ViewRootImpl.java:3980)
    android.view.ViewRootImpl$InputStage.deliver (ViewRootImpl.java:3953)
    android.view.ViewRootImpl.deliverInputEvent (ViewRootImpl.java:6443)
    android.view.ViewRootImpl.doProcessInputEvents (ViewRootImpl.java:6417)
    android.view.ViewRootImpl.enqueueInputEvent (ViewRootImpl.java:6378)
    android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent 
    (ViewRootImpl.java:6577)
    android.view.InputEventReceiver.dispatchInputEvent 
    (InputEventReceiver.java:185)
    android.view.InputEventReceiver.nativeConsumeBatchedInputEvents 
    (InputEventReceiver.java)
    android.view.InputEventReceiver.consumeBatchedInputEvents 
    (InputEventReceiver.java:176)
    android.view.ViewRootImpl.doConsumeBatchedInput (ViewRootImpl.java:6525)
    android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run 
    (ViewRootImpl.java:6600)
    android.view.Choreographer$CallbackRecord.run (Choreographer.java:871)
    android.view.Choreographer.doCallbacks (Choreographer.java:683)
    android.view.Choreographer.doFrame (Choreographer.java:613)
    android.view.Choreographer$FrameDisplayEventReceiver.run 
    (Choreographer.java:857)
    android.os.Handler.handleCallback (Handler.java:751)
    android.os.Handler.dispatchMessage (Handler.java:95)
    android.os.Looper.loop (Looper.java:154)
    android.app.ActivityThread.main (ActivityThread.java:6310)
    java.lang.reflect.Method.invoke (Method.java)
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run 
    (ZygoteInit.java:872)
    com.android.internal.os.ZygoteInit.main (ZygoteInit.java:762)

I have 2 viewpagers in the application.Both are custom – One returns false in onInterceptTouchEvent. The other has a try catch implemented in onInterceptTouchEvent. How is this error occuring then?
Added code for the 2 viewpagers below

public class BottomBarViewPager extends ViewPager {

    public BottomBarViewPager(Context context) {
        super(context);
    }

    public BottomBarViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return false;
    }
}


public class GalleryViewPager extends ViewPager {

    public static final String TAG = GalleryViewPager.class.getSimpleName();

    private boolean swipeEnabled;

    public GalleryViewPager(Context context, AttributeSet attributes) {
        super(context, attributes);
        this.swipeEnabled = true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent te) {
        if (this.swipeEnabled) {
            return super.onTouchEvent(te);
        }

        return false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent me) {
        if (this.swipeEnabled) {


            /*
            This is caused by a bug in ViewPager and a solution is to catch the Exception
            and continue on.  This only happens if you quickly tap with 2 fingers in different
            locations, rapidly.


             */
            try {
                return super.onInterceptTouchEvent(me);
            } catch (IllegalArgumentException ex) {
//                SinclairLogger.e(TAG, "Caught exception in onInterceptTouchEvent", ex);
            }
        }

        return false;
    }

    public void setPagingEnabled(boolean enabled) {
        this.swipeEnabled = enabled;
    }
}

Added code for the viewpagers as requested


Get this bounty!!!

#StackBounty: #android #comparison Android app to create and visualise price history database

Bounty: 50

I am looking for app that allows easy adding and comparing price entries.

For example I would be able to enter that 500GB SSD disk costs 400PLN or that 2kg bag of onions costs 2 PLN, with two effects

  • I get answer whatever it is attractive price, based on stored data
  • price is saved for future comparisons

Price entry would be info how much something cost at certain shop and may include things like

  • price
  • name
  • amount (with unit)
  • date
  • shop
  • and potentially more (barcode, category etc)

App would allow adding such entries and comparing them with existing ones.

So it is absolutely necessary for me that

  • app works on Android 6
  • it is possible to import/export my own data
  • it is possible to maintain unit list (so I can add custom unit and delete predefined ones useless for me)
  • if app is paid then one can test it without paying (version with adds or with some other limit like database size)
  • app is capable of working offline
  • app is available in English or Polish or has a very intuitive interface

It would be even better if

  • there is available version without adds (may be paid)
  • app is open-sourced
  • app is actively developed
  • it is possible to set currency symbol
  • date is autoset to current one
  • shop is preset to one from definable list based on location
  • app is using price entries sourced from Internet/other users/whatever else, not only ones that I collected
  • there is possible to maintain list of synonyms (one product may have several names)


Get this bounty!!!

#StackBounty: #android Fade effect in Navigation drawer header

Bounty: 50

I have been working in an app where i need to include Navigation Drawer and bottom navigation.

I coded like below following androidhive tutorials.

appbarhome.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
        tools:context="com.app.standardcoldpressoil.Activity.HomeActivity">

        <include layout="@layout/content_home" />

</android.support.design.widget.CoordinatorLayout>

content_home.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:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.app.standardcoldpressoil.Activity.HomeActivity"
    tools:showIn="@layout/app_bar_home">

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        android:foreground="?attr/selectableItemBackground"
        app:itemBackground="@color/colorPrimary"
        app:itemIconTint="@android:color/white"
        app:itemTextColor="@android:color/white"
        app:menu="@menu/navigation" />

</RelativeLayout>

activity_home.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="false"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_home"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:layout_marginTop="?attr/actionBarSize"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_home"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

But i am getting the header of navigation header faded. I have attached the screenshot of my screen.

I need Actionbar to display fragment name, so i added code in my activity like,

ActionBar toolbar;
 toolbar = getSupportActionBar();
        toolbar.setDisplayHomeAsUpEnabled(true);
        toolbar.setHomeButtonEnabled(true);

      /*  BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);*/

        toolbar.setTitle("Shop");

Any help would be greatly appreciated. It will help for my better understanding !

enter image description here

nav_header_home.xml

<?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="@dimen/nav_header_height"
    android:background="@drawable/side_nav_bar"
    android:gravity="bottom"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:src="@drawable/app_logo" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="Welcome Ram"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        android:textColor="@color/colorPrimaryDark" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ramjay@gmail.com"
        android:textColor="@color/colorPrimaryDark" />

</LinearLayout>


Get this bounty!!!

#StackBounty: #java #android #xml #android-layout #android-textview Horizontally Scrolling Text Performance Issues (updated)

Bounty: 50

My app contains a few textViews which are supposed to be scrolling horizontally. This works when my layout is first loaded but after clicking on a button to load another layout and then re-click on that button to load back the first layout those textViews start scrolling after a “big” delay(like 20 secs+). I’m trying to figure out the source of this problem with the Layout Inspector, Hierarchy Viewer etc. but no luck. Any ideas?

UPDATE: I noticed something strange today.When i go from the 1st layout to the 2nd one and vice versa, although the TextViews stop scrolling, if i close and re-open the phone’s screen, the scrolling works like a charm. This seems totally strange to me, do you know what’s causing this and why?

CardViewActivity.java:

public class CardViewActivity extends AppCompatActivity {

private ImageView cardArtImageView;
private TextView leaderSkillDescText;
private TextView superAttackTitleText;
private TextView superAttackDescText;
private TextView passiveSkillTitleText;
private TextView passiveSkillDescText;
private TextView hpText;
private TextView attText;
private TextView defText;
private TextView costText;
private Button arrowButton;
private int selectedItemPosition;
private boolean isBtnClicked = false;

// Listener member field for each layout's button. This listener will be used recursively
private View.OnClickListener arrowButtonListener = new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        // When the arrowButton is clicked, choose the right layout based on the button's state
        int resID = isBtnClicked ? R.layout.cardview_refined : R.layout.cardview_expand_details;

        setContentView(resID);

        // If we're in the first layout, initialize the cardArtImageView field
        if(isBtnClicked) {
            cardArtImageView = findViewById(R.id.cardArtImageView);
        }

        viewDefinitions(!isBtnClicked);
        initCardViewData(selectedItemPosition);
        setSelectedViewsInit();

        // Set the arrowButton's listener to this listener (recursively)
        arrowButton.setOnClickListener(arrowButtonListener);

        // toggle our flag field
        isBtnClicked = !isBtnClicked;
    }
};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cardview_refined);

    // Retrieving the data sent over from MainActivity
    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();

    if (bundle != null) {
        selectedItemPosition = bundle.getInt("Card Index");
    }
    //Toast.makeText(this, "WIDTH: " + SCREEN_WIDTH, Toast.LENGTH_SHORT).show();

    // Initializing our views
    cardArtImageView = findViewById(R.id.cardArtImageView);
    viewDefinitions(false);
    setSelectedViewsInit();

    initCardViewData(selectedItemPosition);

    arrowButton.setOnClickListener(arrowButtonListener);
}

/**
 * Sets the required textViews as selected to allow automatic scrolling
 */
private void setSelectedViewsInit() {
    leaderSkillDescText.setSelected(true);
    superAttackTitleText.setSelected(true);
    superAttackDescText.setSelected(true);
    if (passiveSkillTitleText != null && passiveSkillDescText != null) {
        passiveSkillTitleText.setSelected(true);
        passiveSkillDescText.setSelected(true);
    }
}

/**
 * Adds the views's definitions
 *
 * @param initPassiveInfo used to decide whether or not the passiveSkillDesc & ..Title != null
 *                        so that they can be defined
 */
private void viewDefinitions(boolean initPassiveInfo) {
    leaderSkillDescText = findViewById(R.id.leaderSkillDesc);
    superAttackTitleText = findViewById(R.id.superAttackTitle);
    superAttackDescText = findViewById(R.id.superAttackDesc);
    if (initPassiveInfo) {
        passiveSkillTitleText = findViewById(R.id.passiveSkillTitle);
        passiveSkillDescText = findViewById(R.id.passiveSkillDesc);
    } else {
        Log.d("Definitions", "Passive info == null");
    }
    hpText = findViewById(R.id.HP);
    attText = findViewById(R.id.ATT);
    defText = findViewById(R.id.DEF);
    costText = findViewById(R.id.COST);
    arrowButton = findViewById(R.id.arrowButton);
}

/**
 * Initialize the cardViewActivity's views with the data from the CardInfoDatabase.java class
 *
 * @param selectedItemPosition Used to initialize this activity's views if the intent was called from the MainScreen Fragment
 */
private void initCardViewData(int selectedItemPosition) {
    if (cardArtImageView != null) {
        cardArtImageView.setImageResource(CardInfoDatabase.cardArts[selectedItemPosition]);
    }
    leaderSkillDescText.setText(CardInfoDatabase.leaderSkills[selectedItemPosition]);
    superAttackTitleText.setText(CardInfoDatabase.superAttacksName[selectedItemPosition]);
    superAttackDescText.setText(CardInfoDatabase.superAttacksDesc[selectedItemPosition]);
    if (passiveSkillTitleText != null && passiveSkillDescText != null) {
        passiveSkillTitleText.setText(CardInfoDatabase.passiveSkillsName[selectedItemPosition]);
        passiveSkillDescText.setText(CardInfoDatabase.passiveSkillsDesc[selectedItemPosition]);
    }
    hpText.setText(CardInfoDatabase.hp[selectedItemPosition].toString());
    attText.setText(CardInfoDatabase.att[selectedItemPosition].toString());
    defText.setText(CardInfoDatabase.def[selectedItemPosition].toString());
    costText.setText(CardInfoDatabase.cost[selectedItemPosition].toString());
}
}

firstLayout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/card_view_bg"
    tools:layout_editor_absoluteY="25dp">

<ImageView
    android:id="@+id/cardArtImageView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_weight="1"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"
    app:layout_constraintBottom_toTopOf="@+id/cardDetailsImageView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:contentDescription="@string/card_image" />

<!--
<ImageView
    android:id="@+id/cardDetailsImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:adjustViewBounds="true"
    android:cropToPadding="false"
    android:scaleType="fitXY"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:srcCompat="@drawable/card_details_box" /> -->

<!-- Implement scrolling text
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:singleLine="true"
        android:scrollHorizontally="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
 -->

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="225dp"
    android:background="@drawable/card_details_closed">

    <TextView
        android:id="@+id/COST"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/leaderSkillDesc"
        android:layout_marginBottom="9dp"
        android:layout_toEndOf="@+id/superAttackDesc"
        android:paddingLeft="3dp"
        android:paddingRight="3dp"
        android:text="00"
        android:textColor="@color/white"
        android:textSize="17sp"
        android:textStyle="bold" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true">

        <TextView
            android:id="@+id/HP"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="33dp"
            android:fontFamily="monospace"
            android:text="0000"
            android:textColor="@color/white"
            android:textSize="18sp"
            android:textStyle="bold"
            tools:text="0000" />

        <TextView
            android:id="@+id/ATT"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginStart="73dp"
            android:layout_toEndOf="@+id/HP"
            android:fontFamily="monospace"
            android:text="0000"
            android:textColor="@color/white"
            android:textSize="18sp"
            android:textStyle="bold"
            tools:text="0000" />

        <TextView
            android:id="@+id/DEF"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentTop="true"
            android:layout_marginEnd="42dp"
            android:fontFamily="monospace"
            android:text="0000"
            android:textColor="@color/white"
            android:textSize="18sp"
            android:textStyle="bold"
            tools:text="0000" />

    </RelativeLayout>

    <TextView
        android:id="@+id/leaderSkillDesc"
        android:layout_width="250dp"
        android:layout_height="15dp"
        android:layout_above="@+id/superAttackTitle"
        android:layout_alignParentEnd="true"
        android:layout_marginBottom="13dp"
        android:layout_marginEnd="37dp"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:fontFamily="monospace"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:textAlignment="viewStart"
        android:textColor="@color/white"
        android:textSize="13sp"
        android:textStyle="italic"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.496"
        app:layout_constraintVertical_bias="0.626" />

    <TextView
        android:id="@+id/superAttackTitle"
        android:layout_width="245dp"
        android:layout_height="15dp"
        android:layout_above="@+id/superAttackDesc"
        android:layout_alignStart="@+id/superAttackDesc"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:fontFamily="monospace"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:textAlignment="viewStart"
        android:textColor="@android:color/holo_blue_light"
        android:textSize="12sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/superAttackDesc"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
         />

    <TextView
        android:id="@+id/superAttackDesc"
        android:layout_width="255dp"
        android:layout_height="15dp"
        android:layout_alignEnd="@+id/leaderSkillDesc"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="74dp"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:fontFamily="monospace"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:textAlignment="viewStart"
        android:textColor="@android:color/white"
        android:textSize="13sp"
        android:textStyle="italic"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
         />

    <Button
        android:id="@+id/arrowButton"
        android:layout_width="60dp"
        android:layout_height="35dp"
        android:layout_alignParentBottom="true"
        android:layout_alignStart="@+id/leaderSkillDesc"
        android:layout_marginBottom="7dp"
        android:layout_marginStart="75dp"
        android:background="@drawable/arrow_up"
        android:textOff=""
        android:textOn="" />

</RelativeLayout>

</LinearLayout>

SecondLayout.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:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/card_view_bg"
    tools:layout_editor_absoluteY="25dp">

<!-- Implement scrolling text
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:singleLine="true"
        android:scrollHorizontally="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
 -->

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="@drawable/card_details_open">

    <TextView
        android:id="@+id/COST"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/relativeLayout3"
        android:layout_marginTop="48dp"
        android:layout_toEndOf="@+id/superAttackDesc"
        android:paddingLeft="3dp"
        android:paddingRight="3dp"
        android:text="00"
        android:textColor="@color/white"
        android:textSize="17sp"
        android:textStyle="bold" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:id="@+id/relativeLayout3">

        <TextView
            android:id="@+id/HP"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="33dp"
            android:fontFamily="monospace"
            android:text="0000"
            android:textColor="@color/white"
            android:textSize="18sp"
            android:textStyle="bold"
            tools:text="0000" />

        <TextView
            android:id="@+id/ATT"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginStart="73dp"
            android:layout_toEndOf="@+id/HP"
            android:fontFamily="monospace"
            android:text="0000"
            android:textColor="@color/white"
            android:textSize="18sp"
            android:textStyle="bold"
            tools:text="0000" />

        <TextView
            android:id="@+id/DEF"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentTop="true"
            android:layout_marginEnd="42dp"
            android:fontFamily="monospace"
            android:text="0000"
            android:textColor="@color/white"
            android:textSize="18sp"
            android:textStyle="bold"
            tools:text="0000" />

    </RelativeLayout>

    <TextView
        android:id="@+id/leaderSkillDesc"
        android:layout_width="250dp"
        android:layout_height="15dp"
        android:layout_alignParentEnd="true"
        android:layout_below="@+id/COST"
        android:layout_marginEnd="36dp"
        android:layout_marginTop="16dp"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:fontFamily="monospace"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:textAlignment="viewStart"
        android:textColor="@color/white"
        android:textSize="13sp"
        android:textStyle="italic"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.496"
        app:layout_constraintVertical_bias="0.626" />

    <TextView
        android:id="@+id/superAttackTitle"
        android:layout_width="245dp"
        android:layout_height="15dp"
        android:layout_alignStart="@+id/leaderSkillDesc"
        android:layout_below="@+id/leaderSkillDesc"
        android:layout_marginTop="23dp"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:fontFamily="monospace"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:textAlignment="viewStart"
        android:textColor="@android:color/holo_blue_light"
        android:textSize="12sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/superAttackDesc"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/superAttackDesc"
        android:layout_width="255dp"
        android:layout_height="15dp"
        android:layout_alignStart="@+id/superAttackTitle"
        android:layout_below="@+id/superAttackTitle"
        android:layout_marginTop="5dp"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:fontFamily="monospace"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:textAlignment="viewStart"
        android:textColor="@android:color/white"
        android:textSize="13sp"
        android:textStyle="italic"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />


    <TextView
        android:id="@+id/passiveSkillTitle"
        android:layout_width="255dp"
        android:layout_height="17dp"
        android:layout_alignStart="@+id/superAttackDesc"
        android:layout_below="@+id/superAttackDesc"
        android:layout_marginBottom="3dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="23dp"
        android:background="@color/passiveSkillNameBackground"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:fontFamily="monospace"
        android:marqueeRepeatLimit="marquee_forever"
        android:paddingLeft="7dp"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:textAlignment="viewStart"
        android:textColor="@android:color/holo_blue_light"
        android:textColorHighlight="@android:color/black"
        android:textSize="13sp"
        android:textStyle="italic"
        android:visibility="visible"
        tools:layout_editor_absoluteX="207dp"
        tools:layout_editor_absoluteY="543dp" />

    <TextView
        android:id="@+id/passiveSkillDesc"
        android:layout_width="250dp"
        android:layout_height="15dp"
        android:layout_alignStart="@+id/passiveSkillTitle"
        android:layout_below="@+id/passiveSkillTitle"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:fontFamily="monospace"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:textAlignment="center"
        android:textColor="@android:color/white"
        android:textSize="13sp"
        android:textStyle="italic"
        android:visibility="visible"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="602dp" />

    <Button
        android:id="@+id/arrowButton"
        android:layout_width="60dp"
        android:layout_height="35dp"
        android:layout_alignParentBottom="true"
        android:layout_alignStart="@+id/leaderSkillDesc"
        android:layout_marginBottom="7dp"
        android:layout_marginStart="75dp"
        android:background="@drawable/arrow_down"
        android:textOff=""
        android:textOn="" />

</RelativeLayout>

</RelativeLayout>


Get this bounty!!!

#StackBounty: #java #android #android-layout #android-xml Some layouts don't draw when the keyboard goes down

Bounty: 50

I’m working an app that draws objects on SurfaceView under some parameters, defined by the user. I created the layout for the app, which involves an header, footer, input (where user enters parameters to draw) and a custom SurfaceView.

Here’s the layout that simplified:

<android.support.constraint.ConstraintLayout 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:layout_height="match_parent">

    <!-- Custom SurfaceView Layout -->

    <org.firengine.myapp.CustomPreview
        android:id="@+id/container_content"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <!-- Header Layout: This is basically a Toolbar, with having a top padding of status bar height (see Note). -->

    <FrameLayout
        android:id="@+id/container_header"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/colorTint"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">...</FrameLayout>

    <!-- Footer Layout - This is just a container, with having a bottom padding of navigation bar height (see Note). -->

    <FrameLayout
        android:id="@+id/container_footer"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/colorTint"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">...</FrameLayout>

    <!-- Input Layout - This is a empty container that shows views dynamically. -->

    <LinearLayout
        android:id="@+id/container_input"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:gravity="bottom|center_horizontal"
        android:orientation="vertical"
        app:layout_constraintBottom_toTopOf="@+id/container_footer"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/container_header" />

</android.support.constraint.ConstraintLayout>

The input layout generate dynamic views, based on parameters.

Suppose I create a EditText inside input layout, so that the keyboard appears if you interact with that.

This is before the keyboard shows up.

Before Keyboard Shows Up.

This is when the keyboard is showing. Notice that the app layout is panned and the header layout got invisible.

Keyboard Is Showing.

The problem occurs when the keyboard goes down. The header layout, which is invisible earlier, stays invisible all the time (until the user interacts with the app).

After Keyboard Shows Up.

I turned on GPU overdraw to check the problem (also setting SurfaceView background color to white), and here is the result.

Before:

Before with GPU overdraw.

After:

After with GPU overdraw.

Also, here’s my custom SurfaceView class (I think that the custom SurfaceView is blocking the UI thread to draw the layouts that disappeared).

public class CustomPreview extends SurfaceView implements SurfaceHolder.Callback {

    private final Handler handler = new Handler();
    private final Runnable runnable = new Runnable() {
        @Override
        public void run() {
            draw();
        }
    };

    private Renderer renderer;

    private boolean visible;
    private boolean canLoadData;

    private HashMap<String, String> data;

    public CustomPreview(Context context, AttributeSet attrs) {
        super(context, attrs);
        getHolder().addCallback(this);
        getHolder().setFormat(PixelFormat.RGBA_8888);
        visible = false;
        canLoadData = false;
        renderer= new Renderer(context);
        data = new HashMap<>();
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        visible = true;
        refreshHandler(0);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        renderer.updateDimensions(width, height);
        canLoadData = true;
        renderer.updateData(data);
        refreshHandler(0);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        renderer.clearData();
        visible = false;
        canLoadData = false;
        refreshHandler(0);
    }

    public void setVisibility(boolean visible) {
        this.visible = visible;
        refreshHandler(0);
    }

    private void draw() {
        Canvas canvas = null;
        try {
            canvas = getHolder().lockCanvas();
            if (canvas != null) {
                renderer.draw(canvas);
            }
        } finally {
            if (canvas != null) {
                getHolder().unlockCanvasAndPost(canvas);
            }
        }
        refreshHandler(60000);
    }

    public void saveData(String key, String value) {
        data.put(key, value);
        if (canLoadData) {
            renderer.updateData(data);
        }
        refreshHandler(0);
    }

    private void refreshHandler(long delay) {
        handler.removeCallbacks(runnable);
        if (visible) {
            if (delay > 0) {
                handler.postDelayed(runnable, delay);
            } else {
                handler.post(runnable);
            }
        }
    }

    public String loadData(String key) {
        return data.get(key);
    }

Am I doing something wrong? If so, can someone explain what it is?

I’m glad if someone helps me out.
Thank you.

Note: Heres some extra information about the layout.

  1. Footer layout was originally had a AdView, which is not included in the layout.
  2. SYSTEM_UI_FLAG_HIDE_NAVIGATION and SYSTEM_UI_FLAG_FULLSCREEN flags was added to the activity and because of that, paddings were set in respective layouts to avoid overlapping.
  3. And, header layout has 2 Toolbar views, which one of them are seen in the images above.


Get this bounty!!!

#StackBounty: #android #memory-leaks #garbage-collection #admob #google-admob High ram usage with admob

Bounty: 50

I am trying to place ads inside my app. According to Admob Documentation I have to initialize Mobile Ads SDK

MobileAds.initialize(this, "YOUR_ADMOB_APP_ID");

This causes spike in high ram usage in code.

enter image description here

But if i remove this line then ram usage drops & and this line of code doesn’t seem to have any affect on servering ads inside the app.

enter image description here

Also when requesting ad from admob ram usage again spike up and causes 3-4 GC events on app startup. I believe this is memory leak.

Here’s how i am requesting ad in onCreate method

AdRequest request = null;
        if (BuildConfig.DEBUG) {
            //Facebook Audience Network
            List<String> testDeviceId = new ArrayList<>();
            testDeviceId.add("TESTID");//Redmi Note 3
            testDeviceId.add("TESTID");//Moto G 1st Gen

            AdSettings.addTestDevices(testDeviceId);

            //Google Ad-mob
            request = new AdRequest.Builder()
                    .addTestDevice("TESTID")//Redmi Note 3
                    .addTestDevice("TESTID")//Mot G 1st Gen
                    .build();
        } else {
            request = new AdRequest.Builder()
                    .build();
        }

        AdView mAdView = findViewById(R.id.adView);
        mAdView.loadAd(request);

When loading this banner ads several GC event are kicked in. If i don’t load ads GC event are never kicked in.

enter image description here

Is this behavior normal with admob? How can i resolve this?


Get this bounty!!!