How to add action to only Strings.xml files in Android in IntelliJIdea Plugin development
I have created an IntelliJIdea plugin using action recording in plugin.xml
eg
<actions>
<!-- Add your actions here -->
<group id="AL.Localize" text="_Localize" description="Localize strings" >
<add-to-group group-id="ProjectViewPopupMenu"/>
<action id="AL.Convert" class="action.ConvertToOtherLanguages" text="Convert to other languages"
description="Convert this strings.xml to other languages that can be used to localize your Android app.">
</action>
</group>
</actions>
Using this option my action will appear after right clicking on the file. Like this:
The problem is the menu is Convert to other languages
displayed all the time, I want this menu to be displayed when the user right-clicks on the file string.xml
, like a menu does Open Translation Editor(Preview)
. ( Open Translation Editor(Preview)
is a feature of Android Studio introduced in version 0.8.7 )
What should I do?
source to share
Not sure if there is a way to do this purely in XML, so someone else is free to call if you know, but there is a way to do it in Java.
In your action update method, you can set whether the action is visible or not based on the filename using the action object Presentation
. Here's an example based on ConvertToNinePatchAction
in Android Studio:
package com.example.plugin;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.Nullable;
public class ConvertToOtherLanguages extends AnAction {
public ConvertToOtherLanguages() {
super("Convert to other languages");
}
@Override
public void update(AnActionEvent e) {
final VirtualFile file = CommonDataKeys.VIRTUAL_FILE.getData(e.getDataContext());
final boolean isStringsXML = isStringsFile(file);
e.getPresentation().setEnabled(isStringsXML);
e.getPresentation().setVisible(isStringsXML);
}
@Contract("null -> false")
private static boolean isStringsFile(@Nullable VirtualFile file) {
return file != null && file.getName().equals("string.xml");
}
@Override
public void actionPerformed(AnActionEvent e) {
// Do your action here
}
}
Then in XML your action will look like this:
<action id="AL.Convert" class="com.example.plugin.ConvertToOtherLanguages">
<add-to-group group-id="ProjectViewPopupMenu" anchor="last" />
</action>
source to share