Categories
discuss

scrollIntoView() is not working: does not taking in account fixed element

I’m trying to use scrollIntoView() in my application, but because I have a top fixed bar, when I use the scrollIntoView(), the elements will be scroll to the fixed bar back.

This means that when I try to put some element visible to the user, by scrolling the element to a visible area, it will be scrolled, but to another invisible ate that is were this fixed bar is.

Follows an example of what I’m trying to do:

let element = document.getElementsByClassName('second-element')[0];
element.scrollIntoView();
.fixed-element{
  height: 30px;
  width: 100%;
  background-color:black;
  position:fixed;
}

.parent-element {
   width: 100%;
   height: 40000px;
   background-color:blue;
}

.element {
   width: 100%;
   height:100px;
   background-color: yellow;
   margin-top:10px;
}

.second-element{
   width: 100%;
   background-color: red;
   height:200px;
}
<div class="fixed-element"></div>
<div class='parent-element'>
  <div class='element'></div>
  <div class='element'></div>
  <div class='element'></div>
  <div class='element'></div>
  <div class='element'></div>
  <div class='element'></div>
  <div class='element'></div>
  <div class='element'></div>
  <div class='second-element'></div>
  <div class='element'></div>
  <div class='element'></div>
  <div class='element'></div>
  <div class='element'></div>
  <div class='element'></div>
  <div class='element'></div>
  <div class='element'></div>
  <div class='element'></div>
  <div class='element'></div>
</div>

There is any way that I could use this function in a way that the scroll elements not became invisible because of the fixed bar?

I would like a vanilla JavaScript solution. Also, and only if it is possible, a solution that doesn’t need to know the existent of any fixed elements.

Answer

You can make the window scrollTo x position 0 and y position the element’s offsetTop subtracted by the fixed element’s offsetHeight.

JSFiddle with your code: http://jsfiddle.net/3sa2L14k/

.header{
  position: fixed;
  background-color: green;
  width: 100%;
  top: 0;
  left: 0;
  right: 0;
}
html, body{
  height: 1000px;
}

#toBeScrolledTo{
  position: relative;
  top: 500px;
}
<div class="header">
Header
</div>
<div id="toBeScrolledTo">
Text Text Text
</div>
<script>
window.scrollTo(0, document.getElementById('toBeScrolledTo').offsetTop - document.getElementsByClassName('header')[0].offsetHeight);
</script>
Categories
discuss

Change JDK in Maven Projects (Eclipse)

New Maven projects created in Eclipse (on Windows) use the default JRE System Library “J2SE-1.5”.

Default System Library in Maven Project

The project was created the following way:

  • New Project
  • Maven -> Maven Project
  • Archtype: maven-archetype-quickstart

I want to use the JDK 1.8.0, which is my default JDK for non Maven projects.

My JAVA_HOME variable is set to

C:Program FilesJavajdk1.8.0_152

I tried switching the system library via Right Click on the System Library and set the path to my JDK 1.8.0. But after a Maven update it resets back to the 1.5.

Other Stackoverflow questions just suggest to set the JAVA_HOME, but that did not work for me. Specify JDK for Maven to use

I looked for some POM.xml commands to set the library, but couldn’t find anything. My POM looks pretty standard right now:

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.test</groupId>
<artifactId>Test123</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Test123</name>
<url>http://maven.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

What do I need to change to get the JDK 1.8 as my default Library?

Answer

Set the JDK in the pom.xml:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

in eclipse do a ‘Maven->Update Project’

Categories
discuss

What is lifecycle of DialogFragment

I could not find proper lifecycle of android.support.v4.app.DialogFragment by searching on Google. I need this for some implementation. As we know DialogFragment has some methods same like Dialog.

DialogFragment extends Fragment so its lifecycle is same as Fragment. But what about other methods of DialogFragment?

Here is Fragment lifecycle. Can one provide for DialogFragment?

enter image description here

Answer

DialogFragment life cycle is similar to the life cycle of fragment:. To test yourself put logs in each of the overrided methods of dialogFragment and then run your code you will understand the working of dialogFragment.

onAttach
onCreate
onCreateDialog
onCreateView
onActivityCreated
onStart
onResume

And as far as finishing or destroying dialogFragment is concerned the lifeCycle is as follows:

onPause
onStop
onDestroyView
onDestroy
onDetach

Also I believe this method will also help you know the lifecycle :

@NonNull
@Override
public Lifecycle getLifecycle() {
    return super.getLifecycle();
}
Categories
discuss

Registering 2 activities to receive images, it doesn’t work when sharing from Google Photos app

Let’s say in my app, users can make posts & send messages to other users, & I want to register my app as a component among those appears in the Share-via picker when sharing either text or an image, I’m defining my 2 activities in my AndroidManifest.xml as follows (like this official example):

<activity 
    android:name=".SharePostActivity"
    android:label="MyApp (Post)">
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="image/*" />
    </intent-filter>
</activity>

<activity
    android:name=".ShareMessageActivity"
    android:label="MyApp (Message)">
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="image/*" />
    </intent-filter>
</activity>

When sharing text, this works perfectly fine, where I see 2 icons of my app among available apps for sharing text, named MyApp (Post) & MyApp (Message),

Problem happens when sharing an image, because only one icon appears with the second defined label in the manifest (which is MyApp (Message)), and actually it opens the first defined activity in the manifest (which is SharePostActivity),

So, how to show the 2 options when sharing an image (just like what happens when sharing text)?

(I’ve tried on an emulator running Nougat & a real device running Oreo)

—– Update —–

I’ve found that this weird behavior happens only when sharing images from Google’s Photos app, but everything works fine when sharing an image from other apps!

Answer

Some investigation leads to a solution: each of your intent-filter must contain an android:label which points to a resource, not a hardcoded string.

...
<intent-filter android:label="@string/first_value">
...

Different Activities must have different labels, so in your case the Manifest should look like

<activity 
    android:name=".SharePostActivity"
    android:label="@string/my_app_post">
    <intent-filter
        android:label="@string/my_app_post">
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain" />
    </intent-filter>
    <intent-filter
        android:label="@string/my_app_post">
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="image/*" />
    </intent-filter>
</activity>

<activity
    android:name=".ShareMessageActivity"
    android:label="@string/my_app_message">
    <intent-filter
        android:label="@string/my_app_message">
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain" />
    </intent-filter>
    <intent-filter
        android:label="@string/my_app_message">
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="image/*" />
    </intent-filter>
</activity>
Categories
discuss

Can a custom view get something like “onClose” or “onDestroy” event?

I have created a custom compound view by extending FrameLayout. That view can be on a dialogue box (among others). But what if I want to do some clean-up work when the view disappears from the screen, such as the user’s closing the dialogue box? Can I get some kind of onDestroy event? Or should I make the owner (such as the dialogue box or the fragment) call the view’s clean-up method on its (owner’s) onDestroy or dismissed event?

Answer

try to use

@Override
protected void onDetachedFromWindow() {
    super.onDetachedFromWindow();
}
Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..