private static void createValueResource( @NotNull PsiFile file, @NotNull AndroidFacet facet, @NotNull PsiDirectory dir, @NotNull final String resName, @NotNull final String value, @NotNull final ResourceType type, @NotNull final String oldTagText) { final String filename = file.getName(); final List<String> dirNames = Collections.singletonList(dir.getName()); final Module module = facet.getModule(); final AtomicReference<PsiElement> openAfter = new AtomicReference<PsiElement>(); final WriteCommandAction<Void> action = new WriteCommandAction<Void>( facet.getModule().getProject(), "Override Resource " + resName, file) { @Override protected void run(@NotNull Result<Void> result) { List<ResourceElement> elements = Lists.newArrayListWithExpectedSize(1); // AndroidResourceUtil.createValueResource will create a new resource value in the given // resource // folder (and record the corresponding tags added in the elements list passed into it). // However, it only creates a new element and sets the name attribute on it; it does not // transfer attributes, child content etc. Therefore, we use this utility method first // to // create the corresponding tag, and then *afterwards* we will replace the tag with a // text copy // from the resource tag we are overriding. We do this all under a single write lock // such // that it becomes a single atomic operation. AndroidResourceUtil.createValueResource( module, resName, type, filename, dirNames, value, elements); if (elements.size() == 1) { final XmlTag tag = elements.get(0).getXmlTag(); if (tag != null && tag.isValid()) { try { XmlTag tagFromText = XmlElementFactory.getInstance(tag.getProject()).createTagFromText(oldTagText); PsiElement replaced = tag.replace(tagFromText); openAfter.set(replaced); } catch (IncorrectOperationException e) { // The user tried to override an invalid XML fragment: don't attempt to do a // replacement in that case openAfter.set(tag); } } } } }; action.execute(); PsiElement tag = openAfter.get(); if (tag != null) { NavigationUtil.openFileWithPsiElement(tag, true, true); } }